11
11
// See the License for the specific language governing permissions and
12
12
// limitations under the License.
13
13
// ----------------------------------------------------------------------------------
14
-
14
+ using Microsoft . Azure . Attestation ;
15
15
using Microsoft . Azure . Commands . Common . Authentication ;
16
16
using Microsoft . Azure . Management . Attestation ;
17
17
using Microsoft . Azure . ServiceManagement . Common . Models ;
23
23
using System . IO ;
24
24
using System . Linq ;
25
25
using Microsoft . Azure . Management . Internal . Resources ;
26
+ using Microsoft . IdentityModel . Clients . ActiveDirectory ;
26
27
using Microsoft . Rest . ClientRuntime . Azure . TestFramework ;
27
28
28
29
namespace Microsoft . Azure . Commands . Attestation . Test
@@ -31,19 +32,13 @@ class AttestationController
31
32
{
32
33
private readonly EnvironmentSetupHelper _helper ;
33
34
34
-
35
- public ResourceManagementClient ResourceClient { get ; private set ; }
36
-
37
- public AttestationManagementClient AttestationManagementClient { get ; private set ; }
38
-
39
35
public static AttestationController NewInstance => new AttestationController ( ) ;
40
36
41
37
public AttestationController ( )
42
38
{
43
39
_helper = new EnvironmentSetupHelper ( ) ;
44
40
}
45
41
46
-
47
42
public void RunPowerShellTest ( XunitTracingInterceptor logger , params string [ ] scripts )
48
43
{
49
44
var sf = new StackTrace ( ) . GetFrame ( 1 ) ;
@@ -58,14 +53,37 @@ public void RunPowerShellTest(XunitTracingInterceptor logger, params string[] sc
58
53
// no custom cleanup
59
54
null ,
60
55
callingClassType ,
61
- mockName ) ;
56
+ mockName ,
57
+ true ,
58
+ false ) ;
59
+ }
60
+
61
+ public void RunDataPowerShellTest ( XunitTracingInterceptor logger , params string [ ] scripts )
62
+ {
63
+ var sf = new StackTrace ( ) . GetFrame ( 1 ) ;
64
+ var callingClassType = sf . GetMethod ( ) . ReflectedType ? . ToString ( ) ;
65
+ var mockName = sf . GetMethod ( ) . Name ;
66
+
67
+ logger . Information ( string . Format ( "Test method entered: {0}.{1}" , callingClassType , mockName ) ) ;
68
+ _helper . TracingInterceptor = logger ;
69
+
70
+ RunPowerShellTestWorkflow (
71
+ ( ) => scripts ,
72
+ // no custom cleanup
73
+ null ,
74
+ callingClassType ,
75
+ mockName ,
76
+ false ,
77
+ true ) ;
62
78
}
63
79
64
80
public void RunPowerShellTestWorkflow (
65
81
Func < string [ ] > scriptBuilder ,
66
82
Action cleanup ,
67
83
string callingClassType ,
68
- string mockName )
84
+ string mockName ,
85
+ bool setupManagementClients ,
86
+ bool setupDataClient )
69
87
{
70
88
var providers = new Dictionary < string , string >
71
89
{
@@ -82,8 +100,17 @@ public void RunPowerShellTestWorkflow(
82
100
HttpMockServer . RecordsDirectory = Path . Combine ( AppDomain . CurrentDomain . BaseDirectory , "SessionRecords" ) ;
83
101
using ( var context = MockContext . Start ( callingClassType , mockName ) )
84
102
{
85
- SetupManagementClients ( context ) ;
86
- _helper . SetupEnvironment ( AzureModule . AzureResourceManager ) ;
103
+ if ( setupManagementClients )
104
+ {
105
+ SetupManagementClients ( context ) ;
106
+ _helper . SetupEnvironment ( AzureModule . AzureResourceManager ) ;
107
+ }
108
+
109
+ if ( setupDataClient )
110
+ {
111
+ SetupDataClient ( context ) ;
112
+ }
113
+
87
114
var callingClassName =
88
115
callingClassType . Split ( new [ ] { "." } , StringSplitOptions . RemoveEmptyEntries ) . Last ( ) ;
89
116
_helper . SetupModules ( AzureModule . AzureResourceManager ,
@@ -109,9 +136,19 @@ public void RunPowerShellTestWorkflow(
109
136
}
110
137
private void SetupManagementClients ( MockContext context )
111
138
{
112
- ResourceClient = GetResourceManagementClient ( context ) ;
113
- AttestationManagementClient = GetAttestationManagementClient ( context ) ;
114
- _helper . SetupManagementClients ( ResourceClient , AttestationManagementClient ) ;
139
+ _helper . SetupManagementClients (
140
+ GetResourceManagementClient ( context ) ,
141
+ GetAttestationManagementClient ( context )
142
+ ) ;
143
+ }
144
+
145
+ private void SetupDataClient ( MockContext context )
146
+ {
147
+ _helper . SetupManagementClients (
148
+ GetResourceManagementClient ( context ) ,
149
+ GetAttestationManagementClient ( context ) ,
150
+ GetAttestationClient ( context )
151
+ ) ;
115
152
}
116
153
117
154
private static ResourceManagementClient GetResourceManagementClient ( MockContext context )
@@ -123,5 +160,28 @@ private static AttestationManagementClient GetAttestationManagementClient(MockCo
123
160
{
124
161
return context . GetServiceClient < AttestationManagementClient > ( TestEnvironmentFactory . GetTestEnvironment ( ) ) ;
125
162
}
163
+
164
+ private static AttestationClient GetAttestationClient ( MockContext context )
165
+ {
166
+ string environmentConnectionString = Environment . GetEnvironmentVariable ( "TEST_CSM_ORGID_AUTHENTICATION" ) ;
167
+ string accessToken = "fakefakefake" ;
168
+
169
+ // When recording, we should have a connection string passed into the code from the environment
170
+ if ( ! string . IsNullOrEmpty ( environmentConnectionString ) )
171
+ {
172
+ // Gather test client credential information from the environment
173
+ var connectionInfo = new ConnectionString ( Environment . GetEnvironmentVariable ( "TEST_CSM_ORGID_AUTHENTICATION" ) ) ;
174
+ string servicePrincipal = connectionInfo . GetValue < string > ( ConnectionStringKeys . ServicePrincipalKey ) ;
175
+ string servicePrincipalSecret = connectionInfo . GetValue < string > ( ConnectionStringKeys . ServicePrincipalSecretKey ) ;
176
+ string aadTenant = connectionInfo . GetValue < string > ( ConnectionStringKeys . AADTenantKey ) ;
177
+
178
+ // Create credentials
179
+ var clientCredentials = new ClientCredential ( servicePrincipal , servicePrincipalSecret ) ;
180
+ var authContext = new AuthenticationContext ( $ "https://login.windows.net/{ aadTenant } ", TokenCache . DefaultShared ) ;
181
+ accessToken = authContext . AcquireTokenAsync ( "https://attest.azure.net" , clientCredentials ) . Result . AccessToken ;
182
+ }
183
+
184
+ return new AttestationClient ( new AttestationCredentials ( accessToken ) , HttpMockServer . CreateInstance ( ) ) ;
185
+ }
126
186
}
127
187
}
0 commit comments