PageRenderTime 54ms CodeModel.GetById 26ms RepoModel.GetById 1ms app.codeStats 0ms

/src/ResourceManager/SiteRecovery/Commands.SiteRecovery.Test/ScenarioTests/SiteRecoveryTestsBase.cs

https://gitlab.com/jslee1/azure-powershell
C# | 277 lines | 251 code | 13 blank | 13 comment | 1 complexity | ce093972a5ba526f60f37b911273f154 MD5 | raw file
  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. using Microsoft.Azure.Commands.Common.Authentication;
  15. using Microsoft.Azure.Management.SiteRecovery;
  16. using Microsoft.Azure.Management.SiteRecoveryVault;
  17. using Microsoft.Azure.Portal.RecoveryServices.Models.Common;
  18. using Microsoft.Azure.Test;
  19. using Microsoft.Azure.Test.HttpRecorder;
  20. using Microsoft.WindowsAzure.Commands.ScenarioTest;
  21. using Microsoft.WindowsAzure.Commands.Test.Utilities.Common;
  22. using System;
  23. using System.Collections.Generic;
  24. using System.IO;
  25. using System.Net;
  26. using System.Net.Http;
  27. using System.Net.Security;
  28. using System.Reflection;
  29. using System.Runtime.Serialization;
  30. using System.Xml;
  31. namespace Microsoft.Azure.Commands.SiteRecovery.Test.ScenarioTests
  32. {
  33. public abstract class SiteRecoveryTestsBase : RMTestBase
  34. {
  35. private CSMTestEnvironmentFactory armTestFactory;
  36. private EnvironmentSetupHelper helper;
  37. protected string vaultSettingsFilePath;
  38. private ASRVaultCreds asrVaultCreds = null;
  39. public SiteRecoveryManagementClient SiteRecoveryMgmtClient { get; private set; }
  40. public SiteRecoveryVaultManagementClient RecoveryServicesMgmtClient { get; private set; }
  41. protected SiteRecoveryTestsBase()
  42. {
  43. this.vaultSettingsFilePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "ScenarioTests\\vaultSettings.VaultCredentials");
  44. if (File.Exists(this.vaultSettingsFilePath))
  45. {
  46. try
  47. {
  48. var serializer1 = new DataContractSerializer(typeof(ASRVaultCreds));
  49. using (var s = new FileStream(
  50. this.vaultSettingsFilePath,
  51. FileMode.Open,
  52. FileAccess.Read,
  53. FileShare.Read))
  54. {
  55. asrVaultCreds = (ASRVaultCreds)serializer1.ReadObject(s);
  56. }
  57. }
  58. catch (XmlException xmlException)
  59. {
  60. throw new XmlException(
  61. "XML is malformed or file is empty", xmlException);
  62. }
  63. catch (SerializationException serializationException)
  64. {
  65. throw new SerializationException(
  66. "XML is malformed or file is empty", serializationException);
  67. }
  68. }
  69. else
  70. {
  71. throw new FileNotFoundException(
  72. string.Format(
  73. "Vault settings file not found at '{0}', please pass the file downloaded from portal",
  74. this.vaultSettingsFilePath));
  75. }
  76. helper = new EnvironmentSetupHelper();
  77. }
  78. protected void SetupManagementClients()
  79. {
  80. RecoveryServicesMgmtClient = GetSiteRecoveryVaultManagementClient();
  81. SiteRecoveryMgmtClient = GetSiteRecoveryManagementClient();
  82. helper.SetupManagementClients(RecoveryServicesMgmtClient, SiteRecoveryMgmtClient);
  83. }
  84. protected void RunPowerShellTest(params string[] scripts)
  85. {
  86. Dictionary<string, string> d = new Dictionary<string, string>();
  87. d.Add("Microsoft.Resources", null);
  88. d.Add("Microsoft.Features", null);
  89. d.Add("Microsoft.Authorization", null);
  90. var providersToIgnore = new Dictionary<string, string>();
  91. providersToIgnore.Add("Microsoft.Azure.Management.Resources.ResourceManagementClient", "2016-02-01");
  92. HttpMockServer.Matcher = new PermissiveRecordMatcherWithApiExclusion(true, d, providersToIgnore);
  93. using (UndoContext context = UndoContext.Current)
  94. {
  95. context.Start(TestUtilities.GetCallingClass(2), TestUtilities.GetCurrentMethodName(2));
  96. this.armTestFactory = new CSMTestEnvironmentFactory();
  97. SetupManagementClients();
  98. helper.SetupEnvironment(AzureModule.AzureResourceManager);
  99. helper.SetupModules(AzureModule.AzureResourceManager,
  100. "ScenarioTests\\" + this.GetType().Name + ".ps1",
  101. helper.RMProfileModule,
  102. helper.GetRMModulePath(@"AzureRM.SiteRecovery.psd1"));
  103. helper.RunPowerShellTest(scripts);
  104. }
  105. }
  106. private SiteRecoveryVaultManagementClient GetSiteRecoveryVaultManagementClient()
  107. {
  108. return GetServiceClient<SiteRecoveryVaultManagementClient>();
  109. }
  110. private SiteRecoveryManagementClient GetSiteRecoveryManagementClient()
  111. {
  112. return GetServiceClient<SiteRecoveryManagementClient>();
  113. }
  114. public T GetServiceClient<T>() where T : class
  115. {
  116. var factory = (TestEnvironmentFactory)new CSMTestEnvironmentFactory();
  117. var testEnvironment = factory.GetTestEnvironment();
  118. ServicePointManager.ServerCertificateValidationCallback = IgnoreCertificateErrorHandler;
  119. if (typeof(T) == typeof(SiteRecoveryVaultManagementClient))
  120. {
  121. SiteRecoveryVaultManagementClient client;
  122. if (testEnvironment.UsesCustomUri())
  123. {
  124. client = new SiteRecoveryVaultManagementClient(
  125. "Microsoft.SiteRecoveryBVTD2",
  126. "SiteRecoveryVault",
  127. testEnvironment.Credentials as SubscriptionCloudCredentials,
  128. testEnvironment.BaseUri);
  129. }
  130. else
  131. {
  132. client = new SiteRecoveryVaultManagementClient(
  133. "Microsoft.SiteRecovery",
  134. "SiteRecoveryVault",
  135. testEnvironment.Credentials as SubscriptionCloudCredentials);
  136. }
  137. return GetRSMServiceClient<T>(factory, client);
  138. }
  139. else
  140. {
  141. SiteRecoveryManagementClient client;
  142. if (testEnvironment.UsesCustomUri())
  143. {
  144. client = new SiteRecoveryManagementClient(
  145. asrVaultCreds.ResourceName,
  146. asrVaultCreds.ResourceGroupName,
  147. "Microsoft.SiteRecoveryBVTD2",
  148. "SiteRecoveryVault",
  149. testEnvironment.Credentials as SubscriptionCloudCredentials,
  150. testEnvironment.BaseUri);
  151. }
  152. else
  153. {
  154. client = new SiteRecoveryManagementClient(
  155. asrVaultCreds.ResourceName,
  156. asrVaultCreds.ResourceGroupName,
  157. "Microsoft.SiteRecovery",
  158. "vaults",
  159. testEnvironment.Credentials as SubscriptionCloudCredentials);
  160. }
  161. return GetSRMServiceClient<T>(factory, client);
  162. }
  163. }
  164. public static T GetRSMServiceClient<T>(TestEnvironmentFactory factory, SiteRecoveryVaultManagementClient client) where T : class
  165. {
  166. TestEnvironment testEnvironment = factory.GetTestEnvironment();
  167. HttpMockServer instance;
  168. try
  169. {
  170. instance = HttpMockServer.CreateInstance();
  171. }
  172. catch (ApplicationException)
  173. {
  174. HttpMockServer.Initialize("TestEnvironment", "InitialCreation");
  175. instance = HttpMockServer.CreateInstance();
  176. }
  177. T obj2 = typeof(T).GetMethod("WithHandler", new Type[1]
  178. {
  179. typeof (DelegatingHandler)
  180. }).Invoke((object)client, new object[1]
  181. {
  182. (object) instance
  183. }) as T;
  184. if (HttpMockServer.Mode == HttpRecorderMode.Record)
  185. {
  186. HttpMockServer.Variables[TestEnvironment.SubscriptionIdKey] = testEnvironment.SubscriptionId;
  187. }
  188. if (HttpMockServer.Mode == HttpRecorderMode.Playback)
  189. {
  190. PropertyInfo property1 = typeof(T).GetProperty("LongRunningOperationInitialTimeout", typeof(int));
  191. PropertyInfo property2 = typeof(T).GetProperty("LongRunningOperationRetryTimeout", typeof(int));
  192. if (property1 != (PropertyInfo)null && property2 != (PropertyInfo)null)
  193. {
  194. property1.SetValue((object)obj2, (object)0);
  195. property2.SetValue((object)obj2, (object)0);
  196. }
  197. }
  198. return obj2;
  199. }
  200. public static T GetSRMServiceClient<T>(TestEnvironmentFactory factory, SiteRecoveryManagementClient client) where T : class
  201. {
  202. TestEnvironment testEnvironment = factory.GetTestEnvironment();
  203. HttpMockServer instance;
  204. try
  205. {
  206. instance = HttpMockServer.CreateInstance();
  207. }
  208. catch (ApplicationException)
  209. {
  210. HttpMockServer.Initialize("TestEnvironment", "InitialCreation");
  211. instance = HttpMockServer.CreateInstance();
  212. }
  213. T obj2 = typeof(T).GetMethod("WithHandler", new Type[1]
  214. {
  215. typeof (DelegatingHandler)
  216. }).Invoke((object)client, new object[1]
  217. {
  218. (object) instance
  219. }) as T;
  220. if (HttpMockServer.Mode == HttpRecorderMode.Record)
  221. {
  222. HttpMockServer.Variables[TestEnvironment.SubscriptionIdKey] = testEnvironment.SubscriptionId;
  223. }
  224. if (HttpMockServer.Mode == HttpRecorderMode.Playback)
  225. {
  226. PropertyInfo property1 = typeof(T).GetProperty("LongRunningOperationInitialTimeout", typeof(int));
  227. PropertyInfo property2 = typeof(T).GetProperty("LongRunningOperationRetryTimeout", typeof(int));
  228. if (property1 != (PropertyInfo)null && property2 != (PropertyInfo)null)
  229. {
  230. property1.SetValue((object)obj2, (object)0);
  231. property2.SetValue((object)obj2, (object)0);
  232. }
  233. }
  234. return obj2;
  235. }
  236. private static bool IgnoreCertificateErrorHandler
  237. (object sender,
  238. System.Security.Cryptography.X509Certificates.X509Certificate certificate,
  239. System.Security.Cryptography.X509Certificates.X509Chain chain,
  240. SslPolicyErrors sslPolicyErrors)
  241. {
  242. return true;
  243. }
  244. }
  245. }