Skip to content

Clu #300

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 9 commits into from
Jan 5, 2016
Merged

Clu #300

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
17 changes: 10 additions & 7 deletions clu-getstart.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ Please set the environment variables for either Username/Password (no 2FA) or Se
| ------------- |:-------------|
| azureUser | an OrgId user name |
| password | a service principal name |
| subscription | (optional) Selects a particular subscription by id. If not provided, the first listed subscription will be selected |
| userSubscription | (optional) Selects a particular subscription by id. If not provided, the first listed subscription will be selected |

**Service Principal:**

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

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

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

```bash
export azureUser=<[email protected]>
export password=<your_password>
export PATH=$PATH:/<path-to-drop>/clurun/win7-x64/
. /examples/lib/testrunner.sh
export PATH=/<path-to-drop>/clurun/win7-x64/:$PATH
```
- All the parameters to the cmdlets should be passed in as environment variables
- 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.
Expand Down
4 changes: 2 additions & 2 deletions examples/compute-management/02-VirtualMachineCreation.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ printf "\n2. Creating a new storage account '%s' in type '%s'.\n" "$storageAccou
azure storage account new --resourcegroupname "$groupName" --name "$storageAccountName" --location "$location" --type "$storageAccountType"

printf "\n3. Create virtual network.\n"
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`
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`

contextResult=`azure context get`

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

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

nicId="/subscriptions/$subId/resourceGroups/$groupName/providers/Microsoft.Network/networkInterfaces/test"
Expand Down
2 changes: 1 addition & 1 deletion examples/lib/loginService.sh
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
#!/bin/bash
azure account add --spn --appid "$spn" --secret "$secret" -t "$tenant" -s "$subscription"
azure account add --spn --appid "$spn" --secret "$secret" -t "$tenant" -s "$spnSubscription"
2 changes: 1 addition & 1 deletion examples/lib/loginUser.sh
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
#!/bin/bash
azure account add -u "$azureUser" -p "$password" -s "$subscription"
azure account add -u "$azureUser" -p "$password" -s "$userSubscription"
12 changes: 6 additions & 6 deletions examples/lib/testrunner.sh
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
#!/bin/bash
export BASEDIR=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)
. $BASEDIR/helper.sh
export TESTDIR=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)
. $TESTDIR/helper.sh
export groupName=`randomName testrg`
export location="westus"
export CmdletSessionID=1010
export MSYS_NO_PATHCONV=1

echo "Logging in as user"
. $BASEDIR/loginUser.sh
. $TESTDIR/loginUser.sh

for d in $( ls $BASEDIR/.. --ignore=lib ); do
for f in $( ls $BASEDIR/../$d/*.sh ); do
for d in $( ls $TESTDIR/.. --ignore=lib ); do
for f in $( ls $TESTDIR/../$d/*.sh ); do
echo "running: $f"
BASEDIR=$(cd "$(dirname "$f")" && pwd)
. $f
set +e
printf "\nCleanup: removing resource group: %s\n" $groupName
Expand Down
16 changes: 9 additions & 7 deletions examples/resource-management/02-Resource.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ printf "\n=== Managing Resources in Azure ===\n"

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

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

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

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

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

printf "\n8. Removing resource: %s.\n" "$resourceName"
foundResource=$(azure resource find -n "clu" -t $resourceType)
foundResource=$(azure resource find -n "$resourceName" -t $resourceType)
resourceId=$(echo $foundResource | jq '.Id' --raw-output)
azure resource remove --Id "$resourceId" -f
echo $resourceId
export MSYS_NO_PATHCONV=1
azure resource remove --Id "$resourceId" -f
export MSYS_NO_PATHCONV=
7 changes: 3 additions & 4 deletions examples/resource-management/03-Deployments.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,11 @@ printf "\n1. Creating a new resource group: %s and location: %s.\n" "$groupName"
azure group create --name "$groupName" --location "$location"

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

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

printf "\n4. Provisioning Deployment\n"
deploymentInfo=`azure group deployment create --Name "$siteName" --ResourceGroupName "$groupName" --TemplateFile $BASEDIR/resource-management/sampleTemplate.json --TemplateParameterFile $BASEDIR/resource-management/sampleTemplateParams.json`
deploymentInfo=`azure group deployment create --Name "$resourceName" --ResourceGroupName "$groupName" --TemplateFile $BASEDIR/sampleTemplate.json --TemplateParameterFile $BASEDIR/sampleTemplateParams.json`
echo $deploymentInfo
5 changes: 5 additions & 0 deletions examples/resource-management/04-RoleAssignments.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,21 @@ printf "\n1. Creating a new resource group: %s and location: %s.\n" "$groupName"
azure group create --name "$groupName" --location "$location"

printf "\n2. Creating a new Role Assignment.\n"
export MSYS_NO_PATHCONV=1
users=$(azure ad users get)
userId=$(echo $users | cat | jq '.[0].Id' -s --raw-output)
echo "UserID: $userId"
roleDefinitions=$(azure role definition get)
roleDefinitionId=$(echo $roleDefinitions | cat | jq '.[0].Id' -s --raw-output)
echo "RoleDefinitionId: $roleDefinitionId"
subsciptions=$(azure subscription get)
subscriptionId=$(echo $subsciptions | cat | jq '.[0].SubscriptionId' -s --raw-output)
scope="/subscriptions/$subscriptionId/resourceGroups/$groupName"
echo "Scope: $scope"
azure role assignment create --ObjectId "$userId" --RoleDefinitionId "$roleDefinitionId" --Scope "$scope"

printf "\n3. Delete last created Role Assignment.\n"
assignments=$(azure role assignment get)
assignmentId=$(echo $assignments | cat | jq '.[-1].ObjectId' -s --raw-output)
azure role assignment remove --ObjectId "$assignmentId" --Scope "$scope" --RoleDefinitionId "$roleDefinitionId" -f
export MSYS_NO_PATHCONV=
4 changes: 3 additions & 1 deletion examples/resource-management/05-RoleDefinitions.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,18 @@ printf "\n1. Creating a new resource group: %s and location: %s.\n" "$groupName"
azure group create --name "$groupName" --location "$location"

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

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

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

printf "\n5. Delete Role Definition.\n"
roleDefinitionId=$(echo $roleDefinition | jq '.Id' --raw-output)
azure role definition remove --Id $roleDefinitionId --PassThru -f
export MSYS_NO_PATHCONV=
2 changes: 1 addition & 1 deletion examples/resource-management/roleDefinition.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@
"Microsoft.Support/*"
],
"NotActions": [],
"AssignableScopes": [ "/subscriptions/3ca49042-782a-4cc9-89b5-ee1b487fe115" ]
"AssignableScopes": [ "/subscriptions/2c224e7e-3ef5-431d-a57b-e71f4662e3a6" ]
}
15 changes: 3 additions & 12 deletions src/CLU/Commands.Common.ScenarioTest/EnvironmentContextFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,7 @@ namespace Microsoft.Azure.Commands.Common.ScenarioTest
{
public class EnvironmentContextFactory
{
ICredentialsProvider _credentials;


public EnvironmentContextFactory(ICredentialsProvider credentials)
{
_credentials = credentials;
}

public TestContext GetTestContext(string scriptDirectoryName)
public static TestContext GetTestContext(string scriptDirectoryName)
{
var context = new TestContext();
context.ExecutionDirectory = GetBaseDirectory();
Expand All @@ -37,7 +29,7 @@ public TestContext GetTestContext(string scriptDirectoryName)
return context;
}

private string GetExamplesDirectory(string executionDirectory, string scriptDirectoryName)
private static string GetExamplesDirectory(string executionDirectory, string scriptDirectoryName)
{
string examplesDirectory;
if (!Utilities.TryGetEnvironmentVariable(EnvironmentConstants.ExampleDirectory, out examplesDirectory))
Expand All @@ -48,7 +40,7 @@ private string GetExamplesDirectory(string executionDirectory, string scriptDire

return Path.Combine(examplesDirectory, scriptDirectoryName);
}
private string GetBaseDirectory()
private static string GetBaseDirectory()
{
string baseDirectory;
if (!Utilities.TryGetEnvironmentVariable(EnvironmentConstants.TestRunDirectory, out baseDirectory))
Expand All @@ -59,6 +51,5 @@ private string GetBaseDirectory()
Utilities.EnsureDirectoryExists(baseDirectory);
return baseDirectory;
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,25 +18,33 @@ namespace Microsoft.Azure.Commands.Common.ScenarioTest
{
public class EnvironmentCredentialsProvider : ICredentialsProvider
{
public const string userScript = "loginUser";
public const string serviceScript = "loginService";
public const string LoginUserScript = "loginUser";
public const string LoginServiceScript = "loginService";

public const string SecretVariable = "secret";
public const string SpnVariable = "spn";
public const string TenantVariable = "tenant";
public const string SpnSubscriptionVariable = "spnSubscription";

public const string UsernameVariable = "azureUser";
public const string PasswordVariable = "password";
public const string UserSubscriptionVariable = "userSubscription";

public const string SubscriptionVariable = "subscription";

public string LoginScriptName { get; protected set; }

public virtual void Initialize()
{
if (!TryInitializeServiceCredentials() && !TryInitializeUserCredentials())
var isSpnSetup = TryInitializeServiceCredentials();
if (!isSpnSetup)
{
throw new InvalidOperationException($"Unable to create credentials. " +
"Please ensure your environment is correctly set up.");
var isUserSetup = TryInitializeUserCredentials();
if (!isUserSetup)
{
throw new InvalidOperationException($"Unable to create credentials. " +
"Please ensure your environment is correctly set up.");
}
}
}

Expand All @@ -46,13 +54,13 @@ protected virtual bool TryInitializeServiceCredentials()
!string.IsNullOrWhiteSpace(Environment.GetEnvironmentVariable(SecretVariable)) &&
!string.IsNullOrWhiteSpace(Environment.GetEnvironmentVariable(TenantVariable)))
{
LoginScriptName = serviceScript;
LoginScriptName = LoginServiceScript;
Logger.Instance.WriteMessage($"Logging in using ServicePrincipal: {Environment.GetEnvironmentVariable(SpnVariable)}");
Logger.Instance.WriteMessage($"Logging in using Key: *********");
Logger.Instance.WriteMessage($"Logging in using Tenant: {Environment.GetEnvironmentVariable(TenantVariable)}");
if (!string.IsNullOrWhiteSpace(Environment.GetEnvironmentVariable(SubscriptionVariable)))
if (!string.IsNullOrWhiteSpace(Environment.GetEnvironmentVariable(SpnSubscriptionVariable)))
{
Logger.Instance.WriteMessage($"Logging in using Subscription: {Environment.GetEnvironmentVariable(SubscriptionVariable)}");
Logger.Instance.WriteMessage($"Logging in using Subscription: {Environment.GetEnvironmentVariable(SpnSubscriptionVariable)}");
}
return true;
}
Expand All @@ -64,12 +72,12 @@ protected virtual bool TryInitializeUserCredentials()
if (!string.IsNullOrWhiteSpace(Environment.GetEnvironmentVariable(UsernameVariable)) &&
!string.IsNullOrWhiteSpace(Environment.GetEnvironmentVariable(PasswordVariable)))
{
LoginScriptName = userScript;
LoginScriptName = LoginUserScript;
Logger.Instance.WriteMessage($"Logging in using UserName: {Environment.GetEnvironmentVariable(UsernameVariable)}");
Logger.Instance.WriteMessage($"Logging in using Password: *********");
if (!string.IsNullOrWhiteSpace(Environment.GetEnvironmentVariable(SubscriptionVariable)))
if (!string.IsNullOrWhiteSpace(Environment.GetEnvironmentVariable(UserSubscriptionVariable)))
{
Logger.Instance.WriteMessage($"Logging in using Subscription: {Environment.GetEnvironmentVariable(SubscriptionVariable)}");
Logger.Instance.WriteMessage($"Logging in using Subscription: {Environment.GetEnvironmentVariable(UserSubscriptionVariable)}");
}
return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public class ExampleScriptRunner
const string ResourceGroupNameKey = "groupName";
const string LocationKey = "location";
const string BaseDir = "BASEDIR";
const string SessionKey = "CmdletSessionID";
const string SessionKey = "AzureProfile";
const string StorageAccountTypeKey = "storageAccountType";
const string StorageAccountNameKey = "storageAccountName";
const string DefaultStorageAccountType = "Standard_GRS";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,14 @@
namespace Microsoft.Azure.Commands.Common.ScenarioTest
{
[Collection("SampleCollection")]
public class SampleTest
public class ComputeTests
{
ScenarioTestFixture _collectionState;
public SampleTest(ScenarioTestFixture fixture)
public ComputeTests(ScenarioTestFixture fixture)
{
_collectionState = fixture;
}

[Fact]
public void ResourceGroupsTest()
{
var helper = _collectionState.GetRunner("resource-management");
helper.RunScript("01-ResourceGroups");
}

[Fact]
public void VirtualHardDisksTest()
{
Expand Down
Loading