Skip to content

Removed connection string format for scenario tests #1575

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 4 commits into from
Jan 4, 2016
Merged
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
51 changes: 35 additions & 16 deletions clu-getstart.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,26 @@ Testing will consist of scenario tests and unit tests. Scenario tests should be
#### Scenario Tests
- 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.

##### Environment Variables for Authentication
Please set the environment variables for either Username/Password (no 2FA) or ServicePrincipal authentication:

**Username/Password (without 2-factor auth):**

| Field (case sensitive) | Description |
| ------------- |:-------------|
| 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 |

**Service Principal:**

| Field (case sensitive) | Description |
| ------------- |:-------------|
| 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 |

##### 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 @@ -116,20 +136,18 @@ Testing will consist of scenario tests and unit tests. Scenario tests should be
_fixture.GetRunner("resource-management").RunScript("01-ResourceGroups");
}
```
- Set the environment variable 'TestCredentials' to a connection string providing the credentials to use during test execution. Possible fields include:

| Field (case sensitive) | Description |
| ------------- |:-------------|
| Username | an OrgId user name |
| ServicePrincipal | a service principal name |
| Password | the password or application secret to sue for authentication |
| TenantId | (required for Service authentication) The tenant guid to authenticate against |
| SubscriptionId | (optional) Selects a particular subscription by id. If not provided, the first listed subscription will be selected |
- 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).
- 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:
- `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)
- `storageAccountType` - default "Standard_GRS" storage account type
- `storageAccountName` - randomly generated storage account name
- 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).
```C#
runner.EnvironmentVariables.Add("myVariableName", runner.GenerateName("myres"));
runner.EnvironmentVariables.Add("myVariableName", runner.GenerateName("myres"));
```
- 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:
- 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:

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

##### 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 the following envt. variables for authentication 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 azurepassword=<your_password>
export azureUser=<[email protected]>
export password=<your_password>
export PATH=$PATH:/<path-to-drop>/clurun/win7-x64/
. /examples/lib/testrunner.sh
```
- All the parameters to the cmdlets should be passed in as envt. variables
- 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.
- The location for ARM will be provided via variable `$location`.
- "jq" package and BASH assert (e.g. `[ "foo" == "bar" ]`) should be used to validate the responses.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,6 @@ public TestContext GetTestContext(string scriptDirectoryName)
context.TestScriptDirectory =GetExamplesDirectory(context.ExecutionDirectory, scriptDirectoryName);
context.TestExecutableName = "bash.exe";
context.TestScriptSuffix = ".sh";
var helpers = new List<IScriptEnvironmentHelper>();
helpers.Add(_credentials.EnvironmentProvider);
context.EnvironmentHelpers = helpers;
return context;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,6 @@
// ----------------------------------------------------------------------------------

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.Azure.Commands.Common.ScenarioTest;

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

public IScriptEnvironmentHelper EnvironmentProvider { get; protected set; }
public const string SecretVariable = "secret";
public const string SpnVariable = "spn";
public const string TenantVariable = "tenant";

public const string UsernameVariable = "azureUser";
public const string PasswordVariable = "password";
public const string SubscriptionVariable = "subscription";

public string LoginScriptName { get; protected set; }

public virtual void Initialize(string key)
public virtual void Initialize()
{
IDictionary<string, string> settings = GetSettings(key);
if (!TryInitializeServiceCredentials(settings) && !TryInitializeUserCredentials(settings))
if (!TryInitializeServiceCredentials() && !TryInitializeUserCredentials())
{
throw new InvalidOperationException($"Unable to create credentials using key {key}. " +
throw new InvalidOperationException($"Unable to create credentials. " +
"Please ensure your environment is correctly set up.");
}
}

protected virtual IDictionary<string, string> GetSettings(string key)
{
var environmentValue = Environment.GetEnvironmentVariable(key);
if (string.IsNullOrWhiteSpace(environmentValue))
{
throw new InvalidOperationException($"Unable to create credentials. Please set environment variable `{key}`");
}
IDictionary<string, string> settings = new Dictionary<string, string>();
foreach (
var setting in
environmentValue.Split(new string[] {$"{Path.PathSeparator}"},
StringSplitOptions.RemoveEmptyEntries)
)
{
string[] pair = setting.Split(new char[] { '='}, 2, StringSplitOptions.RemoveEmptyEntries);
var pairKey = pair[0].Trim();
var pairValue = pair[1].Trim();
settings[pairKey] = pairValue;
}

return settings;
}

protected virtual bool TryInitializeServiceCredentials(IDictionary<string, string> settings )

protected virtual bool TryInitializeServiceCredentials()
{
if (settings.ContainsKey(EnvironmentConstants.ServicePrincipalKey) &&
settings.ContainsKey(EnvironmentConstants.PasswordKey) &&
settings.ContainsKey(EnvironmentConstants.TenantKey))
if (!string.IsNullOrWhiteSpace(Environment.GetEnvironmentVariable(SpnVariable)) &&
!string.IsNullOrWhiteSpace(Environment.GetEnvironmentVariable(SecretVariable)) &&
!string.IsNullOrWhiteSpace(Environment.GetEnvironmentVariable(TenantVariable)))
{
LoginScriptName = serviceScript;
EnvironmentProvider = new ServiceAuthenticationHelper(
settings[EnvironmentConstants.ServicePrincipalKey],
settings[EnvironmentConstants.PasswordKey],
settings[EnvironmentConstants.TenantKey],
settings.ContainsKey(EnvironmentConstants.SubscriptionKey) ? settings[EnvironmentConstants.SubscriptionKey] : null);
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)))
{
Logger.Instance.WriteMessage($"Logging in using Subscription: {Environment.GetEnvironmentVariable(SubscriptionVariable)}");
}
return true;
}
return false;
}

protected virtual bool TryInitializeUserCredentials(IDictionary<string, string> settings )
protected virtual bool TryInitializeUserCredentials()
{
if (settings.ContainsKey(EnvironmentConstants.UsernameKey) &&
settings.ContainsKey(EnvironmentConstants.PasswordKey))
if (!string.IsNullOrWhiteSpace(Environment.GetEnvironmentVariable(UsernameVariable)) &&
!string.IsNullOrWhiteSpace(Environment.GetEnvironmentVariable(PasswordVariable)))
{
LoginScriptName = userScript;
EnvironmentProvider = new UserAuthenticationHelper(
settings[EnvironmentConstants.UsernameKey],
settings[EnvironmentConstants.PasswordKey],
settings.ContainsKey(EnvironmentConstants.SubscriptionKey) ? settings[EnvironmentConstants.SubscriptionKey] : null);
Logger.Instance.WriteMessage($"Logging in using UserName: {Environment.GetEnvironmentVariable(UsernameVariable)}");
Logger.Instance.WriteMessage($"Logging in using Password: *********");
if (!string.IsNullOrWhiteSpace(Environment.GetEnvironmentVariable(SubscriptionVariable)))
{
Logger.Instance.WriteMessage($"Logging in using Subscription: {Environment.GetEnvironmentVariable(SubscriptionVariable)}");
}
return true;
}
return false;
Expand Down
4 changes: 0 additions & 4 deletions src/CLU/Commands.Common.ScenarioTest/ExampleScriptRunner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -113,10 +113,6 @@ public string RunScript(string testName)
process.EnvironmentVariables[LocationKey] = DefaultLocation;
process.EnvironmentVariables[StorageAccountTypeKey] = DefaultStorageAccountType;
process.EnvironmentVariables[StorageAccountNameKey] = _storageAccountName;
foreach (var helper in _context.EnvironmentHelpers)
{
helper.TrySetupScriptEnvironment(_context, _clientFactory, process.EnvironmentVariables);
}

foreach (var environmentVar in EnvironmentVariables.Keys)
{
Expand Down
3 changes: 1 addition & 2 deletions src/CLU/Commands.Common.ScenarioTest/ICredentialsProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@ namespace Microsoft.Azure.Commands.Common.ScenarioTest
{
public interface ICredentialsProvider
{
void Initialize(string key);
void Initialize();
string LoginScriptName { get; }
IScriptEnvironmentHelper EnvironmentProvider { get; }
}
}
24 changes: 0 additions & 24 deletions src/CLU/Commands.Common.ScenarioTest/IScriptEnvironmentHelper.cs

This file was deleted.

7 changes: 0 additions & 7 deletions src/CLU/Commands.Common.ScenarioTest/SampleTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,7 @@
// limitations under the License.
// ----------------------------------------------------------------------------------

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.Azure.Commands.Common.ScenarioTest;
using Xunit;
using Xunit.Abstractions;

namespace Microsoft.Azure.Commands.Common.ScenarioTest
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public ScenarioTestFixture()
Generator = new Random();
SessionId = $"{Generator.Next(10000, 99999)}";
var credentials = new EnvironmentCredentialsProvider();
credentials.Initialize("TestCredentials");
credentials.Initialize();
_contextFactory = new EnvironmentContextFactory(credentials);
var helper = GetRunner("lib");
var profileText = helper.RunScript(credentials.LoginScriptName);
Expand Down

This file was deleted.

1 change: 0 additions & 1 deletion src/CLU/Commands.Common.ScenarioTest/TestContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,5 @@ public struct TestContext
public string TestScriptSuffix { get; set; }
public string TestScriptDirectory { get; set; }
public string ExecutionDirectory { get; set; }
public IEnumerable<IScriptEnvironmentHelper> EnvironmentHelpers { get; set; }
}
}
Loading