public void GalleryImageVersion_CRUD_Tests()
        {
            string originalTestLocation = Environment.GetEnvironmentVariable("AZURE_VM_TEST_LOCATION");

            using (MockContext context = MockContext.Start(this.GetType()))
            {
                Environment.SetEnvironmentVariable("AZURE_VM_TEST_LOCATION", galleryHomeLocation);
                EnsureClientsInitialized(context);
                string         rgName    = ComputeManagementTestUtilities.GenerateName(ResourceGroupPrefix);
                VirtualMachine vm        = null;
                string         imageName = ComputeManagementTestUtilities.GenerateName("psTestSourceImage");

                try
                {
                    string sourceImageId = "";
                    vm = CreateCRPImage(rgName, imageName, ref sourceImageId);
                    Assert.False(string.IsNullOrEmpty(sourceImageId));
                    Trace.TraceInformation(string.Format("Created the source image id: {0}", sourceImageId));

                    string  galleryName = ComputeManagementTestUtilities.GenerateName(GalleryNamePrefix);
                    Gallery gallery     = GetTestInputGallery();
                    m_CrpClient.Galleries.CreateOrUpdate(rgName, galleryName, gallery);
                    Trace.TraceInformation(string.Format("Created the gallery: {0} in resource group: {1}", galleryName,
                                                         rgName));
                    string       galleryImageName  = ComputeManagementTestUtilities.GenerateName(GalleryImageNamePrefix);
                    GalleryImage inputGalleryImage = GetTestInputGalleryImage();
                    m_CrpClient.GalleryImages.CreateOrUpdate(rgName, galleryName, galleryImageName, inputGalleryImage);
                    Trace.TraceInformation(string.Format("Created the gallery image: {0} in gallery: {1}", galleryImageName,
                                                         galleryName));

                    string galleryImageVersionName        = "1.0.0";
                    GalleryImageVersion inputImageVersion = GetTestInputGalleryImageVersion(sourceImageId);
                    m_CrpClient.GalleryImageVersions.CreateOrUpdate(rgName, galleryName, galleryImageName,
                                                                    galleryImageVersionName, inputImageVersion);
                    Trace.TraceInformation(string.Format("Created the gallery image version: {0} in gallery image: {1}",
                                                         galleryImageVersionName, galleryImageName));

                    GalleryImageVersion imageVersionFromGet = m_CrpClient.GalleryImageVersions.Get(rgName,
                                                                                                   galleryName, galleryImageName, galleryImageVersionName);
                    Assert.NotNull(imageVersionFromGet);
                    ValidateGalleryImageVersion(inputImageVersion, imageVersionFromGet);
                    imageVersionFromGet = m_CrpClient.GalleryImageVersions.Get(rgName, galleryName, galleryImageName,
                                                                               galleryImageVersionName, ReplicationStatusTypes.ReplicationStatus);
                    Assert.Equal(StorageAccountType.StandardLRS, imageVersionFromGet.PublishingProfile.StorageAccountType);
                    Assert.Equal(StorageAccountType.StandardLRS,
                                 imageVersionFromGet.PublishingProfile.TargetRegions.First().StorageAccountType);
                    Assert.NotNull(imageVersionFromGet.ReplicationStatus);
                    Assert.NotNull(imageVersionFromGet.ReplicationStatus.Summary);

                    inputImageVersion.PublishingProfile.EndOfLifeDate = DateTime.Now.AddDays(100).Date;
                    m_CrpClient.GalleryImageVersions.CreateOrUpdate(rgName, galleryName, galleryImageName,
                                                                    galleryImageVersionName, inputImageVersion);
                    Trace.TraceInformation(string.Format("Updated the gallery image version: {0} in gallery image: {1}",
                                                         galleryImageVersionName, galleryImageName));
                    imageVersionFromGet = m_CrpClient.GalleryImageVersions.Get(rgName, galleryName,
                                                                               galleryImageName, galleryImageVersionName);
                    Assert.NotNull(imageVersionFromGet);
                    ValidateGalleryImageVersion(inputImageVersion, imageVersionFromGet);

                    Trace.TraceInformation("Listing the gallery image versions");
                    IPage <GalleryImageVersion> listGalleryImageVersionsResult = m_CrpClient.GalleryImageVersions.
                                                                                 ListByGalleryImage(rgName, galleryName, galleryImageName);
                    Assert.Single(listGalleryImageVersionsResult);
                    Assert.Null(listGalleryImageVersionsResult.NextPageLink);

                    m_CrpClient.GalleryImageVersions.Delete(rgName, galleryName, galleryImageName, galleryImageVersionName);
                    listGalleryImageVersionsResult = m_CrpClient.GalleryImageVersions.
                                                     ListByGalleryImage(rgName, galleryName, galleryImageName);
                    Assert.Empty(listGalleryImageVersionsResult);
                    Assert.Null(listGalleryImageVersionsResult.NextPageLink);
                    Trace.TraceInformation(string.Format("Deleted the gallery image version: {0} in gallery image: {1}",
                                                         galleryImageVersionName, galleryImageName));

                    ComputeManagementTestUtilities.WaitMinutes(5);
                    m_CrpClient.Images.Delete(rgName, imageName);
                    Trace.TraceInformation("Deleted the CRP image.");
                    m_CrpClient.VirtualMachines.Delete(rgName, vm.Name);
                    Trace.TraceInformation("Deleted the virtual machine.");
                    m_CrpClient.GalleryImages.Delete(rgName, galleryName, galleryImageName);
                    Trace.TraceInformation("Deleted the gallery image.");
                    m_CrpClient.Galleries.Delete(rgName, galleryName);
                    Trace.TraceInformation("Deleted the gallery.");
                }
                finally
                {
                    Environment.SetEnvironmentVariable("AZURE_VM_TEST_LOCATION", originalTestLocation);
                    if (vm != null)
                    {
                        m_CrpClient.VirtualMachines.Delete(rgName, vm.Name);
                    }
                    m_CrpClient.Images.Delete(rgName, imageName);
                }
            }
        }
示例#2
0
        private void TestVMScenarioOperationsInternal(string methodName, bool hasManagedDisks = false, IList <string> zones = null, string vmSize = "Standard_A0",
                                                      string osDiskStorageAccountType         = "Standard_LRS", string dataDiskStorageAccountType = "Standard_LRS", bool?writeAcceleratorEnabled = null,
                                                      bool hasDiffDisks = false, bool callUpdateVM = false)
        {
            using (MockContext context = MockContext.Start(this.GetType().FullName, methodName))
            {
                EnsureClientsInitialized(context);

                ImageReference imageRef = GetPlatformVMImage(useWindowsImage: true);
                const string   expectedOSName = "Windows Server 2012 R2 Datacenter", expectedOSVersion = "Microsoft Windows NT 6.3.9600.0", expectedComputerName = ComputerName;
                // Create resource group
                var            rgName             = ComputeManagementTestUtilities.GenerateName(TestPrefix);
                string         storageAccountName = ComputeManagementTestUtilities.GenerateName(TestPrefix);
                string         asName             = ComputeManagementTestUtilities.GenerateName("as");
                VirtualMachine inputVM;
                try
                {
                    if (!hasManagedDisks)
                    {
                        CreateStorageAccount(rgName, storageAccountName);
                    }

                    CreateVM(rgName, asName, storageAccountName, imageRef, out inputVM, hasManagedDisks: hasManagedDisks, hasDiffDisks: hasDiffDisks, vmSize: vmSize, osDiskStorageAccountType: osDiskStorageAccountType,
                             dataDiskStorageAccountType: dataDiskStorageAccountType, writeAcceleratorEnabled: writeAcceleratorEnabled, zones: zones);

                    // Instance view is not completely populated just after VM is provisioned. So we wait here for a few minutes to
                    // allow GA blob to populate.
                    ComputeManagementTestUtilities.WaitMinutes(2);

                    var getVMWithInstanceViewResponse = m_CrpClient.VirtualMachines.Get(rgName, inputVM.Name, InstanceViewTypes.InstanceView);
                    Assert.True(getVMWithInstanceViewResponse != null, "VM in Get");
                    ValidateVMInstanceView(inputVM, getVMWithInstanceViewResponse, hasManagedDisks, expectedComputerName, expectedOSName, expectedOSVersion);

                    var getVMInstanceViewResponse = m_CrpClient.VirtualMachines.InstanceView(rgName, inputVM.Name);
                    Assert.True(getVMInstanceViewResponse != null, "VM in InstanceView");
                    ValidateVMInstanceView(inputVM, getVMInstanceViewResponse, hasManagedDisks, expectedComputerName, expectedOSName, expectedOSVersion);

                    bool hasUserDefinedAS = zones == null;

                    var listResponse = m_CrpClient.VirtualMachines.List(rgName);
                    ValidateVM(inputVM, listResponse.FirstOrDefault(x => x.Name == inputVM.Name),
                               Helpers.GetVMReferenceId(m_subId, rgName, inputVM.Name), hasManagedDisks, hasUserDefinedAS, writeAcceleratorEnabled, hasDiffDisks);

                    var listVMSizesResponse = m_CrpClient.VirtualMachines.ListAvailableSizes(rgName, inputVM.Name);
                    Helpers.ValidateVirtualMachineSizeListResponse(listVMSizesResponse, hasAZ: zones != null, writeAcceleratorEnabled: writeAcceleratorEnabled, hasDiffDisks: hasDiffDisks);

                    listVMSizesResponse = m_CrpClient.AvailabilitySets.ListAvailableSizes(rgName, asName);
                    Helpers.ValidateVirtualMachineSizeListResponse(listVMSizesResponse, hasAZ: zones != null, writeAcceleratorEnabled: writeAcceleratorEnabled, hasDiffDisks: hasDiffDisks);

                    if (callUpdateVM)
                    {
                        VirtualMachineUpdate updateParams = new VirtualMachineUpdate()
                        {
                            Tags = inputVM.Tags
                        };

                        string updateKey = "UpdateTag";
                        updateParams.Tags.Add(updateKey, "UpdateTagValue");
                        VirtualMachine updateResponse = m_CrpClient.VirtualMachines.Update(rgName, inputVM.Name, updateParams);
                        Assert.True(updateResponse.Tags.ContainsKey(updateKey));
                    }
                }
                finally
                {
                    // Fire and forget. No need to wait for RG deletion completion
                    try
                    {
                        m_ResourcesClient.ResourceGroups.BeginDelete(rgName);
                    }
                    catch (Exception e)
                    {
                        // Swallow this exception so that the original exception is thrown
                        Console.WriteLine(e);
                    }
                }
            }
        }
示例#3
0
        private void TestVMScenarioOperationsInternal(string methodName, bool hasManagedDisks = false, IList <string> zones = null, string vmSize = "Standard_A1_v2",
                                                      string osDiskStorageAccountType         = "Standard_LRS", string dataDiskStorageAccountType = "Standard_LRS", bool?writeAcceleratorEnabled = null,
                                                      bool hasDiffDisks   = false, bool callUpdateVM = false, bool isPpgScenario = false, string diskEncryptionSetId = null, bool?encryptionAtHostEnabled = null,
                                                      string securityType = null, bool isAutomaticPlacementOnDedicatedHostGroupScenario = false, ImageReference imageReference = null, bool validateListAvailableSize = true)
        {
            using (MockContext context = MockContext.Start(this.GetType(), methodName))
            {
                EnsureClientsInitialized(context);

                ImageReference imageRef = imageReference ?? GetPlatformVMImage(useWindowsImage: true);
                const string   expectedOSName = "Windows Server 2012 R2 Datacenter", expectedOSVersion = "Microsoft Windows NT 6.3.9600.0", expectedComputerName = ComputerName;
                // Create resource group
                var    rgName = ComputeManagementTestUtilities.GenerateName(TestPrefix);
                string storageAccountName = ComputeManagementTestUtilities.GenerateName(TestPrefix);
                string asName = ComputeManagementTestUtilities.GenerateName("as");
                string ppgName = null, expectedPpgReferenceId = null;
                string dedicatedHostGroupName = null, dedicatedHostName = null, dedicatedHostGroupReferenceId = null, dedicatedHostReferenceId = null;

                if (isPpgScenario)
                {
                    ppgName = ComputeManagementTestUtilities.GenerateName("ppgtest");
                    expectedPpgReferenceId = Helpers.GetProximityPlacementGroupRef(m_subId, rgName, ppgName);
                }

                if (isAutomaticPlacementOnDedicatedHostGroupScenario)
                {
                    dedicatedHostGroupName        = ComputeManagementTestUtilities.GenerateName("dhgtest");
                    dedicatedHostName             = ComputeManagementTestUtilities.GenerateName("dhtest");
                    dedicatedHostGroupReferenceId = Helpers.GetDedicatedHostGroupRef(m_subId, rgName, dedicatedHostGroupName);
                    dedicatedHostReferenceId      = Helpers.GetDedicatedHostRef(m_subId, rgName, dedicatedHostGroupName, dedicatedHostName);
                }

                VirtualMachine inputVM;
                try
                {
                    if (!hasManagedDisks)
                    {
                        CreateStorageAccount(rgName, storageAccountName);
                    }

                    CreateVM(rgName, asName, storageAccountName, imageRef, out inputVM, hasManagedDisks: hasManagedDisks, hasDiffDisks: hasDiffDisks, vmSize: vmSize, osDiskStorageAccountType: osDiskStorageAccountType,
                             dataDiskStorageAccountType: dataDiskStorageAccountType, writeAcceleratorEnabled: writeAcceleratorEnabled, zones: zones, ppgName: ppgName,
                             diskEncryptionSetId: diskEncryptionSetId, encryptionAtHostEnabled: encryptionAtHostEnabled, securityType: securityType, dedicatedHostGroupReferenceId: dedicatedHostGroupReferenceId,
                             dedicatedHostGroupName: dedicatedHostGroupName, dedicatedHostName: dedicatedHostName);

                    // Instance view is not completely populated just after VM is provisioned. So we wait here for a few minutes to
                    // allow GA blob to populate.
                    ComputeManagementTestUtilities.WaitMinutes(5);

                    var getVMWithInstanceViewResponse = m_CrpClient.VirtualMachines.Get(rgName, inputVM.Name, InstanceViewTypes.InstanceView);
                    Assert.True(getVMWithInstanceViewResponse != null, "VM in Get");

                    if (diskEncryptionSetId != null)
                    {
                        Assert.True(getVMWithInstanceViewResponse.StorageProfile.OsDisk.ManagedDisk.DiskEncryptionSet != null, "OsDisk.ManagedDisk.DiskEncryptionSet is null");
                        Assert.True(string.Equals(diskEncryptionSetId, getVMWithInstanceViewResponse.StorageProfile.OsDisk.ManagedDisk.DiskEncryptionSet.Id, StringComparison.OrdinalIgnoreCase),
                                    "OsDisk.ManagedDisk.DiskEncryptionSet.Id is not matching with expected DiskEncryptionSet resource");

                        Assert.Equal(1, getVMWithInstanceViewResponse.StorageProfile.DataDisks.Count);
                        Assert.True(getVMWithInstanceViewResponse.StorageProfile.DataDisks[0].ManagedDisk.DiskEncryptionSet != null, ".DataDisks.ManagedDisk.DiskEncryptionSet is null");
                        Assert.True(string.Equals(diskEncryptionSetId, getVMWithInstanceViewResponse.StorageProfile.DataDisks[0].ManagedDisk.DiskEncryptionSet.Id, StringComparison.OrdinalIgnoreCase),
                                    "DataDisks.ManagedDisk.DiskEncryptionSet.Id is not matching with expected DiskEncryptionSet resource");
                    }

                    ValidateVMInstanceView(inputVM, getVMWithInstanceViewResponse, hasManagedDisks, expectedComputerName, expectedOSName, expectedOSVersion, dedicatedHostReferenceId);

                    var getVMInstanceViewResponse = m_CrpClient.VirtualMachines.InstanceView(rgName, inputVM.Name);
                    Assert.True(getVMInstanceViewResponse != null, "VM in InstanceView");
                    ValidateVMInstanceView(inputVM, getVMInstanceViewResponse, hasManagedDisks, expectedComputerName, expectedOSName, expectedOSVersion, dedicatedHostReferenceId);

                    bool hasUserDefinedAS = inputVM.AvailabilitySet != null;

                    string expectedVMReferenceId = Helpers.GetVMReferenceId(m_subId, rgName, inputVM.Name);
                    var    listResponse          = m_CrpClient.VirtualMachines.List(rgName);
                    ValidateVM(inputVM, listResponse.FirstOrDefault(x => x.Name == inputVM.Name),
                               expectedVMReferenceId, hasManagedDisks, hasUserDefinedAS, writeAcceleratorEnabled, hasDiffDisks, expectedPpgReferenceId: expectedPpgReferenceId,
                               encryptionAtHostEnabled: encryptionAtHostEnabled, expectedDedicatedHostGroupReferenceId: dedicatedHostGroupReferenceId);

                    if (validateListAvailableSize)
                    {
                        var listVMSizesResponse = m_CrpClient.VirtualMachines.ListAvailableSizes(rgName, inputVM.Name);
                        Helpers.ValidateVirtualMachineSizeListResponse(listVMSizesResponse, hasAZ: zones != null, writeAcceleratorEnabled: writeAcceleratorEnabled,
                                                                       hasDiffDisks: hasDiffDisks);

                        listVMSizesResponse = m_CrpClient.AvailabilitySets.ListAvailableSizes(rgName, asName);
                        Helpers.ValidateVirtualMachineSizeListResponse(listVMSizesResponse, hasAZ: zones != null, writeAcceleratorEnabled: writeAcceleratorEnabled, hasDiffDisks: hasDiffDisks);
                    }

                    if (securityType != null && securityType.Equals("TrustedLaunch"))
                    {
                        Assert.True(inputVM.SecurityProfile.UefiSettings.VTpmEnabled);
                        Assert.True(inputVM.SecurityProfile.UefiSettings.SecureBootEnabled);
                    }

                    if (isPpgScenario)
                    {
                        ProximityPlacementGroup outProximityPlacementGroup = m_CrpClient.ProximityPlacementGroups.Get(rgName, ppgName);
                        string expectedAvSetReferenceId = Helpers.GetAvailabilitySetRef(m_subId, rgName, asName);
                        Assert.Equal(1, outProximityPlacementGroup.VirtualMachines.Count);
                        Assert.Equal(1, outProximityPlacementGroup.AvailabilitySets.Count);
                        Assert.Equal(expectedVMReferenceId, outProximityPlacementGroup.VirtualMachines.First().Id, StringComparer.OrdinalIgnoreCase);
                        Assert.Equal(expectedAvSetReferenceId, outProximityPlacementGroup.AvailabilitySets.First().Id, StringComparer.OrdinalIgnoreCase);
                    }

                    if (callUpdateVM)
                    {
                        VirtualMachineUpdate updateParams = new VirtualMachineUpdate()
                        {
                            Tags = inputVM.Tags
                        };

                        string updateKey = "UpdateTag";
                        updateParams.Tags.Add(updateKey, "UpdateTagValue");
                        VirtualMachine updateResponse = m_CrpClient.VirtualMachines.Update(rgName, inputVM.Name, updateParams);
                        Assert.True(updateResponse.Tags.ContainsKey(updateKey));
                    }
                }
                finally
                {
                    // Fire and forget. No need to wait for RG deletion completion
                    try
                    {
                        m_ResourcesClient.ResourceGroups.BeginDelete(rgName);
                    }
                    catch (Exception e)
                    {
                        // Swallow this exception so that the original exception is thrown
                        Console.WriteLine(e);
                    }
                }
            }
        }