forked from Azure/azure-powershell
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TestController.cs
135 lines (118 loc) · 5.38 KB
/
TestController.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
// ----------------------------------------------------------------------------------
//
// 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.Diagnostics;
using System.IO;
using System.Linq;
using Microsoft.Azure.Commands.Common.Authentication;
using Microsoft.Azure.Management.Media;
using Microsoft.Azure.Management.Storage.Version2017_10_01;
using Microsoft.Azure.Test.HttpRecorder;
using Microsoft.Rest.ClientRuntime.Azure.TestFramework;
using Microsoft.WindowsAzure.Commands.ScenarioTest;
using Microsoft.WindowsAzure.Commands.Test.Utilities.Common;
using TestEnvironmentFactory = Microsoft.Rest.ClientRuntime.Azure.TestFramework.TestEnvironmentFactory;
using Microsoft.Azure.Management.Internal.Resources;
namespace Microsoft.Azure.Commands.Media.Test.ScenarioTests
{
/// <summary>
/// Setup for Scenario Tests
/// </summary>
public class TestController : RMTestBase
{
private readonly EnvironmentSetupHelper _helper;
public ResourceManagementClient ResourceManagementClient { get; private set; }
public MediaServicesManagementClient MediaServicesManagementClient { get; private set; }
public StorageManagementClient StorageManagementClient { get; private set; }
public static TestController NewInstance => new TestController();
protected TestController()
{
_helper = new EnvironmentSetupHelper();
}
protected void SetupManagementClients(MockContext context)
{
ResourceManagementClient = context.GetServiceClient<ResourceManagementClient>(TestEnvironmentFactory.GetTestEnvironment());
StorageManagementClient = context.GetServiceClient<StorageManagementClient>(TestEnvironmentFactory.GetTestEnvironment());
MediaServicesManagementClient = context.GetServiceClient<MediaServicesManagementClient>(TestEnvironmentFactory.GetTestEnvironment());
_helper.SetupManagementClients(
ResourceManagementClient,
StorageManagementClient,
MediaServicesManagementClient);
}
/// <summary>
/// Methods for invoking PowerShell scripts
/// </summary>
/// <param name="logger"></param>
/// <param name="scripts"></param>
public void RunPowerShellTest(ServiceManagement.Common.Models.XunitTracingInterceptor logger, params string[] scripts)
{
var sf = new StackTrace().GetFrame(1);
var callingClassType = sf.GetMethod().ReflectedType?.ToString();
var mockName = sf.GetMethod().Name;
_helper.TracingInterceptor = logger;
RunPsTestWorkflow(
() => scripts,
// no custom cleanup
null,
callingClassType,
mockName);
}
private void RunPsTestWorkflow(
Func<string[]> scriptBuilder,
Action cleanup,
string callingClassType,
string mockName)
{
var d = new Dictionary<string, string>
{
{"Microsoft.Resources", null},
{"Microsoft.Features", null},
{"Microsoft.Authorization", null},
{"Microsoft.Compute", null}
};
var providersToIgnore = new Dictionary<string, string>
{
{"Microsoft.Azure.Management.Resources.ResourceManagementClient", "2016-02-01"}
};
HttpMockServer.Matcher = new PermissiveRecordMatcherWithApiExclusion(true, d, providersToIgnore);
HttpMockServer.RecordsDirectory = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "SessionRecords");
using (var context = MockContext.Start(callingClassType, mockName))
{
SetupManagementClients(context);
_helper.SetupEnvironment(AzureModule.AzureResourceManager);
var callingClassName = callingClassType.Split(new[] { "." }, StringSplitOptions.RemoveEmptyEntries).Last();
_helper.SetupModules(AzureModule.AzureResourceManager,
"ScenarioTests\\Common.ps1",
"ScenarioTests\\" + callingClassName + ".ps1",
_helper.RMProfileModule,
_helper.GetRMModulePath("AzureRM.Media.psd1"),
"AzureRM.Resources.ps1",
"AzureRM.Storage.ps1");
try
{
var psScripts = scriptBuilder?.Invoke();
if (psScripts != null)
{
_helper.RunPowerShellTest(psScripts);
}
}
finally
{
cleanup?.Invoke();
}
}
}
}
}