Skip to content

Commit fcbce5d

Browse files
committed
Merge pull request #448 from huangpf/dtest
AzureRT - Add Dynamic Tests for Project and/or Test Runner
2 parents fcada69 + d154355 commit fcbce5d

File tree

10 files changed

+10408
-3681
lines changed

10 files changed

+10408
-3681
lines changed

src/ResourceManager/Compute/Commands.Compute.Test/Commands.Compute.Test.csproj

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,7 @@
145145
</ItemGroup>
146146
<ItemGroup>
147147
<Compile Include="Common\ComputeTestController.cs" />
148+
<Compile Include="ScenarioTests\VMDynamicTests.cs" />
148149
<Compile Include="ScenarioTests\VirtualMachineProfileTests.cs" />
149150
<Compile Include="ScenarioTests\AvailabilitySetTests.cs" />
150151
<Compile Include="ScenarioTests\VirtualMachineExtensionTests.cs" />
@@ -193,6 +194,18 @@
193194
<Link>ScenarioTests\Common.ps1</Link>
194195
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
195196
</None>
197+
<None Include="ScenarioTests\Generated\VirtualMachineDynamicTest1.ps1">
198+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
199+
</None>
200+
<None Include="ScenarioTests\Generated\VirtualMachineDynamicTest2.ps1">
201+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
202+
</None>
203+
<None Include="ScenarioTests\Generated\VirtualMachineDynamicTest3.ps1">
204+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
205+
</None>
206+
<None Include="ScenarioTests\VMDynamicTests.ps1">
207+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
208+
</None>
196209
<None Include="MSSharedLibKey.snk" />
197210
<None Include="packages.config" />
198211
<None Include="ScenarioTests\VirtualMachineProfileTests.ps1">
@@ -276,6 +289,9 @@
276289
<None Include="SessionRecords\Microsoft.Azure.Commands.Compute.Test.ScenarioTests.VirtualMachineTests\TestVirtualMachineWithVMAgentAutoUpdate.json">
277290
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
278291
</None>
292+
<None Include="SessionRecords\Microsoft.Azure.Commands.Compute.Test.ScenarioTests.VMDynamicTests\RunVMDynamicTests.json">
293+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
294+
</None>
279295
</ItemGroup>
280296
<ItemGroup>
281297
<Service Include="{82A7F48D-3B50-4B1E-B82E-3ADA8210C358}" />

src/ResourceManager/Compute/Commands.Compute.Test/ScenarioTests/ComputeTestCommon.ps1

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,40 @@ function Get-ComputeTestResourceName
5454
return $assetName
5555
}
5656

57+
58+
<#
59+
.SYNOPSIS
60+
Gets test mode - 'Record' or 'Playback'
61+
#>
62+
function Get-ComputeTestMode
63+
{
64+
$oldErrorActionPreferenceValue = $ErrorActionPreference;
65+
$ErrorActionPreference = "SilentlyContinue";
66+
67+
try
68+
{
69+
$testMode = [Microsoft.Azure.Test.HttpRecorder.HttpMockServer]::Mode;
70+
$testMode = $testMode.ToString();
71+
}
72+
catch
73+
{
74+
if (($Error.Count -gt 0) -and ($Error[0].Exception.Message -like '*Unable to find type*'))
75+
{
76+
$testMode = 'Record';
77+
}
78+
else
79+
{
80+
throw;
81+
}
82+
}
83+
finally
84+
{
85+
$ErrorActionPreference = $oldErrorActionPreferenceValue;
86+
}
87+
88+
return $testMode;
89+
}
90+
5791
######################
5892
#
5993
# Retry the given code block until it succeeds or times out.
Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
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+
# Warning: This code was generated by a tool.
16+
#
17+
# Changes to this file may cause incorrect behavior and will be lost if the
18+
# code is regenerated.
19+
20+
21+
function get_vm_config_object
22+
{
23+
param ([string] $rgname, [string] $vmsize)
24+
25+
$vmname = 'vm' + $rgname;
26+
$p = New-AzureVMConfig -VMName $vmname -VMSize $vmsize;
27+
28+
return $p;
29+
}
30+
31+
32+
function get_created_storage_account_name
33+
{
34+
param ([string] $loc, [string] $rgname)
35+
36+
$stoname = 'sto' + $rgname;
37+
$stotype = 'Standard_GRS';
38+
39+
$st = New-AzureStorageAccount -ResourceGroupName $rgname -Name $stoname -Location $loc -Type $stotype;
40+
$st = Get-AzureStorageAccount -ResourceGroupName $rgname -Name $stoname;
41+
42+
return $stoname;
43+
}
44+
45+
46+
function create_and_setup_nic_ids
47+
{
48+
param ([string] $loc, [string] $rgname, $vmconfig)
49+
50+
$subnet = New-AzureVirtualNetworkSubnetConfig -Name ('subnet' + $rgname) -AddressPrefix "10.0.0.0/24";
51+
$vnet = New-AzureVirtualNetwork -Force -Name ('vnet' + $rgname) -ResourceGroupName $rgname -Location $loc -AddressPrefix "10.0.0.0/16" -DnsServer "10.1.1.1" -Subnet $subnet;
52+
$vnet = Get-AzureVirtualNetwork -Name ('vnet' + $rgname) -ResourceGroupName $rgname;
53+
$subnetId = $vnet.Subnets[0].Id;
54+
$nic_ids = @($null) * 1;
55+
$nic0 = New-AzureNetworkInterface -Force -Name ('nic0' + $rgname) -ResourceGroupName $rgname -Location $loc -SubnetId $subnetId;
56+
$nic_ids[0] = $nic0.Id;
57+
$vmconfig = Add-AzureVMNetworkInterface -VM $vmconfig -Id $nic0.Id -Primary;
58+
59+
return $nic_ids;
60+
}
61+
62+
function create_and_setup_vm_config_object
63+
{
64+
param ([string] $loc, [string] $rgname, [string] $vmsize)
65+
66+
$vmconfig = get_vm_config_object $rgname $vmsize
67+
68+
$user = "Foo12";
69+
$password = "BaR#123" + $rgname;
70+
$securePassword = ConvertTo-SecureString $password -AsPlainText -Force;
71+
$cred = New-Object System.Management.Automation.PSCredential ($user, $securePassword);
72+
$computerName = "cn" + $rgname;
73+
$vmconfig = Set-AzureVMOperatingSystem -VM $vmconfig -Windows -ComputerName $computerName -Credential $cred;
74+
75+
return $vmconfig;
76+
}
77+
78+
79+
function setup_image_and_disks
80+
{
81+
param ([string] $loc, [string] $rgname, [string] $stoname, $vmconfig)
82+
83+
$osDiskName = 'osDisk';
84+
$osDiskVhdUri = "https://$stoname.blob.core.windows.net/test/os.vhd";
85+
$osDiskCaching = 'ReadWrite';
86+
87+
$vmconfig = Set-AzureVMOSDisk -VM $vmconfig -Name $osDiskName -VhdUri $osDiskVhdUri -Caching $osDiskCaching -CreateOption FromImage;
88+
89+
# Image Reference;
90+
$vmconfig.StorageProfile.SourceImage = $null;
91+
$imgRef = Get-DefaultCRPImage;
92+
$vmconfig = ($imgRef | Set-AzureVMSourceImage -VM $vmconfig);
93+
94+
# TODO: Remove Data Disks for now
95+
$vmconfig.StorageProfile.DataDisks = $null;
96+
97+
return $vmconfig;
98+
}
99+
100+
101+
function ps_vm_dynamic_test_func_1_pstestrg1166
102+
{
103+
# Setup
104+
$rgname = 'pstestrg1166';
105+
106+
try
107+
{
108+
$loc = 'westeurope';
109+
$vmsize = 'Standard_A5';
110+
111+
$st = New-AzureResourceGroup -Location $loc -Name $rgname;
112+
113+
$vmconfig = create_and_setup_vm_config_object $loc $rgname $vmsize;
114+
115+
# Setup Storage Account
116+
$stoname = get_created_storage_account_name $loc $rgname;
117+
118+
# Setup Network Interface IDs
119+
$nicids = create_and_setup_nic_ids $loc $rgname $vmconfig;
120+
121+
# Setup Image and Disks
122+
$st = setup_image_and_disks $loc $rgname $stoname $vmconfig;
123+
124+
# Virtual Machine
125+
$vmname = 'vm' + $rgname;
126+
$st = New-AzureVM -ResourceGroupName $rgname -Location $loc -Name $vmname -VM $vmconfig;
127+
128+
# Get VM
129+
$vm1 = Get-AzureVM -Name $vmname -ResourceGroupName $rgname;
130+
131+
# Remove
132+
$st = Remove-AzureVM -Name $vmname -ResourceGroupName $rgname -Force;
133+
}
134+
finally
135+
{
136+
# Cleanup
137+
Clean-ResourceGroup $rgname
138+
}
139+
}
140+
Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
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+
# Warning: This code was generated by a tool.
16+
#
17+
# Changes to this file may cause incorrect behavior and will be lost if the
18+
# code is regenerated.
19+
20+
21+
function get_vm_config_object
22+
{
23+
param ([string] $rgname, [string] $vmsize)
24+
25+
$vmname = 'vm' + $rgname;
26+
$p = New-AzureVMConfig -VMName $vmname -VMSize $vmsize;
27+
28+
return $p;
29+
}
30+
31+
32+
function get_created_storage_account_name
33+
{
34+
param ([string] $loc, [string] $rgname)
35+
36+
$stoname = 'sto' + $rgname;
37+
$stotype = 'Standard_GRS';
38+
39+
$st = New-AzureStorageAccount -ResourceGroupName $rgname -Name $stoname -Location $loc -Type $stotype;
40+
$st = Get-AzureStorageAccount -ResourceGroupName $rgname -Name $stoname;
41+
42+
return $stoname;
43+
}
44+
45+
46+
function create_and_setup_nic_ids
47+
{
48+
param ([string] $loc, [string] $rgname, $vmconfig)
49+
50+
$subnet = New-AzureVirtualNetworkSubnetConfig -Name ('subnet' + $rgname) -AddressPrefix "10.0.0.0/24";
51+
$vnet = New-AzureVirtualNetwork -Force -Name ('vnet' + $rgname) -ResourceGroupName $rgname -Location $loc -AddressPrefix "10.0.0.0/16" -DnsServer "10.1.1.1" -Subnet $subnet;
52+
$vnet = Get-AzureVirtualNetwork -Name ('vnet' + $rgname) -ResourceGroupName $rgname;
53+
$subnetId = $vnet.Subnets[0].Id;
54+
$nic_ids = @($null) * 1;
55+
$nic0 = New-AzureNetworkInterface -Force -Name ('nic0' + $rgname) -ResourceGroupName $rgname -Location $loc -SubnetId $subnetId;
56+
$nic_ids[0] = $nic0.Id;
57+
$vmconfig = Add-AzureVMNetworkInterface -VM $vmconfig -Id $nic0.Id -Primary;
58+
59+
return $nic_ids;
60+
}
61+
62+
function create_and_setup_vm_config_object
63+
{
64+
param ([string] $loc, [string] $rgname, [string] $vmsize)
65+
66+
$vmconfig = get_vm_config_object $rgname $vmsize
67+
68+
$user = "Foo12";
69+
$password = "BaR#123" + $rgname;
70+
$securePassword = ConvertTo-SecureString $password -AsPlainText -Force;
71+
$cred = New-Object System.Management.Automation.PSCredential ($user, $securePassword);
72+
$computerName = "cn" + $rgname;
73+
$vmconfig = Set-AzureVMOperatingSystem -VM $vmconfig -Windows -ComputerName $computerName -Credential $cred;
74+
75+
return $vmconfig;
76+
}
77+
78+
79+
function setup_image_and_disks
80+
{
81+
param ([string] $loc, [string] $rgname, [string] $stoname, $vmconfig)
82+
83+
$osDiskName = 'osDisk';
84+
$osDiskVhdUri = "https://$stoname.blob.core.windows.net/test/os.vhd";
85+
$osDiskCaching = 'ReadWrite';
86+
87+
$vmconfig = Set-AzureVMOSDisk -VM $vmconfig -Name $osDiskName -VhdUri $osDiskVhdUri -Caching $osDiskCaching -CreateOption FromImage;
88+
89+
# Image Reference;
90+
$vmconfig.StorageProfile.SourceImage = $null;
91+
$imgRef = Get-DefaultCRPImage;
92+
$vmconfig = ($imgRef | Set-AzureVMSourceImage -VM $vmconfig);
93+
94+
# TODO: Remove Data Disks for now
95+
$vmconfig.StorageProfile.DataDisks = $null;
96+
97+
return $vmconfig;
98+
}
99+
100+
101+
function ps_vm_dynamic_test_func_2_pstestrg7266
102+
{
103+
# Setup
104+
$rgname = 'pstestrg7266';
105+
106+
try
107+
{
108+
$loc = 'eastasia';
109+
$vmsize = 'Standard_A5';
110+
111+
$st = New-AzureResourceGroup -Location $loc -Name $rgname;
112+
113+
$vmconfig = create_and_setup_vm_config_object $loc $rgname $vmsize;
114+
115+
# Setup Storage Account
116+
$stoname = get_created_storage_account_name $loc $rgname;
117+
118+
# Setup Network Interface IDs
119+
$nicids = create_and_setup_nic_ids $loc $rgname $vmconfig;
120+
121+
# Setup Image and Disks
122+
$st = setup_image_and_disks $loc $rgname $stoname $vmconfig;
123+
124+
# Virtual Machine
125+
$vmname = 'vm' + $rgname;
126+
$st = New-AzureVM -ResourceGroupName $rgname -Location $loc -Name $vmname -VM $vmconfig;
127+
128+
# Get VM
129+
$vm1 = Get-AzureVM -Name $vmname -ResourceGroupName $rgname;
130+
131+
# Remove
132+
$st = Remove-AzureVM -Name $vmname -ResourceGroupName $rgname -Force;
133+
}
134+
finally
135+
{
136+
# Cleanup
137+
Clean-ResourceGroup $rgname
138+
}
139+
}
140+

0 commit comments

Comments
 (0)