Skip to content

Commit a229858

Browse files
author
Ziyue Zheng
authored
Add AksTestRunner and replace TestController (#17962)
* Add AksTestRunner and replace TestController * Add AksTestRunner.cs to replace TestController.cs * Add license
1 parent a31337c commit a229858

File tree

4 files changed

+116
-171
lines changed

4 files changed

+116
-171
lines changed
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
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+
}
Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,83 +1,76 @@
1-
using Microsoft.Azure.Commands.ScenarioTest;
2-
using Microsoft.Azure.ServiceManagement.Common.Models;
31
using Microsoft.WindowsAzure.Commands.ScenarioTest;
4-
using Microsoft.WindowsAzure.Commands.Test.Utilities.Common;
52
using Xunit;
63
using Xunit.Abstractions;
74

85
namespace Commands.Aks.Test.ScenarioTests
96
{
10-
public class KubernetesTests : RMTestBase
7+
public class KubernetesTests : AksTestRunner
118
{
12-
XunitTracingInterceptor _logger;
13-
public KubernetesTests(ITestOutputHelper output)
9+
public KubernetesTests(ITestOutputHelper output) : base(output)
1410
{
15-
_logger = new XunitTracingInterceptor(output);
16-
XunitTracingInterceptor.AddToContext(_logger);
17-
TestExecutionHelpers.SetUpSessionAndProfile();
1811
}
1912

2013
[Fact]
2114
[Trait(Category.AcceptanceType, Category.CheckIn)]
2215
public void TestSimpleAzureKubernetes()
2316
{
24-
TestController.NewInstance.RunPowerShellTest(_logger, "Test-NewAzAksSimple");
17+
TestRunner.RunTestScript("Test-NewAzAksSimple");
2518
}
2619

2720
[Fact]
2821
[Trait(Category.AcceptanceType, Category.CheckIn)]
2922
public void TestAzureKubernetes()
3023
{
31-
TestController.NewInstance.RunPowerShellTest(_logger, "Test-NewAzAks");
24+
TestRunner.RunTestScript("Test-NewAzAks");
3225
}
3326

3427
[Fact]
3528
[Trait(Category.AcceptanceType, Category.CheckIn)]
3629
public void TestNewAzureKubernetesByServicePrincipal()
3730
{
38-
TestController.NewInstance.RunPowerShellTest(_logger, "Test-NewAzAksByServicePrincipal");
31+
TestRunner.RunTestScript("Test-NewAzAksByServicePrincipal");
3932
}
4033

4134
[Fact]
4235
[Trait(Category.AcceptanceType, Category.CheckIn)]
4336
public void TestAzureKubernetesAddons()
4437
{
45-
TestController.NewInstance.RunPowerShellTest(_logger, "Test-NewAzAksAddons");
38+
TestRunner.RunTestScript("Test-NewAzAksAddons");
4639
}
4740

4841
[Fact(Skip = "Please make sure you have graph directory.read permission which is required for grant acrpull permission.")]
4942
[Trait(Category.AcceptanceType, Category.CheckIn)]
5043
public void TestNewAzAksWithAcr()
5144
{
52-
TestController.NewInstance.RunPowerShellTest(_logger, "Test-NewAzAksWithAcr");
45+
TestRunner.RunTestScript("Test-NewAzAksWithAcr");
5346
}
5447

5548
[Fact(Skip = "Updating service principal profile is not allowed on MSI cluster.")]
5649
[Trait(Category.AcceptanceType, Category.CheckIn)]
5750
public void TestResetAzureKubernetesServicePrincipal()
5851
{
59-
TestController.NewInstance.RunPowerShellTest(_logger, "Test-ResetAzureKubernetesServicePrincipal");
52+
TestRunner.RunTestScript("Test-ResetAzureKubernetesServicePrincipal");
6053
}
6154

6255
[Fact]
6356
[Trait(Category.AcceptanceType, Category.CheckIn)]
6457
public void TestUpgradeKubernetesVersion()
6558
{
66-
TestController.NewInstance.RunPowerShellTest(_logger, "Test-UpgradeKubernetesVersion");
59+
TestRunner.RunTestScript("Test-UpgradeKubernetesVersion");
6760
}
6861

6962
[Fact]
7063
[Trait(Category.AcceptanceType, Category.CheckIn)]
7164
public void TestLoadBalancer()
7265
{
73-
TestController.NewInstance.RunPowerShellTest(_logger, "Test-LoadBalancer");
66+
TestRunner.RunTestScript("Test-LoadBalancer");
7467
}
7568

7669
[Fact]
7770
[Trait(Category.AcceptanceType, Category.CheckIn)]
7871
public void TestApiServiceAccess()
7972
{
80-
TestController.NewInstance.RunPowerShellTest(_logger, "Test-ApiServiceAccess");
73+
TestRunner.RunTestScript("Test-ApiServiceAccess");
8174
}
8275
}
8376
}
Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,20 @@
1-
using Microsoft.Azure.Commands.ScenarioTest;
2-
using Microsoft.Azure.ServiceManagement.Common.Models;
3-
using Microsoft.WindowsAzure.Commands.ScenarioTest;
4-
using Microsoft.WindowsAzure.Commands.Test.Utilities.Common;
1+
using Microsoft.WindowsAzure.Commands.ScenarioTest;
52
using Xunit;
63
using Xunit.Abstractions;
74

85
namespace Commands.Aks.Test.ScenarioTests
96
{
10-
public class NodePoolTests : RMTestBase
7+
public class NodePoolTests : AksTestRunner
118
{
12-
XunitTracingInterceptor _logger;
13-
public NodePoolTests(ITestOutputHelper output)
9+
public NodePoolTests(ITestOutputHelper output) : base(output)
1410
{
15-
_logger = new XunitTracingInterceptor(output);
16-
XunitTracingInterceptor.AddToContext(_logger);
17-
TestExecutionHelpers.SetUpSessionAndProfile();
1811
}
1912

2013
[Fact]
2114
[Trait(Category.AcceptanceType, Category.CheckIn)]
2215
public void TestAksNodePool()
2316
{
24-
TestController.NewInstance.RunPowerShellTest(_logger, "Test-NewNodePool");
17+
TestRunner.RunTestScript("Test-NewNodePool");
2518
}
2619
}
2720
}

src/Aks/Aks.Test/ScenarioTests/TestController.cs

Lines changed: 0 additions & 142 deletions
This file was deleted.

0 commit comments

Comments
 (0)