|
| 1 | +# ---------------------------------------------------------------------------------- |
| 2 | +# |
| 3 | +# Copyright Microsoft Corporation |
| 4 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +# you may not use this file except in compliance with the License. |
| 6 | +# You may obtain a copy of the License at |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# Unless required by applicable law or agreed to in writing, software |
| 9 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 10 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 | +# See the License for the specific language governing permissions and |
| 12 | +# limitations under the License. |
| 13 | +# ---------------------------------------------------------------------------------- |
| 14 | + |
| 15 | + |
| 16 | +# |
| 17 | +# ChefExtensionTests.ps1 |
| 18 | +# |
| 19 | + |
| 20 | +<# |
| 21 | +.SYNOPSIS |
| 22 | +Test the usage of the Set virtual machine chef extension command |
| 23 | +#> |
| 24 | +function Test-SetChefExtensionBasic |
| 25 | +{ |
| 26 | + $rgname = Get-ComputeTestResourceName |
| 27 | + $loc = Get-ComputeVMLocation |
| 28 | + $TestOutputRoot = [System.AppDomain]::CurrentDomain.BaseDirectory; |
| 29 | + |
| 30 | + try |
| 31 | + { |
| 32 | + ############ Create Virtual Machine ############# |
| 33 | + # Common |
| 34 | + New-AzureRmResourceGroup -Name $rgname -Location $loc -Force; |
| 35 | + |
| 36 | + # VM Profile & Hardware |
| 37 | + $vmsize = 'Standard_A2'; |
| 38 | + $vmname = 'vm' + $rgname; |
| 39 | + $p = New-AzureRmVMConfig -VMName $vmname -VMSize $vmsize; |
| 40 | + Assert-AreEqual $p.HardwareProfile.VmSize $vmsize; |
| 41 | + |
| 42 | + # NRP |
| 43 | + $subnet = New-AzureRmVirtualNetworkSubnetConfig -Name ('subnet' + $rgname) -AddressPrefix "10.0.0.0/24"; |
| 44 | + $vnet = New-AzureRmVirtualNetwork -Force -Name ('vnet' + $rgname) -ResourceGroupName $rgname -Location $loc -AddressPrefix "10.0.0.0/16" -Subnet $subnet; |
| 45 | + $vnet = Get-AzureRmVirtualNetwork -Name ('vnet' + $rgname) -ResourceGroupName $rgname; |
| 46 | + $subnetId = $vnet.Subnets[0].Id; |
| 47 | + $pubip = New-AzureRmPublicIpAddress -Force -Name ('pubip' + $rgname) -ResourceGroupName $rgname -Location $loc -AllocationMethod Dynamic -DomainNameLabel ('pubip' + $rgname); |
| 48 | + $pubip = Get-AzureRmPublicIpAddress -Name ('pubip' + $rgname) -ResourceGroupName $rgname; |
| 49 | + $pubipId = $pubip.Id; |
| 50 | + $nic = New-AzureRmNetworkInterface -Force -Name ('nic' + $rgname) -ResourceGroupName $rgname -Location $loc -SubnetId $subnetId -PublicIpAddressId $pubip.Id; |
| 51 | + $nic = Get-AzureRmNetworkInterface -Name ('nic' + $rgname) -ResourceGroupName $rgname; |
| 52 | + $nicId = $nic.Id; |
| 53 | + |
| 54 | + $p = Add-AzureRmVMNetworkInterface -VM $p -Id $nicId; |
| 55 | + Assert-AreEqual $p.NetworkProfile.NetworkInterfaces.Count 1; |
| 56 | + Assert-AreEqual $p.NetworkProfile.NetworkInterfaces[0].Id $nicId; |
| 57 | + |
| 58 | + # Storage Account |
| 59 | + $stoname = 'sto' + $rgname; |
| 60 | + $stotype = 'Standard_GRS'; |
| 61 | + New-AzureRmStorageAccount -ResourceGroupName $rgname -Name $stoname -Location $loc -Type $stotype; |
| 62 | + Retry-IfException { $global:stoaccount = Get-AzureRmStorageAccount -ResourceGroupName $rgname -Name $stoname; } |
| 63 | + |
| 64 | + $osDiskName = 'osDisk'; |
| 65 | + $osDiskCaching = 'ReadWrite'; |
| 66 | + $osDiskVhdUri = "https://$stoname.blob.core.windows.net/test/os.vhd"; |
| 67 | + $dataDiskVhdUri1 = "https://$stoname.blob.core.windows.net/test/data1.vhd"; |
| 68 | + |
| 69 | + $p = Set-AzureRmVMOSDisk -VM $p -Name $osDiskName -VhdUri $osDiskVhdUri -Caching $osDiskCaching -CreateOption FromImage; |
| 70 | + $p = Add-AzureRmVMDataDisk -VM $p -Name 'testDataDisk1' -Caching 'ReadOnly' -DiskSizeInGB 10 -Lun 1 -VhdUri $dataDiskVhdUri1 -CreateOption Empty; |
| 71 | + |
| 72 | + # OS & Image |
| 73 | + $user = "localadmin"; |
| 74 | + $password = $PLACEHOLDER; |
| 75 | + $securePassword = ConvertTo-SecureString $password -AsPlainText -Force; |
| 76 | + $cred = New-Object System.Management.Automation.PSCredential ($user, $securePassword); |
| 77 | + $computerName = 'test'; |
| 78 | + $vhdContainer = "https://$stoname.blob.core.windows.net/test"; |
| 79 | + |
| 80 | + $p = Set-AzureRmVMOperatingSystem -VM $p -Windows -ComputerName $computerName -Credential $cred -ProvisionVMAgent; |
| 81 | + $p = Set-AzureRmVMSourceImage -VM $p -PublisherName MicrosoftWindowsServer -Offer WindowsServer -Skus 2012-R2-Datacenter -Version "latest" |
| 82 | + |
| 83 | + # Virtual Machine |
| 84 | + New-AzureRmVM -ResourceGroupName $rgname -Location $loc -VM $p; |
| 85 | + |
| 86 | + ############ Created Virtual Machine ############# |
| 87 | + |
| 88 | + ############ Test Chef Extension ################# |
| 89 | + $version = "1210.12" |
| 90 | + $client_rb = "$TestOutputRoot\Templates\client.rb"; |
| 91 | + $validationPemFile = "$TestOutputRoot\Templates\tstorgnztn-validator.pem"; |
| 92 | + |
| 93 | + # Set Chef extension |
| 94 | + Set-AzureRmVMChefExtension -ResourceGroupName $rgname -VMName $vmname -TypeHandlerVersion $version -ClientRb $client_rb -ValidationPem $validationPemFile -Windows |
| 95 | + $extension = Get-AzureRmVMChefExtension -ResourceGroupName $rgname -VMName $vmname -Windows |
| 96 | + |
| 97 | + Assert-NotNull $extension |
| 98 | + Assert-AreEqual $extension.Publisher 'Chef.Bootstrap.WindowsAzure' |
| 99 | + Assert-AreEqual $extension.ExtensionType 'ChefClient' |
| 100 | + Assert-AreEqual $extension.Name 'ChefClient' |
| 101 | + |
| 102 | + # Test Remove command. |
| 103 | + Remove-AzureRmVMChefExtension -ResourceGroupName $rgname -VMName $vmname -Windows |
| 104 | + } |
| 105 | + finally |
| 106 | + { |
| 107 | + # Cleanup |
| 108 | + Clean-ResourceGroup $rgname |
| 109 | + } |
| 110 | +} |
0 commit comments