Skip to content

Commit b522ffa

Browse files
committed
Merge pull request Azure#1584 from stankovski/clu
Added "examples" scripts to scenario tests
2 parents 25dae4d + 617c65b commit b522ffa

16 files changed

+189
-77
lines changed

clu-getstart.md

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ Please set the environment variables for either Username/Password (no 2FA) or Se
101101
| ------------- |:-------------|
102102
| azureUser | an OrgId user name |
103103
| password | a service principal name |
104-
| subscription | (optional) Selects a particular subscription by id. If not provided, the first listed subscription will be selected |
104+
| userSubscription | (optional) Selects a particular subscription by id. If not provided, the first listed subscription will be selected |
105105

106106
**Service Principal:**
107107

@@ -110,7 +110,7 @@ Please set the environment variables for either Username/Password (no 2FA) or Se
110110
| spn | The tenant guid to authenticate against |
111111
| secret | the password or application secret to sue for authentication |
112112
| 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 |
113+
| spnSubscription | (optional) Selects a particular subscription by id. If not provided, the first listed subscription will be selected |
114114

115115
##### XUnit Automation For Bash Scenario Tests
116116
- The ```Commands.Common.ScenarioTest``` project contains classes that enable executing bash scenario tests in Visual Studio, or cross-platform using dnx.
@@ -136,8 +136,12 @@ Please set the environment variables for either Username/Password (no 2FA) or Se
136136
_fixture.GetRunner("resource-management").RunScript("01-ResourceGroups");
137137
}
138138
```
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:
139+
- Set the [environment variables](#Environment_Variables_for_Authentication) for either Username/Password (no 2FA) or ServicePrincipal authentication
140+
- Update PATH to include location of CLU bin drop.
141+
```bash
142+
export PATH=/<path-to-drop>/clurun/win7-x64/:$PATH
143+
```
144+
- The infrastructure automatically generates the following environment variables:
141145
- `BASEDIR` - directory path where test script is located
142146
- `location` - default "WestUS" location
143147
- `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)
@@ -156,13 +160,12 @@ runner.EnvironmentVariables.Add("myVariableName", runner.GenerateName("myres"));
156160

157161
##### Running Bash Tests using Bash shell
158162
- Bash tests should be runnable from bash shell in windows/linux/mac environments.
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`
163+
- 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`
160164

161165
```bash
162166
export azureUser=<[email protected]>
163167
export password=<your_password>
164-
export PATH=$PATH:/<path-to-drop>/clurun/win7-x64/
165-
. /examples/lib/testrunner.sh
168+
export PATH=/<path-to-drop>/clurun/win7-x64/:$PATH
166169
```
167170
- All the parameters to the cmdlets should be passed in as environment variables
168171
- 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.

examples/compute-management/02-VirtualMachineCreation.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ printf "\n2. Creating a new storage account '%s' in type '%s'.\n" "$storageAccou
99
azure storage account new --resourcegroupname "$groupName" --name "$storageAccountName" --location "$location" --type "$storageAccountType"
1010

1111
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`
12+
result=`azure virtual network new --resourcegroupname "$groupName" --name test --location "$location" --addressprefix "[\"10.0.0.0/16\"]" --subnet "[{\"Name\":\"test\",\"AddressPrefix\":\"10.0.0.0/24\"}]" --force`
1313

1414
contextResult=`azure context get`
1515

@@ -19,7 +19,7 @@ subnetId="/subscriptions/$subId/resourceGroups/$groupName/providers/Microsoft.Ne
1919

2020
printf "\n4. Create network interface with:\r\nsubId='%s' \r\n& \r\nsubnetId='$subnetId'.\n" "$subId"
2121
export MSYS_NO_PATHCONV=1
22-
azure network interface create --name test --resourcegroupname "$groupName" --location "$location" --subnetid "$subnetId"
22+
azure network interface new --name test --resourcegroupname "$groupName" --location "$location" --subnetid "$subnetId"
2323
export MSYS_NO_PATHCONV=
2424

2525
nicId="/subscriptions/$subId/resourceGroups/$groupName/providers/Microsoft.Network/networkInterfaces/test"

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" -t "$tenant" -s "$subscription"
2+
azure account add --spn --appid "$spn" --secret "$secret" -t "$tenant" -s "$spnSubscription"

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" -s "$subscription"
2+
azure account add -u "$azureUser" -p "$password" -s "$userSubscription"

examples/lib/testrunner.sh

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
#!/bin/bash
2-
export BASEDIR=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)
3-
. $BASEDIR/helper.sh
2+
export TESTDIR=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)
3+
. $TESTDIR/helper.sh
44
export groupName=`randomName testrg`
55
export location="westus"
6-
export CmdletSessionID=1010
76
export MSYS_NO_PATHCONV=1
87

98
echo "Logging in as user"
10-
. $BASEDIR/loginUser.sh
9+
. $TESTDIR/loginUser.sh
1110

12-
for d in $( ls $BASEDIR/.. --ignore=lib ); do
13-
for f in $( ls $BASEDIR/../$d/*.sh ); do
11+
for d in $( ls $TESTDIR/.. --ignore=lib ); do
12+
for f in $( ls $TESTDIR/../$d/*.sh ); do
1413
echo "running: $f"
14+
BASEDIR=$(cd "$(dirname "$f")" && pwd)
1515
. $f
1616
set +e
1717
printf "\nCleanup: removing resource group: %s\n" $groupName

examples/resource-management/02-Resource.sh

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ printf "\n=== Managing Resources in Azure ===\n"
44

55
printf "\n1. Creating a new resource group: %s and location: %s.\n" "$groupName" "$location"
66
azure group create --name "$groupName" --location "$location"
7-
resourceName="CluResource"
87
destinationGroupName=$groupName"Destination"
98

109
printf "\n2. Registering Resource Provider Namespace.\n"
@@ -23,14 +22,14 @@ resourceInfo=$(azure resource get -n $resourceName)
2322
printf "\nValidating Resource name is: %s\n" "$resourceName"
2423
[ $(echo $resourceInfo | jq '.Name' --raw-output) == "$resourceName" ]
2524

26-
printf "\n5. Find Resource with name = %s and type =.\n" "$resourceName" "$resourceType"
27-
foundResource=$(azure resource find -n "clu" -t $resourceType)
28-
printf "\nValidating Resource name is: %s\n" "$resourceName"
25+
printf "\n5. Find Resource with name '%s' and type '%s'.\n" "$resourceName" "$resourceType"
26+
foundResource=$(azure resource find -n "$resourceName" -t $resourceType)
27+
printf "\nValidating Resource name is: %s.\n" "$resourceName"
2928
[ $(echo $foundResource | jq '.Name' --raw-output) == "$resourceName" ]
3029

3130
printf "\n6. Update Resource.\n"
3231
tagsUpdate='[{"Name": "testtagUpdated", "Value": "testvalueUpdated"}]'
33-
azure resource set --ResourceGroupName $groupName --ResourceName $resourceName --ResourceType $resourceType --Tags "$tagsUpdate" -f
32+
azure resource set --ResourceGroupName $groupName --ResourceName $resourceName --ResourceType $resourceType --Tags "$tagsUpdate" -f
3433

3534
printf "\n7. Move Resource to resource group: %s.\n" "$destinationGroupName"
3635
azure group create --name "$destinationGroupName" --location "$location"
@@ -39,6 +38,9 @@ arrayId="[$resourceId]"
3938
azure resource move -g "$destinationGroupName" --ResourceId "$arrayId" -f
4039

4140
printf "\n8. Removing resource: %s.\n" "$resourceName"
42-
foundResource=$(azure resource find -n "clu" -t $resourceType)
41+
foundResource=$(azure resource find -n "$resourceName" -t $resourceType)
4342
resourceId=$(echo $foundResource | jq '.Id' --raw-output)
44-
azure resource remove --Id "$resourceId" -f
43+
echo $resourceId
44+
export MSYS_NO_PATHCONV=1
45+
azure resource remove --Id "$resourceId" -f
46+
export MSYS_NO_PATHCONV=

examples/resource-management/03-Deployments.sh

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,11 @@ printf "\n1. Creating a new resource group: %s and location: %s.\n" "$groupName"
66
azure group create --name "$groupName" --location "$location"
77

88
printf "\n2. Test template with dynamic parameters\n"
9-
siteName=`randomName testrn`
10-
azure group deployment test -g "$groupName" --templatefile $BASEDIR/resource-management/sampleTemplate.json --siteName "$siteName" --hostingPlanName "$siteName" --siteLocation "$location" --sku "Standard" --workerSize "0"
9+
azure group deployment test -g "$groupName" --templatefile $BASEDIR/sampleTemplate.json --siteName "$resourceName" --hostingPlanName "$resourceName" --siteLocation "$location" --sku "Standard" --workerSize "0"
1110

1211
printf "\n3. Test template with JSON parameter object\n"
13-
azure group deployment test -g "$groupName" --templatefile $BASEDIR/resource-management/sampleTemplate.json --templateparameterobject "{\"siteName\":\"$siteName\",\"hostingPlanName\":\"$siteName\",\"siteLocation\":\"$location\",\"sku\":\"Standard\",\"workerSize\": 0 }"
12+
azure group deployment test -g "$groupName" --templatefile $BASEDIR/sampleTemplate.json --templateparameterobject "{\"siteName\":\"$resourceName\",\"hostingPlanName\":\"$resourceName\",\"siteLocation\":\"$location\",\"sku\":\"Standard\",\"workerSize\": 0 }"
1413

1514
printf "\n4. Provisioning Deployment\n"
16-
deploymentInfo=`azure group deployment create --Name "$siteName" --ResourceGroupName "$groupName" --TemplateFile $BASEDIR/resource-management/sampleTemplate.json --TemplateParameterFile $BASEDIR/resource-management/sampleTemplateParams.json`
15+
deploymentInfo=`azure group deployment create --Name "$resourceName" --ResourceGroupName "$groupName" --TemplateFile $BASEDIR/sampleTemplate.json --TemplateParameterFile $BASEDIR/sampleTemplateParams.json`
1716
echo $deploymentInfo

examples/resource-management/04-RoleAssignments.sh

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,21 @@ printf "\n1. Creating a new resource group: %s and location: %s.\n" "$groupName"
66
azure group create --name "$groupName" --location "$location"
77

88
printf "\n2. Creating a new Role Assignment.\n"
9+
export MSYS_NO_PATHCONV=1
910
users=$(azure ad users get)
1011
userId=$(echo $users | cat | jq '.[0].Id' -s --raw-output)
12+
echo "UserID: $userId"
1113
roleDefinitions=$(azure role definition get)
1214
roleDefinitionId=$(echo $roleDefinitions | cat | jq '.[0].Id' -s --raw-output)
15+
echo "RoleDefinitionId: $roleDefinitionId"
1316
subsciptions=$(azure subscription get)
1417
subscriptionId=$(echo $subsciptions | cat | jq '.[0].SubscriptionId' -s --raw-output)
1518
scope="/subscriptions/$subscriptionId/resourceGroups/$groupName"
19+
echo "Scope: $scope"
1620
azure role assignment create --ObjectId "$userId" --RoleDefinitionId "$roleDefinitionId" --Scope "$scope"
1721

1822
printf "\n3. Delete last created Role Assignment.\n"
1923
assignments=$(azure role assignment get)
2024
assignmentId=$(echo $assignments | cat | jq '.[-1].ObjectId' -s --raw-output)
2125
azure role assignment remove --ObjectId "$assignmentId" --Scope "$scope" --RoleDefinitionId "$roleDefinitionId" -f
26+
export MSYS_NO_PATHCONV=

examples/resource-management/05-RoleDefinitions.sh

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,18 @@ printf "\n1. Creating a new resource group: %s and location: %s.\n" "$groupName"
66
azure group create --name "$groupName" --location "$location"
77

88
printf "\n2. Creating a new Role Definition.\n"
9-
roleDefinition=$(azure role definition create --inputfile $BASEDIR/resource-management/roleDefinition.json)
9+
roleDefinition=$(azure role definition create --inputfile $BASEDIR/roleDefinition.json)
1010

1111
printf "\n3. Get information about Role Definitions.\n"
1212
roleDefinitionName=$(echo $roleDefinition | jq '.Name' --raw-output)
1313
azure role definition get -n $roleDefinitionName
1414

1515
printf "\n4. Update Role Definition.\n"
16+
export MSYS_NO_PATHCONV=1
1617
updatedRoleDefinition=$(echo $roleDefinition | jq '.Actions |= .+ ["Microsoft.Authorization/*/write"]')
1718
azure role definition set --Role "$updatedRoleDefinition"
1819

1920
printf "\n5. Delete Role Definition.\n"
2021
roleDefinitionId=$(echo $roleDefinition | jq '.Id' --raw-output)
2122
azure role definition remove --Id $roleDefinitionId --PassThru -f
23+
export MSYS_NO_PATHCONV=

examples/resource-management/roleDefinition.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,5 @@
66
"Microsoft.Support/*"
77
],
88
"NotActions": [],
9-
"AssignableScopes": [ "/subscriptions/3ca49042-782a-4cc9-89b5-ee1b487fe115" ]
9+
"AssignableScopes": [ "/subscriptions/2c224e7e-3ef5-431d-a57b-e71f4662e3a6" ]
1010
}

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

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,7 @@ namespace Microsoft.Azure.Commands.Common.ScenarioTest
1919
{
2020
public class EnvironmentContextFactory
2121
{
22-
ICredentialsProvider _credentials;
23-
24-
25-
public EnvironmentContextFactory(ICredentialsProvider credentials)
26-
{
27-
_credentials = credentials;
28-
}
29-
30-
public TestContext GetTestContext(string scriptDirectoryName)
22+
public static TestContext GetTestContext(string scriptDirectoryName)
3123
{
3224
var context = new TestContext();
3325
context.ExecutionDirectory = GetBaseDirectory();
@@ -37,7 +29,7 @@ public TestContext GetTestContext(string scriptDirectoryName)
3729
return context;
3830
}
3931

40-
private string GetExamplesDirectory(string executionDirectory, string scriptDirectoryName)
32+
private static string GetExamplesDirectory(string executionDirectory, string scriptDirectoryName)
4133
{
4234
string examplesDirectory;
4335
if (!Utilities.TryGetEnvironmentVariable(EnvironmentConstants.ExampleDirectory, out examplesDirectory))
@@ -48,7 +40,7 @@ private string GetExamplesDirectory(string executionDirectory, string scriptDire
4840

4941
return Path.Combine(examplesDirectory, scriptDirectoryName);
5042
}
51-
private string GetBaseDirectory()
43+
private static string GetBaseDirectory()
5244
{
5345
string baseDirectory;
5446
if (!Utilities.TryGetEnvironmentVariable(EnvironmentConstants.TestRunDirectory, out baseDirectory))
@@ -59,6 +51,5 @@ private string GetBaseDirectory()
5951
Utilities.EnsureDirectoryExists(baseDirectory);
6052
return baseDirectory;
6153
}
62-
6354
}
6455
}

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

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,25 +18,33 @@ namespace Microsoft.Azure.Commands.Common.ScenarioTest
1818
{
1919
public class EnvironmentCredentialsProvider : ICredentialsProvider
2020
{
21-
public const string userScript = "loginUser";
22-
public const string serviceScript = "loginService";
21+
public const string LoginUserScript = "loginUser";
22+
public const string LoginServiceScript = "loginService";
2323

2424
public const string SecretVariable = "secret";
2525
public const string SpnVariable = "spn";
2626
public const string TenantVariable = "tenant";
27+
public const string SpnSubscriptionVariable = "spnSubscription";
2728

2829
public const string UsernameVariable = "azureUser";
2930
public const string PasswordVariable = "password";
31+
public const string UserSubscriptionVariable = "userSubscription";
32+
3033
public const string SubscriptionVariable = "subscription";
3134

3235
public string LoginScriptName { get; protected set; }
3336

3437
public virtual void Initialize()
3538
{
36-
if (!TryInitializeServiceCredentials() && !TryInitializeUserCredentials())
39+
var isSpnSetup = TryInitializeServiceCredentials();
40+
if (!isSpnSetup)
3741
{
38-
throw new InvalidOperationException($"Unable to create credentials. " +
39-
"Please ensure your environment is correctly set up.");
42+
var isUserSetup = TryInitializeUserCredentials();
43+
if (!isUserSetup)
44+
{
45+
throw new InvalidOperationException($"Unable to create credentials. " +
46+
"Please ensure your environment is correctly set up.");
47+
}
4048
}
4149
}
4250

@@ -46,13 +54,13 @@ protected virtual bool TryInitializeServiceCredentials()
4654
!string.IsNullOrWhiteSpace(Environment.GetEnvironmentVariable(SecretVariable)) &&
4755
!string.IsNullOrWhiteSpace(Environment.GetEnvironmentVariable(TenantVariable)))
4856
{
49-
LoginScriptName = serviceScript;
57+
LoginScriptName = LoginServiceScript;
5058
Logger.Instance.WriteMessage($"Logging in using ServicePrincipal: {Environment.GetEnvironmentVariable(SpnVariable)}");
5159
Logger.Instance.WriteMessage($"Logging in using Key: *********");
5260
Logger.Instance.WriteMessage($"Logging in using Tenant: {Environment.GetEnvironmentVariable(TenantVariable)}");
53-
if (!string.IsNullOrWhiteSpace(Environment.GetEnvironmentVariable(SubscriptionVariable)))
61+
if (!string.IsNullOrWhiteSpace(Environment.GetEnvironmentVariable(SpnSubscriptionVariable)))
5462
{
55-
Logger.Instance.WriteMessage($"Logging in using Subscription: {Environment.GetEnvironmentVariable(SubscriptionVariable)}");
63+
Logger.Instance.WriteMessage($"Logging in using Subscription: {Environment.GetEnvironmentVariable(SpnSubscriptionVariable)}");
5664
}
5765
return true;
5866
}
@@ -64,12 +72,12 @@ protected virtual bool TryInitializeUserCredentials()
6472
if (!string.IsNullOrWhiteSpace(Environment.GetEnvironmentVariable(UsernameVariable)) &&
6573
!string.IsNullOrWhiteSpace(Environment.GetEnvironmentVariable(PasswordVariable)))
6674
{
67-
LoginScriptName = userScript;
75+
LoginScriptName = LoginUserScript;
6876
Logger.Instance.WriteMessage($"Logging in using UserName: {Environment.GetEnvironmentVariable(UsernameVariable)}");
6977
Logger.Instance.WriteMessage($"Logging in using Password: *********");
70-
if (!string.IsNullOrWhiteSpace(Environment.GetEnvironmentVariable(SubscriptionVariable)))
78+
if (!string.IsNullOrWhiteSpace(Environment.GetEnvironmentVariable(UserSubscriptionVariable)))
7179
{
72-
Logger.Instance.WriteMessage($"Logging in using Subscription: {Environment.GetEnvironmentVariable(SubscriptionVariable)}");
80+
Logger.Instance.WriteMessage($"Logging in using Subscription: {Environment.GetEnvironmentVariable(UserSubscriptionVariable)}");
7381
}
7482
return true;
7583
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public class ExampleScriptRunner
4141
const string ResourceGroupNameKey = "groupName";
4242
const string LocationKey = "location";
4343
const string BaseDir = "BASEDIR";
44-
const string SessionKey = "CmdletSessionID";
44+
const string SessionKey = "AzureProfile";
4545
const string StorageAccountTypeKey = "storageAccountType";
4646
const string StorageAccountNameKey = "storageAccountName";
4747
const string DefaultStorageAccountType = "Standard_GRS";

src/CLU/Commands.Common.ScenarioTest/SampleTest.cs renamed to src/CLU/Commands.Common.ScenarioTest/ExamplesTests/ComputeTests.cs

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,21 +17,14 @@
1717
namespace Microsoft.Azure.Commands.Common.ScenarioTest
1818
{
1919
[Collection("SampleCollection")]
20-
public class SampleTest
20+
public class ComputeTests
2121
{
2222
ScenarioTestFixture _collectionState;
23-
public SampleTest(ScenarioTestFixture fixture)
23+
public ComputeTests(ScenarioTestFixture fixture)
2424
{
2525
_collectionState = fixture;
2626
}
2727

28-
[Fact]
29-
public void ResourceGroupsTest()
30-
{
31-
var helper = _collectionState.GetRunner("resource-management");
32-
helper.RunScript("01-ResourceGroups");
33-
}
34-
3528
[Fact]
3629
public void VirtualHardDisksTest()
3730
{

0 commit comments

Comments
 (0)