Skip to content

Commit 098f4cd

Browse files
author
Ziyue Zheng
committed
Add AksTestRunner and replace TestController
1 parent c647847 commit 098f4cd

File tree

4 files changed

+124
-171
lines changed

4 files changed

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