15
15
using Microsoft . Azure . Commands . Common . Authentication ;
16
16
using Microsoft . Azure . Test . HttpRecorder ;
17
17
using System . Collections . Generic ;
18
+ using System . Diagnostics ;
18
19
using Microsoft . Azure . Management . Internal . Resources ;
20
+ using Microsoft . Azure . Management . TrafficManager ;
21
+ using Microsoft . WindowsAzure . Commands . ScenarioTest ;
22
+ using Microsoft . Azure . ServiceManagemenet . Common . Models ;
23
+ using System ;
24
+ using System . IO ;
25
+ using System . Linq ;
26
+ using Microsoft . WindowsAzure . Commands . Test . Utilities . Common ;
27
+ using RestTestFramework = Microsoft . Rest . ClientRuntime . Azure . TestFramework ;
19
28
20
29
namespace Microsoft . Azure . Commands . TrafficManager . Test . ScenarioTests
21
30
{
22
- using Microsoft . Azure . Gallery ;
23
- using Microsoft . Azure . Management . Authorization ;
24
- using Microsoft . Azure . Management . TrafficManager ;
25
- using Microsoft . Azure . Subscriptions ;
26
- using Microsoft . Azure . Test ;
27
- using Microsoft . WindowsAzure . Commands . ScenarioTest ;
28
- using ServiceManagemenet . Common . Models ;
29
- using System ;
30
- using System . IO ;
31
- using System . Linq ;
32
- using WindowsAzure . Commands . Test . Utilities . Common ;
33
-
34
- using RestTestFramework = Microsoft . Rest . ClientRuntime . Azure . TestFramework ;
35
-
36
31
public class TestController : RMTestBase
37
32
{
38
- private CSMTestEnvironmentFactory csmTestFactory ;
39
-
40
- private EnvironmentSetupHelper helper ;
33
+ private readonly EnvironmentSetupHelper _helper ;
41
34
42
35
public ResourceManagementClient ResourceManagementClient { get ; private set ; }
43
36
44
- public SubscriptionClient SubscriptionClient { get ; private set ; }
45
-
46
- public GalleryClient GalleryClient { get ; private set ; }
47
-
48
- public AuthorizationManagementClient AuthorizationManagementClient { get ; private set ; }
49
-
50
37
public TrafficManagerManagementClient TrafficManagerManagementClient { get ; private set ; }
51
38
52
- public static TestController NewInstance
53
- {
54
- get
55
- {
56
- return new TestController ( ) ;
57
- }
58
- }
39
+ public static TestController NewInstance => new TestController ( ) ;
59
40
60
41
protected TestController ( )
61
42
{
62
- this . helper = new EnvironmentSetupHelper ( ) ;
43
+ _helper = new EnvironmentSetupHelper ( ) ;
63
44
}
64
45
65
46
protected void SetupManagementClients ( RestTestFramework . MockContext context )
66
47
{
67
- this . ResourceManagementClient = this . GetResourceManagementClient ( context ) ;
68
- this . SubscriptionClient = this . GetSubscriptionClient ( ) ;
69
- this . GalleryClient = this . GetGalleryClient ( ) ;
70
- this . AuthorizationManagementClient = this . GetAuthorizationManagementClient ( ) ;
71
- this . TrafficManagerManagementClient = this . GetFeatureClient ( context ) ;
72
-
73
- this . helper . SetupManagementClients (
74
- this . ResourceManagementClient ,
75
- this . SubscriptionClient ,
76
- this . GalleryClient ,
77
- this . AuthorizationManagementClient ,
78
- this . TrafficManagerManagementClient ) ;
48
+ ResourceManagementClient = GetResourceManagementClient ( context ) ;
49
+ TrafficManagerManagementClient = GetFeatureClient ( context ) ;
50
+
51
+ _helper . SetupManagementClients (
52
+ ResourceManagementClient ,
53
+ TrafficManagerManagementClient ) ;
79
54
}
80
55
81
56
public void RunPowerShellTest ( XunitTracingInterceptor logger , params string [ ] scripts )
82
57
{
83
- string callingClassType = TestUtilities . GetCallingClass ( 2 ) ;
84
- string mockName = TestUtilities . GetCurrentMethodName ( 2 ) ;
58
+ var sf = new StackTrace ( ) . GetFrame ( 1 ) ;
59
+ var callingClassType = sf . GetMethod ( ) . ReflectedType ? . ToString ( ) ;
60
+ var mockName = sf . GetMethod ( ) . Name ;
85
61
86
- helper . TracingInterceptor = logger ;
62
+ _helper . TracingInterceptor = logger ;
87
63
88
- this . RunPsTestWorkflow (
64
+ RunPsTestWorkflow (
89
65
( ) => scripts ,
90
- // no custom initializer
91
- null ,
92
66
// no custom cleanup
93
67
null ,
94
68
callingClassType ,
@@ -97,62 +71,49 @@ public void RunPowerShellTest(XunitTracingInterceptor logger, params string[] sc
97
71
98
72
public void RunPsTestWorkflow (
99
73
Func < string [ ] > scriptBuilder ,
100
- Action < CSMTestEnvironmentFactory > initialize ,
101
74
Action cleanup ,
102
75
string callingClassType ,
103
76
string mockName )
104
77
{
105
- Dictionary < string , string > d = new Dictionary < string , string > ( ) ;
106
- d . Add ( "Microsoft.Resources" , null ) ;
107
- d . Add ( "Microsoft.Features" , null ) ;
108
- d . Add ( "Microsoft.Authorization" , null ) ;
109
- var providersToIgnore = new Dictionary < string , string > ( ) ;
110
- providersToIgnore . Add ( "Microsoft.Azure.Management.Resources.ResourceManagementClient" , "2016-02-01" ) ;
78
+ var d = new Dictionary < string , string >
79
+ {
80
+ { "Microsoft.Resources" , null } ,
81
+ { "Microsoft.Features" , null } ,
82
+ { "Microsoft.Authorization" , null }
83
+ } ;
84
+ var providersToIgnore = new Dictionary < string , string >
85
+ {
86
+ { "Microsoft.Azure.Management.Resources.ResourceManagementClient" , "2016-02-01" }
87
+ } ;
111
88
HttpMockServer . Matcher = new PermissiveRecordMatcherWithApiExclusion ( true , d , providersToIgnore ) ;
112
89
113
90
HttpMockServer . RecordsDirectory = Path . Combine ( AppDomain . CurrentDomain . BaseDirectory , "SessionRecords" ) ;
114
- using ( RestTestFramework . MockContext context = RestTestFramework . MockContext . Start ( callingClassType , mockName ) )
91
+ using ( var context = RestTestFramework . MockContext . Start ( callingClassType , mockName ) )
115
92
{
116
- this . csmTestFactory = new CSMTestEnvironmentFactory ( ) ;
93
+ _helper . SetupEnvironment ( AzureModule . AzureResourceManager ) ;
117
94
118
- if ( initialize != null )
119
- {
120
- initialize ( this . csmTestFactory ) ;
121
- }
95
+ SetupManagementClients ( context ) ;
122
96
123
- this . helper . SetupEnvironment ( AzureModule . AzureResourceManager ) ;
97
+ var callingClassName = callingClassType . Split ( new [ ] { "." } , StringSplitOptions . RemoveEmptyEntries ) . Last ( ) ;
124
98
125
- this . SetupManagementClients ( context ) ;
126
-
127
- string callingClassName = callingClassType
128
- . Split ( new [ ] { "." } , StringSplitOptions . RemoveEmptyEntries )
129
- . Last ( ) ;
130
-
131
- this . helper . SetupModules ( AzureModule . AzureResourceManager ,
99
+ _helper . SetupModules ( AzureModule . AzureResourceManager ,
132
100
"ScenarioTests\\ Common.ps1" ,
133
101
"ScenarioTests\\ " + callingClassName + ".ps1" ,
134
- helper . RMProfileModule ,
135
- helper . GetRMModulePath ( @"AzureRM.TrafficManager.psd1" ) ,
102
+ _helper . RMProfileModule ,
103
+ _helper . GetRMModulePath ( @"AzureRM.TrafficManager.psd1" ) ,
136
104
"AzureRM.Resources.ps1" ) ;
137
105
138
106
try
139
107
{
140
- if ( scriptBuilder != null )
108
+ var psScripts = scriptBuilder ? . Invoke ( ) ;
109
+ if ( psScripts != null )
141
110
{
142
- string [ ] psScripts = scriptBuilder ( ) ;
143
-
144
- if ( psScripts != null )
145
- {
146
- this . helper . RunPowerShellTest ( psScripts ) ;
147
- }
111
+ _helper . RunPowerShellTest ( psScripts ) ;
148
112
}
149
113
}
150
114
finally
151
115
{
152
- if ( cleanup != null )
153
- {
154
- cleanup ( ) ;
155
- }
116
+ cleanup ? . Invoke ( ) ;
156
117
}
157
118
}
158
119
}
@@ -162,22 +123,7 @@ protected ResourceManagementClient GetResourceManagementClient(RestTestFramework
162
123
return context . GetServiceClient < ResourceManagementClient > ( RestTestFramework . TestEnvironmentFactory . GetTestEnvironment ( ) ) ;
163
124
}
164
125
165
- private AuthorizationManagementClient GetAuthorizationManagementClient ( )
166
- {
167
- return TestBase . GetServiceClient < AuthorizationManagementClient > ( this . csmTestFactory ) ;
168
- }
169
-
170
- private SubscriptionClient GetSubscriptionClient ( )
171
- {
172
- return TestBase . GetServiceClient < SubscriptionClient > ( this . csmTestFactory ) ;
173
- }
174
-
175
- private GalleryClient GetGalleryClient ( )
176
- {
177
- return TestBase . GetServiceClient < GalleryClient > ( this . csmTestFactory ) ;
178
- }
179
-
180
- private TrafficManagerManagementClient GetFeatureClient ( RestTestFramework . MockContext context )
126
+ private static TrafficManagerManagementClient GetFeatureClient ( RestTestFramework . MockContext context )
181
127
{
182
128
return context . GetServiceClient < TrafficManagerManagementClient > ( RestTestFramework . TestEnvironmentFactory . GetTestEnvironment ( ) ) ;
183
129
}
0 commit comments