|
| 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 System; |
| 16 | +using Microsoft.Azure.Commands.Common.Authentication; |
| 17 | +using Microsoft.Rest.ClientRuntime.Azure.TestFramework; |
| 18 | +using System.Collections.Generic; |
| 19 | +using Microsoft.Azure.Test.HttpRecorder; |
| 20 | +using Microsoft.Azure.Commands.Common.Authentication.Models; |
| 21 | +using System.Reflection; |
| 22 | +using System.IO; |
| 23 | +using Microsoft.Azure.Management.Internal.Resources; |
| 24 | +using Microsoft.Azure.Management.ContainerService; |
| 25 | +using Microsoft.Azure.Management.Authorization.Version2015_07_01; |
| 26 | +using Microsoft.Azure.Commands.Common.MSGraph.Version1_0; |
| 27 | +using Microsoft.Azure.Commands.TestFx; |
| 28 | +using Xunit.Abstractions; |
| 29 | + |
| 30 | +namespace Commands.Aks.Test.ScenarioTests |
| 31 | +{ |
| 32 | + public class AksTestRunner |
| 33 | + { |
| 34 | + protected readonly ITestRunner TestRunner; |
| 35 | + |
| 36 | + protected AksTestRunner(ITestOutputHelper output) |
| 37 | + { |
| 38 | + TestRunner = TestManager.CreateInstance (output) |
| 39 | + .WithNewPsScriptFilename ($"{GetType().Name}.ps1") |
| 40 | + .WithProjectSubfolderForTests ("ScenarioTests") |
| 41 | + .WithCommonPsScripts (new[] |
| 42 | + { |
| 43 | + @"Common.ps1", |
| 44 | + @"../AzureRM.Resources.ps1" |
| 45 | + }) |
| 46 | + .WithNewRmModules (helper => new[] |
| 47 | + { |
| 48 | + helper.RMProfileModule, |
| 49 | + helper.GetRMModulePath("Az.Aks.psd1") |
| 50 | + }) |
| 51 | + .WithNewRecordMatcherArguments ( |
| 52 | + userAgentsToIgnore: new Dictionary<string, string> |
| 53 | + { |
| 54 | + {"Microsoft.Azure.Management.Resources.ResourceManagementClient", "2017-05-10"}, |
| 55 | + {"Microsoft.Azure.Management.ResourceManager.ResourceManagementClient", "2017-05-10"} |
| 56 | + }, |
| 57 | + resourceProviders: new Dictionary<string, string> |
| 58 | + { |
| 59 | + {"Microsoft.Resources", null}, |
| 60 | + {"Microsoft.Features", null}, |
| 61 | + {"Microsoft.Authorization", null} |
| 62 | + } |
| 63 | + ).WithMockContextAction(() => |
| 64 | + { |
| 65 | + if (HttpMockServer.GetCurrentMode() == HttpRecorderMode.Playback) |
| 66 | + { |
| 67 | + AzureSession.Instance.DataStore = new MemoryDataStore(); |
| 68 | + var home = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile); |
| 69 | + var dir = Path.GetDirectoryName(new Uri(Assembly.GetExecutingAssembly().CodeBase).AbsolutePath); |
| 70 | + var subscription = HttpMockServer.Variables["SubscriptionId"]; |
| 71 | + AzureSession.Instance.DataStore.WriteFile(Path.Combine(home, ".ssh", "id_rsa.pub"), File.ReadAllText(dir + "/Fixtures/id_rsa.pub")); |
| 72 | + var jsonOutput = @"{""" + subscription + @""":{ ""service_principal"":""foo"",""client_secret"":""bar""}}"; |
| 73 | + AzureSession.Instance.DataStore.WriteFile(Path.Combine(home, ".azure", "acsServicePrincipal.json"), jsonOutput); |
| 74 | + } |
| 75 | + else if (HttpMockServer.GetCurrentMode() == HttpRecorderMode.Record) |
| 76 | + { |
| 77 | + AzureSession.Instance.DataStore = new MemoryDataStore(); |
| 78 | + var home = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile); |
| 79 | + var dir = Path.GetDirectoryName(new Uri(Assembly.GetExecutingAssembly().CodeBase).AbsolutePath); |
| 80 | + var subscription = HttpMockServer.Variables["SubscriptionId"]; |
| 81 | + var currentEnvironment = TestEnvironmentFactory.GetTestEnvironment(); |
| 82 | + string spn = null; |
| 83 | + string spnSecret = null; |
| 84 | + if (currentEnvironment.ConnectionString.KeyValuePairs.ContainsKey("ServicePrincipal")) |
| 85 | + { |
| 86 | + spn = currentEnvironment.ConnectionString.KeyValuePairs["ServicePrincipal"]; |
| 87 | + } |
| 88 | + if (currentEnvironment.ConnectionString.KeyValuePairs.ContainsKey("ServicePrincipalSecret")) |
| 89 | + { |
| 90 | + spnSecret = currentEnvironment.ConnectionString.KeyValuePairs["ServicePrincipalSecret"]; |
| 91 | + } |
| 92 | + AzureSession.Instance.DataStore.WriteFile(Path.Combine(home, ".ssh", "id_rsa.pub"), File.ReadAllText(dir + "/Fixtures/id_rsa.pub")); |
| 93 | + var jsonOutput = @"{""" + subscription + @""":{ ""service_principal"":""" + spn + @""",""client_secret"":"""+ spnSecret + @"""}}"; |
| 94 | + AzureSession.Instance.DataStore.WriteFile(Path.Combine(home, ".azure", "acsServicePrincipal.json"), jsonOutput); |
| 95 | + } |
| 96 | + } |
| 97 | + ) |
| 98 | + .Build(); |
| 99 | + } |
| 100 | + } |
| 101 | +} |
0 commit comments