|
12 | 12 | // limitations under the License.
|
13 | 13 | // ----------------------------------------------------------------------------------
|
14 | 14 |
|
15 |
| -using Microsoft.Azure.Common.Authentication; |
16 |
| -using Microsoft.Azure.Management.Resources; |
17 |
| -using Microsoft.Azure.Test; |
18 |
| -using Microsoft.WindowsAzure.Commands.ScenarioTest; |
19 |
| -using Microsoft.WindowsAzure.Commands.Utilities.Common; |
20 |
| - |
21 | 15 | namespace Microsoft.Azure.Commands.ScenarioTest.DnsTests
|
22 | 16 | {
|
23 |
| - public class DnsTestsBase |
24 |
| - { |
25 |
| - private EnvironmentSetupHelper helper; |
| 17 | + using System; |
| 18 | + using System.Linq; |
| 19 | + using Microsoft.Azure.Common.Authentication; |
| 20 | + using Microsoft.Azure.Gallery; |
| 21 | + using Microsoft.Azure.Management.Authorization; |
| 22 | + using Microsoft.Azure.Management.Resources; |
| 23 | + using Microsoft.Azure.Subscriptions.Csm; |
| 24 | + using Microsoft.Azure.Test; |
| 25 | + using Microsoft.WindowsAzure.Commands.ScenarioTest; |
| 26 | + using Microsoft.Azure.Management.Dns; |
26 | 27 |
|
27 |
| - protected DnsTestsBase() |
28 |
| - { |
29 |
| - helper = new EnvironmentSetupHelper(); |
30 |
| - } |
31 | 28 |
|
32 |
| - protected void SetupManagementClients() |
33 |
| - { |
34 |
| - var resourcesClient = GetResourcesClient(); |
35 |
| - helper.SetupSomeOfManagementClients(resourcesClient); |
36 |
| - } |
| 29 | + public class DnsTestsBase |
| 30 | + { |
| 31 | + private CSMTestEnvironmentFactory csmTestFactory; |
37 | 32 |
|
38 |
| - protected void RunPowerShellTest(params string[] scripts) |
39 |
| - { |
40 |
| - // Enable undo functionality as well as mock recording |
41 |
| - using (UndoContext context = UndoContext.Current) |
42 |
| - { |
43 |
| - // Configure recordings |
44 |
| - context.Start(TestUtilities.GetCallingClass(2), TestUtilities.GetCurrentMethodName(2)); |
45 | 33 |
|
46 |
| - SetupManagementClients(); |
| 34 | + private readonly EnvironmentSetupHelper helper; |
| 35 | + |
| 36 | + |
| 37 | + public ResourceManagementClient ResourceManagementClient { get; private set; } |
| 38 | + |
| 39 | + |
| 40 | + public SubscriptionClient SubscriptionClient { get; private set; } |
| 41 | + |
| 42 | + |
| 43 | + public GalleryClient GalleryClient { get; private set; } |
| 44 | + |
| 45 | + |
| 46 | + public AuthorizationManagementClient AuthorizationManagementClient { get; private set; } |
| 47 | + |
| 48 | + |
| 49 | + public DnsManagementClient DnsClient { get; private set; } |
| 50 | + |
| 51 | + |
| 52 | + public static DnsTestsBase NewInstance |
| 53 | + { |
| 54 | + get |
| 55 | + { |
| 56 | + return new DnsTestsBase(); |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + |
| 61 | + protected DnsTestsBase() |
| 62 | + { |
| 63 | + this.helper = new EnvironmentSetupHelper(); |
| 64 | + } |
| 65 | + |
| 66 | + |
| 67 | + protected void SetupManagementClients() |
| 68 | + { |
| 69 | + this.ResourceManagementClient = this.GetResourceManagementClient(); |
| 70 | + this.SubscriptionClient = this.GetSubscriptionClient(); |
| 71 | + this.GalleryClient = this.GetGalleryClient(); |
| 72 | + this.AuthorizationManagementClient = this.GetAuthorizationManagementClient(); |
| 73 | + this.DnsClient = this.GetFeatureClient(); |
| 74 | + |
| 75 | + |
| 76 | + this.helper.SetupManagementClients( |
| 77 | + this.ResourceManagementClient, |
| 78 | + this.SubscriptionClient, |
| 79 | + this.GalleryClient, |
| 80 | + this.AuthorizationManagementClient, |
| 81 | + this.DnsClient); |
| 82 | + } |
| 83 | + |
| 84 | + |
| 85 | + public void RunPowerShellTest(params string[] scripts) |
| 86 | + { |
| 87 | + string callingClassType = TestUtilities.GetCallingClass(2); |
| 88 | + string mockName = TestUtilities.GetCurrentMethodName(2); |
| 89 | + |
47 | 90 |
|
48 |
| - helper.SetupEnvironment(AzureModule.AzureResourceManager); |
| 91 | + this.RunPsTestWorkflow( |
| 92 | + () => scripts, |
| 93 | + // no custom initializer |
| 94 | + null, |
| 95 | + // no custom cleanup |
| 96 | + null, |
| 97 | + callingClassType, |
| 98 | + mockName); |
| 99 | + } |
49 | 100 |
|
50 |
| - helper.SetupModules(AzureModule.AzureProfile, "ScenarioTests\\Common.ps1", |
51 |
| - "ScenarioTests\\" + this.GetType().Name + ".ps1"); |
52 | 101 |
|
53 |
| - helper.RunPowerShellTest(scripts); |
54 |
| - } |
| 102 | + public void RunPsTestWorkflow( |
| 103 | + Func<string[]> scriptBuilder, |
| 104 | + Action<CSMTestEnvironmentFactory> initialize, |
| 105 | + Action cleanup, |
| 106 | + string callingClassType, |
| 107 | + string mockName) |
| 108 | + { |
| 109 | + using (UndoContext context = UndoContext.Current) |
| 110 | + { |
| 111 | + context.Start(callingClassType, mockName); |
| 112 | + |
| 113 | + |
| 114 | + this.csmTestFactory = new CSMTestEnvironmentFactory(); |
| 115 | + |
| 116 | + |
| 117 | + if (initialize != null) |
| 118 | + { |
| 119 | + initialize(this.csmTestFactory); |
| 120 | + } |
| 121 | + |
| 122 | + |
| 123 | + this.SetupManagementClients(); |
| 124 | + |
| 125 | + |
| 126 | + this.helper.SetupEnvironment(AzureModule.AzureResourceManager); |
| 127 | + |
| 128 | + |
| 129 | + string callingClassName = callingClassType |
| 130 | + .Split(new[] { "." }, StringSplitOptions.RemoveEmptyEntries) |
| 131 | + .Last(); |
| 132 | + |
| 133 | + |
| 134 | + this.helper.SetupModules( |
| 135 | + AzureModule.AzureResourceManager, |
| 136 | + "ScenarioTests\\Common.ps1", |
| 137 | + "ScenarioTests\\" + callingClassName + ".ps1"); |
| 138 | + |
| 139 | + |
| 140 | + try |
| 141 | + { |
| 142 | + if (scriptBuilder != null) |
| 143 | + { |
| 144 | + string[] psScripts = scriptBuilder(); |
| 145 | + |
| 146 | + |
| 147 | + if (psScripts != null) |
| 148 | + { |
| 149 | + this.helper.RunPowerShellTest(psScripts); |
| 150 | + } |
| 151 | + } |
| 152 | + } |
| 153 | + finally |
| 154 | + { |
| 155 | + if (cleanup != null) |
| 156 | + { |
| 157 | + cleanup(); |
| 158 | + } |
| 159 | + } |
| 160 | + } |
| 161 | + } |
| 162 | + |
| 163 | + |
| 164 | + protected ResourceManagementClient GetResourceManagementClient() |
| 165 | + { |
| 166 | + return TestBase.GetServiceClient<ResourceManagementClient>(this.csmTestFactory); |
| 167 | + } |
| 168 | + |
| 169 | + |
| 170 | + private AuthorizationManagementClient GetAuthorizationManagementClient() |
| 171 | + { |
| 172 | + return TestBase.GetServiceClient<AuthorizationManagementClient>(this.csmTestFactory); |
| 173 | + } |
| 174 | + |
| 175 | + |
| 176 | + private SubscriptionClient GetSubscriptionClient() |
| 177 | + { |
| 178 | + return TestBase.GetServiceClient<SubscriptionClient>(this.csmTestFactory); |
| 179 | + } |
| 180 | + |
| 181 | + |
| 182 | + private GalleryClient GetGalleryClient() |
| 183 | + { |
| 184 | + return TestBase.GetServiceClient<GalleryClient>(this.csmTestFactory); |
55 | 185 | }
|
56 | 186 |
|
57 |
| - protected ResourceManagementClient GetResourcesClient() |
| 187 | + |
| 188 | + private DnsManagementClient GetFeatureClient() |
58 | 189 | {
|
59 |
| - return TestBase.GetServiceClient<ResourceManagementClient>(new CSMTestEnvironmentFactory()); |
60 |
| - } |
61 |
| - } |
62 |
| -} |
| 190 | + return TestBase.GetServiceClient<DnsManagementClient>(this.csmTestFactory); |
| 191 | + } |
| 192 | + } |
| 193 | +} |
0 commit comments