PageRenderTime 27ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 1ms

/src/ResourceManager/Profile/Commands.Profile.Test/EnvironmentCmdletTests.cs

https://gitlab.com/jslee1/azure-powershell
C# | 368 lines | 312 code | 42 blank | 14 comment | 0 complexity | 650748ebdf7ec2f126bd6860ee555780 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.Commands.Common.Authentication.Models;
  16. using Microsoft.Azure.Commands.Profile;
  17. using Microsoft.Azure.Commands.Profile.Models;
  18. using Microsoft.Azure.Commands.ResourceManager.Common;
  19. using Microsoft.Azure.ServiceManagemenet.Common.Models;
  20. using Microsoft.WindowsAzure.Commands.Common;
  21. using Microsoft.WindowsAzure.Commands.ScenarioTest;
  22. using Microsoft.WindowsAzure.Commands.Test.Utilities.Common;
  23. using Microsoft.WindowsAzure.Commands.Utilities.Common;
  24. using Moq;
  25. using System;
  26. using System.Collections.Generic;
  27. using System.Management.Automation;
  28. using Xunit;
  29. using Xunit.Abstractions;
  30. namespace Microsoft.Azure.Commands.ResourceManager.Profile.Test
  31. {
  32. public class EnvironmentCmdletTests : RMTestBase
  33. {
  34. private MemoryDataStore dataStore;
  35. public EnvironmentCmdletTests(ITestOutputHelper output)
  36. {
  37. XunitTracingInterceptor.AddToContext(new XunitTracingInterceptor(output));
  38. dataStore = new MemoryDataStore();
  39. AzureSession.DataStore = dataStore;
  40. }
  41. public void Cleanup()
  42. {
  43. currentProfile = null;
  44. }
  45. [Fact]
  46. [Trait(Category.AcceptanceType, Category.CheckIn)]
  47. public void AddsAzureEnvironment()
  48. {
  49. Mock<ICommandRuntime> commandRuntimeMock = new Mock<ICommandRuntime>();
  50. var cmdlet = new AddAzureRMEnvironmentCommand()
  51. {
  52. CommandRuntime = commandRuntimeMock.Object,
  53. Name = "Katal",
  54. PublishSettingsFileUrl = "http://microsoft.com",
  55. ServiceEndpoint = "endpoint.net",
  56. ManagementPortalUrl = "management portal url",
  57. StorageEndpoint = "endpoint.net",
  58. GalleryEndpoint = "http://galleryendpoint.com",
  59. };
  60. cmdlet.InvokeBeginProcessing();
  61. cmdlet.ExecuteCmdlet();
  62. cmdlet.InvokeEndProcessing();
  63. commandRuntimeMock.Verify(f => f.WriteObject(It.IsAny<PSAzureEnvironment>()), Times.Once());
  64. var profileClient = new RMProfileClient(AzureRmProfileProvider.Instance.Profile);
  65. AzureEnvironment env = AzureRmProfileProvider.Instance.Profile.Environments["KaTaL"];
  66. Assert.Equal(env.Name, cmdlet.Name);
  67. Assert.Equal(env.Endpoints[AzureEnvironment.Endpoint.PublishSettingsFileUrl], cmdlet.PublishSettingsFileUrl);
  68. Assert.Equal(env.Endpoints[AzureEnvironment.Endpoint.ServiceManagement], cmdlet.ServiceEndpoint);
  69. Assert.Equal(env.Endpoints[AzureEnvironment.Endpoint.ManagementPortalUrl], cmdlet.ManagementPortalUrl);
  70. Assert.Equal(env.Endpoints[AzureEnvironment.Endpoint.Gallery], "http://galleryendpoint.com");
  71. }
  72. [Fact]
  73. [Trait(Category.AcceptanceType, Category.CheckIn)]
  74. public void AddsEnvironmentWithMinimumInformation()
  75. {
  76. Mock<ICommandRuntime> commandRuntimeMock = new Mock<ICommandRuntime>();
  77. var cmdlet = new AddAzureRMEnvironmentCommand()
  78. {
  79. CommandRuntime = commandRuntimeMock.Object,
  80. Name = "Katal",
  81. PublishSettingsFileUrl = "http://microsoft.com",
  82. EnableAdfsAuthentication = true,
  83. };
  84. cmdlet.InvokeBeginProcessing();
  85. cmdlet.ExecuteCmdlet();
  86. cmdlet.InvokeEndProcessing();
  87. commandRuntimeMock.Verify(f => f.WriteObject(It.IsAny<PSAzureEnvironment>()), Times.Once());
  88. AzureEnvironment env = AzureRmProfileProvider.Instance.Profile.Environments["KaTaL"];
  89. Assert.Equal(env.Name, cmdlet.Name);
  90. Assert.True(env.OnPremise);
  91. Assert.Equal(env.Endpoints[AzureEnvironment.Endpoint.PublishSettingsFileUrl], cmdlet.PublishSettingsFileUrl);
  92. }
  93. [Fact]
  94. [Trait(Category.AcceptanceType, Category.CheckIn)]
  95. public void IgnoresAddingDuplicatedEnvironment()
  96. {
  97. var profile = new AzureSMProfile();
  98. var commandRuntimeMock = new Mock<ICommandRuntime>();
  99. var cmdlet = new AddAzureRMEnvironmentCommand()
  100. {
  101. CommandRuntime = commandRuntimeMock.Object,
  102. Name = "Katal",
  103. PublishSettingsFileUrl = "http://microsoft.com",
  104. ServiceEndpoint = "endpoint.net",
  105. ManagementPortalUrl = "management portal url",
  106. StorageEndpoint = "endpoint.net"
  107. };
  108. cmdlet.InvokeBeginProcessing();
  109. cmdlet.ExecuteCmdlet();
  110. cmdlet.InvokeEndProcessing();
  111. ProfileClient client = new ProfileClient(profile);
  112. int count = client.Profile.Environments.Count;
  113. // Add again
  114. cmdlet.Name = "kAtAl";
  115. cmdlet.ExecuteCmdlet();
  116. AzureEnvironment env = AzureRmProfileProvider.Instance.Profile.Environments["KaTaL"];
  117. Assert.Equal(env.Name, cmdlet.Name);
  118. }
  119. [Fact]
  120. [Trait(Category.AcceptanceType, Category.CheckIn)]
  121. public void IgnoresAddingPublicEnvironment()
  122. {
  123. var commandRuntimeMock = new Mock<ICommandRuntime>();
  124. var cmdlet = new AddAzureRMEnvironmentCommand()
  125. {
  126. CommandRuntime = commandRuntimeMock.Object,
  127. Name = EnvironmentName.AzureCloud,
  128. PublishSettingsFileUrl = "http://microsoft.com"
  129. };
  130. Assert.Throws<InvalidOperationException>(() => cmdlet.ExecuteCmdlet());
  131. }
  132. [Fact]
  133. [Trait(Category.AcceptanceType, Category.CheckIn)]
  134. public void AddsEnvironmentWithStorageEndpoint()
  135. {
  136. Mock<ICommandRuntime> commandRuntimeMock = new Mock<ICommandRuntime>();
  137. PSAzureEnvironment actual = null;
  138. commandRuntimeMock.Setup(f => f.WriteObject(It.IsAny<object>()))
  139. .Callback((object output) => actual = (PSAzureEnvironment)output);
  140. var cmdlet = new AddAzureRMEnvironmentCommand()
  141. {
  142. CommandRuntime = commandRuntimeMock.Object,
  143. Name = "Katal",
  144. PublishSettingsFileUrl = "http://microsoft.com",
  145. StorageEndpoint = "core.windows.net",
  146. };
  147. cmdlet.InvokeBeginProcessing();
  148. cmdlet.ExecuteCmdlet();
  149. cmdlet.InvokeEndProcessing();
  150. commandRuntimeMock.Verify(f => f.WriteObject(It.IsAny<PSAzureEnvironment>()), Times.Once());
  151. AzureEnvironment env = AzureRmProfileProvider.Instance.Profile.Environments["KaTaL"];
  152. Assert.Equal(env.Name, cmdlet.Name);
  153. Assert.Equal(env.Endpoints[AzureEnvironment.Endpoint.StorageEndpointSuffix], actual.StorageEndpointSuffix);
  154. }
  155. [Fact]
  156. [Trait(Category.AcceptanceType, Category.CheckIn)]
  157. public void CanCreateEnvironmentWithAllProperties()
  158. {
  159. Mock<ICommandRuntime> commandRuntimeMock = new Mock<ICommandRuntime>();
  160. PSAzureEnvironment actual = null;
  161. commandRuntimeMock.Setup(f => f.WriteObject(It.IsAny<PSAzureEnvironment>()))
  162. .Callback((object output) => actual = (PSAzureEnvironment)output);
  163. var cmdlet = new AddAzureRMEnvironmentCommand()
  164. {
  165. CommandRuntime = commandRuntimeMock.Object,
  166. Name = "Katal",
  167. ActiveDirectoryEndpoint = "ActiveDirectoryEndpoint",
  168. AdTenant = "AdTenant",
  169. AzureKeyVaultDnsSuffix = "AzureKeyVaultDnsSuffix",
  170. ActiveDirectoryServiceEndpointResourceId = "ActiveDirectoryServiceEndpointResourceId",
  171. AzureKeyVaultServiceEndpointResourceId = "AzureKeyVaultServiceEndpointResourceId",
  172. EnableAdfsAuthentication = true,
  173. GalleryEndpoint = "GalleryEndpoint",
  174. GraphEndpoint = "GraphEndpoint",
  175. ManagementPortalUrl = "ManagementPortalUrl",
  176. PublishSettingsFileUrl = "PublishSettingsFileUrl",
  177. ResourceManagerEndpoint = "ResourceManagerEndpoint",
  178. ServiceEndpoint = "ServiceEndpoint",
  179. StorageEndpoint = "StorageEndpoint",
  180. SqlDatabaseDnsSuffix = "SqlDatabaseDnsSuffix",
  181. TrafficManagerDnsSuffix = "TrafficManagerDnsSuffix",
  182. GraphAudience = "GaraphAudience"
  183. };
  184. cmdlet.InvokeBeginProcessing();
  185. cmdlet.ExecuteCmdlet();
  186. cmdlet.InvokeEndProcessing();
  187. Assert.Equal(cmdlet.Name, actual.Name);
  188. Assert.Equal(cmdlet.EnableAdfsAuthentication.ToBool(), actual.EnableAdfsAuthentication);
  189. Assert.Equal(cmdlet.ActiveDirectoryEndpoint, actual.ActiveDirectoryAuthority);
  190. Assert.Equal(cmdlet.ActiveDirectoryServiceEndpointResourceId,
  191. actual.ActiveDirectoryServiceEndpointResourceId);
  192. Assert.Equal(cmdlet.AdTenant, actual.AdTenant);
  193. Assert.Equal(cmdlet.AzureKeyVaultDnsSuffix, actual.AzureKeyVaultDnsSuffix);
  194. Assert.Equal(cmdlet.AzureKeyVaultServiceEndpointResourceId, actual.AzureKeyVaultServiceEndpointResourceId);
  195. Assert.Equal(cmdlet.GalleryEndpoint, actual.GalleryUrl);
  196. Assert.Equal(cmdlet.GraphEndpoint, actual.GraphUrl);
  197. Assert.Equal(cmdlet.ManagementPortalUrl, actual.ManagementPortalUrl);
  198. Assert.Equal(cmdlet.PublishSettingsFileUrl, actual.PublishSettingsFileUrl);
  199. Assert.Equal(cmdlet.ResourceManagerEndpoint, actual.ResourceManagerUrl);
  200. Assert.Equal(cmdlet.ServiceEndpoint, actual.ServiceManagementUrl);
  201. Assert.Equal(cmdlet.StorageEndpoint, actual.StorageEndpointSuffix);
  202. Assert.Equal(cmdlet.SqlDatabaseDnsSuffix, actual.SqlDatabaseDnsSuffix);
  203. Assert.Equal(cmdlet.TrafficManagerDnsSuffix, actual.TrafficManagerDnsSuffix);
  204. Assert.Equal(cmdlet.GraphAudience, actual.GraphEndpointResourceId);
  205. commandRuntimeMock.Verify(f => f.WriteObject(It.IsAny<PSAzureEnvironment>()), Times.Once());
  206. AzureEnvironment env = AzureRmProfileProvider.Instance.Profile.Environments["KaTaL"];
  207. Assert.Equal(env.Name, cmdlet.Name);
  208. }
  209. [Fact]
  210. [Trait(Category.AcceptanceType, Category.CheckIn)]
  211. public void GetsAzureEnvironments()
  212. {
  213. List<PSAzureEnvironment> environments = null;
  214. Mock<ICommandRuntime> commandRuntimeMock = new Mock<ICommandRuntime>();
  215. commandRuntimeMock.Setup(c => c.WriteObject(It.IsAny<object>(), It.IsAny<bool>()))
  216. .Callback<object, bool>((e, _) => environments = (List<PSAzureEnvironment>)e);
  217. var cmdlet = new GetAzureRMEnvironmentCommand()
  218. {
  219. CommandRuntime = commandRuntimeMock.Object
  220. };
  221. cmdlet.InvokeBeginProcessing();
  222. cmdlet.ExecuteCmdlet();
  223. cmdlet.InvokeEndProcessing();
  224. Assert.Equal(4, environments.Count);
  225. }
  226. [Fact]
  227. [Trait(Category.AcceptanceType, Category.CheckIn)]
  228. public void GetsAzureEnvironment()
  229. {
  230. List<PSAzureEnvironment> environments = null;
  231. Mock<ICommandRuntime> commandRuntimeMock = new Mock<ICommandRuntime>();
  232. commandRuntimeMock.Setup(c => c.WriteObject(It.IsAny<object>(), It.IsAny<bool>()))
  233. .Callback<object, bool>((e, _) => environments = (List<PSAzureEnvironment>)e);
  234. var cmdlet = new GetAzureRMEnvironmentCommand()
  235. {
  236. CommandRuntime = commandRuntimeMock.Object,
  237. Name = EnvironmentName.AzureChinaCloud
  238. };
  239. cmdlet.InvokeBeginProcessing();
  240. cmdlet.ExecuteCmdlet();
  241. cmdlet.InvokeEndProcessing();
  242. Assert.Equal(1, environments.Count);
  243. }
  244. [Fact]
  245. [Trait(Category.AcceptanceType, Category.CheckIn)]
  246. public void ThrowsWhenSettingPublicEnvironment()
  247. {
  248. Mock<ICommandRuntime> commandRuntimeMock = new Mock<ICommandRuntime>();
  249. foreach (string name in AzureEnvironment.PublicEnvironments.Keys)
  250. {
  251. var cmdlet = new SetAzureRMEnvironmentCommand()
  252. {
  253. CommandRuntime = commandRuntimeMock.Object,
  254. Name = name,
  255. PublishSettingsFileUrl = "http://microsoft.com"
  256. };
  257. var savedValue = AzureEnvironment.PublicEnvironments[name].GetEndpoint(AzureEnvironment.Endpoint.PublishSettingsFileUrl);
  258. cmdlet.InvokeBeginProcessing();
  259. Assert.Throws<InvalidOperationException>(() => cmdlet.ExecuteCmdlet());
  260. var newValue = AzureRmProfileProvider.Instance.Profile.Environments[name].GetEndpoint(AzureEnvironment.Endpoint.PublishSettingsFileUrl);
  261. Assert.Equal(savedValue, newValue);
  262. Assert.NotEqual(cmdlet.PublishSettingsFileUrl, newValue);
  263. }
  264. }
  265. [Fact]
  266. [Trait(Category.AcceptanceType, Category.CheckIn)]
  267. public void RemovesAzureEnvironment()
  268. {
  269. var commandRuntimeMock = new Mock<ICommandRuntime>();
  270. commandRuntimeMock.Setup(f => f.ShouldProcess(It.IsAny<string>(), It.IsAny<string>())).Returns(true);
  271. const string name = "test";
  272. RMProfileClient client = new RMProfileClient(AzureRmProfileProvider.Instance.Profile);
  273. client.AddOrSetEnvironment(new AzureEnvironment
  274. {
  275. Name = name
  276. });
  277. var cmdlet = new RemoveAzureRMEnvironmentCommand()
  278. {
  279. CommandRuntime = commandRuntimeMock.Object,
  280. Force = true,
  281. Name = name
  282. };
  283. cmdlet.InvokeBeginProcessing();
  284. cmdlet.ExecuteCmdlet();
  285. cmdlet.InvokeEndProcessing();
  286. Assert.False(AzureRmProfileProvider.Instance.Profile.Environments.ContainsKey(name));
  287. }
  288. [Fact]
  289. [Trait(Category.AcceptanceType, Category.CheckIn)]
  290. public void ThrowsForUnknownEnvironment()
  291. {
  292. Mock<ICommandRuntime> commandRuntimeMock = new Mock<ICommandRuntime>();
  293. commandRuntimeMock.Setup(f => f.ShouldProcess(It.IsAny<string>(), It.IsAny<string>())).Returns(true);
  294. var cmdlet = new RemoveAzureRMEnvironmentCommand()
  295. {
  296. CommandRuntime = commandRuntimeMock.Object,
  297. Name = "test2",
  298. Force = true
  299. };
  300. cmdlet.InvokeBeginProcessing();
  301. Assert.Throws<ArgumentException>(() => cmdlet.ExecuteCmdlet());
  302. }
  303. [Fact]
  304. [Trait(Category.AcceptanceType, Category.CheckIn)]
  305. public void ThrowsForPublicEnvironment()
  306. {
  307. Mock<ICommandRuntime> commandRuntimeMock = new Mock<ICommandRuntime>();
  308. commandRuntimeMock.Setup(f => f.ShouldProcess(It.IsAny<string>(), It.IsAny<string>())).Returns(true);
  309. foreach (string name in AzureEnvironment.PublicEnvironments.Keys)
  310. {
  311. var cmdlet = new RemoveAzureRMEnvironmentCommand()
  312. {
  313. CommandRuntime = commandRuntimeMock.Object,
  314. Force = true,
  315. Name = name
  316. };
  317. cmdlet.InvokeBeginProcessing();
  318. Assert.Throws<ArgumentException>(() => cmdlet.ExecuteCmdlet());
  319. }
  320. }
  321. public void Dispose()
  322. {
  323. Cleanup();
  324. }
  325. }
  326. }