Skip to content

Fix for #2387, #2388 fix subscription and tenant parameter attributes #2484

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jun 21, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,7 @@
<Compile Include="PowerShellExtensions.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="RMTestBase.cs" />
<Compile Include="TestExecutionHelpers.cs" />
<Compile Include="XunitTracingInterceptor.cs" />
</ItemGroup>
<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// ----------------------------------------------------------------------------------
//
// Copyright Microsoft Corporation
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// ----------------------------------------------------------------------------------

using System;

namespace Microsoft.Azure.Commands.ScenarioTest
{
public static class TestExecutionHelpers
{
/// <summary>
/// Retry an action until it succeeds - use for flaky tests
/// </summary>
/// <param name="testAction">Action tor etry</param>
/// <param name="maxTries">The maximum number of times to try the action</param>
public static void RetryAction(Action testAction, int maxTries = 3)
{
var tries = 0;
var succeeded = false;
do
{
try
{
testAction();
succeeded = true;
}
catch (Exception)
{
if (++tries >= maxTries)
{
throw;
}
}
} while (!succeeded);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@
// limitations under the License.
// ----------------------------------------------------------------------------------

using System;
using System.CodeDom;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using Microsoft.Azure.Commands.Common.Authentication;
using Microsoft.Azure.Commands.Common.Authentication.Models;
using Microsoft.Azure.Commands.Profile;
Expand All @@ -25,14 +30,18 @@
using System.Management.Automation;
using Xunit;
using Xunit.Abstractions;
using Xunit.Sdk;

namespace Microsoft.Azure.Commands.ResourceManager.Profile.Test
{
public class ContextCmdletTests : RMTestBase
{
private MemoryDataStore dataStore;
private MockCommandRuntime commandRuntimeMock;

const string guid1 = "a0cc8bd7-2c6a-47e9-a4c4-3f6ed136e240";
const string guid2 = "eab635c0-a35a-4f70-9e46-e5351c7b5c8b";
const string guid3 = "52f66548-2550-417b-941e-9d6e04f3ac8d";
const string guid4 = "40e67ee2-1a1a-4517-9253-ab6f93c5710f";
public ContextCmdletTests(ITestOutputHelper output)
{
XunitTracingInterceptor.AddToContext(new XunitTracingInterceptor(output));
Expand Down Expand Up @@ -78,8 +87,15 @@ public void SelectAzureContextWithNoSubscriptionAndTenant()
var existingTenants = account.GetProperty(AzureAccount.Property.Tenants);
var allowedTenants = existingTenants == null ? tenantToSet : existingTenants + "," + tenantToSet;
account.SetProperty(AzureAccount.Property.Tenants, allowedTenants);
account.SetProperty(AzureAccount.Property.Subscriptions, new string[0]);

((RuntimeDefinedParameterDictionary)cmdlt.GetDynamicParameters())["TenantId"].Value = tenantToSet;
var paramDictionary =
((RuntimeDefinedParameterDictionary)cmdlt.GetDynamicParameters());
var tenantParam = paramDictionary["TenantId"];
Assert.True(tenantParam.Attributes.Any(a => a is ValidateSetAttribute
&& ((ValidateSetAttribute)a).ValidValues.Any(v => string.Equals(v, tenantToSet, StringComparison.OrdinalIgnoreCase))));
Assert.False(paramDictionary["SubscriptionId"].Attributes.Any(a => a is ValidateSetAttribute));
tenantParam.Value = tenantToSet;

// Act
cmdlt.InvokeBeginProcessing();
Expand All @@ -94,6 +110,50 @@ public void SelectAzureContextWithNoSubscriptionAndTenant()
Assert.NotEqual(tenantToSet, context.Tenant.TenantId);
}

[Theory]
[InlineData(null, null)]
[InlineData(new string[0], new string[0])]
[InlineData(new string[] { guid1}, new string[] {guid2})]
[InlineData(new string[] { guid1, guid2 }, new string[] { guid3, guid4 })]
[Trait(Category.AcceptanceType, Category.CheckIn)]
public void SetsDynamicParametersForContext(string[] subscriptions, string[] tenants)
{
var cmdlt = new SetAzureRMContextCommand();

// Setup
cmdlt.CommandRuntime = commandRuntimeMock;

// Make sure that the tenant ID we are attempting to set is
// valid for the account
var account = AzureRmProfileProvider.Instance.Profile.Context.Account;
account.SetProperty(AzureAccount.Property.Tenants, tenants);
account.SetProperty(AzureAccount.Property.Subscriptions, subscriptions);

var paramDictionary =
((RuntimeDefinedParameterDictionary)cmdlt.GetDynamicParameters());
var subscriptionParams = paramDictionary["SubscriptionId"];
VerifyValidationAttribute(subscriptionParams, subscriptions);
var tenantParams = paramDictionary["TenantId"];
VerifyValidationAttribute(tenantParams, tenants);
}

private void VerifyValidationAttribute(RuntimeDefinedParameter parameter, string[] expectedValues)
{
if (expectedValues != null && expectedValues.Length > 0)
{
var validateAttribute = parameter.Attributes.First(a => a is ValidateSetAttribute) as ValidateSetAttribute;
Assert.NotNull(validateAttribute);
foreach (var expectedValue in expectedValues)
{
Assert.Contains(expectedValue, validateAttribute.ValidValues, StringComparer.OrdinalIgnoreCase);
}
}
else
{
Assert.False(parameter.Attributes.Any(a => a is ValidateSetAttribute));
}
}

[Fact]
[Trait(Category.AcceptanceType, Category.CheckIn)]
public void SelectAzureContextWithNoSubscriptionAndNoTenant()
Expand All @@ -113,5 +173,7 @@ public void SelectAzureContextWithNoSubscriptionAndNoTenant()
var context = (PSAzureContext)commandRuntimeMock.OutputPipeline[0];
Assert.NotNull(context);
}


}
}
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,6 @@ private RuntimeDefinedParameterDictionary CreateDynamicParameterDictionary()
HelpMessage = "Subscription",
ValueFromPipelineByPropertyName = true
},
new ValidateSetAttribute(AzureRmProfileProvider.Instance.Profile.Context.Account.GetPropertyAsArray(AzureAccount.Property.Subscriptions)),
};

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

if (AzureRmProfileProvider.Instance != null
&& AzureRmProfileProvider.Instance.Profile != null
&& AzureRmProfileProvider.Instance.Profile.Context != null
&& AzureRmProfileProvider.Instance.Profile.Context.Account != null)
{
var account = AzureRmProfileProvider.Instance.Profile.Context.Account;
if (account.IsPropertySet(AzureAccount.Property.Subscriptions))
{
var subscriptions = account.GetPropertyAsArray(AzureAccount.Property.Subscriptions);
if (subscriptions != null && subscriptions.Length > 0)
{
subscriptionIdAttributes.Add(
new ValidateSetAttribute(subscriptions));
}
}
if (account.IsPropertySet(AzureAccount.Property.Tenants))
{
var tenants = account.GetPropertyAsArray(AzureAccount.Property.Tenants);
if (tenants != null && tenants.Length > 0)
{
tenantIdAttributes.Add(
new ValidateSetAttribute(tenants));
}
}
}

_tenantId = new RuntimeDefinedParameter("TenantId", typeof(string), tenantIdAttributes);
_subscriptionId = new RuntimeDefinedParameter("SubscriptionId", typeof(string), subscriptionIdAttributes);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
using System.Linq;
using System.Management.Automation;
using System.Security;
using Microsoft.Azure.Commands.ScenarioTest;
using Newtonsoft.Json.Linq;
using Xunit;
using Xunit.Abstractions;
Expand Down Expand Up @@ -498,10 +499,14 @@ public void HandlesInvalidTemplateFiles()
[Trait(Category.AcceptanceType, Category.CheckIn)]
public void ParseTemplateParameterFileContents_DeserializeWithCorrectType()
{
Dictionary<string, TemplateFileParameterV1> result =
TemplateUtility.ParseTemplateParameterFileContents(@"Resources\WebSite.param.dev.json");
Assert.Equal(true, result["isWorker"].Value);
Assert.Equal((System.Int64)1, result["numberOfWorker"].Value);
// Add up to 3 retries for flaky test
TestExecutionHelpers.RetryAction(() =>
{
Dictionary<string, TemplateFileParameterV1> result =
TemplateUtility.ParseTemplateParameterFileContents(@"Resources\WebSite.param.dev.json");
Assert.Equal(true, result["isWorker"].Value);
Assert.Equal((System.Int64) 1, result["numberOfWorker"].Value);
});
}
}
}
4 changes: 2 additions & 2 deletions src/ResourceManager/Resources/Resources.sln
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 2013
VisualStudioVersion = 12.0.31101.0
# Visual Studio 14
VisualStudioVersion = 14.0.25123.0
MinimumVisualStudioVersion = 10.0.40219.1
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Tests", "Tests", "{95C16AED-FD57-42A0-86C3-2CF4300A4817}"
EndProject
Expand Down