|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.IO; |
| 4 | +using Microsoft.IdentityModel.Clients.ActiveDirectory; |
| 5 | +using Microsoft.Rest.ClientRuntime.Azure.TestFramework; |
| 6 | +using Xunit; |
| 7 | + |
| 8 | +namespace Microsoft.Azure.Commands.Compute.Test.ScenarioTests |
| 9 | +{ |
| 10 | + public class RunnerTests |
| 11 | + { |
| 12 | + [Fact] |
| 13 | + public void ExecuteRunnerTests() |
| 14 | + { |
| 15 | + var mode = Environment.GetEnvironmentVariable("AZURE_TEST_MODE"); |
| 16 | + var csmAuth = Environment.GetEnvironmentVariable("TEST_CSM_ORGID_AUTHENTICATION"); |
| 17 | + |
| 18 | + if (mode == null || csmAuth == null || mode.ToLower() != "record") |
| 19 | + { |
| 20 | + return; |
| 21 | + } |
| 22 | + |
| 23 | + Assert.False(string.IsNullOrEmpty(csmAuth)); |
| 24 | + Assert.True(csmAuth.Contains("AADTenant")); |
| 25 | + |
| 26 | + var envDictionary = TestUtilities.ParseConnectionString(csmAuth); |
| 27 | + var testEnv = new TestEnvironment(envDictionary); |
| 28 | + Assert.NotNull(testEnv.Tenant); |
| 29 | + Assert.NotNull(testEnv.SubscriptionId); |
| 30 | + Assert.NotNull(testEnv.ClientId); |
| 31 | + Assert.True(envDictionary.ContainsKey("ApplicationSecret")); |
| 32 | + |
| 33 | + var authenticationContext = new AuthenticationContext("https://login.windows.net/" + testEnv.Tenant); |
| 34 | + var credential = new ClientCredential(testEnv.ClientId, envDictionary["ApplicationSecret"]); |
| 35 | + |
| 36 | + var result = authenticationContext.AcquireToken("https://management.core.windows.net/", clientCredential: credential); |
| 37 | + Assert.NotNull(result.AccessToken); |
| 38 | + envDictionary["RawToken"] = result.AccessToken; |
| 39 | + |
| 40 | + FixCSMAuthEnvVariable(envDictionary); |
| 41 | + |
| 42 | + Console.WriteLine(Environment.GetEnvironmentVariable("TEST_CSM_ORGID_AUTHENTICATION")); |
| 43 | + |
| 44 | + var testFile = File.ReadAllLines("ScenarioTests\\RunnerTests.csv"); |
| 45 | + foreach (var line in testFile) |
| 46 | + { |
| 47 | + var tokens = line.Split(';'); |
| 48 | + var className = tokens[0]; |
| 49 | + var type = Type.GetType(className); |
| 50 | + var constructorInfo = type.GetConstructor(Type.EmptyTypes); |
| 51 | + for (int i = 1; i < tokens.Length; i++) |
| 52 | + { |
| 53 | + var method = tokens[i]; |
| 54 | + var testClassInstance = constructorInfo.Invoke(new object[] {}); |
| 55 | + var testMethod = type.GetMethod(method); |
| 56 | + |
| 57 | + testMethod.Invoke(testClassInstance, new object[] {}); |
| 58 | + } |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + private void FixCSMAuthEnvVariable(IDictionary<string, string> envDictionary) |
| 63 | + { |
| 64 | + var str = string.Empty; |
| 65 | + foreach (var entry in envDictionary) |
| 66 | + { |
| 67 | + if (entry.Key != "AADClientId" && entry.Key != "ApplicationSecret") |
| 68 | + { |
| 69 | + str += string.Format("{0}={1};", entry.Key, entry.Value); |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + Environment.SetEnvironmentVariable("TEST_CSM_ORGID_AUTHENTICATION", str); |
| 74 | + } |
| 75 | + } |
| 76 | +} |
0 commit comments