Skip to content

Commit 2b5322e

Browse files
author
Samuel Anudeep
committed
Enhanced tests in RecoveryServices to do custom setup and tear down. Enabled Vault Credentials test case to be CheckIn Acceptance type.
1 parent 0a3403d commit 2b5322e

File tree

9 files changed

+1877
-786
lines changed

9 files changed

+1877
-786
lines changed

src/ResourceManager/RecoveryServices/Commands.RecoveryServices.Test/Commands.RecoveryServices.Test.csproj

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,10 @@
4949
<SpecificVersion>False</SpecificVersion>
5050
<HintPath>..\..\..\packages\Microsoft.Azure.Management.RecoveryServices.4.2.0-preview\lib\net45\Microsoft.Azure.Management.RecoveryServices.dll</HintPath>
5151
</Reference>
52+
<Reference Include="Microsoft.Azure.ResourceManager, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
53+
<SpecificVersion>False</SpecificVersion>
54+
<HintPath>..\..\..\..\..\..\Azure\powershell\src\packages\Microsoft.Azure.Management.Resources.2.20.0-preview\lib\net40\Microsoft.Azure.ResourceManager.dll</HintPath>
55+
</Reference>
5256
<Reference Include="Microsoft.Azure.Test.Framework">
5357
<SpecificVersion>False</SpecificVersion>
5458
<HintPath>..\..\..\packages\Microsoft.Azure.Test.Framework.1.0.6179.26854-prerelease\lib\net45\Microsoft.Azure.Test.Framework.dll</HintPath>
@@ -144,10 +148,10 @@
144148
<None Include="ScenarioTests\RecoveryServicesTests.ps1">
145149
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
146150
</None>
147-
<None Include="SessionRecords\Microsoft.Azure.Commands.RecoveryServices.Test.ScenarioTests.RecoveryServicesTests\VaultCredFileDownloadTest.json">
148-
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
151+
<None Include="SessionRecords\Microsoft.Azure.Commands.RecoveryServices.Test.ScenarioTests.RecoveryServicesTests\TestGetRSVaultSettingsFile.json">
152+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
149153
</None>
150-
<None Include="SessionRecords\Microsoft.Azure.Commands.RecoveryServices.Test.ScenarioTests.RecoveryServicesTests\VaultCRUDTests.json">
154+
<None Include="SessionRecords\Microsoft.Azure.Commands.RecoveryServices.Test.ScenarioTests.RecoveryServicesTests\TestRecoveryServicesVaultCRUD.json">
151155
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
152156
</None>
153157
</ItemGroup>

src/ResourceManager/RecoveryServices/Commands.RecoveryServices.Test/ScenarioTests/RecoveryServicesTests.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,16 +29,16 @@ public RecoveryServicesTests(ITestOutputHelper output)
2929

3030
[Fact]
3131
[Trait(Category.AcceptanceType, Category.CheckIn)]
32-
public void VaultCRUDTests()
32+
public void TestRecoveryServicesVaultCRUD()
3333
{
34-
TestController.NewInstance.RunPsTest("Test-RecoveryServicesVaultCRUDTests");
34+
TestController.NewInstance.RunPsTest("Test-RecoveryServicesVaultCRUD");
3535
}
3636

3737
[Fact]
38-
[Trait(Category.RunType, Category.LiveOnly)]
39-
public void VaultCredFileDownloadTest()
38+
[Trait(Category.AcceptanceType, Category.CheckIn)]
39+
public void TestGetRSVaultSettingsFile()
4040
{
41-
TestController.NewInstance.RunPsTest("Test-RecoveryServicesVaultCredFileDownloadTest");
41+
TestController.NewInstance.RunPsTest("Test-GetRSVaultSettingsFile");
4242
}
4343
}
4444
}

src/ResourceManager/RecoveryServices/Commands.RecoveryServices.Test/ScenarioTests/RecoveryServicesTests.ps1

Lines changed: 137 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -14,65 +14,154 @@
1414

1515
########################## Recovery Services Tests #############################
1616

17+
function Get-ResourceGroupLocation
18+
{
19+
$namespace = "Microsoft.RecoveryServices"
20+
$type = "vaults"
21+
$resourceProvider = Get-AzureRmResourceProvider -ProviderNamespace $namespace | where {$_.ResourceTypes[0].ResourceTypeName -eq $type}
22+
23+
if ($resourceProvider -eq $null)
24+
{
25+
return "westus";
26+
}
27+
else
28+
{
29+
return $resourceProvider.Locations[0]
30+
}
31+
}
32+
33+
function Get-RandomSuffix(
34+
[int] $size = 8)
35+
{
36+
$variableName = "NamingSuffix"
37+
if ([Microsoft.Azure.Test.HttpRecorder.HttpMockServer]::Mode -eq [Microsoft.Azure.Test.HttpRecorder.HttpRecorderMode]::Record)
38+
{
39+
if ([Microsoft.Azure.Test.HttpRecorder.HttpMockServer]::Variables.ContainsKey($variableName))
40+
{
41+
$suffix = [Microsoft.Azure.Test.HttpRecorder.HttpMockServer]::Variables[$variableName]
42+
}
43+
else
44+
{
45+
$suffix = @((New-Guid).Guid)
46+
47+
[Microsoft.Azure.Test.HttpRecorder.HttpMockServer]::Variables[$variableName] = $suffix
48+
}
49+
}
50+
else
51+
{
52+
$suffix = [Microsoft.Azure.Test.HttpRecorder.HttpMockServer]::Variables[$variableName]
53+
}
54+
55+
return $suffix.Substring(0, $size)
56+
}
57+
58+
function Create-ResourceGroup(
59+
[string] $location)
60+
{
61+
$name = "PSTestRG" + @(Get-RandomSuffix)
62+
63+
$resourceGroup = Get-AzureRmResourceGroup -Name $name -ErrorAction Ignore
64+
65+
if ($resourceGroup -eq $null)
66+
{
67+
New-AzureRmResourceGroup -Name $name -Location $location | Out-Null
68+
}
69+
70+
return $name
71+
}
72+
73+
function Cleanup-ResourceGroup(
74+
[string] $resourceGroupName)
75+
{
76+
$resourceGroup = Get-AzureRmResourceGroup -Name $resourceGroupName -ErrorAction Ignore
77+
78+
if ($resourceGroup -ne $null)
79+
{
80+
# Cleanup Vaults
81+
$vaults = Get-AzureRmRecoveryServicesVault -ResourceGroupName $resourceGroupName
82+
foreach ($vault in $vaults)
83+
{
84+
Remove-AzureRmRecoveryServicesVault -Vault $vault
85+
}
86+
87+
# Cleanup RG. This cleans up all VMs and Storage Accounts.
88+
Remove-AzureRmResourceGroup -Name $resourceGroupName -Force
89+
}
90+
}
91+
1792
<#
1893
.SYNOPSIS
1994
Recovery Services Vault CRUD Tests
2095
#>
21-
function Test-RecoveryServicesVaultCRUDTests
96+
function Test-RecoveryServicesVaultCRUD
2297
{
23-
# Create vault
24-
$vaultCreationResponse = New-AzureRmRecoveryServicesVault -Name rsv1 -ResourceGroupName RsvTestRG -Location westus
25-
Assert-NotNull($vaultCreationResponse.Name)
26-
Assert-NotNull($vaultCreationResponse.ID)
27-
Assert-NotNull($vaultCreationResponse.Type)
28-
29-
# Enumerate Vaults
30-
$vaults = Get-AzureRmRecoveryServicesVault -Name rsv1 -ResourceGroupName RsvTestRG
31-
Assert-True { $vaults.Count -gt 0 }
32-
Assert-NotNull($vaults)
33-
foreach($vault in $vaults)
98+
$location = Get-ResourceGroupLocation
99+
$resourceGroupName = Create-ResourceGroup $location
100+
$name = "PSTestRSV" + @(Get-RandomSuffix)
101+
102+
try
34103
{
35-
Assert-NotNull($vault.Name)
36-
Assert-NotNull($vault.ID)
37-
Assert-NotNull($vault.Type)
38-
}
104+
# 1. New-AzureRmRecoveryServicesVault
105+
$vault1 = New-AzureRmRecoveryServicesVault -Name $name -ResourceGroupName $resourceGroupName -Location $location
106+
107+
Assert-NotNull($vault1.Name)
108+
Assert-NotNull($vault1.ID)
109+
Assert-NotNull($vault1.Type)
110+
111+
# 2. Set-AzureRmRecoveryServicesVaultContext
112+
Set-AzureRmRecoveryServicesVaultContext -Vault $vault1
113+
114+
# 3. Get-AzureRmRecoveryServicesVault
115+
$vaults = Get-AzureRmRecoveryServicesVault -Name $name -ResourceGroupName $resourceGroupName
116+
117+
Assert-NotNull($vaults)
118+
Assert-True { $vaults.Count -gt 0 }
119+
foreach ($vault in $vaults)
120+
{
121+
Assert-NotNull($vault.Name)
122+
Assert-NotNull($vault.ID)
123+
Assert-NotNull($vault.Type)
124+
}
39125

40-
$vaultBackupProperties = Get-AzureRmRecoveryServicesBackupProperty -Vault $vaultCreationResponse
41-
Assert-NotNull($vaultBackupProperties.BackupStorageRedundancy)
126+
# 4. Get-AzureRmRecoveryServicesBackupProperty
127+
$vaultBackupProperties = Get-AzureRmRecoveryServicesBackupProperty -Vault $vault1
42128

43-
Set-AzureRmRecoveryServicesBackupProperties -Vault $vaultCreationResponse -BackupStorageRedundancy "LocallyRedundant"
129+
Assert-NotNull($vaultBackupProperties.BackupStorageRedundancy)
44130

45-
# Get the created vault
46-
$vaultToBeProcessed = Get-AzureRmRecoveryServicesVault -ResourceGroupName RsvTestRG -Name rsv1
47-
Assert-NotNull($vaultToBeProcessed.Name)
48-
Assert-NotNull($vaultToBeProcessed.ID)
49-
Assert-NotNull($vaultToBeProcessed.Type)
131+
# 5. Set-AzureRmRecoveryServicesBackupProperties
132+
Set-AzureRmRecoveryServicesBackupProperties -Vault $vault1 -BackupStorageRedundancy LocallyRedundant
50133

51-
# Remove Vault
52-
Remove-AzureRmRecoveryServicesVault -Vault $vaultToBeProcessed
53-
$vaults = Get-AzureRmRecoveryServicesVault -ResourceGroupName RsvTestRG -Name rsv1
54-
Assert-True { $vaults.Count -eq 0 }
134+
# 6. Remove-AzureRmRecoveryServicesVault
135+
Remove-AzureRmRecoveryServicesVault -Vault $vault1
136+
137+
$vaults = Get-AzureRmRecoveryServicesVault -ResourceGroupName $resourceGroupName -Name $name
138+
Assert-True { $vaults.Count -eq 0 }
139+
}
140+
finally
141+
{
142+
Cleanup-ResourceGroup $resourceGroupName
143+
}
55144
}
56145

57-
function Test-RecoveryServicesVaultCredFileDownloadTest
146+
function Test-GetRSVaultSettingsFile
58147
{
59-
# Create vault
60-
$vaultCreationResponse = New-AzureRmRecoveryServicesVault -Name rsv1 -ResourceGroupName RsvTestRG -Location westus
61-
Assert-NotNull($vaultCreationResponse.Name)
62-
Assert-NotNull($vaultCreationResponse.ID)
63-
Assert-NotNull($vaultCreationResponse.Type)
64-
65-
$drives = Get-PSDrive -PSProvider 'FileSystem'
66-
$folderPath = $drives[0].Root
67-
$file = Get-AzureRmRecoveryServicesVaultSettingsFile -Vault $vaultCreationResponse -Backup -Path $path
68-
Assert-True { Test-Path $file.FilePath }
69-
$fileContent = Get-Content $file.FilePath
70-
Assert-Contains($fileContent, $vaultCreationResponse.Name)
71-
Assert-Contains($fileContent, $vaultCreationResponse.ResourceGroupName)
72-
Assert-Contains($fileContent, $vaultCreationResponse.Location)
73-
74-
# Remove Vault
75-
Remove-AzureRmRecoveryServicesVault -Vault $vaultCreationResponse
76-
$vaults = Get-AzureRmRecoveryServicesVault -ResourceGroupName RsvTestRG -Name rsv1
77-
Assert-True { $vaults.Count -eq 0 }
148+
$location = Get-ResourceGroupLocation
149+
$resourceGroupName = Create-ResourceGroup $location
150+
$name = "PSTestRSV" + @(Get-RandomSuffix)
151+
152+
try
153+
{
154+
# 1. New-AzureRmRecoveryServicesVault
155+
$vault = New-AzureRmRecoveryServicesVault -Name $name -ResourceGroupName $resourceGroupName -Location $location
156+
157+
# 2. Get-AzureRmRecoveryServicesVaultSettingsFile
158+
$file = Get-AzureRmRecoveryServicesVaultSettingsFile -Vault $vault -Backup
159+
160+
Assert-NotNull $file
161+
Assert-NotNull $file.FilePath
162+
}
163+
finally
164+
{
165+
Cleanup-ResourceGroup $resourceGroupName
166+
}
78167
}

src/ResourceManager/RecoveryServices/Commands.RecoveryServices.Test/ScenarioTests/TestsBase.cs

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -17,23 +17,26 @@
1717
using System.IO;
1818
using System.Linq;
1919
using Microsoft.Azure.Commands.Common.Authentication;
20-
using Microsoft.Azure.Management.Internal.Resources;
2120
using Microsoft.Azure.Management.RecoveryServices;
22-
using Microsoft.Azure.Test;
2321
using Microsoft.Azure.Test.HttpRecorder;
2422
using Microsoft.WindowsAzure.Commands.ScenarioTest;
23+
using InternalRmNS = Microsoft.Azure.Management.Internal.Resources;
24+
using LegacyTest = Microsoft.Azure.Test;
25+
using ResourceManagementNS = Microsoft.Azure.Management.Resources;
2526
using RestTestFramework = Microsoft.Rest.ClientRuntime.Azure.TestFramework;
2627

2728
namespace Microsoft.Azure.Commands.RecoveryServices.Test.ScenarioTests
2829
{
2930
public class TestController
3031
{
31-
CSMTestEnvironmentFactory csmTestFactory;
32+
LegacyTest.CSMTestEnvironmentFactory csmTestFactory;
3233
EnvironmentSetupHelper helper;
3334

3435
public RecoveryServicesClient RsClient { get; private set; }
3536

36-
public ResourceManagementClient RmClient { get; private set; }
37+
public ResourceManagementNS.ResourceManagementClient RmClient { get; private set; }
38+
39+
public InternalRmNS.ResourceManagementClient InternalRmClient { get; private set; }
3740

3841
public static TestController NewInstance
3942
{
@@ -51,23 +54,31 @@ public TestController()
5154
protected void SetupManagementClients(RestTestFramework.MockContext context)
5255
{
5356
RsClient = GetRsClient(context);
54-
RmClient = GetRmClient(context);
57+
RmClient = GetRmClient();
58+
InternalRmClient = GetInternalRmClient(context);
5559

5660
helper.SetupManagementClients(
5761
RsClient,
58-
RmClient);
62+
RmClient,
63+
InternalRmClient);
5964
}
6065

61-
private ResourceManagementClient GetRmClient(RestTestFramework.MockContext context)
66+
private InternalRmNS.ResourceManagementClient GetInternalRmClient(RestTestFramework.MockContext context)
6267
{
63-
return context.GetServiceClient<ResourceManagementClient>(
68+
return context.GetServiceClient<InternalRmNS.ResourceManagementClient>(
6469
RestTestFramework.TestEnvironmentFactory.GetTestEnvironment());
6570
}
6671

72+
private ResourceManagementNS.ResourceManagementClient GetRmClient()
73+
{
74+
return LegacyTest.TestBase.GetServiceClient<ResourceManagementNS.ResourceManagementClient>(
75+
this.csmTestFactory);
76+
}
77+
6778
public void RunPsTest(params string[] scripts)
6879
{
69-
var callingClassType = Azure.Test.TestUtilities.GetCallingClass(2);
70-
var mockName = Azure.Test.TestUtilities.GetCurrentMethodName(2);
80+
var callingClassType = LegacyTest.TestUtilities.GetCallingClass(2);
81+
var mockName = LegacyTest.TestUtilities.GetCurrentMethodName(2);
7182

7283
RunPsTestWorkflow(
7384
() => scripts,
@@ -81,7 +92,7 @@ public void RunPsTest(params string[] scripts)
8192

8293
public void RunPsTestWorkflow(
8394
Func<string[]> scriptBuilder,
84-
Action<CSMTestEnvironmentFactory> initialize,
95+
Action<LegacyTest.CSMTestEnvironmentFactory> initialize,
8596
Action cleanup,
8697
string callingClassType,
8798
string mockName)
@@ -101,7 +112,7 @@ public void RunPsTestWorkflow(
101112
using (RestTestFramework.MockContext context =
102113
RestTestFramework.MockContext.Start(callingClassType, mockName))
103114
{
104-
csmTestFactory = new CSMTestEnvironmentFactory();
115+
csmTestFactory = new LegacyTest.CSMTestEnvironmentFactory();
105116

106117
if (initialize != null)
107118
{
@@ -125,6 +136,8 @@ public void RunPsTestWorkflow(
125136
modules.Add(psFile);
126137
modules.Add(rmProfileModule);
127138
modules.Add(rmModulePath);
139+
modules.Add(helper.RMResourceModule);
140+
modules.Add("AzureRM.Resources.ps1");
128141

129142
helper.SetupModules(AzureModule.AzureResourceManager, modules.ToArray());
130143

@@ -153,7 +166,7 @@ public void RunPsTestWorkflow(
153166
private RecoveryServicesClient GetRsClient(RestTestFramework.MockContext context)
154167
{
155168
return context.GetServiceClient<RecoveryServicesClient>(
156-
Rest.ClientRuntime.Azure.TestFramework.TestEnvironmentFactory.GetTestEnvironment());
169+
RestTestFramework.TestEnvironmentFactory.GetTestEnvironment());
157170
}
158171
}
159172
}

0 commit comments

Comments
 (0)