Skip to content

Commit 887cd6f

Browse files
authored
Merge pull request #2484 from markcowl/contextvalidateset
Fix for #2387, #2388 fix subscription and tenant parameter attributes
2 parents 9bdf159 + d00ce3a commit 887cd6f

File tree

6 files changed

+149
-10
lines changed

6 files changed

+149
-10
lines changed

src/ResourceManager/Common/Commands.ScenarioTests.ResourceManager.Common/Commands.ScenarioTests.ResourceManager.Common.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,7 @@
163163
<Compile Include="PowerShellExtensions.cs" />
164164
<Compile Include="Properties\AssemblyInfo.cs" />
165165
<Compile Include="RMTestBase.cs" />
166+
<Compile Include="TestExecutionHelpers.cs" />
166167
<Compile Include="XunitTracingInterceptor.cs" />
167168
</ItemGroup>
168169
<ItemGroup>
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
// ----------------------------------------------------------------------------------
2+
//
3+
// Copyright Microsoft Corporation
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
// Unless required by applicable law or agreed to in writing, software
9+
// distributed under the License is distributed on an "AS IS" BASIS,
10+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
// See the License for the specific language governing permissions and
12+
// limitations under the License.
13+
// ----------------------------------------------------------------------------------
14+
15+
using System;
16+
17+
namespace Microsoft.Azure.Commands.ScenarioTest
18+
{
19+
public static class TestExecutionHelpers
20+
{
21+
/// <summary>
22+
/// Retry an action until it succeeds - use for flaky tests
23+
/// </summary>
24+
/// <param name="testAction">Action tor etry</param>
25+
/// <param name="maxTries">The maximum number of times to try the action</param>
26+
public static void RetryAction(Action testAction, int maxTries = 3)
27+
{
28+
var tries = 0;
29+
var succeeded = false;
30+
do
31+
{
32+
try
33+
{
34+
testAction();
35+
succeeded = true;
36+
}
37+
catch (Exception)
38+
{
39+
if (++tries >= maxTries)
40+
{
41+
throw;
42+
}
43+
}
44+
} while (!succeeded);
45+
}
46+
}
47+
}

src/ResourceManager/Profile/Commands.Profile.Test/ContextCmdletTests.cs

Lines changed: 64 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,11 @@
1212
// limitations under the License.
1313
// ----------------------------------------------------------------------------------
1414

15+
using System;
16+
using System.CodeDom;
17+
using System.Collections.Generic;
18+
using System.Linq;
19+
using System.Linq.Expressions;
1520
using Microsoft.Azure.Commands.Common.Authentication;
1621
using Microsoft.Azure.Commands.Common.Authentication.Models;
1722
using Microsoft.Azure.Commands.Profile;
@@ -25,14 +30,18 @@
2530
using System.Management.Automation;
2631
using Xunit;
2732
using Xunit.Abstractions;
33+
using Xunit.Sdk;
2834

2935
namespace Microsoft.Azure.Commands.ResourceManager.Profile.Test
3036
{
3137
public class ContextCmdletTests : RMTestBase
3238
{
3339
private MemoryDataStore dataStore;
3440
private MockCommandRuntime commandRuntimeMock;
35-
41+
const string guid1 = "a0cc8bd7-2c6a-47e9-a4c4-3f6ed136e240";
42+
const string guid2 = "eab635c0-a35a-4f70-9e46-e5351c7b5c8b";
43+
const string guid3 = "52f66548-2550-417b-941e-9d6e04f3ac8d";
44+
const string guid4 = "40e67ee2-1a1a-4517-9253-ab6f93c5710f";
3645
public ContextCmdletTests(ITestOutputHelper output)
3746
{
3847
XunitTracingInterceptor.AddToContext(new XunitTracingInterceptor(output));
@@ -78,8 +87,15 @@ public void SelectAzureContextWithNoSubscriptionAndTenant()
7887
var existingTenants = account.GetProperty(AzureAccount.Property.Tenants);
7988
var allowedTenants = existingTenants == null ? tenantToSet : existingTenants + "," + tenantToSet;
8089
account.SetProperty(AzureAccount.Property.Tenants, allowedTenants);
90+
account.SetProperty(AzureAccount.Property.Subscriptions, new string[0]);
8191

82-
((RuntimeDefinedParameterDictionary)cmdlt.GetDynamicParameters())["TenantId"].Value = tenantToSet;
92+
var paramDictionary =
93+
((RuntimeDefinedParameterDictionary)cmdlt.GetDynamicParameters());
94+
var tenantParam = paramDictionary["TenantId"];
95+
Assert.True(tenantParam.Attributes.Any(a => a is ValidateSetAttribute
96+
&& ((ValidateSetAttribute)a).ValidValues.Any(v => string.Equals(v, tenantToSet, StringComparison.OrdinalIgnoreCase))));
97+
Assert.False(paramDictionary["SubscriptionId"].Attributes.Any(a => a is ValidateSetAttribute));
98+
tenantParam.Value = tenantToSet;
8399

84100
// Act
85101
cmdlt.InvokeBeginProcessing();
@@ -94,6 +110,50 @@ public void SelectAzureContextWithNoSubscriptionAndTenant()
94110
Assert.NotEqual(tenantToSet, context.Tenant.TenantId);
95111
}
96112

113+
[Theory]
114+
[InlineData(null, null)]
115+
[InlineData(new string[0], new string[0])]
116+
[InlineData(new string[] { guid1}, new string[] {guid2})]
117+
[InlineData(new string[] { guid1, guid2 }, new string[] { guid3, guid4 })]
118+
[Trait(Category.AcceptanceType, Category.CheckIn)]
119+
public void SetsDynamicParametersForContext(string[] subscriptions, string[] tenants)
120+
{
121+
var cmdlt = new SetAzureRMContextCommand();
122+
123+
// Setup
124+
cmdlt.CommandRuntime = commandRuntimeMock;
125+
126+
// Make sure that the tenant ID we are attempting to set is
127+
// valid for the account
128+
var account = AzureRmProfileProvider.Instance.Profile.Context.Account;
129+
account.SetProperty(AzureAccount.Property.Tenants, tenants);
130+
account.SetProperty(AzureAccount.Property.Subscriptions, subscriptions);
131+
132+
var paramDictionary =
133+
((RuntimeDefinedParameterDictionary)cmdlt.GetDynamicParameters());
134+
var subscriptionParams = paramDictionary["SubscriptionId"];
135+
VerifyValidationAttribute(subscriptionParams, subscriptions);
136+
var tenantParams = paramDictionary["TenantId"];
137+
VerifyValidationAttribute(tenantParams, tenants);
138+
}
139+
140+
private void VerifyValidationAttribute(RuntimeDefinedParameter parameter, string[] expectedValues)
141+
{
142+
if (expectedValues != null && expectedValues.Length > 0)
143+
{
144+
var validateAttribute = parameter.Attributes.First(a => a is ValidateSetAttribute) as ValidateSetAttribute;
145+
Assert.NotNull(validateAttribute);
146+
foreach (var expectedValue in expectedValues)
147+
{
148+
Assert.Contains(expectedValue, validateAttribute.ValidValues, StringComparer.OrdinalIgnoreCase);
149+
}
150+
}
151+
else
152+
{
153+
Assert.False(parameter.Attributes.Any(a => a is ValidateSetAttribute));
154+
}
155+
}
156+
97157
[Fact]
98158
[Trait(Category.AcceptanceType, Category.CheckIn)]
99159
public void SelectAzureContextWithNoSubscriptionAndNoTenant()
@@ -113,5 +173,7 @@ public void SelectAzureContextWithNoSubscriptionAndNoTenant()
113173
var context = (PSAzureContext)commandRuntimeMock.OutputPipeline[0];
114174
Assert.NotNull(context);
115175
}
176+
177+
116178
}
117179
}

src/ResourceManager/Profile/Commands.Profile/Context/SetAzureRMContext.cs

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,6 @@ private RuntimeDefinedParameterDictionary CreateDynamicParameterDictionary()
119119
HelpMessage = "Subscription",
120120
ValueFromPipelineByPropertyName = true
121121
},
122-
new ValidateSetAttribute(AzureRmProfileProvider.Instance.Profile.Context.Account.GetPropertyAsArray(AzureAccount.Property.Subscriptions)),
123122
};
124123

125124
var tenantIdAttributes = new Collection<Attribute>
@@ -139,9 +138,34 @@ private RuntimeDefinedParameterDictionary CreateDynamicParameterDictionary()
139138
ValueFromPipelineByPropertyName = true
140139
},
141140
new AliasAttribute("Domain"),
142-
new ValidateSetAttribute(AzureRmProfileProvider.Instance.Profile.Context.Account.GetPropertyAsArray(AzureAccount.Property.Tenants)),
143141
};
144142

143+
if (AzureRmProfileProvider.Instance != null
144+
&& AzureRmProfileProvider.Instance.Profile != null
145+
&& AzureRmProfileProvider.Instance.Profile.Context != null
146+
&& AzureRmProfileProvider.Instance.Profile.Context.Account != null)
147+
{
148+
var account = AzureRmProfileProvider.Instance.Profile.Context.Account;
149+
if (account.IsPropertySet(AzureAccount.Property.Subscriptions))
150+
{
151+
var subscriptions = account.GetPropertyAsArray(AzureAccount.Property.Subscriptions);
152+
if (subscriptions != null && subscriptions.Length > 0)
153+
{
154+
subscriptionIdAttributes.Add(
155+
new ValidateSetAttribute(subscriptions));
156+
}
157+
}
158+
if (account.IsPropertySet(AzureAccount.Property.Tenants))
159+
{
160+
var tenants = account.GetPropertyAsArray(AzureAccount.Property.Tenants);
161+
if (tenants != null && tenants.Length > 0)
162+
{
163+
tenantIdAttributes.Add(
164+
new ValidateSetAttribute(tenants));
165+
}
166+
}
167+
}
168+
145169
_tenantId = new RuntimeDefinedParameter("TenantId", typeof(string), tenantIdAttributes);
146170
_subscriptionId = new RuntimeDefinedParameter("SubscriptionId", typeof(string), subscriptionIdAttributes);
147171

src/ResourceManager/Resources/Commands.Resources.Test/Models.ResourceGroups/GalleryTemplatesClientTests.cs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
using System.Linq;
2727
using System.Management.Automation;
2828
using System.Security;
29+
using Microsoft.Azure.Commands.ScenarioTest;
2930
using Newtonsoft.Json.Linq;
3031
using Xunit;
3132
using Xunit.Abstractions;
@@ -498,10 +499,14 @@ public void HandlesInvalidTemplateFiles()
498499
[Trait(Category.AcceptanceType, Category.CheckIn)]
499500
public void ParseTemplateParameterFileContents_DeserializeWithCorrectType()
500501
{
501-
Dictionary<string, TemplateFileParameterV1> result =
502-
TemplateUtility.ParseTemplateParameterFileContents(@"Resources\WebSite.param.dev.json");
503-
Assert.Equal(true, result["isWorker"].Value);
504-
Assert.Equal((System.Int64)1, result["numberOfWorker"].Value);
502+
// Add up to 3 retries for flaky test
503+
TestExecutionHelpers.RetryAction(() =>
504+
{
505+
Dictionary<string, TemplateFileParameterV1> result =
506+
TemplateUtility.ParseTemplateParameterFileContents(@"Resources\WebSite.param.dev.json");
507+
Assert.Equal(true, result["isWorker"].Value);
508+
Assert.Equal((System.Int64) 1, result["numberOfWorker"].Value);
509+
});
505510
}
506511
}
507512
}

src/ResourceManager/Resources/Resources.sln

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Microsoft Visual Studio Solution File, Format Version 12.00
2-
# Visual Studio 2013
3-
VisualStudioVersion = 12.0.31101.0
2+
# Visual Studio 14
3+
VisualStudioVersion = 14.0.25123.0
44
MinimumVisualStudioVersion = 10.0.40219.1
55
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Tests", "Tests", "{95C16AED-FD57-42A0-86C3-2CF4300A4817}"
66
EndProject

0 commit comments

Comments
 (0)