public void TestCreateNetworkSecurityGroupWithNullNameFails()
        {
            using (var undoContext = UndoContext.Current)
            {
                undoContext.Start();
                using (NetworkTestBase _testFixture = new NetworkTestBase())
                {
                    // setup
                    const string securityGroupName = null;
                    const string securityGroupLabel = null;
                    string securityGroupLocation = _testFixture.DefaultLocation;

                    // action
                    try
                    {
                        _testFixture.CreateNetworkSecurityGroup(securityGroupName, securityGroupLabel, securityGroupLocation);
                    }

                    // assert
                    catch (ArgumentNullException ane)
                    {
                        Assert.Contains("parameters.Name", ane.Message);
                        // succeed
                        return;
                    }
                }

                // fail if the above call succeeded
                Assert.True(false);
            }
        }
        public void TestDisassociateReserveIP()
        {
            using (var undoContext = AZT.UndoContext.Current)
            {
                undoContext.Start();
                using (NetworkTestBase _testFixture = new NetworkTestBase())
                {
                    ComputeManagementClient computeClient = _testFixture.GetComputeManagementClient();
                    StorageManagementClient storageClient = _testFixture.GetStorageManagementClient();
                    var  managementClient      = _testFixture.ManagementClient;
                    bool storageAccountCreated = false;
                    bool hostedServiceCreated  = false;
                    bool reserveIpCreated      = false;

                    string       storageAccountName = HttpMockServer.GetAssetName("teststorage1234", "teststorage").ToLower();
                    string       serviceName        = AZT.TestUtilities.GenerateName("testsvc");
                    string       deploymentName     = string.Format("{0}Prod", serviceName);
                    string       reserveIpName      = HttpMockServer.GetAssetName("rip", "testrip").ToLower();
                    string       location           = managementClient.GetDefaultLocation("Storage", "Compute");
                    const string usWestLocStr       = "West US";
                    try
                    {
                        _testFixture.AssociateReservedIP(
                            usWestLocStr,
                            location,
                            storageAccountName,
                            ref storageAccountCreated,
                            serviceName,
                            deploymentName,
                            reserveIpName,
                            ref hostedServiceCreated,
                            ref reserveIpCreated);

                        Assert.True(storageAccountCreated);
                        Assert.True(hostedServiceCreated);
                        Assert.True(reserveIpCreated);
                        DisassociateReservedIP(_testFixture, reserveIpName, serviceName, deploymentName);
                    }
                    catch (Exception)
                    {
                        throw;
                    }
                    finally
                    {
                        if (storageAccountCreated)
                        {
                            storageClient.StorageAccounts.Delete(storageAccountName);
                        }
                        if (hostedServiceCreated)
                        {
                            computeClient.HostedServices.DeleteAll(serviceName);
                        }
                        if (reserveIpCreated)
                        {
                            _testFixture.NetworkClient.ReservedIPs.Delete(reserveIpName);
                        }
                    }
                }
            }
        }
        public void TestCreateNetworkSecurityGroupFullDetailsSucceeds()
        {
            using (var undoContext = UndoContext.Current)
            {
                undoContext.Start();
                using (NetworkTestBase _testFixture = new NetworkTestBase())
                {
                    // setup
                    string securityGroupName = _testFixture.GenerateRandomNetworkSecurityGroupName();
                    string securityGroupLabel = _testFixture.GenerateRandomName();
                    string securityGroupLocation = _testFixture.DefaultLocation;

                    // action
                    _testFixture.CreateNetworkSecurityGroup(securityGroupName, securityGroupLabel, securityGroupLocation);

                    // assert
                    NetworkSecurityGroupGetResponse response = _testFixture.NetworkClient.NetworkSecurityGroups.Get(securityGroupName, "full");
                    Assert.Equal(securityGroupName, response.Name);
                    Assert.Equal(securityGroupLabel, response.Label);
                    Assert.Equal(securityGroupLocation, response.Location);
                    // Default rules
                    Assert.NotEmpty(response.Rules);
                }
            }
        }
Exemple #4
0
        public void TestSetAndDeleteNetworkSecurityRule()
        {
            using (var undoContext = UndoContext.Current)
            {
                undoContext.Start();
                using (NetworkTestBase _testFixture = new NetworkTestBase())
                {
                    // setup
                    string securityGroupName     = _testFixture.GenerateRandomNetworkSecurityGroupName();
                    string securityGroupLabel    = _testFixture.GenerateRandomName();
                    string securityGroupLocation = _testFixture.DefaultLocation;
                    string ruleName                 = _testFixture.GenerateRandomName();
                    string action                   = "Allow";
                    string sourceAddressPrefix      = "*";
                    string sourcePortRange          = "*";
                    string destinationAddressPrefix = "*";
                    string destinationPortRange     = "*";
                    int    priority                 = 500;
                    string protocol                 = "TCP";
                    string type = "Inbound";

                    // action
                    _testFixture.CreateNetworkSecurityGroup(securityGroupName, securityGroupLabel, securityGroupLocation);
                    _testFixture.SetRuleToSecurityGroup(
                        securityGroupName,
                        ruleName,
                        action,
                        sourceAddressPrefix,
                        sourcePortRange,
                        destinationAddressPrefix,
                        destinationPortRange,
                        priority,
                        protocol,
                        type);

                    // assert
                    NetworkSecurityGroupGetResponse response = _testFixture.NetworkClient.NetworkSecurityGroups.Get(securityGroupName, "full");
                    Assert.Equal(securityGroupName, response.Name);
                    Assert.Equal(securityGroupLabel, response.Label);
                    Assert.Equal(securityGroupLocation, response.Location);
                    Assert.NotEmpty(response.Rules.Where(r => string.Equals(r.Name, ruleName)));

                    NetworkSecurityRule rule = response.Rules.First();
                    Assert.Equal(ruleName, rule.Name);
                    Assert.Equal(sourceAddressPrefix, rule.SourceAddressPrefix);
                    Assert.Equal(sourcePortRange, rule.SourcePortRange);
                    Assert.Equal(destinationAddressPrefix, rule.DestinationAddressPrefix);
                    Assert.Equal(destinationPortRange, rule.DestinationPortRange);
                    Assert.Equal(priority, rule.Priority);
                    Assert.Equal(protocol, rule.Protocol);
                    Assert.Equal(action, rule.Action);
                    Assert.Equal(type, rule.Type);

                    // action
                    _testFixture.NetworkClient.NetworkSecurityGroups.DeleteRule(securityGroupName, ruleName);
                    NetworkSecurityGroupGetResponse afterDeleteGetRuleresponse = _testFixture.NetworkClient.NetworkSecurityGroups.Get(securityGroupName, "full");
                    Assert.Empty(afterDeleteGetRuleresponse.Rules.Where(r => string.Equals(r.Name, ruleName)));
                }
            }
        }
        public void TestDeleteNetworkSecurityGroup()
        {
            using (var undoContext = UndoContext.Current)
            {
                undoContext.Start();
                using (NetworkTestBase _testFixture = new NetworkTestBase())
                {
                    // setup
                    string securityGroupName     = _testFixture.GenerateRandomNetworkSecurityGroupName();
                    string securityGroupLabel    = _testFixture.GenerateRandomName();
                    string securityGroupLocation = _testFixture.DefaultLocation;

                    _testFixture.CreateNetworkSecurityGroup(securityGroupName, securityGroupLabel, securityGroupLocation);
                    NetworkSecurityGroupGetResponse getResponse = _testFixture.NetworkClient.NetworkSecurityGroups.Get(securityGroupName, null);

                    Assert.Equal(securityGroupName, getResponse.Name);

                    var beforeDeletionListResponse = _testFixture.NetworkClient.NetworkSecurityGroups.List();

                    // action
                    _testFixture.DeleteNetworkSecurityGroup(securityGroupName);

                    // assert
                    Assert.Throws <CloudException>(() => _testFixture.NetworkClient.NetworkSecurityGroups.Get(securityGroupName, null));

                    var afterDeletionListResponse = _testFixture.NetworkClient.NetworkSecurityGroups.List();
                    Assert.Equal(beforeDeletionListResponse.NetworkSecurityGroups.Count, afterDeletionListResponse.NetworkSecurityGroups.Count + 1);
                }
            }
        }
        public void TestCreateNetworkSecurityGroupWithNullNameFails()
        {
            using (var undoContext = UndoContext.Current)
            {
                undoContext.Start();
                using (NetworkTestBase _testFixture = new NetworkTestBase())
                {
                    // setup
                    const string securityGroupName     = null;
                    const string securityGroupLabel    = null;
                    string       securityGroupLocation = _testFixture.DefaultLocation;

                    // action
                    try
                    {
                        _testFixture.CreateNetworkSecurityGroup(securityGroupName, securityGroupLabel, securityGroupLocation);
                    }

                    // assert
                    catch (ArgumentNullException ane)
                    {
                        Assert.Contains("parameters.Name", ane.Message);
                        // succeed
                        return;
                    }
                }

                // fail if the above call succeeded
                Assert.True(false);
            }
        }
Exemple #7
0
        public void TestCreateNetworkSecurityGroupFullDetailsSucceeds()
        {
            using (var undoContext = UndoContext.Current)
            {
                undoContext.Start();
                using (NetworkTestBase _testFixture = new NetworkTestBase())
                {
                    // setup
                    string securityGroupName     = _testFixture.GenerateRandomNetworkSecurityGroupName();
                    string securityGroupLabel    = _testFixture.GenerateRandomName();
                    string securityGroupLocation = _testFixture.DefaultLocation;

                    // action
                    _testFixture.CreateNetworkSecurityGroup(securityGroupName, securityGroupLabel, securityGroupLocation);

                    // assert
                    NetworkSecurityGroupGetResponse response = _testFixture.NetworkClient.NetworkSecurityGroups.Get(securityGroupName, "full");
                    Assert.Equal(securityGroupName, response.Name);
                    Assert.Equal(securityGroupLabel, response.Label);
                    Assert.Equal(securityGroupLocation, response.Location);
                    // Default rules
                    Assert.NotEmpty(response.Rules);
                }
            }
        }
        public void SetIPForwardingOnRoleSucceeds()
        {
            using (var undoContext = UndoContext.Current)
            {
                undoContext.Start();
                using (NetworkTestBase _testFixture = new NetworkTestBase())
                {
                    // setup
                    bool storageAccountCreated = false;
                    bool hostedServiceCreated  = false;

                    string serviceName    = _testFixture.GenerateRandomName();
                    string deploymentName = _testFixture.GenerateRandomName();
                    string roleName       = "WebRole1";
                    string location       = _testFixture.ManagementClient.GetDefaultLocation("Storage", "Compute", "PersistentVMRole");

                    string storageAccountName = _testFixture.GenerateRandomName().ToLower();

                    _testFixture.CreateStorageAccount(location, storageAccountName, out storageAccountCreated);
                    _testFixture.SetSimpleVirtualNetwork();
                    _testFixture.CreateHostedService(location, serviceName, out hostedServiceCreated);
                    var deployment = _testFixture.CreatePaaSDeployment(
                        storageAccountName,
                        serviceName,
                        deploymentName,
                        NetworkTestConstants.OneWebOneWorkerPkgFilePath,
                        NetworkTestConstants.VnetOneWebOneWorkerCscfgFilePath,
                        startDeployment: true);

                    try
                    {
                        // action
                        var ipForwardingState = "Enabled";
                        var ipForwarding      = new IPForwardingSetParameters(ipForwardingState);

                        _testFixture.NetworkClient.IPForwarding.SetOnRole(serviceName, deploymentName, roleName,
                                                                          ipForwarding);

                        // assert
                        IPForwardingGetResponse response =
                            _testFixture.NetworkClient.IPForwarding.GetForRole(serviceName, deploymentName, roleName);
                        Assert.Equal(ipForwardingState, response.State);
                    }
                    finally
                    {
                        if (storageAccountCreated)
                        {
                            _testFixture.StorageClient.StorageAccounts.Delete(storageAccountName);
                        }
                        if (hostedServiceCreated)
                        {
                            _testFixture.ComputeClient.HostedServices.DeleteAll(serviceName);
                        }
                    }
                }
            }
        }
        public void SetIPForwardingOnRoleSucceeds()
        {
            using (var undoContext = UndoContext.Current)
            {
                undoContext.Start();
                using (NetworkTestBase _testFixture = new NetworkTestBase())
                {
                    // setup
                    bool storageAccountCreated = false;
                    bool hostedServiceCreated = false;

                    string serviceName = _testFixture.GenerateRandomName();
                    string deploymentName = _testFixture.GenerateRandomName();
                    string roleName = "WebRole1";
                    string location = _testFixture.ManagementClient.GetDefaultLocation("Storage", "Compute", "PersistentVMRole");

                    string storageAccountName = _testFixture.GenerateRandomName().ToLower();

                    _testFixture.CreateStorageAccount(location, storageAccountName, out storageAccountCreated);
                    _testFixture.SetSimpleVirtualNetwork();
                    _testFixture.CreateHostedService(location, serviceName, out hostedServiceCreated);
                    var deployment = _testFixture.CreatePaaSDeployment(
                        storageAccountName,
                        serviceName,
                        deploymentName,
                        NetworkTestConstants.OneWebOneWorkerPkgFilePath,
                        NetworkTestConstants.VnetOneWebOneWorkerCscfgFilePath);

                    try
                    {
                        // action
                        var ipForwardingState = "Enabled";
                        var ipForwarding = new IPForwardingSetParameters(ipForwardingState);

                        _testFixture.NetworkClient.IPForwarding.SetOnRole(serviceName, deploymentName, roleName,
                            ipForwarding);

                        // assert
                        IPForwardingGetResponse response =
                            _testFixture.NetworkClient.IPForwarding.GetForRole(serviceName, deploymentName, roleName);
                        Assert.Equal(ipForwardingState, response.State);
                    }
                    finally
                    {
                        if (storageAccountCreated)
                        {
                            _testFixture.StorageClient.StorageAccounts.Delete(storageAccountName);
                        }
                        if (hostedServiceCreated)
                        {
                            _testFixture.ComputeClient.HostedServices.DeleteAll(serviceName);
                        }
                    }
                }
            }
        }
        private static void AssociateReservedIP(ManagementClient managementClient, string usWestLocStr, string location,
                                                StorageManagementClient storageClient, string storageAccountName, ref bool storageAccountCreated,
                                                ComputeManagementClient computeClient, string serviceName, string deploymentName, string reserveIpName,
                                                NetworkTestBase _testFixture, ref bool hostedServiceCreated, ref bool reserveIpCreated)
        {
            if (managementClient.Locations.List().Any(
                    c => string.Equals(c.Name, usWestLocStr, StringComparison.OrdinalIgnoreCase)))
            {
                location = usWestLocStr;
            }

            CreateStorageAccount(location, storageClient, storageAccountName, out storageAccountCreated);

            CreateHostedService(location, computeClient, serviceName, out hostedServiceCreated);

            CreatePaaSDeployment(storageAccountName, computeClient, serviceName, deploymentName);

            NetworkReservedIPCreateParameters reservedIpCreatePars = new NetworkReservedIPCreateParameters
            {
                Name     = reserveIpName,
                Location = "uswest",
                Label    = "SampleReserveIPLabel"
            };

            OperationStatusResponse reserveIpCreate = _testFixture.NetworkClient.ReservedIPs.Create(reservedIpCreatePars);

            Assert.True(reserveIpCreate.StatusCode == HttpStatusCode.OK);
            reserveIpCreated = true;

            NetworkReservedIPGetResponse reserveIpCreationResponse =
                _testFixture.NetworkClient.ReservedIPs.Get(reserveIpName);

            Assert.True(reserveIpCreationResponse.StatusCode == HttpStatusCode.OK);


            NetworkReservedIPMobilityParameters pars = new NetworkReservedIPMobilityParameters
            {
                ServiceName    = serviceName,
                DeploymentName = deploymentName
            };
            OperationStatusResponse responseAssociateRip = _testFixture.NetworkClient.ReservedIPs.Associate(reserveIpName, pars);

            Assert.True(responseAssociateRip.StatusCode == HttpStatusCode.OK);

            NetworkReservedIPGetResponse receivedReservedIpFromRdfe =
                _testFixture.NetworkClient.ReservedIPs.Get(reserveIpName);

            Assert.True(receivedReservedIpFromRdfe.StatusCode == HttpStatusCode.OK);

            Assert.True(serviceName == receivedReservedIpFromRdfe.ServiceName);
            Assert.True(receivedReservedIpFromRdfe.InUse == true);
            Assert.True(deploymentName == receivedReservedIpFromRdfe.DeploymentName);
        }
Exemple #11
0
        public void TestReserveIPWithIPTagsNegative()
        {
            using (var undoContext = AZT.UndoContext.Current)
            {
                undoContext.Start();
                using (NetworkTestBase _testFixture = new NetworkTestBase())
                {
                    var    managementClient      = _testFixture.ManagementClient;
                    bool   storageAccountCreated = false;
                    string storageAccountName    = HttpMockServer.GetAssetName("tststr1234", "tststr").ToLower();
                    string reserveIpName         = HttpMockServer.GetAssetName("res", "testresIPtagNegative").ToLower();
                    string location = managementClient.GetDefaultLocation("Storage", "Compute");

                    // Create an IPTag Value that doesn't exist
                    IPTag iptag = new IPTag();
                    iptag.IPTagType = "FirstPartyUsage";
                    iptag.Value     = "MyVip";
                    List <IPTag> iptags = new List <IPTag>();
                    iptags.Add(iptag);

                    try
                    {
                        _testFixture.CreateStorageAccount(location, storageAccountName, out storageAccountCreated);

                        NetworkReservedIPCreateParameters reservedIpCreatePars = new NetworkReservedIPCreateParameters
                        {
                            Name     = reserveIpName,
                            Label    = "TestResTagNegLabel",
                            Location = location,
                            IPTags   = iptags
                        };

                        OperationStatusResponse reserveIpCreate = _testFixture.NetworkClient.ReservedIPs.Create(reservedIpCreatePars);
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine("TestReserveIPWithIPTagsNegative test did not succeed with error being ," + ex.Message);
                        Assert.NotNull(ex);
                    }
                    finally
                    {
                        if (storageAccountCreated)
                        {
                            _testFixture.StorageClient.StorageAccounts.Delete(storageAccountName);
                        }
                    }
                }
            }
        }
        public void AddAndRemoveNetworkSecurityGroupToSubnet()
        {
            using (var undoContext = UndoContext.Current)
            {
                undoContext.Start();
                using (NetworkTestBase _testFixture = new NetworkTestBase())
                {
                    //setup

                    // create Network Security Group
                    string securityGroupName     = _testFixture.GenerateRandomNetworkSecurityGroupName();
                    string securityGroupLabel    = _testFixture.GenerateRandomName();
                    string securityGroupLocation = "North Central US";

                    _testFixture.CreateNetworkSecurityGroup(securityGroupName, securityGroupLabel, securityGroupLocation);

                    // create vnet with subnet
                    string vnetName   = "virtualNetworkSiteName";
                    string subnetName = "FrontEndSubnet5";
                    _testFixture.SetSimpleVirtualNetwork();

                    NetworkSecurityGroupAddToSubnetParameters parameters = new NetworkSecurityGroupAddToSubnetParameters()
                    {
                        Name = securityGroupName
                    };

                    // action
                    _testFixture.NetworkClient.NetworkSecurityGroups.AddToSubnet(vnetName, subnetName, parameters);
                    var listNetworkResponse = _testFixture.NetworkClient.Networks.List();

                    // assert
                    var getResponse = _testFixture.NetworkClient.NetworkSecurityGroups.GetForSubnet(vnetName, subnetName);
                    Assert.Equal(securityGroupName, getResponse.Name);
                    Assert.Equal(listNetworkResponse.VirtualNetworkSites.First(vnet =>
                                                                               vnetName.Equals(vnet.Name)).Subnets.First(subnet =>
                                                                                                                         subnetName.Equals(subnet.Name))
                                 .NetworkSecurityGroup, securityGroupName);

                    // action
                    _testFixture.NetworkClient.NetworkSecurityGroups.RemoveFromSubnet(vnetName, subnetName, securityGroupName);


                    // assert
                    Assert.Throws <CloudException>(() => _testFixture.NetworkClient.NetworkSecurityGroups.GetForSubnet(vnetName, subnetName));
                }
            }
        }
Exemple #13
0
        private static void DisassociateReservedIP(NetworkTestBase _testFixture, string reserveIpName, string serviceName, string deploymentName)
        {
            NetworkReservedIPMobilityParameters pars = new NetworkReservedIPMobilityParameters
            {
                ServiceName    = serviceName,
                DeploymentName = deploymentName
            };
            OperationStatusResponse responseDisassociateRip = _testFixture.NetworkClient.ReservedIPs.Disassociate(reserveIpName, pars);

            Assert.True(responseDisassociateRip.StatusCode == HttpStatusCode.OK);

            NetworkReservedIPGetResponse receivedReservedIpFromRdfe =
                _testFixture.NetworkClient.ReservedIPs.Get(reserveIpName);

            Assert.True(receivedReservedIpFromRdfe.StatusCode == HttpStatusCode.OK);

            Assert.True(string.IsNullOrEmpty(receivedReservedIpFromRdfe.ServiceName));
            Assert.True(receivedReservedIpFromRdfe.InUse == false);
            Assert.True(string.IsNullOrEmpty(receivedReservedIpFromRdfe.DeploymentName));
        }
        public void TestListNetworkSecurityGroup()
        {
            using (var undoContext = UndoContext.Current)
            {
                undoContext.Start();
                using (NetworkTestBase _testFixture = new NetworkTestBase())
                {
                    // setup
                    string securityGroupName     = _testFixture.GenerateRandomNetworkSecurityGroupName();
                    string securityGroupLabel    = _testFixture.GenerateRandomName();
                    string securityGroupLocation = _testFixture.DefaultLocation;

                    // action
                    int networkSecurityGroupCount = _testFixture.NetworkClient.NetworkSecurityGroups.List().Count();
                    _testFixture.CreateNetworkSecurityGroup(securityGroupName, securityGroupLabel, securityGroupLocation);
                    var response = _testFixture.NetworkClient.NetworkSecurityGroups.List();

                    // assert
                    Assert.Equal(networkSecurityGroupCount + 1, response.Count());
                }
            }
        }
        public void TestSetAndDeleteNetworkSecurityRule()
        {
            using (var undoContext = UndoContext.Current)
            {
                undoContext.Start();
                using (NetworkTestBase _testFixture = new NetworkTestBase())
                {
                    // setup
                    string securityGroupName = _testFixture.GenerateRandomNetworkSecurityGroupName();
                    string securityGroupLabel = _testFixture.GenerateRandomName();
                    string securityGroupLocation = _testFixture.DefaultLocation;
                    string ruleName = _testFixture.GenerateRandomName();
                    string action = "Allow";
                    string sourceAddressPrefix = "*";
                    string sourcePortRange = "*";
                    string destinationAddressPrefix = "*";
                    string destinationPortRange = "*";
                    int priority = 500;
                    string protocol = "TCP";
                    string type = "Inbound";

                    // action
                    _testFixture.CreateNetworkSecurityGroup(securityGroupName, securityGroupLabel, securityGroupLocation);
                    _testFixture.SetRuleToSecurityGroup(
                        securityGroupName,
                        ruleName,
                        action,
                        sourceAddressPrefix,
                        sourcePortRange,
                        destinationAddressPrefix,
                        destinationPortRange,
                        priority,
                        protocol,
                        type);

                    // assert
                    NetworkSecurityGroupGetResponse response = _testFixture.NetworkClient.NetworkSecurityGroups.Get(securityGroupName, "full");
                    Assert.Equal(securityGroupName, response.Name);
                    Assert.Equal(securityGroupLabel, response.Label);
                    Assert.Equal(securityGroupLocation, response.Location);
                    Assert.NotEmpty(response.Rules.Where(r => string.Equals(r.Name, ruleName)));

                    NetworkSecurityRule rule = response.Rules.First();
                    Assert.Equal(ruleName, rule.Name);
                    Assert.Equal(sourceAddressPrefix, rule.SourceAddressPrefix);
                    Assert.Equal(sourcePortRange, rule.SourcePortRange);
                    Assert.Equal(destinationAddressPrefix, rule.DestinationAddressPrefix);
                    Assert.Equal(destinationPortRange, rule.DestinationPortRange);
                    Assert.Equal(priority, rule.Priority);
                    Assert.Equal(protocol, rule.Protocol);
                    Assert.Equal(action, rule.Action);
                    Assert.Equal(type, rule.Type);

                    // action
                    _testFixture.NetworkClient.NetworkSecurityGroups.DeleteRule(securityGroupName, ruleName);
                    NetworkSecurityGroupGetResponse afterDeleteGetRuleresponse = _testFixture.NetworkClient.NetworkSecurityGroups.Get(securityGroupName, "full");
                    Assert.Empty(afterDeleteGetRuleresponse.Rules.Where(r => string.Equals(r.Name, ruleName)));
                }
            }
        }
Exemple #16
0
        public void TestReserveIPWithIPTagsSimple()
        {
            using (var undoContext = AZT.UndoContext.Current)
            {
                undoContext.Start();
                using (NetworkTestBase _testFixture = new NetworkTestBase())
                {
                    var    managementClient      = _testFixture.ManagementClient;
                    bool   storageAccountCreated = false;
                    string storageAccountName    = HttpMockServer.GetAssetName("tststr1234", "tststr").ToLower();
                    string reserveIpName         = HttpMockServer.GetAssetName("res", "testresIPtag").ToLower();
                    string location          = "West Central US";
                    bool   reservedIpCreated = false;

                    IPTag iptag = new IPTag();
                    iptag.IPTagType = "FirstPartyUsage";
                    iptag.Value     = "/tagTypes/SystemService/operators/Microsoft/platforms/Azure/services/Microsoft.AzureAD";
                    List <IPTag> iptags = new List <IPTag>();
                    iptags.Add(iptag);

                    try
                    {
                        _testFixture.CreateStorageAccount(location, storageAccountName, out storageAccountCreated);

                        NetworkReservedIPCreateParameters reservedIpCreatePars = new NetworkReservedIPCreateParameters
                        {
                            Name     = reserveIpName,
                            Label    = "TestResTagLabel",
                            Location = location,
                            IPTags   = iptags
                        };

                        OperationStatusResponse reserveIpCreate = _testFixture.NetworkClient.ReservedIPs.Create(reservedIpCreatePars);
                        Assert.True(reserveIpCreate.StatusCode == HttpStatusCode.OK);

                        reservedIpCreated = true;
                        NetworkReservedIPGetResponse reserveIpCreationResponse =
                            _testFixture.NetworkClient.ReservedIPs.Get(reserveIpName);

                        Assert.True(reserveIpCreationResponse.StatusCode == HttpStatusCode.OK);
                        Assert.True(reserveIpCreationResponse.IPTags.Count == iptags.Count);

                        foreach (var iptag1 in iptags)
                        {
                            Assert.True(reserveIpCreationResponse.IPTags.Any(x => x.IPTagType == iptag1.IPTagType && x.Value == iptag1.Value));
                        }
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine("TestReserveIPWithIPTagsSimple test did not succeed with error being ," + ex.Message);
                        throw;
                    }
                    finally
                    {
                        if (storageAccountCreated)
                        {
                            _testFixture.StorageClient.StorageAccounts.Delete(storageAccountName);
                        }
                        if (reservedIpCreated)
                        {
                            _testFixture.NetworkClient.ReservedIPs.Delete(reserveIpName);
                        }
                    }
                }
            }
        }
Exemple #17
0
        public void TestAddAndRemoveVip()
        {
            using (var undoContext = AZT.UndoContext.Current)
            {
                undoContext.Start();
                using (NetworkTestBase _testFixture = new NetworkTestBase())
                {
                    bool   hostedServiceCreated  = false;
                    bool   storageAccountCreated = false;
                    string storageAccountName    = HttpMockServer.GetAssetName("tststr1234", "tststr").ToLower();
                    string serviceName           = AZT.TestUtilities.GenerateName("testser");
                    string virtualIPName1        = AZT.TestUtilities.GenerateName("vip1");
                    string virtualIPName2        = AZT.TestUtilities.GenerateName("vip2");
                    string virtualIPName3        = AZT.TestUtilities.GenerateName("vip2");
                    string deploymentName        = AZT.TestUtilities.GenerateName("dep");

                    ComputeManagementClient computeClient    = _testFixture.GetComputeManagementClient();
                    ManagementClient        managementClient = _testFixture.ManagementClient;
                    StorageManagementClient storageClient    = _testFixture.GetStorageManagementClient();

                    try
                    {
                        string location = Utilities.GetTestLocation(managementClient);
                        Assert.True(!string.IsNullOrEmpty(location));

                        // Create hosted service
                        Utilities.CreateHostedService(location, computeClient, serviceName, out hostedServiceCreated);
                        Assert.True(hostedServiceCreated);

                        // Create storage account
                        storageAccountName = HttpMockServer.GetAssetName("tststr1234", "tststr").ToLower();
                        Utilities.CreateStorageAccount(location, storageClient, storageAccountName,
                                                       out storageAccountCreated);
                        Assert.True(storageAccountCreated);

                        // Create a new VM
                        Utilities.CreateAzureVirtualMachine(computeClient, serviceName, deploymentName, storageAccountName,
                                                            "blob.core.windows.net");

                        // Add and assert vip status
                        OperationStatusResponse virtualIPCreate1 =
                            _testFixture.NetworkClient.VirtualIPs.Add(serviceName: serviceName,
                                                                      deploymentName: deploymentName, virtualIPName: virtualIPName1);

                        Assert.True(virtualIPCreate1.StatusCode == HttpStatusCode.OK);

                        Utilities.AssertLogicalVipWithoutIPPresentOrAbsent(computeClient, serviceName: serviceName,
                                                                           deploymentName: deploymentName, virtualIPName: virtualIPName1, expectedVipCount: 2,
                                                                           present: true);

                        OperationStatusResponse virtualIPCreate2 =
                            _testFixture.NetworkClient.VirtualIPs.Add(serviceName: serviceName,
                                                                      deploymentName: deploymentName, virtualIPName: virtualIPName2);

                        Assert.True(virtualIPCreate2.StatusCode == HttpStatusCode.OK);

                        Utilities.AssertLogicalVipWithoutIPPresentOrAbsent(computeClient, serviceName: serviceName,
                                                                           deploymentName: deploymentName, virtualIPName: virtualIPName2, expectedVipCount: 3,
                                                                           present: true);

                        OperationStatusResponse virtualIPCreate3 =
                            _testFixture.NetworkClient.VirtualIPs.Add(serviceName: serviceName,
                                                                      deploymentName: deploymentName, virtualIPName: virtualIPName3);

                        Assert.True(virtualIPCreate3.StatusCode == HttpStatusCode.OK);

                        Utilities.AssertLogicalVipWithoutIPPresentOrAbsent(computeClient, serviceName: serviceName,
                                                                           deploymentName: deploymentName, virtualIPName: virtualIPName3, expectedVipCount: 4,
                                                                           present: true);

                        // Remove and assert vip status
                        OperationStatusResponse virtualIPRemove1 =
                            _testFixture.NetworkClient.VirtualIPs.Remove(serviceName: serviceName,
                                                                         deploymentName: deploymentName, virtualIPName: virtualIPName1);

                        Assert.True(virtualIPRemove1.StatusCode == HttpStatusCode.OK);

                        Utilities.AssertLogicalVipWithoutIPPresentOrAbsent(computeClient, serviceName: serviceName,
                                                                           deploymentName: deploymentName, virtualIPName: virtualIPName1, expectedVipCount: 3,
                                                                           present: false);

                        OperationStatusResponse virtualIPRemove3 =
                            _testFixture.NetworkClient.VirtualIPs.Remove(serviceName: serviceName,
                                                                         deploymentName: deploymentName, virtualIPName: virtualIPName3);

                        Assert.True(virtualIPRemove3.StatusCode == HttpStatusCode.OK);

                        Utilities.AssertLogicalVipWithoutIPPresentOrAbsent(computeClient, serviceName: serviceName,
                                                                           deploymentName: deploymentName, virtualIPName: virtualIPName3, expectedVipCount: 2,
                                                                           present: false);

                        OperationStatusResponse virtualIPRemove2 =
                            _testFixture.NetworkClient.VirtualIPs.Remove(serviceName: serviceName,
                                                                         deploymentName: deploymentName, virtualIPName: virtualIPName2);

                        Assert.True(virtualIPRemove2.StatusCode == HttpStatusCode.OK);

                        Utilities.AssertLogicalVipWithoutIPPresentOrAbsent(computeClient, serviceName: serviceName,
                                                                           deploymentName: deploymentName, virtualIPName: virtualIPName2, expectedVipCount: 1,
                                                                           present: false);
                    }
                    finally
                    {
                        if (hostedServiceCreated)
                        {
                            computeClient.HostedServices.DeleteAll(serviceName);
                        }
                    }
                }
            }
        }
Exemple #18
0
        public void TestAdditionalVipLifeCycle()
        {
            using (var undoContext = AZT.UndoContext.Current)
            {
                undoContext.Start();
                using (NetworkTestBase _testFixture = new NetworkTestBase())
                {
                    bool   hostedServiceCreated  = false;
                    bool   storageAccountCreated = false;
                    string storageAccountName    = HttpMockServer.GetAssetName("tststr1234", "tststr").ToLower();
                    string serviceName           = AZT.TestUtilities.GenerateName("testser");
                    string virtualIPName1        = AZT.TestUtilities.GenerateName("vip1");
                    string deploymentName        = AZT.TestUtilities.GenerateName("dep");

                    ComputeManagementClient computeClient    = _testFixture.GetComputeManagementClient();
                    ManagementClient        managementClient = _testFixture.ManagementClient;
                    StorageManagementClient storageClient    = _testFixture.GetStorageManagementClient();

                    try
                    {
                        string location = Utilities.GetTestLocation(managementClient);
                        Assert.True(!string.IsNullOrEmpty(location));

                        // Create hosted service
                        Utilities.CreateHostedService(location, computeClient, serviceName, out hostedServiceCreated);
                        Assert.True(hostedServiceCreated);

                        // Create storage account
                        storageAccountName = HttpMockServer.GetAssetName("tststr1234", "tststr").ToLower();
                        Utilities.CreateStorageAccount(location, storageClient, storageAccountName,
                                                       out storageAccountCreated);
                        Assert.True(storageAccountCreated);

                        // Create a new VM
                        Utilities.CreateAzureVirtualMachine(computeClient, serviceName, deploymentName,
                                                            storageAccountName, "blob.core.windows.net");

                        DeploymentGetResponse depRetrieved =
                            computeClient.Deployments.GetByName(serviceName: serviceName, deploymentName: deploymentName);

                        IEnumerable <ConfigurationSet> endpointCfgSets = new List <ConfigurationSet>
                        {
                            new ConfigurationSet
                            {
                                ConfigurationSetType = "NetworkConfiguration",
                                InputEndpoints       =
                                    new List <InputEndpoint>
                                {
                                    new InputEndpoint()
                                    {
                                        LocalPort = 3387,
                                        Name      = "RDP2",
                                        Port      = 52777,
                                        Protocol  = InputEndpointTransportProtocol.Tcp,
                                        EnableDirectServerReturn = false
                                    },
                                }
                            }
                        };

                        // Update with single endpoint

                        var updateParams = Utilities.GetVMUpdateParameters(depRetrieved.Roles.First(),
                                                                           storageAccountName, endpointCfgSets, preserveOriginalConfigSets: false);

                        computeClient.VirtualMachines.Update(
                            serviceName,
                            deploymentName,
                            depRetrieved.Roles.First().RoleName,
                            updateParams);

                        // Add and assert vip status
                        OperationStatusResponse virtualIPCreate1 =
                            _testFixture.NetworkClient.VirtualIPs.Add(serviceName: serviceName,
                                                                      deploymentName: deploymentName, virtualIPName: virtualIPName1);

                        Assert.True(virtualIPCreate1.StatusCode == HttpStatusCode.OK);

                        depRetrieved = Utilities.AssertLogicalVipWithoutIPPresentOrAbsent(computeClient, serviceName: serviceName,
                                                                                          deploymentName: deploymentName, virtualIPName: virtualIPName1, expectedVipCount: 2,
                                                                                          present: true);

                        endpointCfgSets = new List <ConfigurationSet>
                        {
                            new ConfigurationSet
                            {
                                ConfigurationSetType = "NetworkConfiguration",
                                InputEndpoints       =
                                    new List <InputEndpoint>
                                {
                                    new InputEndpoint()
                                    {
                                        LocalPort = 3387,
                                        Name      = "RDP2",
                                        Port      = 52777,
                                        Protocol  = InputEndpointTransportProtocol.Tcp,
                                        EnableDirectServerReturn = false,
                                    },
                                    new InputEndpoint()
                                    {
                                        LocalPort = 3379,
                                        Name      = "RDP",
                                        Port      = 52728,
                                        Protocol  = InputEndpointTransportProtocol.Tcp,
                                        EnableDirectServerReturn = false,
                                        VirtualIPName            = virtualIPName1,
                                    }
                                }
                            }
                        };

                        updateParams = Utilities.GetVMUpdateParameters(depRetrieved.Roles.First(),
                                                                       storageAccountName, endpointCfgSets, preserveOriginalConfigSets: false);

                        computeClient.VirtualMachines.Update(
                            serviceName,
                            deploymentName,
                            depRetrieved.Roles.First().RoleName,
                            updateParams);

                        var depRetrievedAfterUpdate = Utilities.AssertLogicalVipWithIPPresent(computeClient, serviceName: serviceName,
                                                                                              deploymentName: deploymentName, virtualIPName: virtualIPName1, expectedVipCount: 2);

                        endpointCfgSets = new List <ConfigurationSet>
                        {
                            new ConfigurationSet
                            {
                                ConfigurationSetType = "NetworkConfiguration",
                                InputEndpoints       =
                                    new List <InputEndpoint>
                                {
                                    new InputEndpoint()
                                    {
                                        LocalPort = 3387,
                                        Name      = "RDP2",
                                        Port      = 52777,
                                        Protocol  = InputEndpointTransportProtocol.Tcp,
                                        EnableDirectServerReturn = false,
                                    },
                                }
                            }
                        };

                        updateParams = Utilities.GetVMUpdateParameters(depRetrieved.Roles.First(),
                                                                       storageAccountName, endpointCfgSets, preserveOriginalConfigSets: false);
                        computeClient.VirtualMachines.Update(
                            serviceName,
                            deploymentName,
                            depRetrieved.Roles.First().RoleName,
                            updateParams);

                        depRetrieved = Utilities.AssertLogicalVipWithoutIPPresentOrAbsent(computeClient,
                                                                                          serviceName: serviceName,
                                                                                          deploymentName: deploymentName, virtualIPName: virtualIPName1, expectedVipCount: 2,
                                                                                          present: true);

                        // Remove and assert vip status
                        OperationStatusResponse virtualIPRemove1 =
                            _testFixture.NetworkClient.VirtualIPs.Remove(serviceName: serviceName,
                                                                         deploymentName: deploymentName, virtualIPName: virtualIPName1);

                        Assert.True(virtualIPRemove1.StatusCode == HttpStatusCode.OK);

                        Utilities.AssertLogicalVipWithoutIPPresentOrAbsent(computeClient, serviceName: serviceName,
                                                                           deploymentName: deploymentName, virtualIPName: virtualIPName1, expectedVipCount: 3,
                                                                           present: false);
                    }
                    finally
                    {
                        if (hostedServiceCreated)
                        {
                            computeClient.HostedServices.DeleteAll(serviceName);
                        }
                    }
                }
            }
        }
Exemple #19
0
        public void TestAssociateDisassociateOnMultivipIaaSDeployment()
        {
            using (var undoContext = AZT.UndoContext.Current)
            {
                undoContext.Start();
                using (NetworkTestBase _testFixture = new NetworkTestBase())
                {
                    bool   hostedServiceCreated  = false;
                    bool   storageAccountCreated = false;
                    string storageAccountName    = HttpMockServer.GetAssetName("tststr1234", "tststr").ToLower();
                    string serviceName           = AZT.TestUtilities.GenerateName("testser");
                    string deploymentName        = AZT.TestUtilities.GenerateName("dep");

                    ComputeManagementClient computeClient    = _testFixture.GetComputeManagementClient();
                    ManagementClient        managementClient = _testFixture.ManagementClient;
                    StorageManagementClient storageClient    = _testFixture.GetStorageManagementClient();
                    List <string>           createdRips      = new List <string>();

                    try
                    {
                        string location = Utilities.GetTestLocation(managementClient);
                        Assert.True(!string.IsNullOrEmpty(location));

                        // Create hosted service
                        Utilities.CreateHostedService(location, computeClient, serviceName, out hostedServiceCreated);
                        Assert.True(hostedServiceCreated);

                        // Create storage account
                        storageAccountName = HttpMockServer.GetAssetName("tststr1234", "tststr").ToLower();
                        Utilities.CreateStorageAccount(location, storageClient, storageAccountName,
                                                       out storageAccountCreated);
                        Assert.True(storageAccountCreated);


                        List <string> vipNames = new List <string>()
                        {
                            AZT.TestUtilities.GenerateName("VipA"),
                            AZT.TestUtilities.GenerateName("VipB"),
                            AZT.TestUtilities.GenerateName("VipC"),
                            AZT.TestUtilities.GenerateName("VipD"),
                            AZT.TestUtilities.GenerateName("VipE")
                        };

                        List <string> reservedIPNames = new List <string>()
                        {
                            AZT.TestUtilities.GenerateName("RipA"),
                            AZT.TestUtilities.GenerateName("RipB"),
                            AZT.TestUtilities.GenerateName("RipC"),
                            AZT.TestUtilities.GenerateName("RipD"),
                            AZT.TestUtilities.GenerateName("RipE")
                        };

                        CreateMultivipDeploymentAndAssertSuccess(_testFixture.NetworkClient, computeClient,
                                                                 vipNames, serviceName, deploymentName, storageAccountName, location);

                        // Associate 5 reserved IPs
                        for (int i = 0; i < 5; i++)
                        {
                            string reserveIpName = reservedIPNames[i];
                            string vipName       = vipNames[i];
                            NetworkReservedIPCreateParameters reservedIpCreatePars = new NetworkReservedIPCreateParameters
                            {
                                Name     = reserveIpName,
                                Location = location,
                                Label    = "SampleReserveIPLabel"
                            };

                            OperationStatusResponse reserveIpCreate = _testFixture.NetworkClient.ReservedIPs.Create(reservedIpCreatePars);
                            Assert.True(reserveIpCreate.StatusCode == HttpStatusCode.OK);
                            createdRips.Add(reserveIpName);

                            NetworkReservedIPGetResponse reserveIpCreationResponse =
                                _testFixture.NetworkClient.ReservedIPs.Get(reserveIpName);

                            Assert.True(reserveIpCreationResponse.StatusCode == HttpStatusCode.OK);

                            NetworkReservedIPMobilityParameters pars = new NetworkReservedIPMobilityParameters
                            {
                                ServiceName    = serviceName,
                                DeploymentName = deploymentName,
                                VirtualIPName  = vipName
                            };
                            OperationStatusResponse responseAssociateRip = _testFixture.NetworkClient.ReservedIPs.Associate(reserveIpName, pars);
                            Assert.True(responseAssociateRip.StatusCode == HttpStatusCode.OK);
                            DeploymentGetResponse deploymentResponse =
                                computeClient.Deployments.GetByName(serviceName: serviceName,
                                                                    deploymentName: deploymentName);

                            NetworkReservedIPGetResponse receivedReservedIpFromRdfe =
                                _testFixture.NetworkClient.ReservedIPs.Get(reserveIpName);

                            Assert.True(receivedReservedIpFromRdfe.StatusCode == HttpStatusCode.OK);

                            Assert.True(serviceName == receivedReservedIpFromRdfe.ServiceName);
                            Assert.True(receivedReservedIpFromRdfe.InUse == true);
                            Assert.True(deploymentName == receivedReservedIpFromRdfe.DeploymentName);
                            Assert.True(reserveIpName == receivedReservedIpFromRdfe.Name);
                            Assert.True(vipName == receivedReservedIpFromRdfe.VirtualIPName);
                            var vipAssociated = deploymentResponse.VirtualIPAddresses.FirstOrDefault(vip => vip.Name == vipName);
                            Assert.NotNull(vipAssociated);
                            Assert.True(vipAssociated.ReservedIPName == reserveIpName);
                        }

                        // Disassociate the associated IPs
                        for (int i = 0; i < 5; i++)
                        {
                            string reserveIpName = reservedIPNames[i];
                            string vipName       = vipNames[i];

                            NetworkReservedIPMobilityParameters pars = new NetworkReservedIPMobilityParameters
                            {
                                ServiceName    = serviceName,
                                DeploymentName = deploymentName,
                                VirtualIPName  = vipName
                            };

                            OperationStatusResponse responseDisassociateRip = _testFixture.NetworkClient.ReservedIPs.Disassociate(reserveIpName, pars);
                            Assert.True(responseDisassociateRip.StatusCode == HttpStatusCode.OK);
                            DeploymentGetResponse deploymentResponse =
                                computeClient.Deployments.GetByName(serviceName: serviceName,
                                                                    deploymentName: deploymentName);

                            NetworkReservedIPGetResponse receivedReservedIpFromRdfe =
                                _testFixture.NetworkClient.ReservedIPs.Get(reserveIpName);

                            Assert.True(receivedReservedIpFromRdfe.StatusCode == HttpStatusCode.OK);

                            Assert.True(string.IsNullOrEmpty(receivedReservedIpFromRdfe.ServiceName));
                            Assert.True(receivedReservedIpFromRdfe.InUse == false);
                            Assert.True(string.IsNullOrEmpty(receivedReservedIpFromRdfe.DeploymentName));
                            Assert.True(reserveIpName == receivedReservedIpFromRdfe.Name);
                            Assert.True(string.IsNullOrEmpty(receivedReservedIpFromRdfe.VirtualIPName));
                            var vipAssociated = deploymentResponse.VirtualIPAddresses.FirstOrDefault(vip => vip.Name == vipName);
                            Assert.NotNull(vipAssociated);
                            Assert.True(string.IsNullOrEmpty(vipAssociated.ReservedIPName));
                        }
                    }
                    finally
                    {
                        if (hostedServiceCreated)
                        {
                            computeClient.HostedServices.DeleteAll(serviceName);
                        }
                        if (createdRips.Any())
                        {
                            foreach (var rip in createdRips)
                            {
                                // Clean up created Reserved IPs
                                _testFixture.NetworkClient.ReservedIPs.Delete(rip);
                            }
                        }
                    }
                }
            }
        }
Exemple #20
0
        public void TestReservingExistingDeploymentIP()
        {
            using (var undoContext = AZT.UndoContext.Current)
            {
                undoContext.Start();
                using (NetworkTestBase _testFixture = new NetworkTestBase())
                {
                    var    managementClient      = _testFixture.ManagementClient;
                    bool   storageAccountCreated = false;
                    bool   hostedServiceCreated  = false;
                    string storageAccountName    = HttpMockServer.GetAssetName("tststr1234", "tststr").ToLower();
                    string serviceName           = AZT.TestUtilities.GenerateName("testser");
                    string deploymentName        = string.Format("{0}Prd", serviceName);
                    string reserveIpName         = HttpMockServer.GetAssetName("res", "testres").ToLower();
                    string location          = managementClient.GetDefaultLocation("Storage", "Compute");
                    bool   reservedIpCreated = false;
                    try
                    {
                        _testFixture.CreateStorageAccount(location, storageAccountName, out storageAccountCreated);

                        _testFixture.CreateHostedService(location, serviceName, out hostedServiceCreated);

                        var deployment = _testFixture.CreatePaaSDeployment(storageAccountName, serviceName, deploymentName, NetworkTestConstants.OneWebOneWorkerPkgFilePath, "OneWebOneWorker.cscfg", startDeployment: true);

                        NetworkReservedIPCreateParameters reservedIpCreatePars = new NetworkReservedIPCreateParameters
                        {
                            Name           = reserveIpName,
                            Label          = "TestLabel",
                            DeploymentName = deploymentName,
                            ServiceName    = serviceName,
                            Location       = location
                        };

                        OperationStatusResponse reserveIpCreate = _testFixture.NetworkClient.ReservedIPs.Create(reservedIpCreatePars);
                        Assert.True(reserveIpCreate.StatusCode == HttpStatusCode.OK);

                        reservedIpCreated = true;
                        NetworkReservedIPGetResponse reserveIpCreationResponse =
                            _testFixture.NetworkClient.ReservedIPs.Get(reserveIpName);


                        Assert.True(reserveIpCreationResponse.StatusCode == HttpStatusCode.OK);

                        Assert.True(reserveIpCreationResponse.ServiceName == serviceName);
                        Assert.True(reserveIpCreationResponse.DeploymentName == deploymentName);
                        Assert.True(reserveIpCreationResponse.InUse == true);
                        Assert.True(reserveIpCreationResponse.Address == deployment.VirtualIPAddresses[0].Address);
                    }
                    catch (Exception)
                    {
                        throw;
                    }
                    finally
                    {
                        if (storageAccountCreated)
                        {
                            _testFixture.StorageClient.StorageAccounts.Delete(storageAccountName);
                        }
                        if (hostedServiceCreated)
                        {
                            _testFixture.ComputeClient.HostedServices.DeleteAll(serviceName);
                        }
                        if (reservedIpCreated)
                        {
                            _testFixture.NetworkClient.ReservedIPs.Delete(reserveIpName);
                        }
                    }
                }
            }
        }
        public void CreateVMWithNetworkSecurityGroupOnNIC()
        {
            using (var undoContext = UndoContext.Current)
            {
                undoContext.Start();
                using (NetworkTestBase _testFixture = new NetworkTestBase())
                {
                    // setup
                    bool storageAccountCreated = false;
                    bool hostedServiceCreated = false;

                    string serviceName = _testFixture.GenerateRandomName();
                    string deploymentName = _testFixture.GenerateRandomName();
                    string roleName = _testFixture.GenerateRandomName();
                    string networkInterfaceName = _testFixture.GenerateRandomName();
                    string location = _testFixture.ManagementClient.GetDefaultLocation("Storage", "Compute", "PersistentVMRole");
                    string virtualNetworkName = "virtualNetworkSiteName";
                    string subnetName = "FrontEndSubnet5";

                    string storageAccountName = _testFixture.GenerateRandomName().ToLower();

                    // create Network Security Group
                    string securityGroupName = _testFixture.GenerateRandomNetworkSecurityGroupName();
                    string securityGroupLabel = _testFixture.GenerateRandomName();
                    string securityGroupLocation = "North Central US";
                    _testFixture.CreateNetworkSecurityGroup(securityGroupName, securityGroupLabel, securityGroupLocation);

                    _testFixture.CreateStorageAccount(location, storageAccountName, out storageAccountCreated);
                    _testFixture.SetSimpleVirtualNetwork();
                    _testFixture.CreateHostedService(location, serviceName, out hostedServiceCreated);

                    var multiNICVMDeployment = _testFixture.CreateMultiNICIaaSDeploymentParameters(
                            serviceName,
                            deploymentName,
                            roleName,
                            networkInterfaceName,
                            storageAccountName,
                            virtualNetworkName,
                            subnetName);

                    var configurationSets = multiNICVMDeployment.Roles.Single(
                        r => string.Equals(r.RoleName, roleName)).ConfigurationSets;

                    configurationSets
                        .Single(
                            cs => string.Equals(cs.ConfigurationSetType, ConfigurationSetTypes.NetworkConfiguration))
                        .NetworkInterfaces.Single(nic => string.Equals(nic.Name, networkInterfaceName))
                        .NetworkSecurityGroup = securityGroupName;

                    try
                    {
                        // action 1: create Deployment with NSG
                        _testFixture.ComputeClient.VirtualMachines.CreateDeployment(
                            serviceName,
                            multiNICVMDeployment);

                        // assert 1
                        NetworkSecurityGroupGetAssociationResponse response =
                            _testFixture.NetworkClient.NetworkSecurityGroups.GetForNetworkInterface(
                            serviceName,
                            deploymentName,
                            roleName,
                            networkInterfaceName);
                        Assert.Equal(securityGroupName, response.Name);

                        var deployment = _testFixture.ComputeClient.Deployments.GetBySlot(serviceName,
                            DeploymentSlot.Production);

                        Assert.Equal(
                            securityGroupName,
                            deployment.Roles.Single(r => string.Equals(r.RoleName, roleName))
                                .ConfigurationSets.Single(
                                    cs =>
                                        string.Equals(cs.ConfigurationSetType,
                                            ConfigurationSetTypes.NetworkConfiguration))
                                .NetworkInterfaces.Single(nic => string.Equals(nic.Name, networkInterfaceName))
                                .NetworkSecurityGroup);

                        // action 2: update deployment without NSG
                        configurationSets
                            .Single(
                                cs => string.Equals(cs.ConfigurationSetType, ConfigurationSetTypes.NetworkConfiguration))
                            .NetworkInterfaces.Single(nic => string.Equals(nic.Name, networkInterfaceName))
                            .NetworkSecurityGroup = null;

                        _testFixture.ComputeClient.VirtualMachines.Update(serviceName, deploymentName, roleName,
                            new VirtualMachineUpdateParameters()
                            {
                                RoleName = roleName,
                                ConfigurationSets = configurationSets,
                                OSVirtualHardDisk = _testFixture.GetOSVirtualHardDisk(storageAccountName, serviceName)
                            });

                        // assert 2
                        deployment = _testFixture.ComputeClient.Deployments.GetBySlot(serviceName,
                            DeploymentSlot.Production);

                        Assert.Null(
                            deployment.Roles.Single(r => string.Equals(r.RoleName, roleName))
                                .ConfigurationSets.Single(
                                    cs =>
                                        string.Equals(cs.ConfigurationSetType,
                                            ConfigurationSetTypes.NetworkConfiguration))
                                .NetworkInterfaces.Single(nic => string.Equals(nic.Name, networkInterfaceName))
                                .NetworkSecurityGroup);
                    }

                    finally
                    {
                        if (hostedServiceCreated)
                        {
                            _testFixture.ComputeClient.HostedServices.DeleteAll(serviceName);
                        }
                    }
                }
            }
        }
Exemple #22
0
        public void AddAndRemoveNetworkSecurityGroupToRole()
        {
            using (var undoContext = UndoContext.Current)
            {
                undoContext.Start();
                using (NetworkTestBase _testFixture = new NetworkTestBase())
                {
                    // setup
                    bool storageAccountCreated = false;
                    bool hostedServiceCreated  = false;

                    string serviceName    = _testFixture.GenerateRandomName();
                    string deploymentName = _testFixture.GenerateRandomName();
                    string roleName       = "WebRole1";
                    string location       = _testFixture.ManagementClient.GetDefaultLocation("Storage", "Compute", "PersistentVMRole");

                    string storageAccountName = _testFixture.GenerateRandomName().ToLower();

                    // create Network Security Group
                    string securityGroupName     = _testFixture.GenerateRandomNetworkSecurityGroupName();
                    string securityGroupLabel    = _testFixture.GenerateRandomName();
                    string securityGroupLocation = "North Central US";
                    _testFixture.CreateNetworkSecurityGroup(securityGroupName, securityGroupLabel, securityGroupLocation);

                    _testFixture.CreateStorageAccount(location, storageAccountName, out storageAccountCreated);
                    _testFixture.SetSimpleVirtualNetwork();
                    _testFixture.CreateHostedService(location, serviceName, out hostedServiceCreated);
                    var deployment = _testFixture.CreatePaaSDeployment(
                        storageAccountName,
                        serviceName,
                        deploymentName,
                        NetworkTestConstants.OneWebOneWorkerPkgFilePath,
                        NetworkTestConstants.VnetOneWebOneWorkerCscfgFilePath,
                        startDeployment: true);

                    try
                    {
                        // action 1
                        var associationParams = new NetworkSecurityGroupAddAssociationParameters(securityGroupName);
                        _testFixture.NetworkClient.NetworkSecurityGroups.AddToRole(serviceName, deploymentName, roleName,
                                                                                   associationParams);

                        // assert 1
                        NetworkSecurityGroupGetAssociationResponse response =
                            _testFixture.NetworkClient.NetworkSecurityGroups.GetForRole(serviceName, deploymentName, roleName);
                        Assert.Equal(associationParams.Name, response.Name);

                        // action 2
                        _testFixture.NetworkClient.NetworkSecurityGroups.RemoveFromRole(
                            serviceName,
                            deploymentName,
                            roleName,
                            securityGroupName);

                        // assert 2
                        Assert.Throws <CloudException>(() =>
                                                       _testFixture.NetworkClient.NetworkSecurityGroups.GetForRole(serviceName, deploymentName, roleName));
                    }
                    finally
                    {
                        if (storageAccountCreated)
                        {
                            _testFixture.StorageClient.StorageAccounts.Delete(storageAccountName);
                        }
                        if (hostedServiceCreated)
                        {
                            _testFixture.ComputeClient.HostedServices.DeleteAll(serviceName);
                        }
                    }
                }
            }
        }
        private static void DisassociateReservedIP(NetworkTestBase _testFixture, string reserveIpName, string serviceName, string deploymentName)
        {
            NetworkReservedIPMobilityParameters pars = new NetworkReservedIPMobilityParameters
            {
                ServiceName = serviceName,
                DeploymentName = deploymentName
            };
            OperationStatusResponse responseDisassociateRip = _testFixture.NetworkClient.ReservedIPs.Disassociate(reserveIpName, pars);
            Assert.True(responseDisassociateRip.StatusCode == HttpStatusCode.OK);

            NetworkReservedIPGetResponse receivedReservedIpFromRdfe =
                _testFixture.NetworkClient.ReservedIPs.Get(reserveIpName);

            Assert.True(receivedReservedIpFromRdfe.StatusCode == HttpStatusCode.OK);

            Assert.True(string.IsNullOrEmpty(receivedReservedIpFromRdfe.ServiceName));
            Assert.True(receivedReservedIpFromRdfe.InUse == false);
            Assert.True(string.IsNullOrEmpty(receivedReservedIpFromRdfe.DeploymentName));
        }
Exemple #24
0
        public void AddAndRemoveNetworkSecurityGroupToNIC()
        {
            using (var undoContext = UndoContext.Current)
            {
                undoContext.Start();
                using (NetworkTestBase _testFixture = new NetworkTestBase())
                {
                    // setup
                    bool storageAccountCreated = false;
                    bool hostedServiceCreated  = false;

                    string serviceName          = _testFixture.GenerateRandomName();
                    string deploymentName       = _testFixture.GenerateRandomName();
                    string roleName             = _testFixture.GenerateRandomName();
                    string networkInterfaceName = _testFixture.GenerateRandomName();
                    string location             = _testFixture.ManagementClient.GetDefaultLocation("Storage", "Compute", "PersistentVMRole");
                    string virtualNetworkName   = "virtualNetworkSiteName";
                    string subnetName           = "FrontEndSubnet5";

                    string storageAccountName = _testFixture.GenerateRandomName().ToLower();

                    // create Network Security Group
                    string securityGroupName     = _testFixture.GenerateRandomNetworkSecurityGroupName();
                    string securityGroupLabel    = _testFixture.GenerateRandomName();
                    string securityGroupLocation = "North Central US";
                    _testFixture.CreateNetworkSecurityGroup(securityGroupName, securityGroupLabel, securityGroupLocation);

                    _testFixture.CreateStorageAccount(location, storageAccountName, out storageAccountCreated);
                    _testFixture.SetSimpleVirtualNetwork();
                    _testFixture.CreateHostedService(location, serviceName, out hostedServiceCreated);
                    _testFixture.ComputeClient.VirtualMachines.CreateDeployment(
                        serviceName,
                        _testFixture.CreateMultiNICIaaSDeploymentParameters(
                            serviceName,
                            deploymentName,
                            roleName,
                            networkInterfaceName,
                            storageAccountName,
                            virtualNetworkName,
                            subnetName));

                    try
                    {
                        // action 1
                        var associationParams = new NetworkSecurityGroupAddAssociationParameters(securityGroupName);
                        _testFixture.NetworkClient.NetworkSecurityGroups.AddToNetworkInterface(
                            serviceName,
                            deploymentName,
                            roleName,
                            networkInterfaceName,
                            associationParams);

                        // assert 1
                        NetworkSecurityGroupGetAssociationResponse response =
                            _testFixture.NetworkClient.NetworkSecurityGroups.GetForNetworkInterface(
                                serviceName,
                                deploymentName,
                                roleName,
                                networkInterfaceName);
                        Assert.Equal(associationParams.Name, response.Name);

                        // action 2
                        _testFixture.NetworkClient.NetworkSecurityGroups.RemoveFromNetworkInterface(
                            serviceName,
                            deploymentName,
                            roleName,
                            networkInterfaceName,
                            securityGroupName);

                        // assert 2
                        Assert.Throws <CloudException>(() => _testFixture.NetworkClient.NetworkSecurityGroups.GetForNetworkInterface(
                                                           serviceName,
                                                           deploymentName,
                                                           roleName,
                                                           networkInterfaceName));
                    }

                    finally
                    {
                        if (hostedServiceCreated)
                        {
                            _testFixture.ComputeClient.HostedServices.DeleteAll(serviceName);
                        }
                    }
                }
            }
        }
        public void SetIPForwardingOnNICSucceeds()
        {
            using (var undoContext = UndoContext.Current)
            {
                undoContext.Start();
                using (NetworkTestBase _testFixture = new NetworkTestBase())
                {
                    // setup
                    bool storageAccountCreated = false;
                    bool hostedServiceCreated  = false;

                    string serviceName          = _testFixture.GenerateRandomName();
                    string deploymentName       = _testFixture.GenerateRandomName();
                    string roleName             = _testFixture.GenerateRandomName();
                    string networkInterfaceName = _testFixture.GenerateRandomName();
                    string location             = _testFixture.ManagementClient.GetDefaultLocation("Storage", "Compute", "PersistentVMRole");
                    string virtualNetworkName   = "virtualNetworkSiteName";
                    string subnetName           = "FrontEndSubnet5";

                    string storageAccountName = _testFixture.GenerateRandomName().ToLower();

                    _testFixture.CreateStorageAccount(location, storageAccountName, out storageAccountCreated);
                    _testFixture.SetSimpleVirtualNetwork();
                    _testFixture.CreateHostedService(location, serviceName, out hostedServiceCreated);
                    _testFixture.ComputeClient.VirtualMachines.CreateDeployment(
                        serviceName,
                        _testFixture.CreateMultiNICIaaSDeploymentParameters(
                            serviceName,
                            deploymentName,
                            roleName,
                            networkInterfaceName,
                            storageAccountName,
                            virtualNetworkName,
                            subnetName));

                    try
                    {
                        // action
                        var ipForwardingState = "Enabled";
                        var ipForwarding      = new IPForwardingSetParameters(ipForwardingState);

                        _testFixture.NetworkClient.IPForwarding.SetOnNetworkInterface(
                            serviceName,
                            deploymentName,
                            roleName,
                            networkInterfaceName,
                            ipForwarding);

                        // assert
                        IPForwardingGetResponse response = _testFixture.NetworkClient.IPForwarding.GetForNetworkInterface(
                            serviceName,
                            deploymentName,
                            roleName,
                            networkInterfaceName);
                        Assert.Equal(ipForwardingState, response.State);
                    }
                    finally
                    {
                        if (hostedServiceCreated)
                        {
                            _testFixture.ComputeClient.HostedServices.DeleteAll(serviceName);
                        }
                    }
                }
            }
        }
        public void TestDeleteNetworkSecurityGroup()
        {
            using (var undoContext = UndoContext.Current)
            {
                undoContext.Start();
                using (NetworkTestBase _testFixture = new NetworkTestBase())
                {
                    // setup
                    string securityGroupName = _testFixture.GenerateRandomNetworkSecurityGroupName();
                    string securityGroupLabel = _testFixture.GenerateRandomName();
                    string securityGroupLocation = _testFixture.DefaultLocation;

                    _testFixture.CreateNetworkSecurityGroup(securityGroupName, securityGroupLabel, securityGroupLocation);
                    NetworkSecurityGroupGetResponse getResponse = _testFixture.NetworkClient.NetworkSecurityGroups.Get(securityGroupName, null);

                    Assert.Equal(securityGroupName, getResponse.Name);

                    var beforeDeletionListResponse = _testFixture.NetworkClient.NetworkSecurityGroups.List();

                    // action
                    _testFixture.DeleteNetworkSecurityGroup(securityGroupName);

                    // assert
                    Assert.Throws<CloudException>(() => _testFixture.NetworkClient.NetworkSecurityGroups.Get(securityGroupName, null));

                    var afterDeletionListResponse = _testFixture.NetworkClient.NetworkSecurityGroups.List();
                    Assert.Equal(beforeDeletionListResponse.NetworkSecurityGroups.Count, afterDeletionListResponse.NetworkSecurityGroups.Count + 1);
                }
            }
        }
        public void TestListNetworkSecurityGroup()
        {
            using (var undoContext = UndoContext.Current)
            {
                undoContext.Start();
                using (NetworkTestBase _testFixture = new NetworkTestBase())
                {
                    // setup
                    string securityGroupName = _testFixture.GenerateRandomNetworkSecurityGroupName();
                    string securityGroupLabel = _testFixture.GenerateRandomName();
                    string securityGroupLocation = _testFixture.DefaultLocation;

                    // action
                    int networkSecurityGroupCount = _testFixture.NetworkClient.NetworkSecurityGroups.List().Count();
                    _testFixture.CreateNetworkSecurityGroup(securityGroupName, securityGroupLabel, securityGroupLocation);
                    var response = _testFixture.NetworkClient.NetworkSecurityGroups.List();

                    // assert
                    Assert.Equal(networkSecurityGroupCount + 1, response.Count());
                }
            }
        }
Exemple #28
0
        public void CreateVMWithNetworkSecurityGroupOnNIC()
        {
            using (var undoContext = UndoContext.Current)
            {
                undoContext.Start();
                using (NetworkTestBase _testFixture = new NetworkTestBase())
                {
                    // setup
                    bool storageAccountCreated = false;
                    bool hostedServiceCreated  = false;

                    string serviceName          = _testFixture.GenerateRandomName();
                    string deploymentName       = _testFixture.GenerateRandomName();
                    string roleName             = _testFixture.GenerateRandomName();
                    string networkInterfaceName = _testFixture.GenerateRandomName();
                    string location             = _testFixture.ManagementClient.GetDefaultLocation("Storage", "Compute", "PersistentVMRole");
                    string virtualNetworkName   = "virtualNetworkSiteName";
                    string subnetName           = "FrontEndSubnet5";

                    string storageAccountName = _testFixture.GenerateRandomName().ToLower();

                    // create Network Security Group
                    string securityGroupName     = _testFixture.GenerateRandomNetworkSecurityGroupName();
                    string securityGroupLabel    = _testFixture.GenerateRandomName();
                    string securityGroupLocation = "North Central US";
                    _testFixture.CreateNetworkSecurityGroup(securityGroupName, securityGroupLabel, securityGroupLocation);

                    _testFixture.CreateStorageAccount(location, storageAccountName, out storageAccountCreated);
                    _testFixture.SetSimpleVirtualNetwork();
                    _testFixture.CreateHostedService(location, serviceName, out hostedServiceCreated);

                    var multiNICVMDeployment = _testFixture.CreateMultiNICIaaSDeploymentParameters(
                        serviceName,
                        deploymentName,
                        roleName,
                        networkInterfaceName,
                        storageAccountName,
                        virtualNetworkName,
                        subnetName);

                    var configurationSets = multiNICVMDeployment.Roles.Single(
                        r => string.Equals(r.RoleName, roleName)).ConfigurationSets;

                    configurationSets
                    .Single(
                        cs => string.Equals(cs.ConfigurationSetType, ConfigurationSetTypes.NetworkConfiguration))
                    .NetworkInterfaces.Single(nic => string.Equals(nic.Name, networkInterfaceName))
                    .NetworkSecurityGroup = securityGroupName;

                    try
                    {
                        // action 1: create Deployment with NSG
                        _testFixture.ComputeClient.VirtualMachines.CreateDeployment(
                            serviceName,
                            multiNICVMDeployment);

                        // assert 1
                        NetworkSecurityGroupGetAssociationResponse response =
                            _testFixture.NetworkClient.NetworkSecurityGroups.GetForNetworkInterface(
                                serviceName,
                                deploymentName,
                                roleName,
                                networkInterfaceName);
                        Assert.Equal(securityGroupName, response.Name);

                        var deployment = _testFixture.ComputeClient.Deployments.GetBySlot(serviceName,
                                                                                          DeploymentSlot.Production);

                        Assert.Equal(
                            securityGroupName,
                            deployment.Roles.Single(r => string.Equals(r.RoleName, roleName))
                            .ConfigurationSets.Single(
                                cs =>
                                string.Equals(cs.ConfigurationSetType,
                                              ConfigurationSetTypes.NetworkConfiguration))
                            .NetworkInterfaces.Single(nic => string.Equals(nic.Name, networkInterfaceName))
                            .NetworkSecurityGroup);

                        // action 2: update deployment without NSG
                        configurationSets
                        .Single(
                            cs => string.Equals(cs.ConfigurationSetType, ConfigurationSetTypes.NetworkConfiguration))
                        .NetworkInterfaces.Single(nic => string.Equals(nic.Name, networkInterfaceName))
                        .NetworkSecurityGroup = null;

                        _testFixture.ComputeClient.VirtualMachines.Update(serviceName, deploymentName, roleName,
                                                                          new VirtualMachineUpdateParameters()
                        {
                            RoleName          = roleName,
                            ConfigurationSets = configurationSets,
                            OSVirtualHardDisk = _testFixture.GetOSVirtualHardDisk(storageAccountName, serviceName)
                        });

                        // assert 2
                        deployment = _testFixture.ComputeClient.Deployments.GetBySlot(serviceName,
                                                                                      DeploymentSlot.Production);

                        Assert.Null(
                            deployment.Roles.Single(r => string.Equals(r.RoleName, roleName))
                            .ConfigurationSets.Single(
                                cs =>
                                string.Equals(cs.ConfigurationSetType,
                                              ConfigurationSetTypes.NetworkConfiguration))
                            .NetworkInterfaces.Single(nic => string.Equals(nic.Name, networkInterfaceName))
                            .NetworkSecurityGroup);
                    }

                    finally
                    {
                        if (hostedServiceCreated)
                        {
                            _testFixture.ComputeClient.HostedServices.DeleteAll(serviceName);
                        }
                    }
                }
            }
        }
        public void TestAssociateDisassociateOnMultivipIaaSDeployment()
        {
            using (var undoContext = AZT.UndoContext.Current)
            {
                undoContext.Start();
                using (NetworkTestBase _testFixture = new NetworkTestBase())
                {
                    bool hostedServiceCreated = false;
                    bool storageAccountCreated = false;
                    string storageAccountName = HttpMockServer.GetAssetName("tststr1234", "tststr").ToLower();
                    string serviceName = AZT.TestUtilities.GenerateName("testser");
                    string deploymentName = AZT.TestUtilities.GenerateName("dep");

                    ComputeManagementClient computeClient = _testFixture.GetComputeManagementClient();
                    ManagementClient managementClient = _testFixture.ManagementClient;
                    StorageManagementClient storageClient = _testFixture.GetStorageManagementClient();
                    List<string> createdRips = new List<string>();

                    try
                    {
                        string location = Utilities.GetTestLocation(managementClient);
                        Assert.True(!string.IsNullOrEmpty(location));

                        // Create hosted service
                        Utilities.CreateHostedService(location, computeClient, serviceName, out hostedServiceCreated);
                        Assert.True(hostedServiceCreated);

                        // Create storage account
                        storageAccountName = HttpMockServer.GetAssetName("tststr1234", "tststr").ToLower();
                        Utilities.CreateStorageAccount(location, storageClient, storageAccountName,
                            out storageAccountCreated);
                        Assert.True(storageAccountCreated);


                        List<string> vipNames = new List<string>()
                        {
                            AZT.TestUtilities.GenerateName("VipA"),
                            AZT.TestUtilities.GenerateName("VipB"),
                            AZT.TestUtilities.GenerateName("VipC"),
                            AZT.TestUtilities.GenerateName("VipD"),
                            AZT.TestUtilities.GenerateName("VipE")
                        };

                        List<string> reservedIPNames = new List<string>()
                        {
                            AZT.TestUtilities.GenerateName("RipA"),
                            AZT.TestUtilities.GenerateName("RipB"),
                            AZT.TestUtilities.GenerateName("RipC"),
                            AZT.TestUtilities.GenerateName("RipD"),
                            AZT.TestUtilities.GenerateName("RipE")
                        };

                        CreateMultivipDeploymentAndAssertSuccess(_testFixture.NetworkClient, computeClient,
                            vipNames, serviceName, deploymentName, storageAccountName, location);

                        // Associate 5 reserved IPs
                        for (int i = 0; i < 5; i++)
                        {
                            string reserveIpName = reservedIPNames[i];
                            string vipName = vipNames[i];
                            NetworkReservedIPCreateParameters reservedIpCreatePars = new NetworkReservedIPCreateParameters
                            {
                                Name = reserveIpName,
                                Location = location,
                                Label = "SampleReserveIPLabel"
                            };

                            OperationStatusResponse reserveIpCreate = _testFixture.NetworkClient.ReservedIPs.Create(reservedIpCreatePars);
                            Assert.True(reserveIpCreate.StatusCode == HttpStatusCode.OK);
                            createdRips.Add(reserveIpName);

                            NetworkReservedIPGetResponse reserveIpCreationResponse =
                                _testFixture.NetworkClient.ReservedIPs.Get(reserveIpName);

                            Assert.True(reserveIpCreationResponse.StatusCode == HttpStatusCode.OK);

                            NetworkReservedIPMobilityParameters pars = new NetworkReservedIPMobilityParameters
                            {
                                ServiceName = serviceName,
                                DeploymentName = deploymentName,
                                VirtualIPName = vipName
                            };
                            OperationStatusResponse responseAssociateRip = _testFixture.NetworkClient.ReservedIPs.Associate(reserveIpName, pars);
                            Assert.True(responseAssociateRip.StatusCode == HttpStatusCode.OK);
                            DeploymentGetResponse deploymentResponse =
                                computeClient.Deployments.GetByName(serviceName: serviceName,
                                    deploymentName: deploymentName);

                            NetworkReservedIPGetResponse receivedReservedIpFromRdfe =
                                _testFixture.NetworkClient.ReservedIPs.Get(reserveIpName);

                            Assert.True(receivedReservedIpFromRdfe.StatusCode == HttpStatusCode.OK);

                            Assert.True(serviceName == receivedReservedIpFromRdfe.ServiceName);
                            Assert.True(receivedReservedIpFromRdfe.InUse == true);
                            Assert.True(deploymentName == receivedReservedIpFromRdfe.DeploymentName);
                            Assert.True(reserveIpName == receivedReservedIpFromRdfe.Name);
                            Assert.True(vipName == receivedReservedIpFromRdfe.VirtualIPName);
                            var vipAssociated = deploymentResponse.VirtualIPAddresses.FirstOrDefault(vip => vip.Name == vipName);
                            Assert.NotNull(vipAssociated);
                            Assert.True(vipAssociated.ReservedIPName == reserveIpName);
                        }

                        // Disassociate the associated IPs
                        for (int i = 0; i < 5; i++)
                        {
                            string reserveIpName = reservedIPNames[i];
                            string vipName = vipNames[i];

                            NetworkReservedIPMobilityParameters pars = new NetworkReservedIPMobilityParameters
                            {
                                ServiceName = serviceName,
                                DeploymentName = deploymentName,
                                VirtualIPName = vipName
                            };

                            OperationStatusResponse responseDisassociateRip = _testFixture.NetworkClient.ReservedIPs.Disassociate(reserveIpName, pars);
                            Assert.True(responseDisassociateRip.StatusCode == HttpStatusCode.OK);
                            DeploymentGetResponse deploymentResponse =
                                computeClient.Deployments.GetByName(serviceName: serviceName,
                                    deploymentName: deploymentName);

                            NetworkReservedIPGetResponse receivedReservedIpFromRdfe =
                                _testFixture.NetworkClient.ReservedIPs.Get(reserveIpName);

                            Assert.True(receivedReservedIpFromRdfe.StatusCode == HttpStatusCode.OK);

                            Assert.True(string.IsNullOrEmpty(receivedReservedIpFromRdfe.ServiceName));
                            Assert.True(receivedReservedIpFromRdfe.InUse == false);
                            Assert.True(string.IsNullOrEmpty(receivedReservedIpFromRdfe.DeploymentName));
                            Assert.True(reserveIpName == receivedReservedIpFromRdfe.Name);
                            Assert.True(string.IsNullOrEmpty(receivedReservedIpFromRdfe.VirtualIPName));
                            var vipAssociated = deploymentResponse.VirtualIPAddresses.FirstOrDefault(vip => vip.Name == vipName);
                            Assert.NotNull(vipAssociated);
                            Assert.True(string.IsNullOrEmpty(vipAssociated.ReservedIPName));
                        }
                    }
                    finally
                    {
                        if (hostedServiceCreated)
                        {
                            computeClient.HostedServices.DeleteAll(serviceName);
                        }
                        if (createdRips.Any())
                        {
                            foreach (var rip in createdRips)
                            {
                                // Clean up created Reserved IPs
                                _testFixture.NetworkClient.ReservedIPs.Delete(rip);
                            }
                        }
                    }
                }
            }
        }
        public void TestAddAndRemoveVip()
        {
            using (var undoContext = AZT.UndoContext.Current)
            {
                undoContext.Start();
                using (NetworkTestBase _testFixture = new NetworkTestBase())
                {
                    bool hostedServiceCreated = false;
                    bool storageAccountCreated = false;
                    string storageAccountName = HttpMockServer.GetAssetName("tststr1234", "tststr").ToLower();
                    string serviceName = AZT.TestUtilities.GenerateName("testser");
                    string virtualIPName1 = AZT.TestUtilities.GenerateName("vip1");
                    string virtualIPName2 = AZT.TestUtilities.GenerateName("vip2");
                    string virtualIPName3 = AZT.TestUtilities.GenerateName("vip2");
                    string deploymentName = AZT.TestUtilities.GenerateName("dep");

                    ComputeManagementClient computeClient = _testFixture.GetComputeManagementClient();
                    ManagementClient managementClient = _testFixture.ManagementClient;
                    StorageManagementClient storageClient = _testFixture.GetStorageManagementClient();

                    try
                    {
                        string location = Utilities.GetTestLocation(managementClient);
                        Assert.True(!string.IsNullOrEmpty(location));

                        // Create hosted service
                        Utilities.CreateHostedService(location, computeClient, serviceName, out hostedServiceCreated);
                        Assert.True(hostedServiceCreated);

                        // Create storage account
                        storageAccountName = HttpMockServer.GetAssetName("tststr1234", "tststr").ToLower();
                        Utilities.CreateStorageAccount(location, storageClient, storageAccountName,
                            out storageAccountCreated);
                        Assert.True(storageAccountCreated);

                        // Create a new VM
                        Utilities.CreateAzureVirtualMachine(computeClient, serviceName, deploymentName, storageAccountName,
                            "blob.core.windows.net");

                        // Add and assert vip status
                        OperationStatusResponse virtualIPCreate1 =
                            _testFixture.NetworkClient.VirtualIPs.Add(serviceName: serviceName,
                                deploymentName: deploymentName, virtualIPName: virtualIPName1);

                        Assert.True(virtualIPCreate1.StatusCode == HttpStatusCode.OK);

                        Utilities.AssertLogicalVipWithoutIPPresentOrAbsent(computeClient, serviceName: serviceName,
                            deploymentName: deploymentName, virtualIPName: virtualIPName1, expectedVipCount: 2,
                            present: true);

                        OperationStatusResponse virtualIPCreate2 =
                            _testFixture.NetworkClient.VirtualIPs.Add(serviceName: serviceName,
                                deploymentName: deploymentName, virtualIPName: virtualIPName2);

                        Assert.True(virtualIPCreate2.StatusCode == HttpStatusCode.OK);

                        Utilities.AssertLogicalVipWithoutIPPresentOrAbsent(computeClient, serviceName: serviceName,
                            deploymentName: deploymentName, virtualIPName: virtualIPName2, expectedVipCount: 3,
                            present: true);

                        OperationStatusResponse virtualIPCreate3 =
                            _testFixture.NetworkClient.VirtualIPs.Add(serviceName: serviceName,
                                deploymentName: deploymentName, virtualIPName: virtualIPName3);

                        Assert.True(virtualIPCreate3.StatusCode == HttpStatusCode.OK);

                        Utilities.AssertLogicalVipWithoutIPPresentOrAbsent(computeClient, serviceName: serviceName,
                            deploymentName: deploymentName, virtualIPName: virtualIPName3, expectedVipCount: 4,
                            present: true);

                        // Remove and assert vip status
                        OperationStatusResponse virtualIPRemove1 =
                            _testFixture.NetworkClient.VirtualIPs.Remove(serviceName: serviceName,
                                deploymentName: deploymentName, virtualIPName: virtualIPName1);

                        Assert.True(virtualIPRemove1.StatusCode == HttpStatusCode.OK);

                        Utilities.AssertLogicalVipWithoutIPPresentOrAbsent(computeClient, serviceName: serviceName,
                            deploymentName: deploymentName, virtualIPName: virtualIPName1, expectedVipCount: 3,
                            present: false);

                        OperationStatusResponse virtualIPRemove3 =
                            _testFixture.NetworkClient.VirtualIPs.Remove(serviceName: serviceName,
                                deploymentName: deploymentName, virtualIPName: virtualIPName3);

                        Assert.True(virtualIPRemove3.StatusCode == HttpStatusCode.OK);

                        Utilities.AssertLogicalVipWithoutIPPresentOrAbsent(computeClient, serviceName: serviceName,
                            deploymentName: deploymentName, virtualIPName: virtualIPName3, expectedVipCount: 2,
                            present: false);

                        OperationStatusResponse virtualIPRemove2 =
                            _testFixture.NetworkClient.VirtualIPs.Remove(serviceName: serviceName,
                                deploymentName: deploymentName, virtualIPName: virtualIPName2);

                        Assert.True(virtualIPRemove2.StatusCode == HttpStatusCode.OK);

                        Utilities.AssertLogicalVipWithoutIPPresentOrAbsent(computeClient, serviceName: serviceName,
                            deploymentName: deploymentName, virtualIPName: virtualIPName2, expectedVipCount: 1,
                            present: false);
                    }
                    finally
                    {
                        if (hostedServiceCreated)
                        {
                            computeClient.HostedServices.DeleteAll(serviceName);
                        }
                    }
                }
            }
        }
        private static void AssociateReservedIP(ManagementClient managementClient, string usWestLocStr, string location,
            StorageManagementClient storageClient, string storageAccountName, ref bool storageAccountCreated,
            ComputeManagementClient computeClient, string serviceName, string deploymentName, string reserveIpName,
            NetworkTestBase _testFixture, ref bool hostedServiceCreated, ref bool reserveIpCreated)
        {
            if (managementClient.Locations.List().Any(
                c => string.Equals(c.Name, usWestLocStr, StringComparison.OrdinalIgnoreCase)))
            {
                location = usWestLocStr;
            }

            CreateStorageAccount(location, storageClient, storageAccountName, out storageAccountCreated);

            CreateHostedService(location, computeClient, serviceName, out hostedServiceCreated);

            CreatePaaSDeployment(storageAccountName, computeClient, serviceName, deploymentName);

            NetworkReservedIPCreateParameters reservedIpCreatePars = new NetworkReservedIPCreateParameters
            {
                Name = reserveIpName,
                Location = "uswest",
                Label = "SampleReserveIPLabel"
            };

            OperationStatusResponse reserveIpCreate = _testFixture.NetworkClient.ReservedIPs.Create(reservedIpCreatePars);
            Assert.True(reserveIpCreate.StatusCode == HttpStatusCode.OK);
            reserveIpCreated = true;

            NetworkReservedIPGetResponse reserveIpCreationResponse =
                _testFixture.NetworkClient.ReservedIPs.Get(reserveIpName);

            Assert.True(reserveIpCreationResponse.StatusCode == HttpStatusCode.OK);


            NetworkReservedIPMobilityParameters pars = new NetworkReservedIPMobilityParameters
            {
                ServiceName = serviceName,
                DeploymentName = deploymentName
            };
            OperationStatusResponse responseAssociateRip = _testFixture.NetworkClient.ReservedIPs.Associate(reserveIpName, pars);
            Assert.True(responseAssociateRip.StatusCode == HttpStatusCode.OK);

            NetworkReservedIPGetResponse receivedReservedIpFromRdfe =
                _testFixture.NetworkClient.ReservedIPs.Get(reserveIpName);

            Assert.True(receivedReservedIpFromRdfe.StatusCode == HttpStatusCode.OK);

            Assert.True(serviceName == receivedReservedIpFromRdfe.ServiceName);
            Assert.True(receivedReservedIpFromRdfe.InUse == true);
            Assert.True(deploymentName == receivedReservedIpFromRdfe.DeploymentName);
        }
        public void SetIPForwardingOnNICSucceeds()
        {
            using (var undoContext = UndoContext.Current)
            {
                undoContext.Start();
                using (NetworkTestBase _testFixture = new NetworkTestBase())
                {
                    // setup
                    bool storageAccountCreated = false;
                    bool hostedServiceCreated = false;

                    string serviceName = _testFixture.GenerateRandomName();
                    string deploymentName = _testFixture.GenerateRandomName();
                    string roleName = _testFixture.GenerateRandomName();
                    string networkInterfaceName = _testFixture.GenerateRandomName();
                    string location = _testFixture.ManagementClient.GetDefaultLocation("Storage", "Compute", "PersistentVMRole");
                    string virtualNetworkName = "virtualNetworkSiteName";
                    string subnetName = "FrontEndSubnet5";

                    string storageAccountName = _testFixture.GenerateRandomName().ToLower();

                    _testFixture.CreateStorageAccount(location, storageAccountName, out storageAccountCreated);
                    _testFixture.SetSimpleVirtualNetwork();
                    _testFixture.CreateHostedService(location, serviceName, out hostedServiceCreated);
                    _testFixture.ComputeClient.VirtualMachines.CreateDeployment(
                        serviceName,
                        _testFixture.CreateMultiNICIaaSDeploymentParameters(
                            serviceName,
                            deploymentName,
                            roleName,
                            networkInterfaceName,
                            storageAccountName,
                            virtualNetworkName,
                            subnetName));

                    try
                    {
                        // action
                        var ipForwardingState = "Enabled";
                        var ipForwarding = new IPForwardingSetParameters(ipForwardingState);

                        _testFixture.NetworkClient.IPForwarding.SetOnNetworkInterface(
                            serviceName,
                            deploymentName,
                            roleName,
                            networkInterfaceName,
                            ipForwarding);

                        // assert
                        IPForwardingGetResponse response = _testFixture.NetworkClient.IPForwarding.GetForNetworkInterface(
                            serviceName,
                            deploymentName,
                            roleName,
                            networkInterfaceName);
                        Assert.Equal(ipForwardingState, response.State);
                    }
                    finally
                    {
                        if (hostedServiceCreated)
                        {
                            _testFixture.ComputeClient.HostedServices.DeleteAll(serviceName);
                        }
                    }
                }
            }
        }
        public void TestAdditionalVipLifeCycle()
        {
            using (var undoContext = AZT.UndoContext.Current)
            {
                undoContext.Start();
                using (NetworkTestBase _testFixture = new NetworkTestBase())
                {
                    bool hostedServiceCreated = false;
                    bool storageAccountCreated = false;
                    string storageAccountName = HttpMockServer.GetAssetName("tststr1234", "tststr").ToLower();
                    string serviceName = AZT.TestUtilities.GenerateName("testser");
                    string virtualIPName1 = AZT.TestUtilities.GenerateName("vip1");
                    string deploymentName = AZT.TestUtilities.GenerateName("dep");

                    ComputeManagementClient computeClient = _testFixture.GetComputeManagementClient();
                    ManagementClient managementClient = _testFixture.ManagementClient;
                    StorageManagementClient storageClient = _testFixture.GetStorageManagementClient();

                    try
                    {
                        string location = Utilities.GetTestLocation(managementClient);
                        Assert.True(!string.IsNullOrEmpty(location));

                        // Create hosted service
                        Utilities.CreateHostedService(location, computeClient, serviceName, out hostedServiceCreated);
                        Assert.True(hostedServiceCreated);

                        // Create storage account
                        storageAccountName = HttpMockServer.GetAssetName("tststr1234", "tststr").ToLower();
                        Utilities.CreateStorageAccount(location, storageClient, storageAccountName,
                            out storageAccountCreated);
                        Assert.True(storageAccountCreated);

                        // Create a new VM
                        Utilities.CreateAzureVirtualMachine(computeClient, serviceName, deploymentName,
                            storageAccountName, "blob.core.windows.net");

                        DeploymentGetResponse depRetrieved =
                            computeClient.Deployments.GetByName(serviceName: serviceName, deploymentName: deploymentName);

                        IEnumerable<ConfigurationSet> endpointCfgSets = new List<ConfigurationSet>
                        {
                            new ConfigurationSet
                            {
                                ConfigurationSetType = "NetworkConfiguration",
                                InputEndpoints =
                                    new List<InputEndpoint>
                                    {
                                        new InputEndpoint()
                                        {
                                            LocalPort = 3387,
                                            Name = "RDP2",
                                            Port = 52777,
                                            Protocol = InputEndpointTransportProtocol.Tcp,
                                            EnableDirectServerReturn = false
                                        },
                                    }
                            }
                        };

                        // Update with single endpoint

                        var updateParams = Utilities.GetVMUpdateParameters(depRetrieved.Roles.First(),
                            storageAccountName, endpointCfgSets, preserveOriginalConfigSets: false);

                        computeClient.VirtualMachines.Update(
                        serviceName,
                        deploymentName,
                        depRetrieved.Roles.First().RoleName,
                        updateParams);

                        // Add and assert vip status
                        OperationStatusResponse virtualIPCreate1 =
                            _testFixture.NetworkClient.VirtualIPs.Add(serviceName: serviceName,
                                deploymentName: deploymentName, virtualIPName: virtualIPName1);

                        Assert.True(virtualIPCreate1.StatusCode == HttpStatusCode.OK);

                        depRetrieved = Utilities.AssertLogicalVipWithoutIPPresentOrAbsent(computeClient, serviceName: serviceName,
                            deploymentName: deploymentName, virtualIPName: virtualIPName1, expectedVipCount: 2,
                            present: true);

                        endpointCfgSets = new List<ConfigurationSet>
                        {
                            new ConfigurationSet
                            {
                                ConfigurationSetType = "NetworkConfiguration",
                                InputEndpoints =
                                    new List<InputEndpoint>
                                    {
                                        new InputEndpoint()
                                        {
                                            LocalPort = 3387,
                                            Name = "RDP2",
                                            Port = 52777,
                                            Protocol = InputEndpointTransportProtocol.Tcp,
                                            EnableDirectServerReturn = false,
                                        },
                                        new InputEndpoint()
                                        {
                                            LocalPort = 3379,
                                            Name = "RDP",
                                            Port = 52728,
                                            Protocol = InputEndpointTransportProtocol.Tcp,
                                            EnableDirectServerReturn = false,
                                            VirtualIPName = virtualIPName1,
                                        }
                                    
                                    }
                            }
                        };

                        updateParams = Utilities.GetVMUpdateParameters(depRetrieved.Roles.First(),
                            storageAccountName, endpointCfgSets, preserveOriginalConfigSets: false);

                        computeClient.VirtualMachines.Update(
                        serviceName,
                        deploymentName,
                        depRetrieved.Roles.First().RoleName,
                        updateParams);

                        var depRetrievedAfterUpdate = Utilities.AssertLogicalVipWithIPPresent(computeClient, serviceName: serviceName,
                            deploymentName: deploymentName, virtualIPName: virtualIPName1, expectedVipCount: 2);

                        endpointCfgSets = new List<ConfigurationSet>
                        {
                            new ConfigurationSet
                            {
                                ConfigurationSetType = "NetworkConfiguration",
                                InputEndpoints =
                                    new List<InputEndpoint>
                                    {
                                        new InputEndpoint()
                                        {
                                            LocalPort = 3387,
                                            Name = "RDP2",
                                            Port = 52777,
                                            Protocol = InputEndpointTransportProtocol.Tcp,
                                            EnableDirectServerReturn = false,
                                        },
                                    }
                            }
                        };

                        updateParams = Utilities.GetVMUpdateParameters(depRetrieved.Roles.First(),
                            storageAccountName, endpointCfgSets, preserveOriginalConfigSets: false);
                        computeClient.VirtualMachines.Update(
                        serviceName,
                        deploymentName,
                        depRetrieved.Roles.First().RoleName,
                        updateParams);

                        depRetrieved = Utilities.AssertLogicalVipWithoutIPPresentOrAbsent(computeClient,
                            serviceName: serviceName,
                            deploymentName: deploymentName, virtualIPName: virtualIPName1, expectedVipCount: 2,
                            present: true);

                        // Remove and assert vip status
                        OperationStatusResponse virtualIPRemove1 =
                            _testFixture.NetworkClient.VirtualIPs.Remove(serviceName: serviceName,
                                deploymentName: deploymentName, virtualIPName: virtualIPName1);

                        Assert.True(virtualIPRemove1.StatusCode == HttpStatusCode.OK);

                        Utilities.AssertLogicalVipWithoutIPPresentOrAbsent(computeClient, serviceName: serviceName,
                            deploymentName: deploymentName, virtualIPName: virtualIPName1, expectedVipCount: 3,
                            present: false);
                    }
                    finally
                    {
                        if (hostedServiceCreated)
                        {
                            computeClient.HostedServices.DeleteAll(serviceName);
                        }
                    }
                }
            }
        }
        public void AddAndRemoveNetworkSecurityGroupToSubnet()
        {
            using (var undoContext = UndoContext.Current)
            {
                undoContext.Start();
                using (NetworkTestBase _testFixture = new NetworkTestBase())
                {
                    //setup

                    // create Network Security Group
                    string securityGroupName = _testFixture.GenerateRandomNetworkSecurityGroupName();
                    string securityGroupLabel = _testFixture.GenerateRandomName();
                    string securityGroupLocation = "North Central US";

                    _testFixture.CreateNetworkSecurityGroup(securityGroupName, securityGroupLabel, securityGroupLocation);

                    // create vnet with subnet
                    string vnetName = "virtualNetworkSiteName";
                    string subnetName = "FrontEndSubnet5";
                    _testFixture.SetSimpleVirtualNetwork();

                    NetworkSecurityGroupAddAssociationParameters parameters = new NetworkSecurityGroupAddAssociationParameters()
                    {
                        Name = securityGroupName
                    };

                    // action
                    _testFixture.NetworkClient.NetworkSecurityGroups.AddToSubnet(vnetName, subnetName, parameters);
                    var listNetworkResponse = _testFixture.NetworkClient.Networks.List();

                    // assert
                    var getResponse = _testFixture.NetworkClient.NetworkSecurityGroups.GetForSubnet(vnetName, subnetName);
                    Assert.Equal(securityGroupName, getResponse.Name);
                    Assert.Equal(listNetworkResponse.VirtualNetworkSites.First(vnet =>
                        vnetName.Equals(vnet.Name)).Subnets.First(subnet =>
                        subnetName.Equals(subnet.Name))
                        .NetworkSecurityGroup, securityGroupName);

                    // action
                    _testFixture.NetworkClient.NetworkSecurityGroups.RemoveFromSubnet(vnetName, subnetName, securityGroupName);


                    // assert
                    Assert.Throws<CloudException>(() => _testFixture.NetworkClient.NetworkSecurityGroups.GetForSubnet(vnetName, subnetName));
                }
            }
        }
        public void TestDisassociateReserveIP()
        {
            using (var undoContext = AZT.UndoContext.Current)
            {
                undoContext.Start();
                using (NetworkTestBase _testFixture = new NetworkTestBase())
                {
                    ComputeManagementClient computeClient = _testFixture.GetComputeManagementClient();
                    StorageManagementClient storageClient = _testFixture.GetStorageManagementClient();
                    var managementClient = _testFixture.ManagementClient;
                    bool storageAccountCreated = false;
                    bool hostedServiceCreated = false;
                    bool reserveIpCreated = false;

                    string storageAccountName = HttpMockServer.GetAssetName("teststorage1234", "teststorage").ToLower();
                    string serviceName = AZT.TestUtilities.GenerateName("testsvc");
                    string deploymentName = string.Format("{0}Prod", serviceName);
                    string reserveIpName = HttpMockServer.GetAssetName("rip", "testrip").ToLower();
                    string location = managementClient.GetDefaultLocation("Storage", "Compute");
                    const string usWestLocStr = "West US";
                    try
                    {
                        _testFixture.AssociateReservedIP(
                            usWestLocStr,
                            location,
                            storageAccountName,
                            ref storageAccountCreated,
                            serviceName,
                            deploymentName,
                            reserveIpName,
                            ref hostedServiceCreated,
                            ref reserveIpCreated);

                        Assert.True(storageAccountCreated);
                        Assert.True(hostedServiceCreated);
                        Assert.True(reserveIpCreated);
                        DisassociateReservedIP(_testFixture, reserveIpName, serviceName, deploymentName);

                    }
                    catch (Exception)
                    {
                        throw;
                    }
                    finally
                    {
                        if (storageAccountCreated)
                        {
                            storageClient.StorageAccounts.Delete(storageAccountName);
                        }
                        if (hostedServiceCreated)
                        {
                            computeClient.HostedServices.DeleteAll(serviceName);
                        }
                        if (reserveIpCreated)
                        {
                            _testFixture.NetworkClient.ReservedIPs.Delete(reserveIpName);
                        }
                    }
                }
            }
        }
        public void AddAndRemoveNetworkSecurityGroupToRole()
        {
            using (var undoContext = UndoContext.Current)
            {
                undoContext.Start();
                using (NetworkTestBase _testFixture = new NetworkTestBase())
                {
                    // setup
                    bool storageAccountCreated = false;
                    bool hostedServiceCreated = false;

                    string serviceName = _testFixture.GenerateRandomName();
                    string deploymentName = _testFixture.GenerateRandomName();
                    string roleName = "WebRole1";
                    string location = _testFixture.ManagementClient.GetDefaultLocation("Storage", "Compute", "PersistentVMRole");

                    string storageAccountName = _testFixture.GenerateRandomName().ToLower();

                    // create Network Security Group
                    string securityGroupName = _testFixture.GenerateRandomNetworkSecurityGroupName();
                    string securityGroupLabel = _testFixture.GenerateRandomName();
                    string securityGroupLocation = "North Central US";
                    _testFixture.CreateNetworkSecurityGroup(securityGroupName, securityGroupLabel, securityGroupLocation);

                    _testFixture.CreateStorageAccount(location, storageAccountName, out storageAccountCreated);
                    _testFixture.SetSimpleVirtualNetwork();
                    _testFixture.CreateHostedService(location, serviceName, out hostedServiceCreated);
                    var deployment = _testFixture.CreatePaaSDeployment(
                        storageAccountName,
                        serviceName,
                        deploymentName,
                        NetworkTestConstants.OneWebOneWorkerPkgFilePath,
                        NetworkTestConstants.VnetOneWebOneWorkerCscfgFilePath);

                    try
                    {
                        // action 1
                        var associationParams = new NetworkSecurityGroupAddAssociationParameters(securityGroupName);
                        _testFixture.NetworkClient.NetworkSecurityGroups.AddToRole(serviceName, deploymentName, roleName,
                            associationParams);

                        // assert 1
                        NetworkSecurityGroupGetAssociationResponse response =
                            _testFixture.NetworkClient.NetworkSecurityGroups.GetForRole(serviceName, deploymentName, roleName);
                        Assert.Equal(associationParams.Name, response.Name);

                        // action 2
                        _testFixture.NetworkClient.NetworkSecurityGroups.RemoveFromRole(
                            serviceName,
                            deploymentName,
                            roleName,
                            securityGroupName);

                        // assert 2
                        Assert.Throws<CloudException>(() =>
                            _testFixture.NetworkClient.NetworkSecurityGroups.GetForRole(serviceName, deploymentName, roleName));
                    }
                    finally
                    {
                        if (storageAccountCreated)
                        {
                            _testFixture.StorageClient.StorageAccounts.Delete(storageAccountName);
                        }
                        if (hostedServiceCreated)
                        {
                            _testFixture.ComputeClient.HostedServices.DeleteAll(serviceName);
                        }
                    }
                }
            }
        }
        public void TestReservingExistingDeploymentIP()
        {
            using (var undoContext = AZT.UndoContext.Current)
            {
                undoContext.Start();
                using (NetworkTestBase _testFixture = new NetworkTestBase())
                {
                    var managementClient = _testFixture.ManagementClient;
                    bool storageAccountCreated = false;
                    bool hostedServiceCreated = false;
                    string storageAccountName = HttpMockServer.GetAssetName("tststr1234", "tststr").ToLower();
                    string serviceName = AZT.TestUtilities.GenerateName("testser");
                    string deploymentName = string.Format("{0}Prd", serviceName);
                    string reserveIpName = HttpMockServer.GetAssetName("res", "testres").ToLower();
                    string location = managementClient.GetDefaultLocation("Storage", "Compute");
                    bool reservedIpCreated = false;
                    try
                    {
                        _testFixture.CreateStorageAccount(location, storageAccountName, out storageAccountCreated);

                        _testFixture.CreateHostedService(location, serviceName, out hostedServiceCreated);

                        var deployment = _testFixture.CreatePaaSDeployment(storageAccountName, serviceName, deploymentName, NetworkTestConstants.OneWebOneWorkerPkgFilePath, "OneWebOneWorker.cscfg");

                        NetworkReservedIPCreateParameters reservedIpCreatePars = new NetworkReservedIPCreateParameters
                        {
                            Name = reserveIpName,
                            Label = "TestLabel",
                            DeploymentName = deploymentName,
                            ServiceName = serviceName,
                            Location = "uswest"
                        };

                        OperationStatusResponse reserveIpCreate = _testFixture.NetworkClient.ReservedIPs.Create(reservedIpCreatePars);
                        Assert.True(reserveIpCreate.StatusCode == HttpStatusCode.OK);

                        reservedIpCreated = true;
                        NetworkReservedIPGetResponse reserveIpCreationResponse =
                            _testFixture.NetworkClient.ReservedIPs.Get(reserveIpName);


                        Assert.True(reserveIpCreationResponse.StatusCode == HttpStatusCode.OK);

                        Assert.True(reserveIpCreationResponse.ServiceName == serviceName);
                        Assert.True(reserveIpCreationResponse.DeploymentName == deploymentName);
                        Assert.True(reserveIpCreationResponse.InUse == true);
                        Assert.True(reserveIpCreationResponse.Address == deployment.VirtualIPAddresses[0].Address);
                    }
                    catch (Exception)
                    {
                        throw;
                    }
                    finally
                    {
                        if (storageAccountCreated)
                        {
                            _testFixture.StorageClient.StorageAccounts.Delete(storageAccountName);
                        }
                        if (hostedServiceCreated)
                        {
                            _testFixture.ComputeClient.HostedServices.DeleteAll(serviceName);
                        }
                        if (reservedIpCreated)
                        {
                            _testFixture.NetworkClient.ReservedIPs.Delete(reserveIpName);
                        }
                    }
                }
            }
        }
        public void AddAndRemoveNetworkSecurityGroupToNIC()
        {
            using (var undoContext = UndoContext.Current)
            {
                undoContext.Start();
                using (NetworkTestBase _testFixture = new NetworkTestBase())
                {
                    // setup
                    bool storageAccountCreated = false;
                    bool hostedServiceCreated = false;

                    string serviceName = _testFixture.GenerateRandomName();
                    string deploymentName = _testFixture.GenerateRandomName();
                    string roleName = _testFixture.GenerateRandomName();
                    string networkInterfaceName = _testFixture.GenerateRandomName();
                    string location = _testFixture.ManagementClient.GetDefaultLocation("Storage", "Compute", "PersistentVMRole");
                    string virtualNetworkName = "virtualNetworkSiteName";
                    string subnetName = "FrontEndSubnet5";

                    string storageAccountName = _testFixture.GenerateRandomName().ToLower();

                    // create Network Security Group
                    string securityGroupName = _testFixture.GenerateRandomNetworkSecurityGroupName();
                    string securityGroupLabel = _testFixture.GenerateRandomName();
                    string securityGroupLocation = "North Central US";
                    _testFixture.CreateNetworkSecurityGroup(securityGroupName, securityGroupLabel, securityGroupLocation);

                    _testFixture.CreateStorageAccount(location, storageAccountName, out storageAccountCreated);
                    _testFixture.SetSimpleVirtualNetwork();
                    _testFixture.CreateHostedService(location, serviceName, out hostedServiceCreated);
                    _testFixture.ComputeClient.VirtualMachines.CreateDeployment(
                        serviceName,
                        _testFixture.CreateMultiNICIaaSDeploymentParameters(
                            serviceName,
                            deploymentName,
                            roleName,
                            networkInterfaceName,
                            storageAccountName,
                            virtualNetworkName,
                            subnetName));

                    try
                    {
                        // action 1
                        var associationParams = new NetworkSecurityGroupAddAssociationParameters(securityGroupName);
                        _testFixture.NetworkClient.NetworkSecurityGroups.AddToNetworkInterface(
                            serviceName,
                            deploymentName,
                            roleName,
                            networkInterfaceName,
                            associationParams);

                        // assert 1
                        NetworkSecurityGroupGetAssociationResponse response =
                            _testFixture.NetworkClient.NetworkSecurityGroups.GetForNetworkInterface(
                            serviceName,
                            deploymentName,
                            roleName,
                            networkInterfaceName);
                        Assert.Equal(associationParams.Name, response.Name);

                        // action 2
                        _testFixture.NetworkClient.NetworkSecurityGroups.RemoveFromNetworkInterface(
                            serviceName,
                            deploymentName,
                            roleName,
                            networkInterfaceName,
                            securityGroupName);

                        // assert 2
                        Assert.Throws<CloudException>(() =>_testFixture.NetworkClient.NetworkSecurityGroups.GetForNetworkInterface(
                                serviceName,
                                deploymentName,
                                roleName,
                                networkInterfaceName));
                    }

                    finally
                    {
                        if (hostedServiceCreated)
                        {
                            _testFixture.ComputeClient.HostedServices.DeleteAll(serviceName);
                        }
                    }
                }
            }
        }