Skip to content

Commit db85975

Browse files
author
Hao Chen
committed
Fixed 85% of test failures in Resources tests.
1 parent 2c9be6f commit db85975

21 files changed

+363
-195
lines changed

src/CLU/Commands.Common/AzurePSCmdlet.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -349,7 +349,11 @@ protected bool IsVerbose()
349349
protected new void WriteObject(object sendToPipeline, bool enumerateCollection)
350350
{
351351
FlushDebugMessages();
352+
#if DEBUG
353+
CommandRuntime.WriteObject(sendToPipeline, enumerateCollection);
354+
#else
352355
base.WriteObject(sendToPipeline, enumerateCollection);
356+
#endif
353357
}
354358

355359
protected new void WriteVerbose(string text)
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
using Microsoft.Azure.Management.Resources.Models;
2+
using System;
3+
using System.Collections.Generic;
4+
using System.Linq;
5+
using System.Reflection;
6+
using System.Threading.Tasks;
7+
8+
namespace Microsoft.Azure.Commands.ScenarioTest
9+
{
10+
public static class PageExtensions
11+
{
12+
public static void SetItemValue<T> (this Page<T> pagableObj, List<T> collection) {
13+
var property = typeof(Page<T>).GetProperty("Items", BindingFlags.Instance | BindingFlags.NonPublic);
14+
property.SetValue(pagableObj, collection);
15+
}
16+
}
17+
}

src/CLU/Microsoft.Azure.Commands.Resources.Test/Features/GetAzureProviderFeatureCmdletTests.cs

Lines changed: 38 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ namespace Microsoft.Azure.Commands.Resources.Test
2323
using Rest.Azure;
2424
using ScenarioTest;
2525
using System;
26+
using System.Collections.Generic;
2627
using System.Linq;
2728
using System.Management.Automation;
2829
using System.Net;
@@ -72,7 +73,8 @@ public GetAzureProviderFeatureCmdletTests()
7273
FeaturesManagementClient = featureClient.Object
7374
}
7475
};
75-
System.Reflection.TypeExtensions.GetProperty(cmdlet.GetType(), "CommandRuntime").SetValue(cmdlet, commandRuntimeMock.Object);
76+
//System.Reflection.TypeExtensions.GetProperty(cmdlet.GetType(), "CommandRuntime").SetValue(cmdlet, commandRuntimeMock.Object);
77+
PSCmdletExtensions.SetCommandRuntimeMock(cmdlet, commandRuntimeMock.Object);
7678
}
7779

7880
/// <summary>
@@ -122,13 +124,18 @@ public void GetProviderFeatureTests()
122124
Type = "Microsoft.Features/feature"
123125
};
124126

125-
var listResult = new Page<FeatureResult>();
126-
var pagableResult = new[] { provider1RegisteredFeature, provider1UnregisteredFeature, provider2UnregisteredFeature };
127-
System.Reflection.TypeExtensions.GetProperty(listResult.GetType(), "Items").SetValue(listResult, pagableResult);
127+
var pagableResult = new Page<FeatureResult>();
128+
//var listResult = new[] { provider1RegisteredFeature, provider1UnregisteredFeature, provider2UnregisteredFeature };
129+
var listResult = new List<FeatureResult>() { provider1RegisteredFeature, provider1UnregisteredFeature, provider2UnregisteredFeature };
130+
pagableResult.SetItemValue<FeatureResult>(listResult);
131+
var result = new AzureOperationResponse<IPage<FeatureResult>>()
132+
{
133+
Body = pagableResult
134+
};
128135

129136
this.featureOperationsMock
130-
.Setup(f => f.ListAllAsync(It.IsAny<CancellationToken>()))
131-
.Returns(() => Task.FromResult((IPage<FeatureResult>)listResult));
137+
.Setup(f => f.ListAllWithHttpMessagesAsync(null, It.IsAny<CancellationToken>()))
138+
.Returns(() => Task.FromResult(result));
132139

133140
// 1. List only registered features of providers
134141
this.commandRuntimeMock
@@ -172,14 +179,16 @@ public void GetProviderFeatureTests()
172179
string providerOfChoice = Provider1Namespace;
173180
this.cmdlet.ListAvailable = false;
174181
this.cmdlet.ProviderNamespace = providerOfChoice;
175-
System.Reflection.TypeExtensions
176-
.GetProperty(listResult.GetType(), "Items")
177-
.SetValue(listResult, new[] { provider1RegisteredFeature, provider1UnregisteredFeature });
182+
pagableResult.SetItemValue<FeatureResult>(new List<FeatureResult>() { provider1RegisteredFeature, provider1UnregisteredFeature });
178183

179184
this.featureOperationsMock
180-
.Setup(f => f.ListAsync(It.IsAny<string>(), It.IsAny<CancellationToken>()))
181-
.Callback((string providerName, CancellationToken ignored) => Assert.Equal(providerOfChoice, providerName, StringComparer.OrdinalIgnoreCase))
182-
.Returns(() => Task.FromResult((IPage<FeatureResult>)listResult ));
185+
.Setup(f => f.ListWithHttpMessagesAsync(It.IsAny<string>(), null, It.IsAny<CancellationToken>()))
186+
.Callback((string providerName, Dictionary<string, List<string>> customHeaders, CancellationToken ignored) => Assert.Equal(providerOfChoice, providerName, StringComparer.OrdinalIgnoreCase))
187+
.Returns(() => Task.FromResult(
188+
new AzureOperationResponse<IPage<FeatureResult>>()
189+
{
190+
Body = pagableResult
191+
}));
183192

184193
this.commandRuntimeMock
185194
.Setup(m => m.WriteObject(It.IsAny<object>()))
@@ -206,9 +215,7 @@ public void GetProviderFeatureTests()
206215
providerOfChoice = Provider2Namespace;
207216
this.cmdlet.ListAvailable = false;
208217
this.cmdlet.ProviderNamespace = providerOfChoice;
209-
System.Reflection.TypeExtensions
210-
.GetProperty(listResult.GetType(), "Items")
211-
.SetValue(listResult, new[] { provider2UnregisteredFeature });
218+
pagableResult.SetItemValue<FeatureResult>(new List<FeatureResult>() { provider2UnregisteredFeature });
212219

213220
this.commandRuntimeMock
214221
.Setup(m => m.WriteObject(It.IsAny<object>()))
@@ -228,9 +235,7 @@ public void GetProviderFeatureTests()
228235
providerOfChoice = Provider1Namespace;
229236
this.cmdlet.ProviderNamespace = providerOfChoice;
230237
this.cmdlet.ListAvailable = true;
231-
System.Reflection.TypeExtensions
232-
.GetProperty(listResult.GetType(), "Items")
233-
.SetValue(listResult, new[] { provider1RegisteredFeature, provider1UnregisteredFeature });
238+
pagableResult.SetItemValue<FeatureResult>(new List<FeatureResult>() { provider1RegisteredFeature, provider1UnregisteredFeature });
234239

235240
this.commandRuntimeMock
236241
.Setup(m => m.WriteObject(It.IsAny<object>()))
@@ -256,13 +261,16 @@ public void GetProviderFeatureTests()
256261
this.cmdlet.ListAvailable = false;
257262

258263
this.featureOperationsMock
259-
.Setup(f => f.GetAsync(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<CancellationToken>()))
260-
.Callback((string providerName, string featureName, CancellationToken ignored) =>
264+
.Setup(f => f.GetWithHttpMessagesAsync(It.IsAny<string>(), It.IsAny<string>(), null, It.IsAny<CancellationToken>()))
265+
.Callback((string providerName, string featureName, Dictionary<string, List<string>> customHeaders, CancellationToken ignored) =>
261266
{
262267
Assert.Equal(Provider2Namespace, providerName, StringComparer.OrdinalIgnoreCase);
263268
Assert.Equal(Feature1Name, featureName, StringComparer.OrdinalIgnoreCase);
264269
})
265-
.Returns(() => Task.FromResult(provider2UnregisteredFeature));
270+
.Returns(() => Task.FromResult( new AzureOperationResponse<FeatureResult>()
271+
{
272+
Body = provider2UnregisteredFeature
273+
}));
266274

267275
this.commandRuntimeMock
268276
.Setup(m => m.WriteObject(It.IsAny<object>()))
@@ -298,11 +306,11 @@ private void ResetCalls()
298306
/// </summary>
299307
private void VerifyGetCallPatternAndReset()
300308
{
301-
this.featureOperationsMock.Verify(f => f.GetAsync(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<CancellationToken>()), Times.Once());
302-
this.featureOperationsMock.Verify(f => f.ListAsync(It.IsAny<string>(), It.IsAny<CancellationToken>()), Times.Never);
303-
this.featureOperationsMock.Verify(f => f.ListAllAsync(It.IsAny<CancellationToken>()), Times.Never);
304-
this.featureOperationsMock.Verify(f => f.ListNextAsync(It.IsAny<string>(), It.IsAny<CancellationToken>()), Times.Never);
305-
this.featureOperationsMock.Verify(f => f.ListAllNextAsync(It.IsAny<string>(), It.IsAny<CancellationToken>()), Times.Never);
309+
this.featureOperationsMock.Verify(f => f.GetWithHttpMessagesAsync(It.IsAny<string>(), It.IsAny<string>(), null, It.IsAny<CancellationToken>()), Times.Once());
310+
this.featureOperationsMock.Verify(f => f.ListWithHttpMessagesAsync(It.IsAny<string>(), null, It.IsAny<CancellationToken>()), Times.Never);
311+
this.featureOperationsMock.Verify(f => f.ListAllWithHttpMessagesAsync(null, It.IsAny<CancellationToken>()), Times.Never);
312+
this.featureOperationsMock.Verify(f => f.ListNextWithHttpMessagesAsync(It.IsAny<string>(), null, It.IsAny<CancellationToken>()), Times.Never);
313+
this.featureOperationsMock.Verify(f => f.ListAllNextWithHttpMessagesAsync(It.IsAny<string>(), null, It.IsAny<CancellationToken>()), Times.Never);
306314
this.commandRuntimeMock.Verify(f => f.WriteObject(It.IsAny<object>(), It.IsAny<bool>()), Times.Once());
307315

308316
this.ResetCalls();
@@ -313,8 +321,8 @@ private void VerifyGetCallPatternAndReset()
313321
/// </summary>
314322
private void VerifyListAllCallPatternAndReset()
315323
{
316-
this.featureOperationsMock.Verify(f => f.ListAllAsync(It.IsAny<CancellationToken>()), Times.Once());
317-
this.featureOperationsMock.Verify(f => f.ListAllNextAsync(It.IsAny<string>(), It.IsAny<CancellationToken>()), Times.Never);
324+
this.featureOperationsMock.Verify(f => f.ListAllWithHttpMessagesAsync(null, It.IsAny<CancellationToken>()), Times.Once());
325+
this.featureOperationsMock.Verify(f => f.ListAllNextWithHttpMessagesAsync(It.IsAny<string>(), null, It.IsAny<CancellationToken>()), Times.Never);
318326
this.commandRuntimeMock.Verify(f => f.WriteObject(It.IsAny<object>(), It.IsAny<bool>()), Times.Once());
319327

320328
this.ResetCalls();
@@ -325,8 +333,8 @@ private void VerifyListAllCallPatternAndReset()
325333
/// </summary>
326334
private void VerifyListProviderFeaturesCallPatternAndReset()
327335
{
328-
this.featureOperationsMock.Verify(f => f.ListAsync(It.IsAny<string>(), It.IsAny<CancellationToken>()), Times.Once());
329-
this.featureOperationsMock.Verify(f => f.ListNextAsync(It.IsAny<string>(), It.IsAny<CancellationToken>()), Times.Never);
336+
this.featureOperationsMock.Verify(f => f.ListWithHttpMessagesAsync(It.IsAny<string>(), null, It.IsAny<CancellationToken>()), Times.Once());
337+
this.featureOperationsMock.Verify(f => f.ListNextWithHttpMessagesAsync(It.IsAny<string>(), null, It.IsAny<CancellationToken>()), Times.Never);
330338
this.commandRuntimeMock.Verify(f => f.WriteObject(It.IsAny<object>(), It.IsAny<bool>()), Times.Once());
331339

332340
this.ResetCalls();

src/CLU/Microsoft.Azure.Commands.Resources.Test/Features/RegisterProviderFeatureCmdletTests.cs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,10 @@ namespace Microsoft.Azure.Commands.Resources.Test
2020
using Microsoft.Azure.Management.Resources;
2121
using Microsoft.Azure.Management.Resources.Models;
2222
using Moq;
23+
using Rest.Azure;
2324
using ScenarioTest;
2425
using System;
26+
using System.Collections.Generic;
2527
using System.Management.Automation;
2628
using System.Net;
2729
using System.Threading;
@@ -74,7 +76,7 @@ public RegisterAzureProviderFeatureCmdletTests()
7476
FeaturesManagementClient = featureClient.Object
7577
}
7678
};
77-
System.Reflection.TypeExtensions.GetProperty(cmdlet.GetType(), "CommandRuntime").SetValue(cmdlet, commandRuntimeMock.Object);
79+
PSCmdletExtensions.SetCommandRuntimeMock(cmdlet, commandRuntimeMock.Object);
7880
}
7981

8082
/// <summary>
@@ -99,13 +101,15 @@ public void RegisterResourceProviderFeatureTests()
99101
};
100102

101103
this.featureOperationsMock
102-
.Setup(client => client.RegisterAsync(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<CancellationToken>()))
103-
.Callback((string providerName, string featureName, CancellationToken ignored) =>
104+
.Setup(client => client.RegisterWithHttpMessagesAsync(It.IsAny<string>(), It.IsAny<string>(), null, It.IsAny<CancellationToken>()))
105+
.Callback((string providerName, string featureName, Dictionary<string, List<string>> customHeaders, CancellationToken ignored) =>
104106
{
105107
Assert.Equal(ProviderName, providerName, StringComparer.OrdinalIgnoreCase);
106108
Assert.Equal(FeatureName, featureName, StringComparer.OrdinalIgnoreCase);
107109
})
108-
.Returns(() => Task.FromResult(registeredFeature));
110+
.Returns(() => Task.FromResult(new AzureOperationResponse<FeatureResult>() {
111+
Body = registeredFeature
112+
}));
109113

110114
this.cmdlet.Force = true;
111115
this.cmdlet.ProviderNamespace = ProviderName;
@@ -131,7 +135,8 @@ public void RegisterResourceProviderFeatureTests()
131135
/// </summary>
132136
private void VerifyCallPatternAndReset(bool succeeded)
133137
{
134-
this.featureOperationsMock.Verify(f => f.RegisterAsync(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<CancellationToken>()), Times.Once());
138+
this.featureOperationsMock.Verify(f => f.RegisterWithHttpMessagesAsync(It.IsAny<string>(),
139+
It.IsAny<string>(), null, It.IsAny<CancellationToken>()), Times.Once());
135140
this.commandRuntimeMock.Verify(f => f.WriteObject(It.IsAny<object>()), succeeded ? Times.Once() : Times.Never());
136141

137142
this.featureOperationsMock.ResetCalls();

src/CLU/Microsoft.Azure.Commands.Resources.Test/Models.ResourceGroups/GalleryTemplatesClientTests.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ public class GalleryTemplatesClientTests : RMTestBase
4242
public GalleryTemplatesClientTests()
4343
{
4444
deploymentCmdlet = new NewAzureResourceGroupDeploymentCommand();
45+
deploymentCmdlet.DataStore = new Common.Authentication.Models.DiskDataStore();
4546
}
4647

4748
[Fact(Skip = "PowerShell runtime only")]
@@ -291,7 +292,7 @@ public void GetsDynamicParametersForTemplateFile()
291292
Assert.Equal(typeof(int), result["int"].ParameterType);
292293

293294
Assert.Equal("securestring", result["securestring"].Name);
294-
Assert.Equal(typeof(string), result["securestring"].ParameterType);
295+
Assert.Equal(typeof(object), result["securestring"].ParameterType);
295296

296297
Assert.Equal("bool", result["bool"].Name);
297298
Assert.Equal(typeof(bool), result["bool"].ParameterType);

0 commit comments

Comments
 (0)