public void ManagingDiskImages()
{
ServiceManagementCmdletTestHelper vmPowershellCmdlets = new ServiceManagementCmdletTestHelper();
vmPowershellCmdlets.ImportAzurePublishSettingsFile(); // Import-AzurePublishSettingsFile
var defaultAzureSubscription = vmPowershellCmdlets.SetDefaultAzureSubscription(Resource.DefaultSubscriptionName); // Set-AzureSubscription
Assert.AreEqual(Resource.DefaultSubscriptionName, defaultAzureSubscription.SubscriptionName);
string imageName = vmPowershellCmdlets.GetAzureVMImageName(new[] { "MSFT", "testvmimage" }, false); // Get-AzureVMImage
Console.WriteLine("Image Name: {0}", imageName);
string locationName = vmPowershellCmdlets.GetAzureLocationName(new[] { Resource.Location }, false); // Get-AzureLocation
Console.WriteLine("Location Name: {0}", locationName);
// Create a unique VM name and Service Name
string newAzureQuickVMName = Utilities.GetUniqueShortName("PSTestVM");
string newAzureQuickVMSvcName = Utilities.GetUniqueShortName("PSTestService");
vmPowershellCmdlets.NewAzureQuickVM(OS.Windows, newAzureQuickVMName, newAzureQuickVMSvcName, imageName, "p@ssw0rd", locationName); // New-AzureQuickVM
Console.WriteLine("VM is created successfully: -Name {0} -ServiceName {1}", newAzureQuickVMName, newAzureQuickVMSvcName);
// starting the test.
Collection<DiskContext> vmDisks = vmPowershellCmdlets.GetAzureDiskAttachedToRoleName(new[] { newAzureQuickVMName }); // Get-AzureDisk | Where {$_.AttachedTo.RoleName -eq $vmname }
foreach (var disk in vmDisks)
Console.WriteLine("The disk, {0}, is created", disk.DiskName);
vmPowershellCmdlets.RemoveAzureVM(newAzureQuickVMName, newAzureQuickVMSvcName); // Remove-AzureVM
Assert.AreEqual(null, vmPowershellCmdlets.GetAzureVM(newAzureQuickVMName, newAzureQuickVMSvcName));
Console.WriteLine("The VM, {0}, is successfully removed.", newAzureQuickVMName);
foreach (var disk in vmDisks)
{
for (int i = 0; i < 3; i++)
{
try
{
vmPowershellCmdlets.RemoveAzureDisk(disk.DiskName, true); // Remove-AzureDisk
break;
}
catch (Exception e)
{
if (e.ToString().ToLowerInvariant().Contains("currently in use") && i != 2)
{
Console.WriteLine("The vhd, {0}, is still in the state of being used by the deleted VM", disk.DiskName);
Thread.Sleep(120000);
continue;
}
else
{
Assert.Fail("error during Remove-AzureDisk: {0}", e.ToString());
}
}
}
try
{
vmPowershellCmdlets.GetAzureDisk(disk.DiskName); // Get-AzureDisk -DiskName (try to get the removed disk.)
Assert.Fail("Disk is not removed: {0}", disk.DiskName);
}
catch (Exception e)
{
if (e.ToString().ToLowerInvariant().Contains("does not exist"))
{
Console.WriteLine("The disk, {0}, is successfully removed.", disk.DiskName);
continue;
}
else
{
Assert.Fail("Exception: {0}", e.ToString());
}
}
}
vmPowershellCmdlets.RemoveAzureService(newAzureQuickVMSvcName); // Clean up the service.
try
{
vmPowershellCmdlets.GetAzureService(newAzureQuickVMSvcName);
Assert.Fail("The service, {0}, is not removed", newAzureQuickVMSvcName);
}
catch (Exception e)
{
if (e.ToString().ToLowerInvariant().Contains("does not exist"))
{
Console.WriteLine("The service, {0}, is successfully removed", newAzureQuickVMSvcName);
}
else
{
Assert.Fail("Error occurred: {0}", e.ToString());
}
}
}