Skip to content

Commit cc433dc

Browse files
committed
Split SampleTest.cs into ComputeTests.cs and ResourceManagementTests.cs
Enabled multiple logins Fixed ResourceManagement Tests
1 parent 5e092a7 commit cc433dc

File tree

13 files changed

+169
-64
lines changed

13 files changed

+169
-64
lines changed

clu-getstart.md

Lines changed: 2 additions & 2 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.

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 & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,18 @@
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"
66
export CmdletSessionID=1010
77
export MSYS_NO_PATHCONV=1
88

99
echo "Logging in as user"
10-
. $BASEDIR/loginUser.sh
10+
. $TESTDIR/loginUser.sh
1111

12-
for d in $( ls $BASEDIR/.. --ignore=lib ); do
13-
for f in $( ls $BASEDIR/../$d/*.sh ); do
12+
for d in $( ls $TESTDIR/.. --ignore=lib ); do
13+
for f in $( ls $TESTDIR/../$d/*.sh ); do
1414
echo "running: $f"
15+
BASEDIR=$(cd "$(dirname "$f")" && pwd)
1516
. $f
1617
set +e
1718
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/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/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
{
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
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 Xunit;
16+
17+
namespace Microsoft.Azure.Commands.Common.ScenarioTest
18+
{
19+
[Collection("SampleCollection")]
20+
public class ResourceManagementTests
21+
{
22+
ScenarioTestFixture _collectionState;
23+
public ResourceManagementTests(ScenarioTestFixture fixture)
24+
{
25+
_collectionState = fixture;
26+
}
27+
28+
[Fact]
29+
public void ResourceGroupsTest()
30+
{
31+
var helper = _collectionState
32+
.GetRunner("resource-management")
33+
.RunScript("01-ResourceGroups");
34+
}
35+
36+
[Fact]
37+
public void ResourcesTest()
38+
{
39+
var helper = _collectionState
40+
.GetRunner("resource-management");
41+
helper.EnvironmentVariables.Add("resourceName", helper.GenerateName("csmr"));
42+
helper.RunScript("02-Resource");
43+
}
44+
45+
[Fact]
46+
public void RoleAssignmentsTest()
47+
{
48+
var helper = _collectionState
49+
.LoginAsUser()
50+
.GetRunner("resource-management")
51+
.RunScript("04-RoleAssignments");
52+
}
53+
54+
[Fact]
55+
public void RoleDefinitionsTest()
56+
{
57+
var helper = _collectionState
58+
.LoginAsService()
59+
.GetRunner("resource-management")
60+
.RunScript("05-RoleDefinitions");
61+
}
62+
}
63+
}

0 commit comments

Comments
 (0)