|
| 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 System.IO; |
| 17 | +using System.Collections.Generic; |
| 18 | +using System.Linq; |
| 19 | +using System.Net.Http; |
| 20 | +using System.Net.Http.Headers; |
| 21 | +using LegacyTest = Microsoft.Azure.Test; |
| 22 | +using Microsoft.Azure.Commands.Common.Authentication; |
| 23 | +using Microsoft.Azure.Commands.ResourceManager.Cmdlets.Components; |
| 24 | +using Microsoft.Azure.Commands.ResourceManager.Cmdlets.Extensions; |
| 25 | +using Microsoft.Azure.Gallery; |
| 26 | +using Microsoft.Azure.Management.Authorization; |
| 27 | +using Microsoft.Azure.Management.Resources; |
| 28 | +using Microsoft.Azure.Management.Storage; |
| 29 | +using Microsoft.Azure.ServiceManagemenet.Common.Models; |
| 30 | +using Microsoft.Azure.Subscriptions; |
| 31 | +using Microsoft.Azure.Test.Authentication; |
| 32 | +using Microsoft.Azure.Test.HttpRecorder; |
| 33 | +using Microsoft.Rest; |
| 34 | +using Microsoft.Rest.ClientRuntime.Azure.TestFramework; |
| 35 | +using Microsoft.WindowsAzure.Commands.ScenarioTest; |
| 36 | + |
| 37 | +namespace Microsoft.Azure.Commands.MachineLearning.Test.ScenarioTests |
| 38 | +{ |
| 39 | + public abstract class BaseTestController<T> where T : ServiceClient<T> |
| 40 | + { |
| 41 | + private readonly EnvironmentSetupHelper helper; |
| 42 | + private LegacyTest.CSMTestEnvironmentFactory csmTestFactory; |
| 43 | + private T serviceClient; |
| 44 | + private ResourceManagementClient resourceManagementClient; |
| 45 | + private StorageManagementClient storageManagementClient; |
| 46 | + |
| 47 | + protected BaseTestController() |
| 48 | + { |
| 49 | + this.helper = new EnvironmentSetupHelper(); |
| 50 | + } |
| 51 | + |
| 52 | + protected abstract T ConstructServiceClient(MockContext context); |
| 53 | + |
| 54 | + public void RunPsTest(XunitTracingInterceptor logger, params string[] scripts) |
| 55 | + { |
| 56 | + var callingClassType = TestUtilities.GetCallingClass(2); |
| 57 | + var mockName = TestUtilities.GetCurrentMethodName(2); |
| 58 | + this.helper.TracingInterceptor = logger; |
| 59 | + |
| 60 | + this.RunPsTestWorkflow( |
| 61 | + () => scripts, |
| 62 | + // no custom initializer |
| 63 | + null, |
| 64 | + // no custom cleanup |
| 65 | + null, |
| 66 | + callingClassType, |
| 67 | + mockName); |
| 68 | + } |
| 69 | + |
| 70 | + public void RunPsTestWorkflow( |
| 71 | + Func<string[]> scriptBuilder, |
| 72 | + Action<LegacyTest.CSMTestEnvironmentFactory> initialize, |
| 73 | + Action cleanup, |
| 74 | + string callingClassType, |
| 75 | + string mockName) |
| 76 | + { |
| 77 | + var providers = new Dictionary<string, string> |
| 78 | + { |
| 79 | + { "Microsoft.Resources", null }, |
| 80 | + { "Microsoft.Features", null }, |
| 81 | + { "Microsoft.Authorization", null } |
| 82 | + }; |
| 83 | + var providersToIgnore = new Dictionary<string, string> |
| 84 | + { |
| 85 | + {"Microsoft.Azure.Management.Resources.ResourceManagementClient", "2016-02-01"} |
| 86 | + }; |
| 87 | + HttpMockServer.Matcher = new PermissiveRecordMatcherWithApiExclusion(true, providers, providersToIgnore); |
| 88 | + |
| 89 | + HttpMockServer.RecordsDirectory = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "SessionRecords"); |
| 90 | + using (var context = MockContext.Start(callingClassType, mockName)) |
| 91 | + { |
| 92 | + this.csmTestFactory = new LegacyTest.CSMTestEnvironmentFactory(); |
| 93 | + if (initialize != null) |
| 94 | + { |
| 95 | + initialize(this.csmTestFactory); |
| 96 | + } |
| 97 | + |
| 98 | + this.SetupManagementClients(context); |
| 99 | + helper.SetupEnvironment(AzureModule.AzureResourceManager); |
| 100 | + |
| 101 | + var callingClassName = callingClassType |
| 102 | + .Split(new[] { "." }, StringSplitOptions.RemoveEmptyEntries) |
| 103 | + .Last(); |
| 104 | + |
| 105 | + helper.SetupModules(AzureModule.AzureResourceManager, |
| 106 | + "ScenarioTests\\Common.ps1", |
| 107 | + "ScenarioTests\\" + callingClassName + ".ps1", |
| 108 | + helper.RMProfileModule, |
| 109 | + helper.RMResourceModule, |
| 110 | + helper.GetRMModulePath(@"AzureRM.MachineLearning.psd1"), |
| 111 | + "AzureRM.Storage.ps1", |
| 112 | + "AzureRM.Resources.ps1"); |
| 113 | + |
| 114 | + try |
| 115 | + { |
| 116 | + if (scriptBuilder != null) |
| 117 | + { |
| 118 | + var psScripts = scriptBuilder(); |
| 119 | + |
| 120 | + if (psScripts != null) |
| 121 | + { |
| 122 | + helper.RunPowerShellTest(psScripts); |
| 123 | + } |
| 124 | + } |
| 125 | + } |
| 126 | + finally |
| 127 | + { |
| 128 | + if (cleanup != null) |
| 129 | + { |
| 130 | + cleanup(); |
| 131 | + } |
| 132 | + } |
| 133 | + } |
| 134 | + } |
| 135 | + |
| 136 | + private void SetupManagementClients(MockContext context) |
| 137 | + { |
| 138 | + this.serviceClient = this.ConstructServiceClient(context); |
| 139 | + |
| 140 | + this.resourceManagementClient = LegacyTest.TestBase.GetServiceClient<ResourceManagementClient>(this.csmTestFactory); |
| 141 | + this.storageManagementClient = LegacyTest.TestBase.GetServiceClient<StorageManagementClient>(this.csmTestFactory); |
| 142 | + var subscriptionClient = LegacyTest.TestBase.GetServiceClient<SubscriptionClient>(this.csmTestFactory); |
| 143 | + var authManagementClient = LegacyTest.TestBase.GetServiceClient<AuthorizationManagementClient>(this.csmTestFactory); |
| 144 | + var gallleryClient = LegacyTest.TestBase.GetServiceClient<GalleryClient>(this.csmTestFactory); |
| 145 | + |
| 146 | + var testEnvironment = this.csmTestFactory.GetTestEnvironment(); |
| 147 | + |
| 148 | + var credentials = new SubscriptionCredentialsAdapter( |
| 149 | + testEnvironment.AuthorizationContext.TokenCredentials[Microsoft.Azure.Test.TokenAudience.Management], |
| 150 | + testEnvironment.SubscriptionId); |
| 151 | + |
| 152 | + HttpClientHelperFactory.Instance = new TestHttpClientHelperFactory(credentials); |
| 153 | + |
| 154 | + helper.SetupManagementClients( |
| 155 | + this.resourceManagementClient, |
| 156 | + subscriptionClient, |
| 157 | + this.serviceClient, |
| 158 | + authManagementClient, |
| 159 | + gallleryClient, |
| 160 | + this.storageManagementClient); |
| 161 | + } |
| 162 | + |
| 163 | + /// <summary> |
| 164 | + /// The test http client helper factory. |
| 165 | + /// </summary> |
| 166 | + private class TestHttpClientHelperFactory : HttpClientHelperFactory |
| 167 | + { |
| 168 | + /// <summary> |
| 169 | + /// The subscription cloud credentials. |
| 170 | + /// </summary> |
| 171 | + private readonly SubscriptionCloudCredentials credential; |
| 172 | + |
| 173 | + /// <summary> |
| 174 | + /// Initializes a new instance of the <see cref="TestHttpClientHelperFactory"/> class. |
| 175 | + /// </summary> |
| 176 | + /// <param name="credentials"></param> |
| 177 | + public TestHttpClientHelperFactory(SubscriptionCloudCredentials credentials) |
| 178 | + { |
| 179 | + this.credential = credentials; |
| 180 | + } |
| 181 | + |
| 182 | + /// <summary> |
| 183 | + /// Creates new instances of the <see cref="HttpClientHelper"/> class. |
| 184 | + /// </summary> |
| 185 | + /// <param name="credentials">The credentials.</param> |
| 186 | + /// <param name="headerValues">The headers.</param> |
| 187 | + public override HttpClientHelper CreateHttpClientHelper( |
| 188 | + SubscriptionCloudCredentials credentials, |
| 189 | + IEnumerable<ProductInfoHeaderValue> headerValues, |
| 190 | + Dictionary<string, string> cmdletHeaderValues) |
| 191 | + { |
| 192 | + return new HttpClientHelperImpl( |
| 193 | + credentials: this.credential, |
| 194 | + headerValues: headerValues, |
| 195 | + cmdletHeaderValues: cmdletHeaderValues); |
| 196 | + } |
| 197 | + |
| 198 | + /// <summary> |
| 199 | + /// An implementation of the <see cref="HttpClientHelper"/> abstract class. |
| 200 | + /// </summary> |
| 201 | + private class HttpClientHelperImpl : HttpClientHelper |
| 202 | + { |
| 203 | + /// <summary> |
| 204 | + /// Initializes new instances of the <see cref="HttpClientHelperImpl"/> class. |
| 205 | + /// </summary> |
| 206 | + /// <param name="credentials">The credentials.</param> |
| 207 | + /// <param name="headerValues">The headers.</param> |
| 208 | + public HttpClientHelperImpl( |
| 209 | + SubscriptionCloudCredentials credentials, |
| 210 | + IEnumerable<ProductInfoHeaderValue> headerValues, |
| 211 | + Dictionary<string, string> cmdletHeaderValues) |
| 212 | + : base( |
| 213 | + credentials: credentials, |
| 214 | + headerValues: headerValues, |
| 215 | + cmdletHeaderValues: cmdletHeaderValues) |
| 216 | + { |
| 217 | + } |
| 218 | + |
| 219 | + /// <summary> |
| 220 | + /// Creates an <see cref="HttpClient"/> |
| 221 | + /// </summary> |
| 222 | + /// <param name="primaryHandlers">The handlers that will be added to the top of the chain.</param> |
| 223 | + public override HttpClient CreateHttpClient(params DelegatingHandler[] primaryHandlers) |
| 224 | + { |
| 225 | + return base.CreateHttpClient(HttpMockServer.CreateInstance() |
| 226 | + .AsArray() |
| 227 | + .Concat(primaryHandlers) |
| 228 | + .ToArray()); |
| 229 | + } |
| 230 | + } |
| 231 | + } |
| 232 | + } |
| 233 | +} |
0 commit comments