public void ExportingImportingVMConfigAsTemplateforRepeatableUsage()
{
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
string newAzureQuickVMName = Utilities.GetUniqueShortName("PSTestVM");
// Create a unique Service Name
string newAzureQuickVMSvcName = Utilities.GetUniqueShortName("PSTestService");
// Create a new Azure quick VM
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.
string path = "C:\\temp\\mytestvmconfig1.xml";
PersistentVMRoleContext vmRole = vmPowershellCmdlets.ExportAzureVM(newAzureQuickVMName, newAzureQuickVMSvcName, path); // Export-AzureVM
Console.WriteLine("Exporting VM is successfully done: path - {0} Name - {1}", path, vmRole.Name);
vmPowershellCmdlets.RemoveAzureVM(newAzureQuickVMName, newAzureQuickVMSvcName); // Remove-AzureVM
Assert.AreEqual(null, vmPowershellCmdlets.GetAzureVM(newAzureQuickVMName, newAzureQuickVMSvcName));
Console.WriteLine("The VM is successfully removed: {0}", newAzureQuickVMName);
List<PersistentVM> VMs = new List<PersistentVM>();
foreach (var pervm in vmPowershellCmdlets.ImportAzureVM(path)) // Import-AzureVM
{
VMs.Add(pervm);
Console.WriteLine("The VM, {0}, is imported.", pervm.RoleName);
}
for (int i = 0; i < 3; i++)
{
try
{
vmPowershellCmdlets.NewAzureVM(newAzureQuickVMSvcName, VMs.ToArray()); // New-AzureVM
Console.WriteLine("All VMs are successfully created.");
foreach (var vm in VMs)
{
Console.WriteLine("created VM: {0}", vm.RoleName);
}
break;
}
catch (Exception e)
{
if (e.ToString().ToLowerInvariant().Contains("currently in use") && i != 2)
{
Console.WriteLine("The removed VM is still using the vhd");
Thread.Sleep(120000);
continue;
}
else
{
Assert.Fail("error during New-AzureVM: {0}", e.ToString());
}
}
}
// 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));
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());
}
}
}