-
Notifications
You must be signed in to change notification settings - Fork 3.9k
/
Copy pathTestsBase.cs
204 lines (171 loc) · 8.11 KB
/
TestsBase.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
// ----------------------------------------------------------------------------------
//
// Copyright Microsoft Corporation
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// ----------------------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Microsoft.Azure.Commands.Common.Authentication;
using Microsoft.Azure.Commands.RecoveryServices.Backup.Cmdlets.Models;
using Microsoft.Azure.Management.RecoveryServices.Backup;
using Microsoft.Azure.Test.HttpRecorder;
using Microsoft.WindowsAzure.Commands.ScenarioTest;
using Microsoft.Rest.ClientRuntime.Azure.TestFramework;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.Management.Automation;
using Microsoft.Azure.Management.Compute;
using Microsoft.Azure.Management.Internal.Resources;
using Microsoft.Azure.Management.Network;
using Microsoft.Azure.Management.RecoveryServices;
using Microsoft.Azure.Management.Storage.Version2017_10_01;
using Microsoft.Azure.ServiceManagement.Common.Models;
namespace Microsoft.Azure.Commands.RecoveryServices.Backup.Test.ScenarioTests
{
public class TestController
{
private readonly EnvironmentSetupHelper _helper;
public RecoveryServicesBackupClient RsBackupClient { get; private set; }
public RecoveryServicesClient RsClient { get; private set; }
public ResourceManagementClient RmRestClient { get; private set; }
public StorageManagementClient StorageClient { get; private set; }
public NetworkManagementClient NetworkManagementClient { get; private set; }
public ComputeManagementClient ComputeManagementClient { get; private set; }
protected string ResourceNamespace { get; private set; }
public static TestController NewInstance => new TestController();
public TestController()
{
_helper = new EnvironmentSetupHelper();
ResourceNamespace = "Microsoft.RecoveryServices";
}
protected void SetResourceNamespace(string resourceNamespace)
{
ResourceNamespace = resourceNamespace;
}
protected void SetupManagementClients(MockContext context)
{
RsBackupClient = GetRsBackupClient(context);
RsClient = GetRsClient(context);
RmRestClient = GetRmRestClient(context);
StorageClient = GetStorageManagementClient(context);
NetworkManagementClient = GetNetworkManagementClient(context);
ComputeManagementClient = GetComputeManagementClient(context);
_helper.SetupManagementClients(
RsBackupClient,
RsClient,
RmRestClient,
StorageClient,
NetworkManagementClient,
ComputeManagementClient);
}
public Collection<PSObject> RunPsTest(XunitTracingInterceptor logger, PsBackupProviderTypes providerType, params string[] scripts)
{
var sf = new StackTrace().GetFrame(1);
var callingClassType = sf.GetMethod().ReflectedType?.ToString();
var mockName = sf.GetMethod().Name;
_helper.TracingInterceptor = logger;
return RunPsTestWorkflow(
providerType,
() => scripts,
// no custom cleanup
null,
callingClassType,
mockName);
}
public Collection<PSObject> RunPsTestWorkflow(
PsBackupProviderTypes providerType,
Func<string[]> scriptBuilder,
Action cleanup,
string callingClassType,
string mockName)
{
var providers = new Dictionary<string, string>
{
{"Microsoft.Resources", null},
{"Microsoft.Features", null},
{"Microsoft.Authorization", null},
{"Microsoft.Compute", null},
{"Microsoft.Network", null},
{"Microsoft.Storage", null}
};
var providersToIgnore = new Dictionary<string, string>
{
{"Microsoft.Azure.Management.Resources.ResourceManagementClient", "2016-02-01"},
{"Microsoft.Azure.Management.Internal.Resources.ResourceManagementClient", "2016-02-01"}
};
HttpMockServer.Matcher = new PermissiveRecordMatcherWithApiExclusion(true, providers, providersToIgnore);
HttpMockServer.RecordsDirectory = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "SessionRecords");
using (var context = MockContext.Start(callingClassType, mockName))
{
SetupManagementClients(context);
_helper.SetupEnvironment(AzureModule.AzureResourceManager);
var testFolderName = providerType.ToString();
var callingClassName = callingClassType.Split(new[] { "." }, StringSplitOptions.RemoveEmptyEntries).Last();
var modules = new List<string>
{
"ScenarioTests\\Common.ps1",
"ScenarioTests\\" + testFolderName + "\\" + callingClassName + ".ps1",
_helper.RMProfileModule,
_helper.GetRMModulePath("AzureRM.RecoveryServices.psd1"),
_helper.GetRMModulePath("AzureRM.Compute.psd1"),
_helper.GetRMModulePath("AzureRM.Network.psd1"),
"AzureRM.Storage.ps1",
"AzureRM.Resources.ps1"
};
var workloadCommonPsFile = Path.Combine("ScenarioTests", testFolderName, "Common.ps1");
if (File.Exists(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, workloadCommonPsFile)))
{
modules.Add(workloadCommonPsFile);
}
_helper.SetupModules(AzureModule.AzureResourceManager, modules.ToArray());
try
{
var psScripts = scriptBuilder?.Invoke();
if (psScripts != null)
{
return _helper.RunPowerShellTest(psScripts);
}
}
finally
{
cleanup?.Invoke();
}
}
return null;
}
private static RecoveryServicesClient GetRsClient(MockContext context)
{
return context.GetServiceClient<RecoveryServicesClient>(TestEnvironmentFactory.GetTestEnvironment());
}
private static RecoveryServicesBackupClient GetRsBackupClient(MockContext context)
{
return context.GetServiceClient<RecoveryServicesBackupClient>(TestEnvironmentFactory.GetTestEnvironment());
}
private static StorageManagementClient GetStorageManagementClient(MockContext context)
{
return context.GetServiceClient<StorageManagementClient>(TestEnvironmentFactory.GetTestEnvironment());
}
private static NetworkManagementClient GetNetworkManagementClient(MockContext context)
{
return context.GetServiceClient<NetworkManagementClient>(TestEnvironmentFactory.GetTestEnvironment());
}
private static ComputeManagementClient GetComputeManagementClient(MockContext context)
{
return context.GetServiceClient<ComputeManagementClient>(TestEnvironmentFactory.GetTestEnvironment());
}
private static ResourceManagementClient GetRmRestClient(MockContext context)
{
return context.GetServiceClient<ResourceManagementClient>(TestEnvironmentFactory.GetTestEnvironment());
}
}
}