Skip to content

Commit e1f0400

Browse files
committed
Responding to review feedback, adding support for selecting subscription, adding environment variable extensibility
1 parent 2ba2a32 commit e1f0400

19 files changed

+131
-52
lines changed

examples/lib/loginService.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
#!/bin/bash
2-
azure account add --spn --appid "$spn" --secret "$secret"
2+
azure account add --spn --appid "$spn" --secret "$secret" -t "$tenant" -s "$subscription"

examples/lib/loginUser.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
#!/bin/bash
2-
azure account add -u "$azureUser" -p "$password"
2+
azure account add -u "$azureUser" -p "$password" -s "$subscription"

src/CLU/Commands.Common.ScenarioTest/Commands.Common.ScenarioTest.xproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
<Import Project="$(VSToolsPath)\DNX\Microsoft.DNX.Props" Condition="'$(VSToolsPath)' != ''" />
88
<PropertyGroup Label="Globals">
99
<ProjectGuid>b1d3cb1f-c0ca-401f-8146-b2e9c1ef460f</ProjectGuid>
10-
<RootNamespace>Commands.Common.ScenarioTest</RootNamespace>
10+
<RootNamespace>Microsoft.Azure.Commands.Common.ScenarioTest</RootNamespace>
1111
<BaseIntermediateOutputPath Condition="'$(BaseIntermediateOutputPath)'=='' ">..\artifacts\obj\$(MSBuildProjectName)</BaseIntermediateOutputPath>
1212
<OutputPath Condition="'$(OutputPath)'=='' ">..\artifacts\bin\$(MSBuildProjectName)\</OutputPath>
1313
</PropertyGroup>

src/CLU/Commands.Common.ScenarioTest/EnvironmentConstants.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,16 @@
1717
using System.Linq;
1818
using System.Threading.Tasks;
1919

20-
namespace Commands.Common.ScenarioTest
20+
namespace Microsoft.Azure.Commands.Common.ScenarioTest
2121
{
2222
public static class EnvironmentConstants
2323
{
2424
public const string UsernameKey = "Username";
2525
public const string PasswordKey = "Password";
2626
public const string ServicePrincipalKey = "ServicePrincipal";
27+
public const string TenantKey = "TenantId";
28+
public const string SubscriptionKey = "SubscriptionId";
29+
public const string TestRunDirectory = "TestRunDirectory";
30+
public const string ExampleDirectory = "ExamplesDirectory";
2731
}
2832
}

src/CLU/Commands.Common.ScenarioTest/EnvironmentContextFactory.cs

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,10 @@
1212
// limitations under the License.
1313
// ----------------------------------------------------------------------------------
1414

15-
using System;
1615
using System.Collections.Generic;
1716
using System.IO;
18-
using Commands.Common.ScenarioTest;
19-
using Microsoft.Azure.Commands.Common.Authentication.Models;
20-
using Microsoft.Azure.Commands.ScenarioTest;
2117

22-
namespace Microsoft.Azure.Commands.Examples.Test
18+
namespace Microsoft.Azure.Commands.Common.ScenarioTest
2319
{
2420
public class EnvironmentContextFactory
2521
{
@@ -35,8 +31,7 @@ public TestContext GetTestContext(string scriptDirectoryName)
3531
{
3632
var context = new TestContext();
3733
context.ExecutionDirectory = GetBaseDirectory();
38-
context.TestScriptDirectory = Path.GetFullPath(Path.Combine(context.ExecutionDirectory, "..",
39-
"..", "..", "examples", scriptDirectoryName));
34+
context.TestScriptDirectory =GetExamplesDirectory(context.ExecutionDirectory, scriptDirectoryName);
4035
context.TestExecutableName = "bash.exe";
4136
context.TestScriptSuffix = ".sh";
4237
var helpers = new List<IScriptEnvironmentHelper>();
@@ -45,9 +40,27 @@ public TestContext GetTestContext(string scriptDirectoryName)
4540
return context;
4641
}
4742

43+
private string GetExamplesDirectory(string executionDirectory, string scriptDirectoryName)
44+
{
45+
string examplesDirectory;
46+
if (!Utilities.TryGetEnvironmentVariable(EnvironmentConstants.ExampleDirectory, out examplesDirectory))
47+
{
48+
examplesDirectory = Path.GetFullPath(Path.Combine(executionDirectory, "..",
49+
"..", "..", "examples"));
50+
}
51+
52+
return Path.Combine(examplesDirectory, scriptDirectoryName);
53+
}
4854
private string GetBaseDirectory()
4955
{
50-
return Path.GetFullPath(Directory.GetCurrentDirectory());
56+
string baseDirectory;
57+
if (!Utilities.TryGetEnvironmentVariable(EnvironmentConstants.TestRunDirectory, out baseDirectory))
58+
{
59+
baseDirectory = Path.GetFullPath(Path.Combine(Directory.GetCurrentDirectory(), "..", "TestResults"));
60+
}
61+
62+
Utilities.EnsureDirectoryExists(baseDirectory);
63+
return baseDirectory;
5164
}
5265

5366
}

src/CLU/Commands.Common.ScenarioTest/EnvironmentCredentialsProvider.cs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@
1717
using System.IO;
1818
using System.Linq;
1919
using System.Threading.Tasks;
20-
using Microsoft.Azure.Commands.ScenarioTest;
20+
using Microsoft.Azure.Commands.Common.ScenarioTest;
2121

22-
namespace Commands.Common.ScenarioTest
22+
namespace Microsoft.Azure.Commands.Common.ScenarioTest
2323
{
2424
public class EnvironmentCredentialsProvider : ICredentialsProvider
2525
{
@@ -67,12 +67,15 @@ var setting in
6767
protected virtual bool TryInitializeServiceCredentials(IDictionary<string, string> settings )
6868
{
6969
if (settings.ContainsKey(EnvironmentConstants.ServicePrincipalKey) &&
70-
settings.ContainsKey(EnvironmentConstants.PasswordKey))
70+
settings.ContainsKey(EnvironmentConstants.PasswordKey) &&
71+
settings.ContainsKey(EnvironmentConstants.TenantKey))
7172
{
7273
LoginScriptName = serviceScript;
7374
EnvironmentProvider = new ServiceAuthenticationHelper(
7475
settings[EnvironmentConstants.ServicePrincipalKey],
75-
settings[EnvironmentConstants.PasswordKey]);
76+
settings[EnvironmentConstants.PasswordKey],
77+
settings[EnvironmentConstants.TenantKey],
78+
settings.ContainsKey(EnvironmentConstants.SubscriptionKey) ? settings[EnvironmentConstants.SubscriptionKey] : null);
7679
return true;
7780
}
7881
return false;
@@ -86,7 +89,8 @@ protected virtual bool TryInitializeUserCredentials(IDictionary<string, string>
8689
LoginScriptName = userScript;
8790
EnvironmentProvider = new UserAuthenticationHelper(
8891
settings[EnvironmentConstants.UsernameKey],
89-
settings[EnvironmentConstants.PasswordKey]);
92+
settings[EnvironmentConstants.PasswordKey],
93+
settings.ContainsKey(EnvironmentConstants.SubscriptionKey) ? settings[EnvironmentConstants.SubscriptionKey] : null);
9094
return true;
9195
}
9296
return false;

src/CLU/Commands.Common.ScenarioTest/ExampleScriptRunner.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,14 @@
1919
using Microsoft.Azure.Commands.Common.Authentication;
2020
using Microsoft.Azure.Commands.Common.Authentication.Factories;
2121
using Microsoft.Azure.Commands.Common.Authentication.Models;
22-
using Microsoft.Azure.Commands.ScenarioTest;
22+
using Microsoft.Azure.Commands.Common.ScenarioTest;
2323
using Microsoft.Azure.Management.Resources;
2424
using Microsoft.Azure.Management.Resources.Models;
2525
using Microsoft.Rest;
2626
using Newtonsoft.Json.Linq;
2727
using Xunit;
2828

29-
namespace Microsoft.Azure.Commands.Examples.Test
29+
namespace Microsoft.Azure.Commands.Common.ScenarioTest
3030
{
3131
public class ExampleScriptRunner
3232
{

src/CLU/Commands.Common.ScenarioTest/ICredentialsProvider.cs

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

15-
using Microsoft.Azure.Commands.ScenarioTest;
15+
using Microsoft.Azure.Commands.Common.ScenarioTest;
1616

17-
namespace Commands.Common.ScenarioTest
17+
namespace Microsoft.Azure.Commands.Common.ScenarioTest
1818
{
1919
public interface ICredentialsProvider
2020
{

src/CLU/Commands.Common.ScenarioTest/IScriptEnvironmentHelper.cs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,10 @@
1212
// limitations under the License.
1313
// ----------------------------------------------------------------------------------
1414

15-
using System;
1615
using System.Collections.Generic;
17-
using System.Linq;
18-
using System.Security.Cryptography.X509Certificates;
19-
using System.Threading.Tasks;
2016
using Microsoft.Azure.Commands.Common.Authentication;
21-
using Microsoft.Azure.Commands.Examples.Test;
2217

23-
namespace Microsoft.Azure.Commands.ScenarioTest
18+
namespace Microsoft.Azure.Commands.Common.ScenarioTest
2419
{
2520
public interface IScriptEnvironmentHelper
2621
{

src/CLU/Commands.Common.ScenarioTest/ITestLogger.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
using Xunit.Abstractions;
1616

17-
namespace Microsoft.Azure.Commands.ScenarioTest
17+
namespace Microsoft.Azure.Commands.Common.ScenarioTest
1818
{
1919
public interface ITestLogger
2020
{

src/CLU/Commands.Common.ScenarioTest/Logger.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
using System.Diagnostics;
1616

17-
namespace Microsoft.Azure.Commands.ScenarioTest
17+
namespace Microsoft.Azure.Commands.Common.ScenarioTest
1818
{
1919
public class Logger : ITestLogger
2020
{

src/CLU/Commands.Common.ScenarioTest/ProcessHelper.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,10 @@
1818
using System.IO;
1919
using System.Text;
2020
using System.Threading;
21-
using Microsoft.Azure.Commands.ScenarioTest;
21+
using Microsoft.Azure.Commands.Common.ScenarioTest;
2222
using Xunit;
2323

24-
namespace Microsoft.Azure.Commands.Examples.Test
24+
namespace Microsoft.Azure.Commands.Common.ScenarioTest
2525
{
2626
public class ProcessHelper : IDisposable
2727
{

src/CLU/Commands.Common.ScenarioTest/SampleTest.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,11 @@
1717
using System.Diagnostics;
1818
using System.Linq;
1919
using System.Threading.Tasks;
20-
using Microsoft.Azure.Commands.Examples.Test;
21-
using Microsoft.Azure.Commands.ScenarioTest;
20+
using Microsoft.Azure.Commands.Common.ScenarioTest;
2221
using Xunit;
2322
using Xunit.Abstractions;
2423

25-
namespace Commands.Common.ScenarioTest
24+
namespace Microsoft.Azure.Commands.Common.ScenarioTest
2625
{
2726
[Collection("SampleCollection")]
2827
public class SampleTest

src/CLU/Commands.Common.ScenarioTest/SampleTestCollection.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
using Xunit;
1616

17-
namespace Commands.Common.ScenarioTest
17+
namespace Microsoft.Azure.Commands.Common.ScenarioTest
1818
{
1919
[CollectionDefinition("SampleCollection")]
2020
public class SampleTestCollection : ICollectionFixture<ScenarioTestFixture>

src/CLU/Commands.Common.ScenarioTest/ScenarioTestFixture.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,12 @@
1414

1515
using System;
1616
using Microsoft.Azure.Commands.Common.Authentication.Models;
17-
using Microsoft.Azure.Commands.Examples.Test;
17+
using Microsoft.Azure.Commands.Common.ScenarioTest;
1818
using Microsoft.Azure.Commands.Models;
1919
using Moq.Protected;
2020
using Newtonsoft.Json;
2121

22-
namespace Commands.Common.ScenarioTest
22+
namespace Microsoft.Azure.Commands.Common.ScenarioTest
2323
{
2424
public class ScenarioTestFixture
2525
{

src/CLU/Commands.Common.ScenarioTest/ServiceAuthenticationHelper.cs

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,33 +12,51 @@
1212
// limitations under the License.
1313
// ----------------------------------------------------------------------------------
1414

15-
using System;
1615
using System.Collections.Generic;
17-
using System.Linq;
18-
using System.Threading.Tasks;
1916
using Microsoft.Azure.Commands.Common.Authentication;
20-
using Microsoft.Azure.Commands.Examples.Test;
21-
using Microsoft.Azure.Commands.ScenarioTest;
17+
using Microsoft.Azure.Commands.Common.ScenarioTest;
2218

23-
namespace Commands.Common.ScenarioTest
19+
namespace Microsoft.Azure.Commands.Common.ScenarioTest
2420
{
21+
/// <summary>
22+
/// Add the given SPN credentials to the environment for the login script
23+
/// </summary>
2524
public class ServiceAuthenticationHelper : IScriptEnvironmentHelper
2625
{
2726
string _spn;
2827
string _secret;
28+
string _tenant;
29+
string _subscription;
2930
const string SecretKey = "secret";
3031
const string SPNKey = "spn";
31-
public ServiceAuthenticationHelper(string spn, string secret)
32+
const string TenantKey = "tenant";
33+
const string SubscriptionKey = "subscription";
34+
public ServiceAuthenticationHelper(string spn, string secret, string tenant)
35+
: this(spn, secret, tenant, null)
36+
{
37+
}
38+
39+
public ServiceAuthenticationHelper(string spn, string secret, string tenant, string subscription)
3240
{
3341
_spn = spn;
3442
_secret = secret;
43+
_tenant = tenant;
44+
_subscription = subscription;
3545
}
3646

3747
public bool TrySetupScriptEnvironment(TestContext testContext, IClientFactory clientFactory, IDictionary<string, string> settings)
3848
{
3949
Logger.Instance.WriteMessage($"Logging in using ServicePrincipal: {_spn}");
4050
settings[SPNKey] = _spn;
4151
settings[SecretKey] = _secret;
52+
Logger.Instance.WriteMessage($"Logging in using Tenant: {_tenant}");
53+
settings[TenantKey] = _tenant;
54+
if (!string.IsNullOrWhiteSpace(_subscription))
55+
{
56+
Logger.Instance.WriteMessage($"Logging in using Subscription: {_subscription}");
57+
settings[SubscriptionKey] = _subscription;
58+
}
59+
4260
return true;
4361
}
4462
}

src/CLU/Commands.Common.ScenarioTest/TestContext.cs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
1-
using System;
2-
using System.Collections.Generic;
1+
using System.Collections.Generic;
32
using Microsoft.Azure.Commands.Common.Authentication.Models;
4-
using Microsoft.Azure.Commands.ScenarioTest;
53
// ----------------------------------------------------------------------------------
64
//
75
// Copyright Microsoft Corporation
@@ -16,14 +14,13 @@
1614
// limitations under the License.
1715
// ----------------------------------------------------------------------------------
1816

19-
using Microsoft.Rest;
2017

21-
namespace Microsoft.Azure.Commands.Examples.Test
18+
namespace Microsoft.Azure.Commands.Common.ScenarioTest
2219
{
2320
public struct TestContext
2421
{
2522
public AzureContext Context { get; set; }
26-
public string TestExecutableName { get; set; }
23+
public string TestExecutableName { get; set; }
2724
public string TestScriptSuffix { get; set; }
2825
public string TestScriptDirectory { get; set; }
2926
public string ExecutionDirectory { get; set; }

src/CLU/Commands.Common.ScenarioTest/UserAuthenticationHelper.cs

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,21 +15,29 @@
1515
using System;
1616
using System.Collections.Generic;
1717
using Microsoft.Azure.Commands.Common.Authentication;
18-
using Microsoft.Azure.Commands.Examples.Test;
18+
using Microsoft.Azure.Commands.Common.ScenarioTest;
1919

20-
namespace Microsoft.Azure.Commands.ScenarioTest
20+
namespace Microsoft.Azure.Commands.Common.ScenarioTest
2121
{
2222
public class UserAuthenticationHelper : IScriptEnvironmentHelper
2323
{
2424
public const string UsernameVariable = "azureUser";
2525
public const string PasswordVariable = "password";
26+
public const string SubscriptionVariable = "subscription";
2627
string _username;
2728
string _password;
29+
string _subscription;
2830

2931
public UserAuthenticationHelper(string username, string password)
32+
: this(username, password, null)
33+
{
34+
}
35+
36+
public UserAuthenticationHelper(string username, string password, string subscription)
3037
{
3138
_username = username;
3239
_password = password;
40+
_subscription = subscription;
3341
}
3442

3543
public bool TrySetupScriptEnvironment(TestContext testContext, IClientFactory clientFactory, IDictionary<string, string> settings)
@@ -49,6 +57,11 @@ public bool TrySetupScriptEnvironment(TestContext testContext, IClientFactory cl
4957
Logger.Instance.WriteMessage($"Setting process environment {UsernameVariable} = {_username}");
5058
settings[PasswordVariable] = _password;
5159
Logger.Instance.WriteMessage($"Setting process environment {PasswordVariable} = ***********");
60+
if (!string.IsNullOrWhiteSpace(_subscription))
61+
{
62+
Logger.Instance.WriteMessage($"Logging in using Subscription: {_subscription}");
63+
settings[SubscriptionVariable] = _subscription;
64+
}
5265
return true;
5366

5467
}

0 commit comments

Comments
 (0)