Skip to content

Commit 3d52731

Browse files
committed
Merge pull request Azure#1414 from yantang-msft/dev
Add test cases for AzureRmVMDiagnosticsExtension command
2 parents b22b122 + 6c743e0 commit 3d52731

File tree

8 files changed

+23100
-0
lines changed

8 files changed

+23100
-0
lines changed

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,7 @@
165165
<Compile Include="Common\ComputeTestController.cs" />
166166
<Compile Include="ScenarioTests\AddVhdTests.cs" />
167167
<Compile Include="ScenarioTests\ComputeCloudExceptionTests.cs" />
168+
<Compile Include="ScenarioTests\DiagnosticsExtensionTests.cs" />
168169
<Compile Include="ScenarioTests\DscExtensionTests.cs" />
169170
<Compile Include="ScenarioTests\VirtualMachineBootDiagnosticsTests.cs" />
170171
<Compile Include="ScenarioTests\VMDynamicTests.cs" />
@@ -236,6 +237,9 @@
236237
<None Include="ScenarioTests\VirtualMachineBootDiagnosticsTests.ps1">
237238
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
238239
</None>
240+
<None Include="ScenarioTests\DiagnosticsExtensionTests.ps1">
241+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
242+
</None>
239243
<None Include="ScenarioTests\VMDynamicTests.ps1">
240244
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
241245
</None>
@@ -268,6 +272,15 @@
268272
<None Include="SessionRecords\Microsoft.Azure.Commands.Compute.Test.ScenarioTests.ComputeCloudExceptionTests\RunComputeCloudExceptionTests.json">
269273
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
270274
</None>
275+
<None Include="SessionRecords\Microsoft.Azure.Commands.Compute.Test.ScenarioTests.DiagnosticsExtensionTests\TestDiagnosticsExtensionBasic.json">
276+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
277+
</None>
278+
<None Include="SessionRecords\Microsoft.Azure.Commands.Compute.Test.ScenarioTests.DiagnosticsExtensionTests\TestDiagnosticsExtensionCantListSepcifyStorageAccountKey.json">
279+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
280+
</None>
281+
<None Include="SessionRecords\Microsoft.Azure.Commands.Compute.Test.ScenarioTests.DiagnosticsExtensionTests\TestDiagnosticsExtensionSepcifyStorageAccountName.json">
282+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
283+
</None>
271284
<None Include="SessionRecords\Microsoft.Azure.Commands.Compute.Test.ScenarioTests.DscExtensionTests\TestGetAzureRmVMDscExtension.json">
272285
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
273286
</None>
@@ -392,6 +405,11 @@
392405
<LastGenOutput>Resources.Designer.cs</LastGenOutput>
393406
</EmbeddedResource>
394407
</ItemGroup>
408+
<ItemGroup>
409+
<Content Include="ConfigFiles\DiagnosticsExtensionConfig.xml">
410+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
411+
</Content>
412+
</ItemGroup>
395413
<ItemGroup />
396414
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
397415
</Project>
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<PublicConfig>
2+
<WadCfg>
3+
<DiagnosticMonitorConfiguration overallQuotaInMB="2048" xmlns="http://schemas.microsoft.com/ServiceHosting/2010/10/DiagnosticsConfiguration">
4+
<DiagnosticInfrastructureLogs scheduledTransferLogLevelFilter="Error" />
5+
<Directories scheduledTransferPeriod="PT1M">
6+
<IISLogs containerName="wad-iis-logfiles" />
7+
<FailedRequestLogs containerName="wad-failedrequestlogs" />
8+
</Directories>
9+
<PerformanceCounters scheduledTransferPeriod="PT2M">
10+
<PerformanceCounterConfiguration counterSpecifier="\Memory\Available MBytes" sampleRate="PT1M" />
11+
<PerformanceCounterConfiguration counterSpecifier="\Web Service(_Total)\ISAPI Extension Requests/sec" sampleRate="PT3M" />
12+
<PerformanceCounterConfiguration counterSpecifier="\Web Service(_Total)\Bytes Total/Sec" sampleRate="PT1M" />
13+
<PerformanceCounterConfiguration counterSpecifier="\ASP.NET Applications(__Total__)\Requests/Sec" sampleRate="PT3M" />
14+
<PerformanceCounterConfiguration counterSpecifier="\ASP.NET Applications(__Total__)\Errors Total/Sec" sampleRate="PT3M" />
15+
<PerformanceCounterConfiguration counterSpecifier="\ASP.NET\Requests Queued" sampleRate="PT3M" />
16+
<PerformanceCounterConfiguration counterSpecifier="\ASP.NET\Requests Rejected" sampleRate="PT3M" />
17+
<PerformanceCounterConfiguration counterSpecifier="\Processor(_Total)\% Processor Time" sampleRate="PT3M" />
18+
</PerformanceCounters>
19+
<WindowsEventLog scheduledTransferPeriod="PT1M">
20+
<DataSource name="Application!*[System[(Level=1 or Level=2)]]" />
21+
<DataSource name="Windows Azure!*[System[(Level=1 or Level=2)]]" />
22+
</WindowsEventLog>
23+
<CrashDumps>
24+
<CrashDumpConfiguration processName="WaIISHost.exe" />
25+
<CrashDumpConfiguration processName="WaWorkerHost.exe" />
26+
<CrashDumpConfiguration processName="w3wp.exe" />
27+
</CrashDumps>
28+
<Logs scheduledTransferPeriod="PT3M" />
29+
</DiagnosticMonitorConfiguration>
30+
</WadCfg>
31+
<StorageAccount>definedinconfigstorage</StorageAccount>
32+
</PublicConfig>

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

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,102 @@ function Get-ComputeDefaultLocation
106106
return $test_location;
107107
}
108108

109+
# Create a new virtual machine with other necessary resources configured
110+
function Create-VirtualMachine($rgname, $vmname, $loc)
111+
{
112+
# Initialize parameters
113+
$rgname = if ([string]::IsNullOrEmpty($rgname)) { Get-ComputeTestResourceName } else { $rgname }
114+
$vmname = if ([string]::IsNullOrEmpty($vmname)) { 'vm' + $rgname } else { $vmname }
115+
$loc = if ([string]::IsNullOrEmpty($loc)) { Get-ComputeVMLocation } else { $loc }
116+
117+
# Common
118+
New-AzureRmResourceGroup -Name $rgname -Location $loc -Force;
119+
120+
# VM Profile & Hardware
121+
$vmsize = 'Standard_A2';
122+
$p = New-AzureRmVMConfig -VMName $vmname -VMSize $vmsize;
123+
Assert-AreEqual $p.HardwareProfile.VirtualMachineSize $vmsize;
124+
125+
# NRP
126+
$subnet = New-AzureRmVirtualNetworkSubnetConfig -Name ('subnet' + $rgname) -AddressPrefix "10.0.0.0/24";
127+
$vnet = New-AzureRmVirtualNetwork -Force -Name ('vnet' + $rgname) -ResourceGroupName $rgname -Location $loc -AddressPrefix "10.0.0.0/16" -DnsServer "10.1.1.1" -Subnet $subnet;
128+
$vnet = Get-AzureRmVirtualNetwork -Name ('vnet' + $rgname) -ResourceGroupName $rgname;
129+
$subnetId = $vnet.Subnets[0].Id;
130+
$pubip = New-AzureRmPublicIpAddress -Force -Name ('pubip' + $rgname) -ResourceGroupName $rgname -Location $loc -AllocationMethod Dynamic -DomainNameLabel ('pubip' + $rgname);
131+
$pubip = Get-AzureRmPublicIpAddress -Name ('pubip' + $rgname) -ResourceGroupName $rgname;
132+
$pubipId = $pubip.Id;
133+
$nic = New-AzureRmNetworkInterface -Force -Name ('nic' + $rgname) -ResourceGroupName $rgname -Location $loc -SubnetId $subnetId -PublicIpAddressId $pubip.Id;
134+
$nic = Get-AzureRmNetworkInterface -Name ('nic' + $rgname) -ResourceGroupName $rgname;
135+
$nicId = $nic.Id;
136+
137+
$p = Add-AzureRmVMNetworkInterface -VM $p -Id $nicId;
138+
Assert-AreEqual $p.NetworkProfile.NetworkInterfaces.Count 1;
139+
Assert-AreEqual $p.NetworkProfile.NetworkInterfaces[0].ReferenceUri $nicId;
140+
141+
# Storage Account (SA)
142+
$stoname = 'sto' + $rgname;
143+
$stotype = 'Standard_GRS';
144+
New-AzureRmStorageAccount -ResourceGroupName $rgname -Name $stoname -Location $loc -Type $stotype;
145+
Retry-IfException { $global:stoaccount = Get-AzureRmStorageAccount -ResourceGroupName $rgname -Name $stoname; }
146+
$stokey = (Get-AzureRmStorageAccountKey -ResourceGroupName $rgname -Name $stoname).Key1;
147+
148+
$osDiskName = 'osDisk';
149+
$osDiskCaching = 'ReadWrite';
150+
$osDiskVhdUri = "https://$stoname.blob.core.windows.net/test/os.vhd";
151+
$dataDiskVhdUri1 = "https://$stoname.blob.core.windows.net/test/data1.vhd";
152+
$dataDiskVhdUri2 = "https://$stoname.blob.core.windows.net/test/data2.vhd";
153+
$dataDiskVhdUri3 = "https://$stoname.blob.core.windows.net/test/data3.vhd";
154+
155+
$p = Set-AzureRmVMOSDisk -VM $p -Name $osDiskName -VhdUri $osDiskVhdUri -Caching $osDiskCaching -CreateOption FromImage;
156+
157+
$p = Add-AzureRmVMDataDisk -VM $p -Name 'testDataDisk1' -Caching 'ReadOnly' -DiskSizeInGB 10 -Lun 1 -VhdUri $dataDiskVhdUri1 -CreateOption Empty;
158+
$p = Add-AzureRmVMDataDisk -VM $p -Name 'testDataDisk2' -Caching 'ReadOnly' -DiskSizeInGB 11 -Lun 2 -VhdUri $dataDiskVhdUri2 -CreateOption Empty;
159+
$p = Add-AzureRmVMDataDisk -VM $p -Name 'testDataDisk3' -Caching 'ReadOnly' -DiskSizeInGB 12 -Lun 3 -VhdUri $dataDiskVhdUri3 -CreateOption Empty;
160+
$p = Remove-AzureRmVMDataDisk -VM $p -Name 'testDataDisk3';
161+
162+
Assert-AreEqual $p.StorageProfile.OSDisk.Caching $osDiskCaching;
163+
Assert-AreEqual $p.StorageProfile.OSDisk.Name $osDiskName;
164+
Assert-AreEqual $p.StorageProfile.OSDisk.VirtualHardDisk.Uri $osDiskVhdUri;
165+
Assert-AreEqual $p.StorageProfile.DataDisks.Count 2;
166+
Assert-AreEqual $p.StorageProfile.DataDisks[0].Caching 'ReadOnly';
167+
Assert-AreEqual $p.StorageProfile.DataDisks[0].DiskSizeGB 10;
168+
Assert-AreEqual $p.StorageProfile.DataDisks[0].Lun 1;
169+
Assert-AreEqual $p.StorageProfile.DataDisks[0].VirtualHardDisk.Uri $dataDiskVhdUri1;
170+
Assert-AreEqual $p.StorageProfile.DataDisks[1].Caching 'ReadOnly';
171+
Assert-AreEqual $p.StorageProfile.DataDisks[1].DiskSizeGB 11;
172+
Assert-AreEqual $p.StorageProfile.DataDisks[1].Lun 2;
173+
Assert-AreEqual $p.StorageProfile.DataDisks[1].VirtualHardDisk.Uri $dataDiskVhdUri2;
174+
175+
# OS & Image
176+
$user = "Foo12";
177+
$password = 'BaR@123' + $rgname;
178+
$securePassword = ConvertTo-SecureString $password -AsPlainText -Force;
179+
$cred = New-Object System.Management.Automation.PSCredential ($user, $securePassword);
180+
$computerName = 'test';
181+
$vhdContainer = "https://$stoname.blob.core.windows.net/test";
182+
183+
$p = Set-AzureRmVMOperatingSystem -VM $p -Windows -ComputerName $computerName -Credential $cred -ProvisionVMAgent;
184+
185+
$imgRef = Get-DefaultCRPWindowsImageOffline;
186+
$p = ($imgRef | Set-AzureRmVMSourceImage -VM $p);
187+
188+
Assert-AreEqual $p.OSProfile.AdminUsername $user;
189+
Assert-AreEqual $p.OSProfile.ComputerName $computerName;
190+
Assert-AreEqual $p.OSProfile.AdminPassword $password;
191+
Assert-AreEqual $p.OSProfile.WindowsConfiguration.ProvisionVMAgent $true;
192+
193+
Assert-AreEqual $p.StorageProfile.ImageReference.Offer $imgRef.Offer;
194+
Assert-AreEqual $p.StorageProfile.ImageReference.Publisher $imgRef.PublisherName;
195+
Assert-AreEqual $p.StorageProfile.ImageReference.Sku $imgRef.Skus;
196+
Assert-AreEqual $p.StorageProfile.ImageReference.Version $imgRef.Version;
197+
198+
# Virtual Machine
199+
New-AzureRmVM -ResourceGroupName $rgname -Location $loc -VM $p;
200+
201+
$vm = Get-AzureRmVM -ResourceGroupName $rgname -VMName $vmname
202+
return $vm
203+
}
204+
109205
# Cleans the created resource group
110206
function Clean-ResourceGroup($rgname)
111207
{
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
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+
using Microsoft.WindowsAzure.Commands.ScenarioTest;
16+
using Xunit;
17+
18+
namespace Microsoft.Azure.Commands.Compute.Test.ScenarioTests
19+
{
20+
public class DiagnosticsExtensionTests
21+
{
22+
[Fact]
23+
[Trait(Category.AcceptanceType, Category.CheckIn)]
24+
public void TestDiagnosticsExtensionBasic()
25+
{
26+
ComputeTestController.NewInstance.RunPsTest("Test-DiagnosticsExtensionBasic");
27+
}
28+
29+
[Fact]
30+
[Trait(Category.AcceptanceType, Category.CheckIn)]
31+
public void TestDiagnosticsExtensionSepcifyStorageAccountName()
32+
{
33+
ComputeTestController.NewInstance.RunPsTest("Test-DiagnosticsExtensionSepcifyStorageAccountName");
34+
}
35+
36+
[Fact]
37+
[Trait(Category.AcceptanceType, Category.CheckIn)]
38+
public void TestDiagnosticsExtensionCantListSepcifyStorageAccountKey()
39+
{
40+
ComputeTestController.NewInstance.RunPsTest("Test-DiagnosticsExtensionCantListSepcifyStorageAccountKey");
41+
}
42+
}
43+
}
Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
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+
.SYNOPSIS
17+
Test the basic usage of the Set/Get/Remove virtual machine diagnostics extension command
18+
#>
19+
function Test-DiagnosticsExtensionBasic
20+
{
21+
$rgname = Get-ComputeTestResourceName
22+
$loc = Get-ComputeVMLocation
23+
24+
try
25+
{
26+
# Setup
27+
$vm = Create-VirtualMachine -rgname $rgname -loc $loc
28+
$vmname = $vm.Name
29+
30+
# This is the storage name defined in config file
31+
$storagename = 'definedinconfigstorage'
32+
$storagetype = 'Standard_GRS'
33+
New-AzureRmStorageAccount -ResourceGroupName $rgname -Name $storagename -Location $loc -Type $storagetype
34+
35+
# If diagnostics extension already exist, remove it
36+
$extension = Get-AzureRmVMDiagnosticsExtension -ResourceGroupName $rgname -VMName $vmname
37+
if ($extension) {
38+
Remove-AzureRmVMDiagnosticsExtension -ResourceGroupName $rgname -VMName $vmname
39+
$extension = Get-AzureRmVMDiagnosticsExtension -ResourceGroupName $rgname -VMName $vmname
40+
Assert-Null $extension
41+
}
42+
43+
# Test Set and Get command. It should use the storage account defined in configuration file
44+
Set-AzureRmVMDiagnosticsExtension -ResourceGroupName $rgname -VMName $vmname -DiagnosticsConfigurationPath '.\ConfigFiles\DiagnosticsExtensionConfig.xml'
45+
$extension = Get-AzureRmVMDiagnosticsExtension -ResourceGroupName $rgname -VMName $vmname
46+
47+
Assert-NotNull $extension
48+
Assert-AreEqual $extension.Publisher 'Microsoft.Azure.Diagnostics'
49+
Assert-AreEqual $extension.ExtensionType 'IaaSDiagnostics'
50+
Assert-AreEqual $extension.Name 'Microsoft.Insights.VMDiagnosticsSettings'
51+
$settings = $extension.PublicSettings | ConvertFrom-Json
52+
Assert-AreEqual $settings.storageAccount $storagename
53+
54+
# Test Remove command.
55+
Remove-AzureRmVMDiagnosticsExtension -ResourceGroupName $rgname -VMName $vmname
56+
$extension = Get-AzureRmVMDiagnosticsExtension -ResourceGroupName $rgname -VMName $vmname
57+
Assert-Null $extension
58+
}
59+
finally
60+
{
61+
# Cleanup
62+
Clean-ResourceGroup $rgname
63+
}
64+
}
65+
66+
<#
67+
.SYNOPSIS
68+
Test specifying storage account name in command line.
69+
The diagnostic extension should use this name instead of the one defined in config file.
70+
#>
71+
function Test-DiagnosticsExtensionSepcifyStorageAccountName
72+
{
73+
$rgname = Get-ComputeTestResourceName
74+
$loc = Get-ComputeVMLocation
75+
76+
try
77+
{
78+
# Setup
79+
$vm = Create-VirtualMachine -rgname $rgname -loc $loc
80+
$vmname = $vm.Name
81+
82+
# This storage name will be used in command line directly when set diagnostics extension
83+
$storagename = 'definedincommandline'
84+
$storagetype = 'Standard_GRS'
85+
New-AzureRmStorageAccount -ResourceGroupName $rgname -Name $storagename -Location $loc -Type $storagetype
86+
87+
# If diagnostics extension already exist, remove it
88+
$extension = Get-AzureRmVMDiagnosticsExtension -ResourceGroupName $rgname -VMName $vmname
89+
if ($extension) {
90+
Remove-AzureRmVMDiagnosticsExtension -ResourceGroupName $rgname -VMName $vmname
91+
$extension = Get-AzureRmVMDiagnosticsExtension -ResourceGroupName $rgname -VMName $vmname
92+
Assert-Null $extension
93+
}
94+
95+
Set-AzureRmVMDiagnosticsExtension -ResourceGroupName $rgname -VMName $vmname -DiagnosticsConfigurationPath '.\ConfigFiles\DiagnosticsExtensionConfig.xml' -StorageAccountName $storagename
96+
$extension = Get-AzureRmVMDiagnosticsExtension -ResourceGroupName $rgname -VMName $vmname
97+
98+
Assert-NotNull $extension
99+
Assert-AreEqual $extension.Publisher 'Microsoft.Azure.Diagnostics'
100+
Assert-AreEqual $extension.ExtensionType 'IaaSDiagnostics'
101+
Assert-AreEqual $extension.Name 'Microsoft.Insights.VMDiagnosticsSettings'
102+
$settings = $extension.PublicSettings | ConvertFrom-Json
103+
Assert-AreEqual $settings.storageAccount $storagename
104+
}
105+
finally
106+
{
107+
# Cleanup
108+
Clean-ResourceGroup $rgname
109+
}
110+
}
111+
112+
<#
113+
.SYNOPSIS
114+
Test the case if we can't list the storage account key, the command should fail.
115+
#>
116+
function Test-DiagnosticsExtensionCantListSepcifyStorageAccountKey
117+
{
118+
$rgname = Get-ComputeTestResourceName
119+
$loc = Get-ComputeVMLocation
120+
121+
try
122+
{
123+
# Setup
124+
$vm = Create-VirtualMachine -rgname $rgname -loc $loc
125+
$vmname = $vm.Name
126+
127+
# If diagnostics extension already exist, remove it
128+
$extension = Get-AzureRmVMDiagnosticsExtension -ResourceGroupName $rgname -VMName $vmname
129+
if ($extension) {
130+
Remove-AzureRmVMDiagnosticsExtension -ResourceGroupName $rgname -VMName $vmname
131+
$extension = Get-AzureRmVMDiagnosticsExtension -ResourceGroupName $rgname -VMName $vmname
132+
Assert-Null $extension
133+
}
134+
135+
# Get a random storage account name, which we can't list the key
136+
$storagename = 'notexiststorage'
137+
Assert-ThrowsContains `
138+
{ Set-AzureRmVMDiagnosticsExtension -ResourceGroupName $rgname -VMName $vmname -DiagnosticsConfigurationPath '.\ConfigFiles\DiagnosticsExtensionConfig.xml' -StorageAccountName $storagename } `
139+
'Failed to list storage account key'
140+
}
141+
finally
142+
{
143+
# Cleanup
144+
Clean-ResourceGroup $rgname
145+
}
146+
}

0 commit comments

Comments
 (0)