12
12
// limitations under the License.
13
13
// ----------------------------------------------------------------------------------
14
14
15
+ using System ;
16
+ using System . Collections . Generic ;
17
+ using System . Management . Automation ;
15
18
using System . Threading ;
19
+ using System . Threading . Tasks ;
16
20
using Microsoft . Azure . Commands . Insights . Diagnostics ;
17
21
using Microsoft . Azure . Management . Insights ;
18
22
using Microsoft . Azure . Management . Insights . Models ;
19
23
using Microsoft . Rest . Azure ;
24
+ using Microsoft . WindowsAzure . Commands . ScenarioTest ;
20
25
using Moq ;
21
- using System ;
22
- using System . Collections . Generic ;
23
- using System . Management . Automation ;
24
- using System . Threading . Tasks ;
26
+ using Xunit ;
25
27
26
28
namespace Microsoft . Azure . Commands . Insights . Test . Diagnostics
27
29
{
@@ -31,27 +33,249 @@ public class SetDiagnosticSettingCommandTests
31
33
private readonly Mock < InsightsManagementClient > insightsManagementClientMock ;
32
34
private readonly Mock < IServiceDiagnosticSettingsOperations > insightsDiagnosticsOperationsMock ;
33
35
private Mock < ICommandRuntime > commandRuntimeMock ;
34
- private ServiceDiagnosticSettingsResource request ;
35
36
private const string resourceId = "/subscriptions/123/resourcegroups/rg/providers/rp/resource/myresource" ;
36
37
private const string storageAccountId = "/subscriptions/123/resourcegroups/rg/providers/microsoft.storage/accounts/myaccount" ;
37
- private string calledResourceId ;
38
- ServiceDiagnosticSettingsResource calledPutParameters ;
38
+ private const string serviceBusRuleId = "/subscriptions/123/resourcegroups/rg/providers/microsoft.eventhub/namespaces/ns/authorizationrules/ar" ;
39
+ private const string workspaceId = "/subscriptions/123/resourcegroups/rg/providers/microsoft.operationalinsights/workspaces/wp" ;
40
+
41
+ ServiceDiagnosticSettingsResource ExistingSetting ;
42
+ ServiceDiagnosticSettingsResource calledSettings = null ;
39
43
40
44
public SetDiagnosticSettingCommandTests ( Xunit . Abstractions . ITestOutputHelper output )
41
45
{
42
- //ServiceManagemenet.Common.Models.XunitTracingInterceptor.AddToContext(new ServiceManagemenet.Common.Models.XunitTracingInterceptor(output));
43
- insightsDiagnosticsOperationsMock = new Mock < IServiceDiagnosticSettingsOperations > ( ) ;
44
- insightsManagementClientMock = new Mock < InsightsManagementClient > ( ) ;
45
- commandRuntimeMock = new Mock < ICommandRuntime > ( ) ;
46
- cmdlet = new SetAzureRmDiagnosticSettingCommand ( )
46
+ this . insightsDiagnosticsOperationsMock = new Mock < IServiceDiagnosticSettingsOperations > ( ) ;
47
+ this . insightsManagementClientMock = new Mock < InsightsManagementClient > ( ) ;
48
+ this . commandRuntimeMock = new Mock < ICommandRuntime > ( ) ;
49
+ this . cmdlet = new SetAzureRmDiagnosticSettingCommand ( )
47
50
{
48
51
CommandRuntime = commandRuntimeMock . Object ,
49
52
InsightsManagementClient = insightsManagementClientMock . Object
50
53
} ;
54
+
55
+ this . ExistingSetting = GetDefaultSetting ( ) ;
56
+
57
+ insightsDiagnosticsOperationsMock . Setup ( f => f . GetWithHttpMessagesAsync (
58
+ It . IsAny < string > ( ) ,
59
+ It . IsAny < Dictionary < string , List < string > > > ( ) ,
60
+ It . IsAny < CancellationToken > ( ) ) )
61
+ . Returns ( Task . FromResult < AzureOperationResponse < ServiceDiagnosticSettingsResource > > ( new AzureOperationResponse < ServiceDiagnosticSettingsResource >
62
+ {
63
+ Body = this . ExistingSetting
64
+ } ) ) ;
65
+
66
+ insightsDiagnosticsOperationsMock . Setup ( f => f . CreateOrUpdateWithHttpMessagesAsync (
67
+ It . IsAny < string > ( ) ,
68
+ It . IsAny < ServiceDiagnosticSettingsResource > ( ) ,
69
+ It . IsAny < Dictionary < string , List < string > > > ( ) ,
70
+ It . IsAny < CancellationToken > ( ) ) )
71
+ . Returns ( ( string a , ServiceDiagnosticSettingsResource x , Dictionary < string , List < string > > b , CancellationToken c ) =>
72
+ {
73
+ calledSettings = x ;
74
+ return Task . FromResult ( new AzureOperationResponse < ServiceDiagnosticSettingsResource >
75
+ {
76
+ Body = x
77
+ } ) ;
78
+ } ) ;
79
+
80
+ insightsManagementClientMock . SetupGet ( f => f . ServiceDiagnosticSettings ) . Returns ( this . insightsDiagnosticsOperationsMock . Object ) ;
81
+
82
+ cmdlet . ResourceId = resourceId ;
83
+ }
84
+
85
+ [ Fact ]
86
+ [ Trait ( Category . AcceptanceType , Category . CheckIn ) ]
87
+ public void DisableStorage ( )
88
+ {
89
+ cmdlet . MyInvocation . BoundParameters [ "StorageAccountId" ] = null ;
90
+ cmdlet . ExecuteCmdlet ( ) ;
91
+
92
+ ServiceDiagnosticSettingsResource expectedSettings = GetDefaultSetting ( ) ;
93
+ expectedSettings . StorageAccountId = null ;
94
+
95
+ VerifyCalledOnce ( ) ;
96
+ VerifySettings ( expectedSettings , this . calledSettings ) ;
97
+ }
98
+
99
+ [ Fact ]
100
+ [ Trait ( Category . AcceptanceType , Category . CheckIn ) ]
101
+ public void SetStorage ( )
102
+ {
103
+ string newStorageId = "otherstorage" ;
104
+ cmdlet . StorageAccountId = newStorageId ;
105
+ cmdlet . MyInvocation . BoundParameters [ SetAzureRmDiagnosticSettingCommand . StorageAccountIdParamName ] = newStorageId ;
106
+ cmdlet . ExecuteCmdlet ( ) ;
107
+
108
+ ServiceDiagnosticSettingsResource expectedSettings = GetDefaultSetting ( ) ;
109
+ expectedSettings . StorageAccountId = newStorageId ;
110
+
111
+ VerifyCalledOnce ( ) ;
112
+ VerifySettings ( expectedSettings , this . calledSettings ) ;
113
+ }
114
+
115
+ [ Fact ]
116
+ [ Trait ( Category . AcceptanceType , Category . CheckIn ) ]
117
+ public void SetServiceBus ( )
118
+ {
119
+ string newServiceBusId = "otherservicebus" ;
120
+ cmdlet . ServiceBusRuleId = newServiceBusId ;
121
+ cmdlet . MyInvocation . BoundParameters [ SetAzureRmDiagnosticSettingCommand . ServiceBusRuleIdParamName ] = newServiceBusId ;
122
+ cmdlet . ExecuteCmdlet ( ) ;
123
+
124
+ ServiceDiagnosticSettingsResource expectedSettings = GetDefaultSetting ( ) ;
125
+ expectedSettings . ServiceBusRuleId = newServiceBusId ;
126
+
127
+ VerifyCalledOnce ( ) ;
128
+ VerifySettings ( expectedSettings , this . calledSettings ) ;
129
+ }
130
+
131
+ [ Fact ]
132
+ [ Trait ( Category . AcceptanceType , Category . CheckIn ) ]
133
+ public void SetWorkspace ( )
134
+ {
135
+ string newWorkspaceId = "otherworkspace" ;
136
+ cmdlet . WorkspaceId = newWorkspaceId ;
137
+ cmdlet . MyInvocation . BoundParameters [ SetAzureRmDiagnosticSettingCommand . WorkspacetIdParamName ] = newWorkspaceId ;
138
+ cmdlet . ExecuteCmdlet ( ) ;
139
+
140
+ ServiceDiagnosticSettingsResource expectedSettings = GetDefaultSetting ( ) ;
141
+ expectedSettings . WorkspaceId = newWorkspaceId ;
142
+
143
+ VerifyCalledOnce ( ) ;
144
+ VerifySettings ( expectedSettings , this . calledSettings ) ;
145
+ }
146
+
147
+ [ Fact ]
148
+ [ Trait ( Category . AcceptanceType , Category . CheckIn ) ]
149
+ public void SetSomeCategories ( )
150
+ {
151
+ cmdlet . Categories = new List < string > { "TestCategory1" } ;
152
+ cmdlet . Enabled = false ;
153
+ cmdlet . MyInvocation . BoundParameters [ SetAzureRmDiagnosticSettingCommand . EnabledParamName ] = false ;
154
+ cmdlet . ExecuteCmdlet ( ) ;
155
+
156
+ ServiceDiagnosticSettingsResource expectedSettings = GetDefaultSetting ( ) ;
157
+ expectedSettings . Logs [ 0 ] . Enabled = false ;
158
+
159
+ VerifyCalledOnce ( ) ;
160
+ VerifySettings ( expectedSettings , this . calledSettings ) ;
161
+ }
162
+
163
+ [ Fact ]
164
+ [ Trait ( Category . AcceptanceType , Category . CheckIn ) ]
165
+ public void SetSomeTimeGrains ( )
166
+ {
167
+ cmdlet . Timegrains = new List < string > { "PT1H" } ;
168
+ cmdlet . Enabled = false ;
169
+ cmdlet . MyInvocation . BoundParameters [ SetAzureRmDiagnosticSettingCommand . EnabledParamName ] = false ;
170
+ cmdlet . ExecuteCmdlet ( ) ;
51
171
52
- request = new ServiceDiagnosticSettingsResource
172
+ ServiceDiagnosticSettingsResource expectedSettings = GetDefaultSetting ( ) ;
173
+ expectedSettings . Metrics [ 1 ] . Enabled = false ;
174
+
175
+ VerifyCalledOnce ( ) ;
176
+ VerifySettings ( expectedSettings , this . calledSettings ) ;
177
+ }
178
+
179
+ [ Fact ]
180
+ [ Trait ( Category . AcceptanceType , Category . CheckIn ) ]
181
+ public void DisableEventHub ( )
182
+ {
183
+ cmdlet . MyInvocation . BoundParameters [ SetAzureRmDiagnosticSettingCommand . ServiceBusRuleIdParamName ] = null ;
184
+ cmdlet . ExecuteCmdlet ( ) ;
185
+
186
+ ServiceDiagnosticSettingsResource expectedSettings = GetDefaultSetting ( ) ;
187
+ expectedSettings . ServiceBusRuleId = null ;
188
+
189
+ VerifyCalledOnce ( ) ;
190
+ VerifySettings ( expectedSettings , this . calledSettings ) ;
191
+ }
192
+
193
+ [ Fact ]
194
+ [ Trait ( Category . AcceptanceType , Category . CheckIn ) ]
195
+ public void DisableWorkspace ( )
196
+ {
197
+ cmdlet . MyInvocation . BoundParameters [ "WorkspaceId" ] = null ;
198
+ cmdlet . ExecuteCmdlet ( ) ;
199
+
200
+ ServiceDiagnosticSettingsResource expectedSettings = GetDefaultSetting ( ) ;
201
+ expectedSettings . WorkspaceId = null ;
202
+
203
+ VerifyCalledOnce ( ) ;
204
+ VerifySettings ( expectedSettings , this . calledSettings ) ;
205
+ }
206
+
207
+ private void VerifyCalledOnce ( )
208
+ {
209
+ insightsDiagnosticsOperationsMock . Verify ( x => x . CreateOrUpdateWithHttpMessagesAsync (
210
+ resourceId ,
211
+ It . IsAny < ServiceDiagnosticSettingsResource > ( ) ,
212
+ It . IsAny < Dictionary < string , List < string > > > ( ) ,
213
+ It . IsAny < CancellationToken > ( ) ) ,
214
+ Times . Once ) ;
215
+ }
216
+
217
+ private void VerifySettings (
218
+ ServiceDiagnosticSettingsResource expectedSettings ,
219
+ ServiceDiagnosticSettingsResource actualSettings )
220
+ {
221
+ Assert . Equal ( expectedSettings . StorageAccountId , actualSettings . StorageAccountId ) ;
222
+ Assert . Equal ( expectedSettings . ServiceBusRuleId , actualSettings . ServiceBusRuleId ) ;
223
+ Assert . Equal ( expectedSettings . WorkspaceId , actualSettings . WorkspaceId ) ;
224
+ if ( expectedSettings . Logs == null )
225
+ {
226
+ Assert . Null ( actualSettings . Logs ) ;
227
+ }
228
+ else
229
+ {
230
+ Assert . Equal ( expectedSettings . Logs . Count , actualSettings . Logs . Count ) ;
231
+ for ( int i = 0 ; i < expectedSettings . Logs . Count ; i ++ )
232
+ {
233
+ var expected = expectedSettings . Logs [ i ] ;
234
+ var actual = actualSettings . Logs [ i ] ;
235
+ Assert . Equal ( expected . Category , actual . Category ) ;
236
+ Assert . Equal ( expected . Enabled , actual . Enabled ) ;
237
+ VerifyRetentionPolicy ( expected . RetentionPolicy , actual . RetentionPolicy ) ;
238
+ }
239
+ }
240
+
241
+ if ( expectedSettings . Metrics == null )
242
+ {
243
+ Assert . Null ( actualSettings . Metrics ) ;
244
+ }
245
+ else
246
+ {
247
+ Assert . Equal ( expectedSettings . Metrics . Count , actualSettings . Metrics . Count ) ;
248
+ for ( int i = 0 ; i < expectedSettings . Metrics . Count ; i ++ )
249
+ {
250
+ var expected = expectedSettings . Metrics [ i ] ;
251
+ var actual = actualSettings . Metrics [ i ] ;
252
+ Assert . Equal ( expected . TimeGrain , actual . TimeGrain ) ;
253
+ Assert . Equal ( expected . Enabled , actual . Enabled ) ;
254
+ VerifyRetentionPolicy ( expected . RetentionPolicy , actual . RetentionPolicy ) ;
255
+ }
256
+ }
257
+ }
258
+
259
+ private static void VerifyRetentionPolicy ( RetentionPolicy expected , RetentionPolicy actual )
260
+ {
261
+ if ( expected == null )
262
+ {
263
+ Assert . Null ( actual ) ;
264
+ }
265
+ else
266
+ {
267
+ Assert . Equal ( expected . Days , actual . Days ) ;
268
+ Assert . Equal ( expected . Enabled , actual . Enabled ) ;
269
+ }
270
+ }
271
+
272
+ private ServiceDiagnosticSettingsResource GetDefaultSetting ( )
273
+ {
274
+ return new ServiceDiagnosticSettingsResource
53
275
{
54
276
StorageAccountId = storageAccountId ,
277
+ ServiceBusRuleId = serviceBusRuleId ,
278
+ WorkspaceId = workspaceId ,
55
279
Logs = new List < LogSettings >
56
280
{
57
281
new LogSettings
@@ -79,28 +303,6 @@ public SetDiagnosticSettingCommandTests(Xunit.Abstractions.ITestOutputHelper out
79
303
}
80
304
}
81
305
} ;
82
-
83
- insightsDiagnosticsOperationsMock . Setup ( f => f . GetAsync ( It . IsAny < string > ( ) , It . IsAny < CancellationToken > ( ) ) )
84
- . Returns ( Task . FromResult < ServiceDiagnosticSettingsResource > ( request ) )
85
- . Callback ( ( string resourceId ) =>
86
- {
87
- this . calledResourceId = resourceId ;
88
- } ) ;
89
-
90
- Rest . Azure . AzureOperationResponse < ServiceDiagnosticSettingsResource > response = new AzureOperationResponse < ServiceDiagnosticSettingsResource >
91
- {
92
- Body = request
93
- } ;
94
-
95
- insightsDiagnosticsOperationsMock . Setup ( f => f . CreateOrUpdateWithHttpMessagesAsync ( It . IsAny < string > ( ) , It . IsAny < ServiceDiagnosticSettingsResource > ( ) , It . IsAny < Dictionary < string , List < string > > > ( ) , It . IsAny < CancellationToken > ( ) ) )
96
- . Returns ( Task . FromResult < Rest . Azure . AzureOperationResponse < ServiceDiagnosticSettingsResource > > ( response ) )
97
- . Callback ( ( string resourceId , ServiceDiagnosticSettingsResource putParameters , Dictionary < string , List < string > > headers ) =>
98
- {
99
- this . calledResourceId = resourceId ;
100
- this . calledPutParameters = putParameters ;
101
- } ) ;
102
-
103
- insightsManagementClientMock . SetupGet ( f => f . ServiceDiagnosticSettings ) . Returns ( this . insightsDiagnosticsOperationsMock . Object ) ;
104
306
}
105
307
}
106
308
}
0 commit comments