Skip to content

Commit e033d12

Browse files
author
Hovsep Mkrtchyan
committed
Merge branch 'clu' of github.com:Azure/azure-powershell into clu
2 parents 67fd171 + ac26cd9 commit e033d12

30 files changed

+245
-266
lines changed

clu-getstart.md

Lines changed: 35 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,26 @@ Testing will consist of scenario tests and unit tests. Scenario tests should be
9292
#### Scenario Tests
9393
- Scenario tests should be saved under `./examples` directory with one directory per package. Each scenario tests should (eventually) consist of both `.ps1` and `.sh` files and should cover "P0" scenarios.
9494

95+
##### Environment Variables for Authentication
96+
Please set the environment variables for either Username/Password (no 2FA) or ServicePrincipal authentication:
97+
98+
**Username/Password (without 2-factor auth):**
99+
100+
| Field (case sensitive) | Description |
101+
| ------------- |:-------------|
102+
| azureUser | an OrgId user name |
103+
| password | a service principal name |
104+
| subscription | (optional) Selects a particular subscription by id. If not provided, the first listed subscription will be selected |
105+
106+
**Service Principal:**
107+
108+
| Field (case sensitive) | Description |
109+
| ------------- |:-------------|
110+
| spn | The tenant guid to authenticate against |
111+
| secret | the password or application secret to sue for authentication |
112+
| tenant | The tenant guid to authenticate against |
113+
| subscription | (optional) Selects a particular subscription by id. If not provided, the first listed subscription will be selected |
114+
95115
##### XUnit Automation For Bash Scenario Tests
96116
- The ```Commands.Common.ScenarioTest``` project contains classes that enable executing bash scenario tests in Visual Studio, or cross-platform using dnx.
97117

@@ -116,20 +136,18 @@ Testing will consist of scenario tests and unit tests. Scenario tests should be
116136
_fixture.GetRunner("resource-management").RunScript("01-ResourceGroups");
117137
}
118138
```
119-
- Set the environment variable 'TestCredentials' to a connection string providing the credentials to use during test execution. Possible fields include:
120-
121-
| Field (case sensitive) | Description |
122-
| ------------- |:-------------|
123-
| Username | an OrgId user name |
124-
| ServicePrincipal | a service principal name |
125-
| Password | the password or application secret to sue for authentication |
126-
| TenantId | (required for Service authentication) The tenant guid to authenticate against |
127-
| SubscriptionId | (optional) Selects a particular subscription by id. If not provided, the first listed subscription will be selected |
128-
- The infrastructure automatically generates a resource group name and assigns the value to the bash variable ```"$resourceGroupName"```. If your scripts require additional variables, you can add these to your environment before running tests, or you can generate values using the ScriptRunner (for the tests using that runner).
139+
- Set the [environment variables](#environment-variables-for-authentication) for either Username/Password (no 2FA) or ServicePrincipal authentication
140+
- The infrastructure automatically generates the following environment variables:
141+
- `BASEDIR` - directory path where test script is located
142+
- `location` - default "WestUS" location
143+
- `groupName` - randomly generated resource group name (note: the test guarantees that this resource group is deleted at the end of a test run; any other resource groups generated as part of the test run need to be deleted by the test)
144+
- `storageAccountType` - default "Standard_GRS" storage account type
145+
- `storageAccountName` - randomly generated storage account name
146+
- If the script require additional variables, you can add these to your environment before running tests, or you can generate values using the ScriptRunner (for the tests using that runner).
129147
```C#
130-
runner.EnvironmentVariables.Add("myVariableName", runner.GenerateName("myres"));
148+
runner.EnvironmentVariables.Add("myVariableName", runner.GenerateName("myres"));
131149
```
132-
- Tests can be executed in vs, or by runnign ```dnx test project.json```. If you execute dnx test from the project directory, it will work without modification and a log file for each script will be written to the test results directory ```..\TestResults```. If you execute dnx test from a different directory, you must set the following environment variables to provide the path to the examples directory and where to write log files:
150+
- Tests can be executed in Visual Studio, or by running ```dnx test project.json```. If you execute dnx test from the project directory, it will work without modification and a log file for each script will be written to the test results directory ```..\TestResults```. If you execute dnx test from a different directory, you must set the following environment variables to provide the path to the examples directory and where to write log files:
133151

134152
| Environment Variable | Description |
135153
| ------------- |:-------------|
@@ -138,14 +156,15 @@ Testing will consist of scenario tests and unit tests. Scenario tests should be
138156

139157
##### Running Bash Tests using Bash shell
140158
- Bash tests should be runnable from bash shell in windows/linux/mac environments.
141-
- To manually run the tests; please set the following envt. variables for authentication and run `./examples/lib/testrunner.sh`
159+
- To manually run the tests; please set [environment variables](#environment-variables-for-authentication) for authentication as well as update PATH and run `./examples/lib/testrunner.sh`
160+
142161
```bash
143-
export azureuser=<[email protected]>
144-
export azurepassword=<your_password>
162+
export azureUser=<[email protected]>
163+
export password=<your_password>
145164
export PATH=$PATH:/<path-to-drop>/clurun/win7-x64/
146165
. /examples/lib/testrunner.sh
147166
```
148-
- All the parameters to the cmdlets should be passed in as envt. variables
167+
- All the parameters to the cmdlets should be passed in as environment variables
149168
- The current test runners will provide a unique resource group name via `$groupName` but may not remove it at the end if the test fails.
150169
- The location for ARM will be provided via variable `$location`.
151170
- "jq" package and BASH assert (e.g. `[ "foo" == "bar" ]`) should be used to validate the responses.
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
Param(
2+
[string]$resourceGroupName,
3+
[string]$resourceGroupLocation
4+
)
5+
6+
Write-Host "Skip"
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#!/bin/bash
2+
set -e
3+
printf "\n=== Managing Virtual Machine Creation in Azure Compute ===\n"
4+
5+
printf "\n1. Creating a new resource group: %s and location: %s.\n" "$groupName" "$location"
6+
azure group create -n "$groupName" --location "$location"
7+
8+
printf "\n2. Creating a new storage account '%s' in type '%s'.\n" "$storageAccountName" "$storageAccountType"
9+
azure storage account new --resourcegroupname "$groupName" --name "$storageAccountName" --location "$location" --type "$storageAccountType"
10+
11+
printf "\n3. Create virtual network.\n"
12+
result=`azure virtual network create --resourcegroupname "$groupName" --name test --location "$location" --addressprefix "[\"10.0.0.0/16\"]" --subnet "[{\"Name\":\"test\",\"AddressPrefix\":\"10.0.0.0/24\"}]" --force`
13+
14+
contextResult=`azure context get`
15+
16+
subId=`echo $contextResult | jq '.Subscription.SubscriptionId' --raw-output`
17+
18+
subnetId="/subscriptions/$subId/resourceGroups/$groupName/providers/Microsoft.Network/virtualNetworks/test/subnets/test"
19+
20+
printf "\n4. Create network interface with:\r\nsubId='%s' \r\n& \r\nsubnetId='$subnetId'.\n" "$subId"
21+
export MSYS_NO_PATHCONV=1
22+
azure network interface create --name test --resourcegroupname "$groupName" --location "$location" --subnetid "$subnetId"
23+
export MSYS_NO_PATHCONV=
24+
25+
nicId="/subscriptions/$subId/resourceGroups/$groupName/providers/Microsoft.Network/networkInterfaces/test"
26+
27+
vhdUri="https://$storageAccountName.blob.core.windows.net/$storageAccountName/$storageAccountName.vhd"
28+
29+
vmStr="{\"Name\":\"test\",\"HardwareProfile\":{\"VmSize\":\"Standard_A1\"},\"NetworkProfile\":{\"NetworkInterfaces\":[{\"Id\":\"$nicId\"}]},\"OSProfile\":{\"ComputerName\":\"test\",\"AdminPassword\":\"BaR@1234\",\"AdminUsername\":\"Foo12\"},\"StorageProfile\":{\"ImageReference\":{\"Offer\":\"WindowsServer\",\"Publisher\":\"MicrosoftWindowsServer\",\"Sku\":\"2008-R2-SP1\",\"Version\":\"latest\"},\"OSDisk\":{\"Caching\":\"ReadWrite\",\"CreateOption\":\"FromImage\",\"Name\":\"osDisk\",\"Vhd\":{\"Uri\":\"$vhdUri\"}}}}"
30+
31+
printf "\n5. Create virtual machine with\r\nnicId='%s'\r\nvhdUri='%s'\r\nvmStr='%s'\n" "$nicId" "$vhdUri" "$vmStr"
32+
33+
azure vm new --resourcegroupname "$groupName" --location "$location" --vmprofile "$vmStr"
34+
35+
printf "\n6. Removing resource group: %s.\n" "$groupName"
36+
azure group remove -n "$groupName" -f

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

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,6 @@ public TestContext GetTestContext(string scriptDirectoryName)
3434
context.TestScriptDirectory =GetExamplesDirectory(context.ExecutionDirectory, scriptDirectoryName);
3535
context.TestExecutableName = "bash.exe";
3636
context.TestScriptSuffix = ".sh";
37-
var helpers = new List<IScriptEnvironmentHelper>();
38-
helpers.Add(_credentials.EnvironmentProvider);
39-
context.EnvironmentHelpers = helpers;
4037
return context;
4138
}
4239

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

Lines changed: 31 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,6 @@
1313
// ----------------------------------------------------------------------------------
1414

1515
using System;
16-
using System.Collections.Generic;
17-
using System.IO;
18-
using System.Linq;
19-
using System.Threading.Tasks;
20-
using Microsoft.Azure.Commands.Common.ScenarioTest;
2116

2217
namespace Microsoft.Azure.Commands.Common.ScenarioTest
2318
{
@@ -26,70 +21,56 @@ public class EnvironmentCredentialsProvider : ICredentialsProvider
2621
public const string userScript = "loginUser";
2722
public const string serviceScript = "loginService";
2823

29-
public IScriptEnvironmentHelper EnvironmentProvider { get; protected set; }
24+
public const string SecretVariable = "secret";
25+
public const string SpnVariable = "spn";
26+
public const string TenantVariable = "tenant";
27+
28+
public const string UsernameVariable = "azureUser";
29+
public const string PasswordVariable = "password";
30+
public const string SubscriptionVariable = "subscription";
3031

3132
public string LoginScriptName { get; protected set; }
3233

33-
public virtual void Initialize(string key)
34+
public virtual void Initialize()
3435
{
35-
IDictionary<string, string> settings = GetSettings(key);
36-
if (!TryInitializeServiceCredentials(settings) && !TryInitializeUserCredentials(settings))
36+
if (!TryInitializeServiceCredentials() && !TryInitializeUserCredentials())
3737
{
38-
throw new InvalidOperationException($"Unable to create credentials using key {key}. " +
38+
throw new InvalidOperationException($"Unable to create credentials. " +
3939
"Please ensure your environment is correctly set up.");
4040
}
4141
}
42-
43-
protected virtual IDictionary<string, string> GetSettings(string key)
44-
{
45-
var environmentValue = Environment.GetEnvironmentVariable(key);
46-
if (string.IsNullOrWhiteSpace(environmentValue))
47-
{
48-
throw new InvalidOperationException($"Unable to create credentials. Please set environment variable `{key}`");
49-
}
50-
IDictionary<string, string> settings = new Dictionary<string, string>();
51-
foreach (
52-
var setting in
53-
environmentValue.Split(new string[] {$"{Path.PathSeparator}"},
54-
StringSplitOptions.RemoveEmptyEntries)
55-
)
56-
{
57-
string[] pair = setting.Split(new char[] { '='}, 2, StringSplitOptions.RemoveEmptyEntries);
58-
var pairKey = pair[0].Trim();
59-
var pairValue = pair[1].Trim();
60-
settings[pairKey] = pairValue;
61-
}
62-
63-
return settings;
64-
}
65-
66-
protected virtual bool TryInitializeServiceCredentials(IDictionary<string, string> settings )
42+
43+
protected virtual bool TryInitializeServiceCredentials()
6744
{
68-
if (settings.ContainsKey(EnvironmentConstants.ServicePrincipalKey) &&
69-
settings.ContainsKey(EnvironmentConstants.PasswordKey) &&
70-
settings.ContainsKey(EnvironmentConstants.TenantKey))
45+
if (!string.IsNullOrWhiteSpace(Environment.GetEnvironmentVariable(SpnVariable)) &&
46+
!string.IsNullOrWhiteSpace(Environment.GetEnvironmentVariable(SecretVariable)) &&
47+
!string.IsNullOrWhiteSpace(Environment.GetEnvironmentVariable(TenantVariable)))
7148
{
7249
LoginScriptName = serviceScript;
73-
EnvironmentProvider = new ServiceAuthenticationHelper(
74-
settings[EnvironmentConstants.ServicePrincipalKey],
75-
settings[EnvironmentConstants.PasswordKey],
76-
settings[EnvironmentConstants.TenantKey],
77-
settings.ContainsKey(EnvironmentConstants.SubscriptionKey) ? settings[EnvironmentConstants.SubscriptionKey] : null);
50+
Logger.Instance.WriteMessage($"Logging in using ServicePrincipal: {Environment.GetEnvironmentVariable(SpnVariable)}");
51+
Logger.Instance.WriteMessage($"Logging in using Key: *********");
52+
Logger.Instance.WriteMessage($"Logging in using Tenant: {Environment.GetEnvironmentVariable(TenantVariable)}");
53+
if (!string.IsNullOrWhiteSpace(Environment.GetEnvironmentVariable(SubscriptionVariable)))
54+
{
55+
Logger.Instance.WriteMessage($"Logging in using Subscription: {Environment.GetEnvironmentVariable(SubscriptionVariable)}");
56+
}
7857
return true;
7958
}
8059
return false;
8160
}
8261

83-
protected virtual bool TryInitializeUserCredentials(IDictionary<string, string> settings )
62+
protected virtual bool TryInitializeUserCredentials()
8463
{
85-
if (settings.ContainsKey(EnvironmentConstants.UsernameKey) &&
86-
settings.ContainsKey(EnvironmentConstants.PasswordKey))
64+
if (!string.IsNullOrWhiteSpace(Environment.GetEnvironmentVariable(UsernameVariable)) &&
65+
!string.IsNullOrWhiteSpace(Environment.GetEnvironmentVariable(PasswordVariable)))
8766
{
8867
LoginScriptName = userScript;
89-
EnvironmentProvider = new UserAuthenticationHelper(
90-
settings[EnvironmentConstants.UsernameKey],
91-
settings[EnvironmentConstants.PasswordKey],
92-
settings.ContainsKey(EnvironmentConstants.SubscriptionKey) ? settings[EnvironmentConstants.SubscriptionKey] : null);
68+
Logger.Instance.WriteMessage($"Logging in using UserName: {Environment.GetEnvironmentVariable(UsernameVariable)}");
69+
Logger.Instance.WriteMessage($"Logging in using Password: *********");
70+
if (!string.IsNullOrWhiteSpace(Environment.GetEnvironmentVariable(SubscriptionVariable)))
71+
{
72+
Logger.Instance.WriteMessage($"Logging in using Subscription: {Environment.GetEnvironmentVariable(SubscriptionVariable)}");
73+
}
9374
return true;
9475
}
9576
return false;

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

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -113,10 +113,6 @@ public string RunScript(string testName)
113113
process.EnvironmentVariables[LocationKey] = DefaultLocation;
114114
process.EnvironmentVariables[StorageAccountTypeKey] = DefaultStorageAccountType;
115115
process.EnvironmentVariables[StorageAccountNameKey] = _storageAccountName;
116-
foreach (var helper in _context.EnvironmentHelpers)
117-
{
118-
helper.TrySetupScriptEnvironment(_context, _clientFactory, process.EnvironmentVariables);
119-
}
120116

121117
foreach (var environmentVar in EnvironmentVariables.Keys)
122118
{

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,7 @@ namespace Microsoft.Azure.Commands.Common.ScenarioTest
1818
{
1919
public interface ICredentialsProvider
2020
{
21-
void Initialize(string key);
21+
void Initialize();
2222
string LoginScriptName { get; }
23-
IScriptEnvironmentHelper EnvironmentProvider { get; }
2423
}
2524
}

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

Lines changed: 0 additions & 24 deletions
This file was deleted.

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

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

15-
using System;
16-
using System.Collections.Generic;
17-
using System.Diagnostics;
18-
using System.Linq;
19-
using System.Threading.Tasks;
20-
using Microsoft.Azure.Commands.Common.ScenarioTest;
2115
using Xunit;
22-
using Xunit.Abstractions;
2316

2417
namespace Microsoft.Azure.Commands.Common.ScenarioTest
2518
{
@@ -52,5 +45,12 @@ public void VirtualMachineSizeTest()
5245
var helper = _collectionState.GetRunner("compute-management");
5346
helper.RunScript("01-VirtualMachineSizes");
5447
}
48+
49+
[Fact]
50+
public void VirtualMachineCreationTest()
51+
{
52+
var helper = _collectionState.GetRunner("compute-management");
53+
helper.RunScript("02-VirtualMachineCreation");
54+
}
5555
}
5656
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public ScenarioTestFixture()
2828
Generator = new Random();
2929
SessionId = $"{Generator.Next(10000, 99999)}";
3030
var credentials = new EnvironmentCredentialsProvider();
31-
credentials.Initialize("TestCredentials");
31+
credentials.Initialize();
3232
_contextFactory = new EnvironmentContextFactory(credentials);
3333
var helper = GetRunner("lib");
3434
var profileText = helper.RunScript(credentials.LoginScriptName);

0 commit comments

Comments
 (0)