Esempio n. 1
0
        public void ListZonesInSubscription()
        {
            using (
                MockContext context = MockContext.Start(this.GetType().FullName)
                )
            {
                var resourcesHandler = new RecordedDelegatingHandler
                {
                    StatusCodeToReturn = System.Net.HttpStatusCode.OK
                };
                var dnsHandler = new RecordedDelegatingHandler
                {
                    StatusCodeToReturn = System.Net.HttpStatusCode.OK
                };
                var networkHandler = new RecordedDelegatingHandler
                {
                    StatusCodeToReturn = System.Net.HttpStatusCode.OK
                };

                var dnsClient = ResourceGroupHelper.GetDnsClient(context, dnsHandler);

                var numPublicZones   = 2;
                var numPrivateZones  = 2;
                var publicZoneNames  = Enumerable.Range(0, numPublicZones).Select(i => TestUtilities.GenerateName("hydratest.dnszone.com")).ToArray();
                var privateZoneNames = Enumerable.Range(0, numPrivateZones).Select(i => TestUtilities.GenerateName("hydratest.privatednszone.com")).ToArray();

                var resourceManagementClient = ResourceGroupHelper.GetResourcesClient(context, resourcesHandler);
                var networkManagementClient  = ResourceGroupHelper.GetNetworkClient(context, networkHandler);

                var resourceGroup = ResourceGroupHelper.CreateResourceGroup(resourceManagementClient);
                CreateZones(dnsClient, resourceGroup, publicZoneNames, resourceManagementClient);
                CreatePrivateZones(dnsClient, resourceGroup, privateZoneNames, resourceManagementClient, networkManagementClient);

                var expectedZoneNames = publicZoneNames.Concat(privateZoneNames).ToArray();
                var listresponse      = dnsClient.Zones.List();

                Assert.NotNull(listresponse);
                Assert.True(listresponse.Count() >= (numPublicZones + numPrivateZones));
                Assert.True(expectedZoneNames.All(expectedZoneName => listresponse.Any(resp => string.Equals(resp.Name, expectedZoneName))), "The response of the List request does not meet expectations.");

                DeleteZones(dnsClient, resourceGroup, expectedZoneNames);
            }
        }
        private static SingleRecordSetTestContext SetupSingleRecordSetTestForPrivateZone(
            MockContext context)
        {
            var testContext = new SingleRecordSetTestContext();

            testContext.ResourcesHandler = new RecordedDelegatingHandler
            {
                StatusCodeToReturn = System.Net.HttpStatusCode.OK
            };
            testContext.DnsHandler = new RecordedDelegatingHandler
            {
                StatusCodeToReturn = System.Net.HttpStatusCode.OK
            };
            testContext.NetworkHandler = new RecordedDelegatingHandler
            {
                StatusCodeToReturn = System.Net.HttpStatusCode.OK
            };

            testContext.DnsClient = ResourceGroupHelper.GetDnsClient(
                context,
                testContext.DnsHandler);
            testContext.NetworkClient = ResourceGroupHelper.GetNetworkClient(
                context,
                testContext.NetworkHandler);
            var resourceManagementClient =
                ResourceGroupHelper.GetResourcesClient(
                    context,
                    testContext.ResourcesHandler);

            testContext.ZoneName =
                TestUtilities.GenerateName("hydratest.dnszone.com");
            testContext.RecordSetName =
                TestUtilities.GenerateName("hydratestdnsrec");
            testContext.Location =
                ResourceGroupHelper.GetResourceLocation(
                    resourceManagementClient,
                    "microsoft.network/dnszones");
            testContext.ResourceGroup =
                ResourceGroupHelper.CreateResourceGroup(
                    resourceManagementClient);
            testContext.RegistationVirtualNetworks = new List <VirtualNetwork>
            {
                ResourceGroupHelper.CreateVirtualNetwork(testContext.ResourceGroup.Name, testContext.NetworkClient)
            };
            testContext.ResolutionVirtualNetworks = new List <VirtualNetwork>
            {
                ResourceGroupHelper.CreateVirtualNetwork(testContext.ResourceGroup.Name, testContext.NetworkClient)
            };
            ResourceGroupHelper.CreatePrivateZone(
                testContext.DnsClient,
                testContext.ZoneName,
                testContext.Location,
                testContext.RegistationVirtualNetworks.Select(vNet => new SubResource {
                Id = vNet.Id
            }).ToList(),
                testContext.ResolutionVirtualNetworks.Select(vNet => new SubResource {
                Id = vNet.Id
            }).ToList(),
                testContext.ResourceGroup);
            return(testContext);
        }
Esempio n. 3
0
        public void CrudPrivateZoneFullCycle()
        {
            using (var context = MockContext.Start(GetType().FullName))
            {
                var resourcesHandler = new RecordedDelegatingHandler
                {
                    StatusCodeToReturn = HttpStatusCode.OK
                };
                var dnsHandler = new RecordedDelegatingHandler
                {
                    StatusCodeToReturn = HttpStatusCode.OK
                };
                var networkHandler = new RecordedDelegatingHandler
                {
                    StatusCodeToReturn = HttpStatusCode.OK
                };

                var dnsClient = ResourceGroupHelper.GetDnsClient(context, dnsHandler);
                var resourceManagementClient = ResourceGroupHelper.GetResourcesClient(context, resourcesHandler);
                var networkManagementClient  = ResourceGroupHelper.GetNetworkClient(context, networkHandler);

                var zoneName         = TestUtilities.GenerateName("hydratest.privatednszone.com");
                var location         = ResourceGroupHelper.GetResourceLocation(resourceManagementClient, "microsoft.network/dnszones");
                var resourceGroup    = ResourceGroupHelper.CreateResourceGroup(resourceManagementClient);
                var registrationVnet = ResourceGroupHelper.CreateVirtualNetwork(resourceGroup.Name, networkManagementClient);
                var resolutionVnet   = ResourceGroupHelper.CreateVirtualNetwork(resourceGroup.Name, networkManagementClient);

                void AssertPrivateZoneInvariants(Zone zone)
                {
                    if (zone == null)
                    {
                        throw new ArgumentNullException(nameof(zone));
                    }

                    Assert.Equal(zoneName, zone.Name);
                    Assert.Equal(ZoneType.Private, zone.ZoneType);
                    Assert.False(string.IsNullOrEmpty(zone.Etag));
                }

                // Create private zone clean, verify reponse
                var createdPrivateZone = dnsClient.Zones.CreateOrUpdate(
                    resourceGroup.Name,
                    zoneName,
                    new Zone
                {
                    Location = location,
                    ZoneType = ZoneType.Private,
                    Tags     = new Dictionary <string, string> {
                        { "tag1", "value1" }
                    },
                    RegistrationVirtualNetworks = new List <SubResource>
                    {
                        new SubResource(registrationVnet.Id)
                    },
                    ResolutionVirtualNetworks = new List <SubResource>
                    {
                        new SubResource(resolutionVnet.Id)
                    }
                });

                AssertPrivateZoneInvariants(createdPrivateZone);
                Assert.Equal(1, createdPrivateZone.Tags.Count);
                Assert.True(!createdPrivateZone.NameServers.IsAny());

                // Ensure that Id is parseable into resourceGroup
                var resourceGroupName = ExtractResourceGroupNameFromId(createdPrivateZone.Id);
                Assert.Equal(resourceGroupName, resourceGroup.Name);

                var regVirtualNetworkName = ExtractVirtualNetworkNameFromId(createdPrivateZone.RegistrationVirtualNetworks[0].Id);
                Assert.Equal(regVirtualNetworkName, registrationVnet.Name);

                var resVirtualNetworkName = ExtractVirtualNetworkNameFromId(createdPrivateZone.ResolutionVirtualNetworks[0].Id);
                Assert.Equal(resVirtualNetworkName, resolutionVnet.Name);

                // Retrieve the zone after create, verify response
                var retrievedPrivateZone = dnsClient.Zones.Get(resourceGroup.Name, zoneName);
                AssertPrivateZoneInvariants(retrievedPrivateZone);
                Assert.Equal(1, retrievedPrivateZone.Tags.Count);
                Assert.True(!retrievedPrivateZone.NameServers.IsAny());

                // Call Update on the object returned by Create (important distinction from Get below)
                createdPrivateZone.Tags = new Dictionary <string, string>
                {
                    { "tag1", "value1" },
                    { "tag2", "value2" }
                };

                var updateResponse = dnsClient.Zones.CreateOrUpdate(resourceGroup.Name, zoneName, ifMatch: null, ifNoneMatch: null, parameters: createdPrivateZone);
                AssertPrivateZoneInvariants(updateResponse);
                Assert.Equal(2, updateResponse.Tags.Count);

                // Retrieve the zone after create, verify response
                retrievedPrivateZone = dnsClient.Zones.Get(resourceGroup.Name, zoneName);
                AssertPrivateZoneInvariants(retrievedPrivateZone);
                Assert.Equal(2, retrievedPrivateZone.Tags.Count);

                // Call Update on the object returned by Get (important distinction from Create above)
                retrievedPrivateZone.Tags = new Dictionary <string, string>
                {
                    { "tag1", "value1" },
                    { "tag2", "value2" },
                    { "tag3", "value3" }
                };

                updateResponse = dnsClient.Zones.CreateOrUpdate(resourceGroup.Name, zoneName, ifMatch: null, ifNoneMatch: null, parameters: retrievedPrivateZone);
                AssertPrivateZoneInvariants(updateResponse);
                Assert.Equal(3, updateResponse.Tags.Count);

                // Delete the zone
                DeleteZones(dnsClient, resourceGroup, new[] { zoneName });
            }
        }