public static void CleanUpAssembly()
        {
            vmPowershellCmdlets = new ServiceManagementCmdletTestHelper();

            var affGroup = vmPowershellCmdlets.GetAzureAffinityGroup();

            if (affGroup.Count > 0)
            {
                foreach (var aff in affGroup)
                {
                    try
                    {
                        vmPowershellCmdlets.RemoveAzureAffinityGroup(aff.Name);
                    }
                    catch (Exception e)
                    {
                        if (e.ToString().Contains(BadRequestException))
                        {
                            Console.WriteLine("Affinity Group, {0}, is not deleted.", aff.Name);
                        }
                    }
                }
            }

            if (defaultAzureSubscription != null)
            {
                Retry(String.Format("Get-AzureDisk | Where {{$_.DiskName.Contains(\"{0}\")}} | Remove-AzureDisk -DeleteVhd", serviceNamePrefix), "in use");
                if (deleteDefaultStorageAccount)
                {
                    //vmPowershellCmdlets.RemoveAzureStorageAccount(defaultAzureSubscription.CurrentStorageAccountName);
                }
            }
        }
        private static void Retry(string cmdlet, string message, int maxTry = 1, int intervalSecond = 10)
        {
            ServiceManagementCmdletTestHelper pscmdlet = new ServiceManagementCmdletTestHelper();

            for (int i = 0; i < maxTry; i++)
            {
                try
                {
                    pscmdlet.RunPSScript(cmdlet);
                    break;
                }
                catch (Exception e)
                {
                    if (i == maxTry)
                    {
                        Console.WriteLine("Max try reached.  Couldn't perform within the given time.");
                    }
                    if (e.ToString().Contains(message))
                    {
                        //Thread.Sleep(intervalSecond * 1000);
                        continue;
                    }
                    else
                    {
                        throw;
                    }
                }
            }
        }
Ejemplo n.º 3
0
        public static bool GetAzureVMAndWaitForReady(string serviceName, string vmName, int waitTime, int maxWaitTime)
        {
            Console.WriteLine("Waiting for the vm {0} to reach \"ReadyRole\" ");
            ServiceManagementCmdletTestHelper vmPowershellCmdlets = new ServiceManagementCmdletTestHelper();
            DateTime startTime  = DateTime.Now;
            DateTime MaxEndTime = startTime.AddMilliseconds(maxWaitTime);

            while (true)
            {
                Console.WriteLine("Getting vm '{0}' details:", vmName);
                var vmRoleContext = vmPowershellCmdlets.GetAzureVM(vmName, serviceName);
                Console.WriteLine("Current status of the VM is {0} ", vmRoleContext.InstanceStatus);
                if (vmRoleContext.InstanceStatus == "ReadyRole")
                {
                    Console.WriteLine("Instance status reached expected ReadyRole state. Exiting wait.");
                    return(true);
                }
                else
                {
                    if (DateTime.Compare(DateTime.Now, MaxEndTime) > 0)
                    {
                        Console.WriteLine("Maximum wait time reached and instance status didnt reach \"ReadyRole\" state. Exiting wait. ");
                        return(false);
                    }
                    else
                    {
                        Console.WriteLine("Waiting for {0} seconds for the {1} status to be ReadyRole", waitTime / 1000, vmName);
                        Thread.Sleep(waitTime);
                    }
                }
            }
        }
        public static void SetTestSettings()
        {
            vmPowershellCmdlets = new ServiceManagementCmdletTestHelper();
            CredentialHelper.GetTestSettings(Resource.TestSettings);

            vmPowershellCmdlets.RemoveAzureSubscriptions();
            vmPowershellCmdlets.ImportAzurePublishSettingsFile(CredentialHelper.PublishSettingsFile);

            if (string.IsNullOrEmpty(CredentialHelper.DefaultSubscriptionName))
            {
                defaultAzureSubscription = vmPowershellCmdlets.GetCurrentAzureSubscription();
                if (string.IsNullOrEmpty(Resource.DefaultSubscriptionName))
                {
                    CredentialHelper.DefaultSubscriptionName = defaultAzureSubscription.SubscriptionName;
                }
            }
            else
            {
                defaultAzureSubscription = vmPowershellCmdlets.SetDefaultAzureSubscription(CredentialHelper.DefaultSubscriptionName);
            }

            locationName = vmPowershellCmdlets.GetAzureLocationName(new[] { CredentialHelper.Location }); // Get-AzureLocation

            if (String.IsNullOrEmpty(locationName))
            {
                Console.WriteLine("No location is selected!");
            }
            Console.WriteLine("Location Name: {0}", locationName);

            if (defaultAzureSubscription.CurrentStorageAccountName == null && !string.IsNullOrEmpty(CredentialHelper.DefaultStorageName))
            {
                SetDefaultStorage();
            }

            try
            {
                imageName = vmPowershellCmdlets.GetAzureVMImageName(new[] { "Windows" }, false); // Get-AzureVMImage
            }
            catch
            {
                Console.WriteLine("Error occurred during Get-AzureVMImageName... imageName is not set.");
            }

            if (String.IsNullOrEmpty(imageName))
            {
                Console.WriteLine("No image is selected!");
            }
            else
            {
                Console.WriteLine("Image Name: {0}", imageName);
            }
        }
Ejemplo n.º 5
0
        public void CleanUp()
        {
            vmPowershellCmdlets = new ServiceManagementCmdletTestHelper();
            try
            {
                vmPowershellCmdlets.RunPSScript("Get-AzureService | Remove-AzureService -DeleteAll -Force");
            }
            catch
            {
            }

            try
            {
                vmPowershellCmdlets.RunPSScript("Get-AzureDisk | Remove-AzureDisk -DeleteVHD");
            }
            catch
            {
            }

            try
            {
                vmPowershellCmdlets.RunPSScript(@"Get-AzureVMImage | where {$_.Category -eq 'User'} | Remove-AzureVMImage -DeleteVHD");
            }
            catch
            {
            }

            try
            {
                vmPowershellCmdlets.RunPSScript("Remove-AzureVNetConfig");
            }
            catch
            {
            }

            try
            {
                vmPowershellCmdlets.RunPSScript("Get-AzureAffinityGroup | Remove-AzureAffinityGroup");
            }
            catch
            {
            }

            try
            {
                vmPowershellCmdlets.RunPSScript("Get-AzureReservedIP | Remove-AzureReservedIP -Force");
            }
            catch
            {
            }
        }
Ejemplo n.º 6
0
        public void CleanUp()
        {
            vmPowershellCmdlets = new ServiceManagementCmdletTestHelper();
            try
            {
                vmPowershellCmdlets.RunPSScript("Get-AzureService | Remove-AzureService -Force");
            }
            catch
            {
            }

            try
            {
                vmPowershellCmdlets.RunPSScript("Get-AzureDisk | Remove-AzureDisk");
            }
            catch
            {
            }

            try
            {
                vmPowershellCmdlets.RunPSScript(@"Get-AzureVMImage | where {$_.Category -eq 'User'} | Remove-AzureVMImage");
            }
            catch
            {
            }

            try
            {
                vmPowershellCmdlets.RunPSScript("Remove-AzureVNetConfig");
            }
            catch
            {
            }

            try
            {
                vmPowershellCmdlets.RunPSScript("Get-AzureAffinityGroup | Remove-AzureAffinityGroup");
            }
            catch
            {
            }

            try
            {
                vmPowershellCmdlets.RunPSScript("Get-AzureReservedIP | Remove-AzureReservedIP -Force");
            }
            catch
            {
            }
        }
Ejemplo n.º 7
0
        public static void CopyTestData(string srcContainer, string srcBlob, string destContainer, string destBlob = null)
        {
            ServiceManagementCmdletTestHelper vmPowershellCmdlets = new ServiceManagementCmdletTestHelper();
            Process          currentProcess = Process.GetCurrentProcess();
            StringDictionary environment    = currentProcess.StartInfo.EnvironmentVariables;

            string storageAccount    = environment[CredentialHelper.StorageAccountVariable];
            string storageAccountKey = environment[CredentialHelper.StorageAccountKeyVariable];

            // Create a container
            try
            {
                vmPowershellCmdlets.RunPSScript(String.Format("{0}-{1} -Name {2}",
                                                              VerbsCommon.Get, "AzureStorageContainer", destContainer));
            }
            catch
            {
                // Create a container.
                vmPowershellCmdlets.RunPSScript(String.Format("{0}-{1} -Name {2}",
                                                              VerbsCommon.New, "AzureStorageContainer", destContainer));
            }

            // Make SAS Uri for the source blob.
            string srcSasUri = Utilities.GenerateSasUri(CredentialHelper.CredentialBlobUriFormat, storageAccount, storageAccountKey, srcContainer, srcBlob);

            if (string.IsNullOrEmpty(destBlob))
            {
                vmPowershellCmdlets.RunPSScript(string.Format("{0}-{1} -SrcContainer {2} -SrcBlob {3} -DestContainer {4} -Force",
                                                              VerbsLifecycle.Start, "AzureStorageBlobCopy", srcContainer, srcBlob, destContainer));
                destBlob = srcBlob;
            }
            else
            {
                vmPowershellCmdlets.RunPSScript(string.Format("{0}-{1} -SrcUri \"{2}\" -DestContainer {3} -DestBlob {4} -Force",
                                                              VerbsLifecycle.Start, "AzureStorageBlobCopy", srcSasUri, destContainer, destBlob));
            }

            for (int i = 0; i < 60; i++)
            {
                var result = vmPowershellCmdlets.CheckCopyBlobStatus(destContainer, destBlob);
                if (result.Status.ToString().Equals("Success"))
                {
                    break;
                }
                else
                {
                    System.Threading.Thread.Sleep(10 * 1000);
                }
            }
        }
        [Ignore] // https://github.com/WindowsAzure/azure-sdk-tools/issues/1402
        public void NewWindowsAzureQuickVM()
        {
            powershell.Invoke();

            ServiceManagementCmdletTestHelper vmPowershellCmdlets = new ServiceManagementCmdletTestHelper();
            
            string imageName = vmPowershellCmdlets.GetAzureVMImageName(new[] { "Windows" }, false);
            string locationName = vmPowershellCmdlets.GetAzureLocationName(new[] { Resource.Location });

            string newAzureQuickVMName = Utilities.GetUniqueShortName("PSTestVM");
            string newAzureQuickVMSvcName = Utilities.GetUniqueShortName("PSTestService");

            vmPowershellCmdlets.NewAzureQuickVM(OS.Windows, newAzureQuickVMName, newAzureQuickVMSvcName, imageName, "pstestuser", "p@ssw0rd", locationName);

            // Verify
            PersistentVMRoleContext vmRoleCtxt = vmPowershellCmdlets.GetAzureVM(newAzureQuickVMName, newAzureQuickVMSvcName);
            Assert.AreEqual(newAzureQuickVMName, vmRoleCtxt.Name, true);

            // Cleanup
            vmPowershellCmdlets.RemoveAzureVM(newAzureQuickVMName, newAzureQuickVMSvcName);

            Assert.AreEqual(null, vmPowershellCmdlets.GetAzureVM(newAzureQuickVMName, newAzureQuickVMSvcName));
        }
Ejemplo n.º 9
0
        public static Uri GetDeploymentAndWaitForReady(string serviceName, string slot, int waitTime, int maxWaitTime)
        {
            ServiceManagementCmdletTestHelper vmPowershellCmdlets = new ServiceManagementCmdletTestHelper();

            DateTime startTime = DateTime.Now;

            while (true)
            {
                bool allReady = true;
                DeploymentInfoContext result = vmPowershellCmdlets.GetAzureDeployment(serviceName, slot);
                int    instanceNum           = result.RoleInstanceList.Count;
                bool[] isReady = new bool[instanceNum];

                for (int j = 0; j < instanceNum; j++)
                {
                    var instance = result.RoleInstanceList[j];
                    Console.WriteLine("Instance: {0}, Status: {1}", instance.InstanceName, instance.InstanceStatus);
                    isReady[j] = (instance.InstanceStatus == "ReadyRole");
                    allReady  &= isReady[j];
                }

                if (!allReady && (DateTime.Now - startTime).TotalSeconds < maxWaitTime)
                {
                    Console.WriteLine("Some roles are not ready, waiting for {0} seconds.", waitTime);
                    Thread.Sleep(waitTime * 1000);
                }
                else if (!allReady) // some roles are not ready, and time-out.
                {
                    Assert.Fail("Deployment is not ready within {0} seconds!", maxWaitTime);
                }
                else // all roles are ready
                {
                    Console.WriteLine("Result of the deployment: {0}", result.Status);
                    return(result.Url);
                }
            }
        }
Ejemplo n.º 10
0
        [Ignore] // https://github.com/WindowsAzure/azure-sdk-tools/issues/1402
        public void NewWindowsAzureQuickVM()
        {
            powershell.Invoke();

            ServiceManagementCmdletTestHelper vmPowershellCmdlets = new ServiceManagementCmdletTestHelper();

            string imageName    = vmPowershellCmdlets.GetAzureVMImageName(new[] { "Windows", "testvmimage" }, false);
            string locationName = vmPowershellCmdlets.GetAzureLocationName(new[] { Resource.Location }, false);

            string newAzureQuickVMName    = Utilities.GetUniqueShortName("PSTestVM");
            string newAzureQuickVMSvcName = Utilities.GetUniqueShortName("PSTestService");

            vmPowershellCmdlets.NewAzureQuickVM(OS.Windows, newAzureQuickVMName, newAzureQuickVMSvcName, imageName, "pstestuser", "p@ssw0rd", locationName);

            // Verify
            PersistentVMRoleContext vmRoleCtxt = vmPowershellCmdlets.GetAzureVM(newAzureQuickVMName, newAzureQuickVMSvcName);

            Assert.AreEqual(newAzureQuickVMName, vmRoleCtxt.Name, true);

            // Cleanup
            vmPowershellCmdlets.RemoveAzureVM(newAzureQuickVMName, newAzureQuickVMSvcName);

            Assert.AreEqual(null, vmPowershellCmdlets.GetAzureVM(newAzureQuickVMName, newAzureQuickVMSvcName));
        }
Ejemplo n.º 11
0
        public static void CopyTestData(string srcContainer, string srcBlob, string destContainer, string destBlob = null)
        {
            ServiceManagementCmdletTestHelper vmPowershellCmdlets = new ServiceManagementCmdletTestHelper();
            Process currentProcess = Process.GetCurrentProcess();
            StringDictionary environment = currentProcess.StartInfo.EnvironmentVariables;

            string storageAccount = environment[CredentialHelper.StorageAccountVariable];
            string storageAccountKey = environment[CredentialHelper.StorageAccountKeyVariable];

            // Create a container
            try
            {
                vmPowershellCmdlets.RunPSScript(String.Format("{0}-{1} -Name {2}",
                    VerbsCommon.Get, StorageNouns.Container, destContainer));
            }
            catch
            {
                // Create a container.
                vmPowershellCmdlets.RunPSScript(String.Format("{0}-{1} -Name {2}",
                    VerbsCommon.New, StorageNouns.Container, destContainer));
            }

            // Make SAS Uri for the source blob.
            string srcSasUri = Utilities.GenerateSasUri(CredentialHelper.CredentialBlobUriFormat, storageAccount, storageAccountKey, srcContainer, srcBlob);

            if (string.IsNullOrEmpty(destBlob))
            {
                vmPowershellCmdlets.RunPSScript(string.Format("{0}-{1} -SrcContainer {2} -SrcBlob {3} -DestContainer {4} -Force",
                    VerbsLifecycle.Start, StorageNouns.CopyBlob, srcContainer, srcBlob, destContainer));
                destBlob = srcBlob;
            }
            else
            {
                vmPowershellCmdlets.RunPSScript(string.Format("{0}-{1} -SrcUri \"{2}\" -DestContainer {3} -DestBlob {4} -Force",
                    VerbsLifecycle.Start, StorageNouns.CopyBlob, srcSasUri, destContainer, destBlob));
            }

            for (int i = 0; i < 60; i++)
            {
                var result = vmPowershellCmdlets.CheckCopyBlobStatus(destContainer, destBlob);
                if (result.Status.ToString().Equals("Success"))
                {
                    break;
                }
                else
                {
                    System.Threading.Thread.Sleep(10 * 1000);
                }
            }
        }
Ejemplo n.º 12
0
        public static void SetTestSettings()
        {
            // Please remove this line once all tests are done
            System.Net.ServicePointManager.ServerCertificateValidationCallback +=
                delegate(object sender, System.Security.Cryptography.X509Certificates.X509Certificate certificate,
                    System.Security.Cryptography.X509Certificates.X509Chain chain,
                    System.Net.Security.SslPolicyErrors sslPolicyErrors)
                {
                    return true; // **** Always accept
                };

            vmPowershellCmdlets = new ServiceManagementCmdletTestHelper();
            CredentialHelper.GetTestSettings(Resource.TestSettings);

            vmPowershellCmdlets.RemoveAzureSubscriptions();
            var ussouthEnv = vmPowershellCmdlets.GetAzureEnvironment("ussouth");
            if (ussouthEnv != null && ussouthEnv.Count > 0)
            {
                Console.WriteLine("Removing ussouth environment...");
                vmPowershellCmdlets.RunPSScript("Remove-AzureEnvironment -Name ussouth -Force");
            }

            List<PSAzureEnvironment> environments = vmPowershellCmdlets.GetAzureEnvironment();
            var serviceManagementUrl = GetServiceManagementUrl(CredentialHelper.PublishSettingsFile);
            var subscriptionId = GetSubscriptionId(CredentialHelper.PublishSettingsFile);

            foreach (var env in environments)
            {
                if (!string.IsNullOrEmpty(env.ServiceManagementUrl))
                {
                    if (env.ServiceManagementUrl.Equals(serviceManagementUrl))
                    {
                        currentEnvName = env.Name;
                        var curEnv = vmPowershellCmdlets.GetAzureEnvironment(currentEnvName)[0];
                        Console.WriteLine("Using the existing environment: {0}", currentEnvName);
                        Console.WriteLine("PublichSettingsFileUrl: {0}", curEnv.PublishSettingsFileUrl);
                        Console.WriteLine("ServiceManagement: {0}", curEnv.ServiceManagementUrl);
                        Console.WriteLine("ManagementPortalUrl: {0}", curEnv.ManagementPortalUrl);
                        Console.WriteLine("ActiveDirectory: {0}", curEnv.ActiveDirectoryAuthority);
                        Console.WriteLine("ActiveDirectoryServiceEndpointResourceId: {0}", curEnv.ActiveDirectoryServiceEndpointResourceId);
                        Console.WriteLine("ResourceManager: {0}", curEnv.ResourceManagerUrl);
                        Console.WriteLine("Gallery: {0}", curEnv.GalleryUrl);
                        Console.WriteLine("Graph: {0}", curEnv.GalleryUrl);
                        break;
                    }
                }
            }

            if (string.IsNullOrEmpty(currentEnvName))
            {
                Console.WriteLine("Creating new environment... : {0}", TempEnvName);
                var prodEnv = vmPowershellCmdlets.GetAzureEnvironment("AzureCloud")[0];
                vmPowershellCmdlets.RunPSScript(string.Format(
                    @"Add-AzureEnvironment -Name {0} `
                    -PublishSettingsFileUrl {1} `
                    -ServiceEndpoint {2} `
                    -ManagementPortalUrl {3} `
                    -ActiveDirectoryEndpoint {4} `
                    -ActiveDirectoryServiceEndpointResourceId {5} `
                    -ResourceManagerEndpoint {6} `
                    -GalleryEndpoint {7} `
                    -GraphEndpoint {8}",
                    TempEnvName,
                    prodEnv.PublishSettingsFileUrl,
                    serviceManagementUrl,
                    prodEnv.ManagementPortalUrl,
                    prodEnv.ActiveDirectoryAuthority,
                    prodEnv.ActiveDirectoryServiceEndpointResourceId,
                    prodEnv.ResourceManagerUrl,
                    prodEnv.GalleryUrl,
                    prodEnv.GalleryUrl));

                vmPowershellCmdlets.ImportAzurePublishSettingsFile(CredentialHelper.PublishSettingsFile, TempEnvName);
            }
            else
            {
                Console.WriteLine("Using existing environment... : {0}", currentEnvName);
                vmPowershellCmdlets.ImportAzurePublishSettingsFile(CredentialHelper.PublishSettingsFile, currentEnvName);
            }

            var firstSub = vmPowershellCmdlets.GetAzureSubscription(subscriptionId);
            vmPowershellCmdlets.SelectAzureSubscription(firstSub.SubscriptionId);
            defaultAzureSubscription = vmPowershellCmdlets.GetCurrentAzureSubscription();
            CredentialHelper.DefaultSubscriptionName = defaultAzureSubscription.SubscriptionName;

            locationName = vmPowershellCmdlets.GetAzureLocationName(new[] { CredentialHelper.Location }); // Get-AzureLocation

            if (String.IsNullOrEmpty(locationName))
            {
                Console.WriteLine("No location is selected!");
            }
            Console.WriteLine("Location Name: {0}", locationName);

            if (defaultAzureSubscription.CurrentStorageAccountName == null && !string.IsNullOrEmpty(CredentialHelper.DefaultStorageName))
            {
                SetDefaultStorage();
            }

            try
            {
                imageName = vmPowershellCmdlets.GetAzureVMImageName(new[] { "Windows" }, false); // Get-AzureVMImage
            }
            catch
            {
                Console.WriteLine("Error occurred during Get-AzureVMImageName... imageName is not set.");
            }

            if (String.IsNullOrEmpty(imageName))
            {
                Console.WriteLine("No image is selected!");
            }
            else
            {
                Console.WriteLine("Image Name: {0}", imageName);
            }
        }
Ejemplo n.º 13
0
        public void AzureAffinityGroupTest()
        {
            StartTest(MethodBase.GetCurrentMethod().Name, testStartTime);

            string affinityName1 = Convert.ToString(TestContext.DataRow["affinityName1"]);
            string affinityLabel1 = Convert.ToString(TestContext.DataRow["affinityLabel1"]);
            string location1 = CheckLocation(Convert.ToString(TestContext.DataRow["location1"]));
            string description1 = Convert.ToString(TestContext.DataRow["description1"]);

            string affinityName2 = Convert.ToString(TestContext.DataRow["affinityName2"]);
            string affinityLabel2 = Convert.ToString(TestContext.DataRow["affinityLabel2"]);
            string location2 = CheckLocation(Convert.ToString(TestContext.DataRow["location2"]));
            string description2 = Convert.ToString(TestContext.DataRow["description2"]);

            try
            {
                ServiceManagementCmdletTestHelper vmPowershellCmdlets = new ServiceManagementCmdletTestHelper();

                // Remove previously created affinity groups
                foreach (var aff in vmPowershellCmdlets.GetAzureAffinityGroup(null))
                {
                    if (aff.Name == affinityName1 || aff.Name == affinityName2)
                    {
                        vmPowershellCmdlets.RemoveAzureAffinityGroup(aff.Name);
                    }
                }

                // New-AzureAffinityGroup
                vmPowershellCmdlets.NewAzureAffinityGroup(affinityName1, location1, affinityLabel1, description1);
                vmPowershellCmdlets.NewAzureAffinityGroup(affinityName2, location2, affinityLabel2, description2);
                Console.WriteLine("Affinity groups created: {0}, {1}", affinityName1, affinityName2);

                // Get-AzureAffinityGroup

                pass = AffinityGroupVerify(vmPowershellCmdlets.GetAzureAffinityGroup(affinityName1)[0], affinityName1, affinityLabel1, location1, description1);
                pass &= AffinityGroupVerify(vmPowershellCmdlets.GetAzureAffinityGroup(affinityName2)[0], affinityName2, affinityLabel2, location2, description2);


                // Set-AzureAffinityGroup
                vmPowershellCmdlets.SetAzureAffinityGroup(affinityName2, affinityLabel1, description1);
                Console.WriteLine("update affinity group: {0}", affinityName2);

                pass &= AffinityGroupVerify(vmPowershellCmdlets.GetAzureAffinityGroup(affinityName2)[0], affinityName2, affinityLabel1, location2, description1);


                // Remove-AzureAffinityGroup
                vmPowershellCmdlets.RemoveAzureAffinityGroup(affinityName2);
                pass &= Utilities.CheckRemove(vmPowershellCmdlets.GetAzureAffinityGroup, affinityName2);
                vmPowershellCmdlets.RemoveAzureAffinityGroup(affinityName1);
                pass &= Utilities.CheckRemove(vmPowershellCmdlets.GetAzureAffinityGroup, affinityName1);

            }
            catch (Exception e)
            {
                pass = false;
                Assert.Fail(e.ToString());
            }
        }
        public static void CleanUpAssembly()
        {
            vmPowershellCmdlets = new ServiceManagementCmdletTestHelper();

            // Cleaning up affinity groups
            var affGroup = vmPowershellCmdlets.GetAzureAffinityGroup();
            if (affGroup.Count > 0)
            {
                foreach (var aff in affGroup)
                {
                    try
                    {
                        vmPowershellCmdlets.RemoveAzureAffinityGroup(aff.Name);
                    }
                    catch (Exception e)
                    {
                        if (e.ToString().Contains(BadRequestException))
                        {
                            Console.WriteLine("Affinity Group, {0}, is not deleted.", aff.Name);
                        }
                    }
                }
            }

            if (defaultAzureSubscription != null)
            {
                // Cleaning up virtual disks
                try
                {
                    Retry(String.Format("Get-AzureDisk | Where {{$_.DiskName.Contains(\"{0}\")}} | Remove-AzureDisk", serviceNamePrefix), "in use");
                    if (deleteDefaultStorageAccount)
                    {
                        //vmPowershellCmdlets.RemoveAzureStorageAccount(defaultAzureSubscription.CurrentStorageAccountName);
                    }
                }
                catch
                {
                    Console.WriteLine("Error occurred during cleaning up disks..");
                }

                // Cleaning up vm images
                try
                {
                    vmPowershellCmdlets.RunPSScript("Get-AzureVMImage | Where {$_.Categori -eq \"User\"} | Remove-AzureVMImage");
                }
                catch
                {
                    Console.WriteLine("Error occurred during cleaning up vm images..");
                }

                // Cleaning up reserved ips
                try
                {
                    vmPowershellCmdlets.RunPSScript("Get-AzureReservedIp | Remove-AzureReservedIp -Force");
                }
                catch
                {
                    Console.WriteLine("Error occurred during cleaning up reserved ips..");
                }
            }

            if (string.IsNullOrEmpty(currentEnvName))
            {
                vmPowershellCmdlets.RunPSScript(string.Format("Remove-AzureEnvironment -Name {0} -Force", TempEnvName));
            }
        }
Ejemplo n.º 15
0
        public static void CleanUpAssembly()
        {
            vmPowershellCmdlets = new ServiceManagementCmdletTestHelper();

            var affGroup = vmPowershellCmdlets.GetAzureAffinityGroup();
            if (affGroup.Count > 0)
            {
                foreach (var aff in affGroup)
                {
                    try
                    {
                        vmPowershellCmdlets.RemoveAzureAffinityGroup(aff.Name);
                    }
                    catch (Exception e)
                    {
                        if (e.ToString().Contains(BadRequestException))
                        {
                            Console.WriteLine("Affinity Group, {0}, is not deleted.", aff.Name);
                        }
                    }
                }
            }

            if (defaultAzureSubscription != null)
            {
                Retry(String.Format("Get-AzureDisk | Where {{$_.DiskName.Contains(\"{0}\")}} | Remove-AzureDisk -DeleteVhd", serviceNamePrefix), "in use");
                if (deleteDefaultStorageAccount)
                {
                    //vmPowershellCmdlets.RemoveAzureStorageAccount(defaultAzureSubscription.CurrentStorageAccountName);
                }
            }
        }
        public static void SetTestSettings()
        {
            vmPowershellCmdlets = new ServiceManagementCmdletTestHelper();
            CredentialHelper.GetTestSettings(Resource.TestSettings);

            vmPowershellCmdlets.RemoveAzureSubscriptions();
            if (vmPowershellCmdlets.GetAzureEnvironment("ussouth").Count > 0)
            {
                Console.WriteLine("Removing ussouth environment...");
                vmPowershellCmdlets.RunPSScript("Remove-AzureEnvironment -Name ussouth -Force");
            }

            List<AzureEnvironment> environments =  vmPowershellCmdlets.GetAzureEnvironment();
            var serviceManagementUrl = GetServiceManagementUrl(CredentialHelper.PublishSettingsFile);

            foreach (var env in environments)
            {
                var envServiceManagementUrl = (string) env.Endpoints[AzureEnvironment.Endpoint.ServiceManagement];
                if (!string.IsNullOrEmpty(envServiceManagementUrl))
                {
                    if (envServiceManagementUrl.Equals(serviceManagementUrl))
                    {
                        currentEnvName = env.Name;
                        var curEnv = vmPowershellCmdlets.GetAzureEnvironment(currentEnvName)[0];
                        Console.WriteLine("Using the existing environment: {0}", currentEnvName);
                        Console.WriteLine("PublichSettingsFileUrl: {0}", curEnv.GetEndpoint(AzureEnvironment.Endpoint.PublishSettingsFileUrl));
                        Console.WriteLine("ServiceManagement: {0}", curEnv.GetEndpoint(AzureEnvironment.Endpoint.ServiceManagement));
                        Console.WriteLine("ManagementPortalUrl: {0}", curEnv.GetEndpoint(AzureEnvironment.Endpoint.ManagementPortalUrl));
                        Console.WriteLine("ActiveDirectory: {0}", curEnv.GetEndpoint(AzureEnvironment.Endpoint.ActiveDirectory));
                        Console.WriteLine("ActiveDirectoryServiceEndpointResourceId: {0}", curEnv.GetEndpoint(AzureEnvironment.Endpoint.ActiveDirectoryServiceEndpointResourceId));
                        Console.WriteLine("ResourceManager: {0}", curEnv.GetEndpoint(AzureEnvironment.Endpoint.ResourceManager));
                        Console.WriteLine("Gallery: {0}", curEnv.GetEndpoint(AzureEnvironment.Endpoint.Gallery));
                        Console.WriteLine("Graph: {0}", curEnv.GetEndpoint(AzureEnvironment.Endpoint.Graph));
                        break;
                    }
                }
            }

            if (string.IsNullOrEmpty(currentEnvName))
            {
                Console.WriteLine("Creating new environment... : {0}", TempEnvName);
                var prodEnv = vmPowershellCmdlets.GetAzureEnvironment("AzureCloud")[0];
                vmPowershellCmdlets.RunPSScript(string.Format(
                    @"Add-AzureEnvironment -Name {0} `
                    -PublishSettingsFileUrl {1} `
                    -ServiceEndpoint {2} `
                    -ManagementPortalUrl {3} `
                    -ActiveDirectoryEndpoint {4} `
                    -ActiveDirectoryServiceEndpointResourceId {5} `
                    -ResourceManagerEndpoint {6} `
                    -GalleryEndpoint {7} `
                    -GraphEndpoint {8}",
                    TempEnvName,
                    prodEnv.GetEndpoint(AzureEnvironment.Endpoint.PublishSettingsFileUrl),
                    serviceManagementUrl,
                    prodEnv.GetEndpoint(AzureEnvironment.Endpoint.ManagementPortalUrl),
                    prodEnv.GetEndpoint(AzureEnvironment.Endpoint.ActiveDirectory),
                    prodEnv.GetEndpoint(AzureEnvironment.Endpoint.ActiveDirectoryServiceEndpointResourceId),
                    prodEnv.GetEndpoint(AzureEnvironment.Endpoint.ResourceManager),
                    prodEnv.GetEndpoint(AzureEnvironment.Endpoint.Gallery),
                    prodEnv.GetEndpoint(AzureEnvironment.Endpoint.Graph)));

                vmPowershellCmdlets.ImportAzurePublishSettingsFile(CredentialHelper.PublishSettingsFile, TempEnvName);
            }
            else
            {
                Console.WriteLine("Using existing environment... : {0}", currentEnvName);
                vmPowershellCmdlets.ImportAzurePublishSettingsFile(CredentialHelper.PublishSettingsFile, currentEnvName);
            }

            var firstSub = vmPowershellCmdlets.GetAzureSubscription().First();
            vmPowershellCmdlets.SelectAzureSubscription(firstSub.SubscriptionName);

            if (string.IsNullOrEmpty(CredentialHelper.DefaultSubscriptionName))
            {
                defaultAzureSubscription = vmPowershellCmdlets.GetCurrentAzureSubscription();
                if (string.IsNullOrEmpty(Resource.DefaultSubscriptionName))
                {
                    CredentialHelper.DefaultSubscriptionName = defaultAzureSubscription.SubscriptionName;
                }
            }
            else
            {
                defaultAzureSubscription = vmPowershellCmdlets.SetDefaultAzureSubscription(CredentialHelper.DefaultSubscriptionName);
            }

            locationName = vmPowershellCmdlets.GetAzureLocationName(new[] { CredentialHelper.Location }); // Get-AzureLocation

            if (String.IsNullOrEmpty(locationName))
            {
                Console.WriteLine("No location is selected!");
            }
            Console.WriteLine("Location Name: {0}", locationName);

            if (defaultAzureSubscription.CurrentStorageAccountName == null && !string.IsNullOrEmpty(CredentialHelper.DefaultStorageName))
            {
                SetDefaultStorage();
            }

            try
            {
                imageName = vmPowershellCmdlets.GetAzureVMImageName(new[] { "Windows" }, false); // Get-AzureVMImage
            }
            catch
            {
                Console.WriteLine("Error occurred during Get-AzureVMImageName... imageName is not set.");
            }

            if (String.IsNullOrEmpty(imageName))
            {
                Console.WriteLine("No image is selected!");
            }
            else
            {
                Console.WriteLine("Image Name: {0}", imageName);
            }
        }
Ejemplo n.º 17
0
        public static void CopyTestData(string srcContainer, string srcBlob, string destContainer, string destBlob)
        {
            ServiceManagementCmdletTestHelper vmPowershellCmdlets = new ServiceManagementCmdletTestHelper();
            Process currentProcess = Process.GetCurrentProcess();
            StringDictionary environment = currentProcess.StartInfo.EnvironmentVariables;

            string storageAccount = environment[CredentialHelper.StorageAccountVariable];
            string storageAccountKey = environment[CredentialHelper.StorageAccountKeyVariable];

            // Create a container
            try
            {
                vmPowershellCmdlets.RunPSScript("Get-AzureStorageContainer -Name " + destContainer);
            }
            catch
            {
                // Create a container.
                vmPowershellCmdlets.RunPSScript("New-AzureStorageContainer -Name " + destContainer);
            }

            // Make SAS Uri for the source blob.
            string srcSasUri = Utilities.GenerateSasUri(CredentialHelper.CredentialBlobUriFormat, storageAccount, storageAccountKey, srcContainer, srcBlob);

            vmPowershellCmdlets.RunPSScript(string.Format("Start-AzureStorageBlobCopy -SrcUri \"{0}\" -DestContainer {1} -DestBlob {2} -Force", srcSasUri, destContainer, destBlob));

            for (int i = 0; i < 60; i++)
            {
                var result = vmPowershellCmdlets.CheckCopyBlobStatus(destContainer, destBlob);
                if (result.Status.ToString().Equals("Success"))
                {
                    break;
                }
                else
                {
                    System.Threading.Thread.Sleep(10 * 1000);
                }
            }
        }
        private void VerifyDscExtensionContext(string vmName, ServiceManagementCmdletTestHelper.SetAzureVMDscExtensionArguments expected)
        {
            Utilities.PrintHeader("Verifiying Dsc extension context.");

            try
            {
                var vm = Utilities.GetAzureVM(vmName, this.testServiceName);

                var context = vmPowershellCmdlets.GetAzureVMDscExtension(vm);
                
                Utilities.LogAssert(() => Assert.AreEqual(DscExtensionName, context.ExtensionName), "Verifiying ExtensionName");
                Utilities.LogAssert(() => Assert.AreEqual(DscExtensionPublisher, context.Publisher), "Verifiying Publisher");
                Utilities.LogAssert(() => Assert.AreEqual(DscExtensionName, context.ReferenceName), "Verifiying ReferenceName");
                Utilities.LogAssert(() => Assert.AreEqual(dscExtensionVersion, context.Version), "Verifiying Version");
                Utilities.LogAssert(() => Assert.AreEqual("Enable", context.State),	"Verifiying State");
                Utilities.LogAssert(() => Assert.AreEqual(vmName, context.RoleName), "Verifiying RoleName");

                var expectedContainer = (expected.ContainerName ?? DefaultContainerName) + "/";
                Utilities.LogAssert(() => Assert.IsNotNull(context.ModulesUrl), "Verifiying ModulesUrl is not null");
                var modulesUrl = new Uri(context.ModulesUrl);
                Utilities.LogAssert(() => Assert.AreEqual(3, modulesUrl.Segments.Length), "Verifiying ModulesUrl is well formed");
                Utilities.LogAssert(() => Assert.AreEqual(expectedContainer, modulesUrl.Segments[1]), "Verifiying the container in ModulesUrl");
                Utilities.LogAssert(() => Assert.AreEqual(expected.ConfigurationArchive, modulesUrl.Segments[2]), "Verifiying the configuration in ModulesUrl");

                var expectedConfigurationName = expected.ConfigurationName ?? Path.GetFileNameWithoutExtension(expected.ConfigurationArchive);
                var expectedConfigurationFunction = Path.GetFileNameWithoutExtension(expected.ConfigurationArchive) + "\\" + expectedConfigurationName;
                Utilities.LogAssert(() => Assert.AreEqual(expectedConfigurationFunction, context.ConfigurationFunction), "Verifiying the configuration in ModulesUrl");

                if (expected.ConfigurationArgument == null)
                {
                    Utilities.LogAssert(() => Assert.IsNull(context.Properties), "Verifiying that Properties is null");
                }
                else
                {
                    Utilities.LogAssert(() => Assert.AreEqual(expected.ConfigurationArgument.Count, context.Properties.Count), "Verifiying number of items in Properties");

                    foreach (var key in expected.ConfigurationArgument.Keys)
                    {
                        var k = key;

                        Utilities.LogAssert(() => Assert.AreEqual(expected.ConfigurationArgument[k], context.Properties[k]), "Verifiying Properties[" + key + "]");
                    }
                }
            }
            finally
            {
                Utilities.PrintHeader("Verifiying Dsc extension context.");
            }
        }
Ejemplo n.º 19
0
        public static void SetTestSettings()
        {
            vmPowershellCmdlets = new ServiceManagementCmdletTestHelper();
            CredentialHelper.GetTestSettings(Resource.TestSettings);

            vmPowershellCmdlets.RemoveAzureSubscriptions();
            vmPowershellCmdlets.ImportAzurePublishSettingsFile(CredentialHelper.PublishSettingsFile);

            if (string.IsNullOrEmpty(CredentialHelper.DefaultSubscriptionName))
            {
                defaultAzureSubscription = vmPowershellCmdlets.GetCurrentAzureSubscription();
                if (string.IsNullOrEmpty(Resource.DefaultSubscriptionName))
                {
                    CredentialHelper.DefaultSubscriptionName = defaultAzureSubscription.SubscriptionName;
                }
            }
            else
            {
                defaultAzureSubscription = vmPowershellCmdlets.SetDefaultAzureSubscription(CredentialHelper.DefaultSubscriptionName);
            }

            locationName = vmPowershellCmdlets.GetAzureLocationName(new[] { CredentialHelper.Location }); // Get-AzureLocation
            if (String.IsNullOrEmpty(locationName))
            {
                Console.WriteLine("No location is selected!");
            }
            Console.WriteLine("Location Name: {0}", locationName);

            if (defaultAzureSubscription.CurrentStorageAccountName == null && !string.IsNullOrEmpty(CredentialHelper.DefaultStorageName))
            {
                SetDefaultStorage();
            }

            imageName = vmPowershellCmdlets.GetAzureVMImageName(new[] { "Windows" }, false); // Get-AzureVMImage
            if (String.IsNullOrEmpty(imageName))
            {
                Console.WriteLine("No image is selected!");
            }
            else
            {
                Console.WriteLine("Image Name: {0}", imageName);
            }
        }
        public static void CleanUpAssembly()
        {
            vmPowershellCmdlets = new ServiceManagementCmdletTestHelper();

            // Cleaning up affinity groups
            var affGroup = vmPowershellCmdlets.GetAzureAffinityGroup();

            if (affGroup.Count > 0)
            {
                foreach (var aff in affGroup)
                {
                    try
                    {
                        vmPowershellCmdlets.RemoveAzureAffinityGroup(aff.Name);
                    }
                    catch (Exception e)
                    {
                        if (e.ToString().Contains(BadRequestException))
                        {
                            Console.WriteLine("Affinity Group, {0}, is not deleted.", aff.Name);
                        }
                    }
                }
            }

            if (defaultAzureSubscription != null)
            {
                // Cleaning up virtual disks
                try
                {
                    Retry(String.Format("Get-AzureDisk | Where {{$_.DiskName.Contains(\"{0}\")}} | Remove-AzureDisk", serviceNamePrefix), "in use");
                    if (deleteDefaultStorageAccount)
                    {
                        //vmPowershellCmdlets.RemoveAzureStorageAccount(defaultAzureSubscription.CurrentStorageAccountName);
                    }
                }
                catch
                {
                    Console.WriteLine("Error occurred during cleaning up disks..");
                }

                // Cleaning up vm images
                try
                {
                    vmPowershellCmdlets.RunPSScript("Get-AzureVMImage | Where {$_.Categori -eq \"User\"} | Remove-AzureVMImage");
                }
                catch
                {
                    Console.WriteLine("Error occurred during cleaning up vm images..");
                }

                // Cleaning up reserved ips
                try
                {
                    vmPowershellCmdlets.RunPSScript("Get-AzureReservedIp | Remove-AzureReservedIp -Force");
                }
                catch
                {
                    Console.WriteLine("Error occurred during cleaning up reserved ips..");
                }
            }

            if (string.IsNullOrEmpty(currentEnvName))
            {
                vmPowershellCmdlets.RunPSScript(string.Format("Remove-AzureEnvironment -Name {0} -Force", TempEnvName));
            }
        }
Ejemplo n.º 21
0
        private static void Retry(string cmdlet, string message, int maxTry = 1, int intervalSecond = 10)
        {

            ServiceManagementCmdletTestHelper pscmdlet = new ServiceManagementCmdletTestHelper();

            for (int i = 0; i < maxTry; i++)
            {
                try
                {
                    pscmdlet.RunPSScript(cmdlet);
                    break;
                }
                catch (Exception e)
                {
                    if (i == maxTry)
                    {
                        Console.WriteLine("Max try reached.  Couldn't perform within the given time.");
                    }
                    if (e.ToString().Contains(message))
                    {
                        //Thread.Sleep(intervalSecond * 1000);
                        continue;
                    }
                    else
                    {
                        throw;
                    }
                }
            }
        }
Ejemplo n.º 22
0
        public static void SetTestSettings()
        {
            // Please remove this line once all tests are done
            System.Net.ServicePointManager.ServerCertificateValidationCallback +=
                delegate(object sender, System.Security.Cryptography.X509Certificates.X509Certificate certificate,
                         System.Security.Cryptography.X509Certificates.X509Chain chain,
                         System.Net.Security.SslPolicyErrors sslPolicyErrors)
            {
                return(true);    // **** Always accept
            };

            vmPowershellCmdlets = new ServiceManagementCmdletTestHelper();
            CredentialHelper.GetTestSettings(Resource.TestSettings);

            vmPowershellCmdlets.RemoveAzureSubscriptions();
            if (vmPowershellCmdlets.GetAzureEnvironment("ussouth").Count > 0)
            {
                Console.WriteLine("Removing ussouth environment...");
                vmPowershellCmdlets.RunPSScript("Remove-AzureEnvironment -Name ussouth -Force");
            }

            List <AzureEnvironment> environments = vmPowershellCmdlets.GetAzureEnvironment();
            var serviceManagementUrl             = GetServiceManagementUrl(CredentialHelper.PublishSettingsFile);

            foreach (var env in environments)
            {
                var envServiceManagementUrl = (string)env.Endpoints[AzureEnvironment.Endpoint.ServiceManagement];
                if (!string.IsNullOrEmpty(envServiceManagementUrl))
                {
                    if (envServiceManagementUrl.Equals(serviceManagementUrl))
                    {
                        currentEnvName = env.Name;
                        var curEnv = vmPowershellCmdlets.GetAzureEnvironment(currentEnvName)[0];
                        Console.WriteLine("Using the existing environment: {0}", currentEnvName);
                        Console.WriteLine("PublichSettingsFileUrl: {0}", curEnv.GetEndpoint(AzureEnvironment.Endpoint.PublishSettingsFileUrl));
                        Console.WriteLine("ServiceManagement: {0}", curEnv.GetEndpoint(AzureEnvironment.Endpoint.ServiceManagement));
                        Console.WriteLine("ManagementPortalUrl: {0}", curEnv.GetEndpoint(AzureEnvironment.Endpoint.ManagementPortalUrl));
                        Console.WriteLine("ActiveDirectory: {0}", curEnv.GetEndpoint(AzureEnvironment.Endpoint.ActiveDirectory));
                        Console.WriteLine("ActiveDirectoryServiceEndpointResourceId: {0}", curEnv.GetEndpoint(AzureEnvironment.Endpoint.ActiveDirectoryServiceEndpointResourceId));
                        Console.WriteLine("ResourceManager: {0}", curEnv.GetEndpoint(AzureEnvironment.Endpoint.ResourceManager));
                        Console.WriteLine("Gallery: {0}", curEnv.GetEndpoint(AzureEnvironment.Endpoint.Gallery));
                        Console.WriteLine("Graph: {0}", curEnv.GetEndpoint(AzureEnvironment.Endpoint.Graph));
                        break;
                    }
                }
            }

            if (string.IsNullOrEmpty(currentEnvName))
            {
                Console.WriteLine("Creating new environment... : {0}", TempEnvName);
                var prodEnv = vmPowershellCmdlets.GetAzureEnvironment("AzureCloud")[0];
                vmPowershellCmdlets.RunPSScript(string.Format(
                                                    @"Add-AzureEnvironment -Name {0} `
                    -PublishSettingsFileUrl {1} `
                    -ServiceEndpoint {2} `
                    -ManagementPortalUrl {3} `
                    -ActiveDirectoryEndpoint {4} `
                    -ActiveDirectoryServiceEndpointResourceId {5} `
                    -ResourceManagerEndpoint {6} `
                    -GalleryEndpoint {7} `
                    -GraphEndpoint {8}",
                                                    TempEnvName,
                                                    prodEnv.GetEndpoint(AzureEnvironment.Endpoint.PublishSettingsFileUrl),
                                                    serviceManagementUrl,
                                                    prodEnv.GetEndpoint(AzureEnvironment.Endpoint.ManagementPortalUrl),
                                                    prodEnv.GetEndpoint(AzureEnvironment.Endpoint.ActiveDirectory),
                                                    prodEnv.GetEndpoint(AzureEnvironment.Endpoint.ActiveDirectoryServiceEndpointResourceId),
                                                    prodEnv.GetEndpoint(AzureEnvironment.Endpoint.ResourceManager),
                                                    prodEnv.GetEndpoint(AzureEnvironment.Endpoint.Gallery),
                                                    prodEnv.GetEndpoint(AzureEnvironment.Endpoint.Graph)));

                vmPowershellCmdlets.ImportAzurePublishSettingsFile(CredentialHelper.PublishSettingsFile, TempEnvName);
            }
            else
            {
                Console.WriteLine("Using existing environment... : {0}", currentEnvName);
                vmPowershellCmdlets.ImportAzurePublishSettingsFile(CredentialHelper.PublishSettingsFile, currentEnvName);
            }

            var firstSub = vmPowershellCmdlets.GetAzureSubscription().First();

            vmPowershellCmdlets.SelectAzureSubscription(firstSub.SubscriptionName);

            if (string.IsNullOrEmpty(CredentialHelper.DefaultSubscriptionName))
            {
                defaultAzureSubscription = vmPowershellCmdlets.GetCurrentAzureSubscription();
                if (string.IsNullOrEmpty(Resource.DefaultSubscriptionName))
                {
                    CredentialHelper.DefaultSubscriptionName = defaultAzureSubscription.SubscriptionName;
                }
            }
            else
            {
                defaultAzureSubscription = vmPowershellCmdlets.SetDefaultAzureSubscription(CredentialHelper.DefaultSubscriptionName);
            }

            locationName = vmPowershellCmdlets.GetAzureLocationName(new[] { CredentialHelper.Location }); // Get-AzureLocation

            if (String.IsNullOrEmpty(locationName))
            {
                Console.WriteLine("No location is selected!");
            }
            Console.WriteLine("Location Name: {0}", locationName);

            if (defaultAzureSubscription.CurrentStorageAccountName == null && !string.IsNullOrEmpty(CredentialHelper.DefaultStorageName))
            {
                SetDefaultStorage();
            }

            try
            {
                imageName = vmPowershellCmdlets.GetAzureVMImageName(new[] { "Windows" }, false); // Get-AzureVMImage
            }
            catch
            {
                Console.WriteLine("Error occurred during Get-AzureVMImageName... imageName is not set.");
            }

            if (String.IsNullOrEmpty(imageName))
            {
                Console.WriteLine("No image is selected!");
            }
            else
            {
                Console.WriteLine("Image Name: {0}", imageName);
            }
        }