|
| 1 | +<# |
| 2 | +.SYNOPSIS |
| 3 | + 1) Installs the SqlIaaS extension by calling Set-AzureRmVMSqlServerExtension cmdlet on a VM. |
| 4 | + 2) Calls Get-AzureRmVMSqlServerExtension cmdlet to check the status of the extension installation. |
| 5 | + 3) Verifies settings are correct given input |
| 6 | + 4) Update extension values |
| 7 | + 5) Verify changes |
| 8 | + 6) Test with correct Name and Version |
| 9 | + 7) Test with correct Name and incorrect Version |
| 10 | + 8) Test with incorrect Name and crrect Version |
| 11 | + 9) Test with incorrect Name and incorrect Version |
| 12 | +#> |
| 13 | +function Test-SetAzureRmVMSqlServerExtension |
| 14 | +{ |
| 15 | + Set-StrictMode -Version latest; $ErrorActionPreference = 'Stop' |
| 16 | + |
| 17 | + # Setup |
| 18 | + $rgname = Get-ComputeTestResourceName |
| 19 | + $loc = Get-ComputeVMLocation |
| 20 | + |
| 21 | + try |
| 22 | + { |
| 23 | + # Common |
| 24 | + New-AzureRmResourceGroup -Name $rgname -Location $loc -Force; |
| 25 | + |
| 26 | + # VM Profile & Hardware |
| 27 | + $vmsize = 'Standard_A2'; |
| 28 | + $vmname = 'vm' + $rgname; |
| 29 | + $p = New-AzureRmVMConfig -VMName $vmname -VMSize $vmsize; |
| 30 | + Assert-AreEqual $p.HardwareProfile.VmSize $vmsize; |
| 31 | + |
| 32 | + # NRP |
| 33 | + $subnet = New-AzureRmVirtualNetworkSubnetConfig -Name ('subnet' + $rgname) -AddressPrefix "10.0.0.0/24"; |
| 34 | + $vnet = New-AzureRmVirtualNetwork -Force -Name ('vnet' + $rgname) -ResourceGroupName $rgname -Location $loc -AddressPrefix "10.0.0.0/16" -DnsServer "10.1.1.1" -Subnet $subnet; |
| 35 | + $vnet = Get-AzureRmVirtualNetwork -Name ('vnet' + $rgname) -ResourceGroupName $rgname; |
| 36 | + $subnetId = $vnet.Subnets[0].Id; |
| 37 | + $pubip = New-AzureRmPublicIpAddress -Force -Name ('pubip' + $rgname) -ResourceGroupName $rgname -Location $loc -AllocationMethod Dynamic -DomainNameLabel ('pubip' + $rgname); |
| 38 | + $pubip = Get-AzureRmPublicIpAddress -Name ('pubip' + $rgname) -ResourceGroupName $rgname; |
| 39 | + $pubipId = $pubip.Id; |
| 40 | + $nic = New-AzureRmNetworkInterface -Force -Name ('nic' + $rgname) -ResourceGroupName $rgname -Location $loc -SubnetId $subnetId -PublicIpAddressId $pubip.Id; |
| 41 | + $nic = Get-AzureRmNetworkInterface -Name ('nic' + $rgname) -ResourceGroupName $rgname; |
| 42 | + $nicId = $nic.Id; |
| 43 | + |
| 44 | + $p = Add-AzureRmVMNetworkInterface -VM $p -Id $nicId; |
| 45 | + Assert-AreEqual $p.NetworkProfile.NetworkInterfaces.Count 1; |
| 46 | + Assert-AreEqual $p.NetworkProfile.NetworkInterfaces[0].Id $nicId; |
| 47 | + |
| 48 | + # Storage Account |
| 49 | + $stoname = 'sto' + $rgname; |
| 50 | + $stotype = 'Standard_GRS'; |
| 51 | + New-AzureRmStorageAccount -ResourceGroupName $rgname -Name $stoname -Location $loc -Type $stotype; |
| 52 | + Retry-IfException { $global:stoaccount = Get-AzureRmStorageAccount -ResourceGroupName $rgname -Name $stoname; } |
| 53 | + |
| 54 | + $osDiskName = 'osDisk'; |
| 55 | + $osDiskCaching = 'ReadWrite'; |
| 56 | + $osDiskVhdUri = "https://$stoname.blob.core.windows.net/test/os.vhd"; |
| 57 | + $dataDiskVhdUri1 = "https://$stoname.blob.core.windows.net/test/data1.vhd"; |
| 58 | + |
| 59 | + $p = Set-AzureRmVMOSDisk -VM $p -Name $osDiskName -VhdUri $osDiskVhdUri -Caching $osDiskCaching -CreateOption FromImage; |
| 60 | + $p = Add-AzureRmVMDataDisk -VM $p -Name 'testDataDisk1' -Caching 'ReadOnly' -DiskSizeInGB 10 -Lun 1 -VhdUri $dataDiskVhdUri1 -CreateOption Empty; |
| 61 | + |
| 62 | + # OS & Image |
| 63 | + $user = "localadmin"; |
| 64 | + $password = 'Bull_dog1'; |
| 65 | + $securePassword = ConvertTo-SecureString $password -AsPlainText -Force; |
| 66 | + $cred = New-Object System.Management.Automation.PSCredential ($user, $securePassword); |
| 67 | + $computerName = 'test'; |
| 68 | + $vhdContainer = "https://$stoname.blob.core.windows.net/test"; |
| 69 | + |
| 70 | + $p = Set-AzureRmVMOperatingSystem -VM $p -Windows -ComputerName $computerName -Credential $cred -ProvisionVMAgent; |
| 71 | + $p = Set-AzureRmVMSourceImage -VM $p -PublisherName MicrosoftWindowsServer -Offer WindowsServer -Skus 2012-R2-Datacenter -Version "latest" |
| 72 | + |
| 73 | + # Virtual Machine |
| 74 | + New-AzureRmVM -ResourceGroupName $rgname -Location $loc -VM $p; |
| 75 | + |
| 76 | + #Do actual changes and work here |
| 77 | + |
| 78 | + $extensionName = "Microsoft.SqlServer.Management.SqlIaaSAgent"; |
| 79 | + |
| 80 | + # 1) Installs the SqlIaaS extension by calling Set-AzureRmVMSqlServerExtension cmdlet on a VM. |
| 81 | + $aps = New-AzureVMSqlServerAutoPatchingConfig -Enable -DayOfWeek "Thursday" -MaintenanceWindowStartingHour 20 -MaintenanceWindowDuration 120 -PatchCategory "Important" |
| 82 | + Set-AzureRmVMSqlServerExtension -AutoPatchingSettings $aps -ResourceGroupName $rgname -VMName $vmname -Version "1.2" -Verbose -Name $extensionName; |
| 83 | + |
| 84 | + # 2) Calls Get-AzureRmVMSqlServerExtension cmdlet to check the status of the extension installation. |
| 85 | + $extension = Get-AzureRmVMSqlServerExtension -ResourceGroupName $rgname -VmName $vmName -Name $extensionName; |
| 86 | + |
| 87 | + # 3) Verifies settings are correct given input |
| 88 | + Assert-AreEqual $extension.AutoPatchingSettings.DayOfWeek "Thursday" |
| 89 | + Assert-AreEqual $extension.AutoPatchingSettings.MaintenanceWindowStartingHour 20 |
| 90 | + Assert-AreEqual $extension.AutoPatchingSettings.MaintenanceWindowDuration 120 |
| 91 | + Assert-AreEqual $extension.AutoPatchingSettings.PatchCategory "Important" |
| 92 | + |
| 93 | + # 4) Update extension values |
| 94 | + $aps = New-AzureVMSqlServerAutoPatchingConfig -Enable -DayOfWeek "Monday" -MaintenanceWindowStartingHour 20 -MaintenanceWindowDuration 120 -PatchCategory "Important" |
| 95 | + Set-AzureRmVMSqlServerExtension -AutoPatchingSettings $aps -ResourceGroupName $rgname -VMName $vmname -Version "1.2" -Verbose -Name $extensionName; |
| 96 | + |
| 97 | + # 5) Verify changes |
| 98 | + $extension = Get-AzureRmVMSqlServerExtension -ResourceGroupName $rgname -VmName $vmName -Name $extensionName; |
| 99 | + Assert-AreEqual $extension.AutoPatchingSettings.DayOfWeek "Monday" |
| 100 | + |
| 101 | + # 6) Test with correct Name and Version |
| 102 | + Set-AzureRmVMSqlServerExtension -AutoPatchingSettings $aps -ResourceGroupName $rgname -VMName $vmName -Name $extensionName -Version "1.2" |
| 103 | + |
| 104 | + # 7) Test with correct Name and incorrect Version |
| 105 | + Set-AzureRmVMSqlServerExtension -AutoPatchingSettings $aps -ResourceGroupName $rgname -VMName $vmName -Name $extensionName -Version "1.*" |
| 106 | + } |
| 107 | + finally |
| 108 | + { |
| 109 | + # Cleanup |
| 110 | + if(Get-AzureRmResourceGroup -Name $rgname -Location $loc) |
| 111 | + { |
| 112 | + #Remove-AzureRmResourceGroup -Name $rgname -Force; |
| 113 | + } |
| 114 | + } |
| 115 | +} |
| 116 | + |
| 117 | +#helper methods for ARM |
| 118 | +function Get-DefaultResourceGroupLocation |
| 119 | +{ |
| 120 | + if ([Microsoft.Azure.Test.HttpRecorder.HttpMockServer]::Mode -ne [Microsoft.Azure.Test.HttpRecorder.HttpRecorderMode]::Playback) |
| 121 | + { |
| 122 | + $namespace = "Microsoft.Resources" |
| 123 | + $type = "resourceGroups" |
| 124 | + $location = Get-AzureRmResourceProvider -ProviderNamespace $namespace | where {$_.ResourceTypes[0].ResourceTypeName -eq $type} |
| 125 | + |
| 126 | + if ($location -eq $null) |
| 127 | + { |
| 128 | + return "West US" |
| 129 | + } else |
| 130 | + { |
| 131 | + return $location.Locations[0] |
| 132 | + } |
| 133 | + } |
| 134 | + |
| 135 | + return "West US" |
| 136 | +} |
0 commit comments