PageRenderTime 36ms CodeModel.GetById 1ms RepoModel.GetById 0ms app.codeStats 0ms

/src/ServiceManagement/Network/Commands.Network.Test/ReservedIP/ReservedIPTest.cs

https://gitlab.com/jslee1/azure-powershell
C# | 405 lines | 324 code | 52 blank | 29 comment | 16 complexity | 61dfa7056d488745e15ae6f5cc8cfe1b 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. namespace Microsoft.WindowsAzure.Commands.ServiceManagement.Network.Test.ReservedIP
  15. {
  16. using Microsoft.Azure;
  17. using Microsoft.WindowsAzure.Commands.Common.Test.Mocks;
  18. using Microsoft.WindowsAzure.Commands.ScenarioTest;
  19. using Microsoft.WindowsAzure.Commands.ServiceManagement.IaaS;
  20. using Microsoft.WindowsAzure.Commands.ServiceManagement.Network.Test.TestInterfaces;
  21. using Microsoft.WindowsAzure.Commands.Utilities.Common;
  22. using Microsoft.WindowsAzure.Management;
  23. using Microsoft.WindowsAzure.Management.Compute;
  24. using Microsoft.WindowsAzure.Management.Compute.Models;
  25. using Microsoft.WindowsAzure.Management.Network;
  26. using Microsoft.WindowsAzure.Management.Network.Models;
  27. using Microsoft.WindowsAzure.Management.Storage;
  28. using Moq;
  29. using System.Threading;
  30. using System.Threading.Tasks;
  31. using Xunit;
  32. using Assert = Xunit.Assert;
  33. public class ReservedIPTest
  34. {
  35. private MockCommandRuntime mockCommandRuntime;
  36. public const string ServiceName = "ServiceName";
  37. public const string DeploymentName = "DeploymentName";
  38. public const string ReservedIPName = "ReservedIPName";
  39. public const string ReservedIPLocation = "West US";
  40. public const string ReservedIPLabel = "SomeLabel";
  41. public const string VipName = "VipName";
  42. private Mock<NetworkManagementClient> networkingClientMock;
  43. private Mock<ComputeManagementClient> computeClientMock;
  44. private Mock<ManagementClient> managementClientMock;
  45. private Mock<StorageManagementClient> storageClientMock;
  46. private IClientProvider testClientProvider;
  47. public ReservedIPTest()
  48. {
  49. this.networkingClientMock = new Mock<NetworkManagementClient>();
  50. this.computeClientMock = new Mock<ComputeManagementClient>();
  51. this.managementClientMock = new Mock<ManagementClient>();
  52. this.storageClientMock = new Mock<StorageManagementClient>();
  53. this.mockCommandRuntime = new MockCommandRuntime();
  54. testClientProvider = new TestClientProvider(this.managementClientMock.Object,
  55. this.computeClientMock.Object, this.storageClientMock.Object, this.networkingClientMock.Object);
  56. this.computeClientMock
  57. .Setup(c => c.Deployments.GetBySlotAsync(ServiceName, DeploymentSlot.Production, It.IsAny<CancellationToken>()))
  58. .Returns(Task.Factory.StartNew(() => new DeploymentGetResponse()
  59. {
  60. Name = DeploymentName
  61. }));
  62. this.computeClientMock
  63. .Setup(
  64. c =>
  65. c.Deployments.GetBySlotAsync(ServiceName, DeploymentSlot.Staging,
  66. It.IsAny<CancellationToken>()))
  67. .Returns(Task.Factory.StartNew(() => new DeploymentGetResponse()
  68. {
  69. Name = DeploymentName
  70. }));
  71. // Reserve IP simple
  72. this.networkingClientMock
  73. .Setup(c => c.ReservedIPs.CreateAsync(
  74. It.Is<NetworkReservedIPCreateParameters>(
  75. p =>
  76. string.Equals(p.Name, ReservedIPName) && string.IsNullOrEmpty(p.ServiceName) &&
  77. string.IsNullOrEmpty(p.VirtualIPName) && string.IsNullOrEmpty(p.DeploymentName)),
  78. It.IsAny<CancellationToken>()))
  79. .Returns(Task.Factory.StartNew(() => new OperationStatusResponse()
  80. {
  81. Status = OperationStatus.Succeeded
  82. }));
  83. // Reserve in use IP single vip
  84. this.networkingClientMock
  85. .Setup(c => c.ReservedIPs.CreateAsync(
  86. It.Is<NetworkReservedIPCreateParameters>(
  87. p =>
  88. string.Equals(p.Name, ReservedIPName) && string.Equals(p.ServiceName, ServiceName) &&
  89. string.IsNullOrEmpty(p.VirtualIPName) && string.Equals(p.DeploymentName, DeploymentName)),
  90. It.IsAny<CancellationToken>()))
  91. .Returns(Task.Factory.StartNew(() => new OperationStatusResponse()
  92. {
  93. Status = OperationStatus.Succeeded
  94. }));
  95. // Reserve in use IP named vip
  96. this.networkingClientMock
  97. .Setup(c => c.ReservedIPs.CreateAsync(
  98. It.Is<NetworkReservedIPCreateParameters>(
  99. p =>
  100. string.Equals(p.Name, ReservedIPName) && string.Equals(p.ServiceName, ServiceName) &&
  101. string.Equals(p.VirtualIPName, VipName) && string.Equals(p.DeploymentName, DeploymentName)),
  102. It.IsAny<CancellationToken>()))
  103. .Returns(Task.Factory.StartNew(() => new OperationStatusResponse()
  104. {
  105. Status = OperationStatus.Succeeded
  106. }));
  107. // Associate a reserved IP with a deployment
  108. this.networkingClientMock
  109. .Setup(c => c.ReservedIPs.AssociateAsync(
  110. ReservedIPName,
  111. It.Is<NetworkReservedIPMobilityParameters>(
  112. p => string.Equals(p.ServiceName, ServiceName) &&
  113. string.IsNullOrEmpty(p.VirtualIPName) && string.Equals(p.DeploymentName, DeploymentName)),
  114. It.IsAny<CancellationToken>()))
  115. .Returns(Task.Factory.StartNew(() => new OperationStatusResponse()
  116. {
  117. Status = OperationStatus.Succeeded
  118. }));
  119. // Associate a reserved IP with a vip
  120. this.networkingClientMock
  121. .Setup(c => c.ReservedIPs.AssociateAsync(
  122. ReservedIPName,
  123. It.Is<NetworkReservedIPMobilityParameters>(
  124. p => string.Equals(p.ServiceName, ServiceName) &&
  125. string.Equals(p.VirtualIPName, VipName) && string.Equals(p.DeploymentName, DeploymentName)),
  126. It.IsAny<CancellationToken>()))
  127. .Returns(Task.Factory.StartNew(() => new OperationStatusResponse()
  128. {
  129. Status = OperationStatus.Succeeded
  130. }));
  131. // Remove Azure Reserved IP
  132. this.networkingClientMock
  133. .Setup(c => c.ReservedIPs.DeleteAsync(
  134. ReservedIPName,
  135. It.IsAny<CancellationToken>()))
  136. .Returns(Task.Factory.StartNew(() => new OperationStatusResponse()
  137. {
  138. Status = OperationStatus.Succeeded
  139. }));
  140. }
  141. [Fact]
  142. [Trait(Category.Service, Category.Network)]
  143. [Trait(Category.AcceptanceType, Category.CheckIn)]
  144. public void NewAzureReservedIPSimple()
  145. {
  146. NewAzureReservedIPCmdlet cmdlet = new NewAzureReservedIPCmdlet(testClientProvider)
  147. {
  148. ReservedIPName = ReservedIPName,
  149. Location = "WestUS",
  150. Label = ReservedIPLabel,
  151. CommandRuntime = mockCommandRuntime,
  152. };
  153. cmdlet.SetParameterSet(NewAzureReservedIPCmdlet.ReserveNewIPParamSet);
  154. // Action
  155. cmdlet.ExecuteCmdlet();
  156. // Assert
  157. computeClientMock.Verify(
  158. c => c.Deployments.GetBySlotAsync(
  159. ServiceName,
  160. DeploymentSlot.Production,
  161. It.IsAny<CancellationToken>()),
  162. Times.Never);
  163. networkingClientMock.Verify(
  164. c => c.ReservedIPs.CreateAsync(
  165. It.Is<NetworkReservedIPCreateParameters>(
  166. p =>
  167. string.Equals(p.Name, ReservedIPName) && string.IsNullOrEmpty(p.ServiceName) &&
  168. string.IsNullOrEmpty(p.VirtualIPName) && string.IsNullOrEmpty(p.DeploymentName)),
  169. It.IsAny<CancellationToken>()),
  170. Times.Once());
  171. Assert.Equal(1, mockCommandRuntime.OutputPipeline.Count);
  172. Assert.Equal("Succeeded", ((ManagementOperationContext)mockCommandRuntime.OutputPipeline[0]).OperationStatus);
  173. }
  174. [Fact]
  175. [Trait(Category.Service, Category.Network)]
  176. [Trait(Category.AcceptanceType, Category.CheckIn)]
  177. public void ReservedExistingIPSingleVip()
  178. {
  179. ReserveExistingDeploymentIPBySlot(DeploymentSlot.Production);
  180. }
  181. [Fact]
  182. [Trait(Category.Service, Category.Network)]
  183. [Trait(Category.AcceptanceType, Category.CheckIn)]
  184. public void ReservedExistingIPSingleVipStaging()
  185. {
  186. ReserveExistingDeploymentIPBySlot(DeploymentSlot.Staging);
  187. }
  188. private void ReserveExistingDeploymentIPBySlot(DeploymentSlot slot)
  189. {
  190. NewAzureReservedIPCmdlet cmdlet = new NewAzureReservedIPCmdlet(testClientProvider)
  191. {
  192. ReservedIPName = ReservedIPName,
  193. Location = "WestUS",
  194. ServiceName = ServiceName,
  195. Label = ReservedIPLabel,
  196. Slot = slot.ToString(),
  197. CommandRuntime = mockCommandRuntime,
  198. };
  199. cmdlet.SetParameterSet(NewAzureReservedIPCmdlet.ReserveInUseIPParamSet);
  200. // Action
  201. cmdlet.ExecuteCmdlet();
  202. // Assert
  203. computeClientMock.Verify(
  204. c => c.Deployments.GetBySlotAsync(
  205. ServiceName,
  206. slot,
  207. It.IsAny<CancellationToken>()),
  208. Times.Once);
  209. networkingClientMock.Verify(
  210. c => c.ReservedIPs.CreateAsync(
  211. It.Is<NetworkReservedIPCreateParameters>(
  212. p =>
  213. string.Equals(p.Name, ReservedIPName) && string.Equals(p.ServiceName, ServiceName) &&
  214. string.IsNullOrEmpty(p.VirtualIPName) && string.Equals(p.DeploymentName, DeploymentName)),
  215. It.IsAny<CancellationToken>()),
  216. Times.Once());
  217. Assert.Equal(1, mockCommandRuntime.OutputPipeline.Count);
  218. Assert.Equal("Succeeded", ((ManagementOperationContext) mockCommandRuntime.OutputPipeline[0]).OperationStatus);
  219. }
  220. [Fact]
  221. [Trait(Category.Service, Category.Network)]
  222. [Trait(Category.AcceptanceType, Category.CheckIn)]
  223. public void RemoveAzureReservedIP()
  224. {
  225. RemoveAzureReservedIPCmdlet removeCmdlet = new RemoveAzureReservedIPCmdlet(testClientProvider)
  226. {
  227. ReservedIPName = ReservedIPName,
  228. Force = true,
  229. CommandRuntime = mockCommandRuntime,
  230. };
  231. // Action
  232. removeCmdlet.ExecuteCmdlet();
  233. networkingClientMock.Verify(
  234. c => c.ReservedIPs.DeleteAsync(
  235. ReservedIPName,
  236. It.IsAny<CancellationToken>()),
  237. Times.Once());
  238. Assert.Equal(1, mockCommandRuntime.OutputPipeline.Count);
  239. Assert.Equal("Succeeded", ((ManagementOperationContext)mockCommandRuntime.OutputPipeline[0]).OperationStatus);
  240. }
  241. [Fact]
  242. [Trait(Category.Service, Category.Network)]
  243. [Trait(Category.AcceptanceType, Category.CheckIn)]
  244. public void ReserveExistingIPNamedVip()
  245. {
  246. NewAzureReservedIPCmdlet cmdlet = new NewAzureReservedIPCmdlet(testClientProvider)
  247. {
  248. ReservedIPName = ReservedIPName,
  249. Location = "WestUS",
  250. ServiceName = ServiceName,
  251. Label = ReservedIPLabel,
  252. VirtualIPName = VipName,
  253. CommandRuntime = mockCommandRuntime,
  254. };
  255. cmdlet.SetParameterSet(NewAzureReservedIPCmdlet.ReserveInUseIPParamSet);
  256. // Action
  257. cmdlet.ExecuteCmdlet();
  258. // Assert
  259. computeClientMock.Verify(
  260. c => c.Deployments.GetBySlotAsync(
  261. ServiceName,
  262. DeploymentSlot.Production,
  263. It.IsAny<CancellationToken>()),
  264. Times.Once);
  265. networkingClientMock.Verify(
  266. c => c.ReservedIPs.CreateAsync(
  267. It.Is<NetworkReservedIPCreateParameters>(
  268. p =>
  269. string.Equals(p.Name, ReservedIPName) && string.Equals(p.ServiceName, ServiceName) &&
  270. string.Equals(p.VirtualIPName, VipName) && string.Equals(p.DeploymentName, DeploymentName)),
  271. It.IsAny<CancellationToken>()),
  272. Times.Once());
  273. Assert.Equal(1, mockCommandRuntime.OutputPipeline.Count);
  274. Assert.Equal("Succeeded", ((ManagementOperationContext)mockCommandRuntime.OutputPipeline[0]).OperationStatus);
  275. }
  276. [Fact]
  277. [Trait(Category.Service, Category.Network)]
  278. [Trait(Category.AcceptanceType, Category.CheckIn)]
  279. public void SetAzureReservedIPAssociationSimple()
  280. {
  281. SetAzureReservedIPAssociationSimpleBySlot(DeploymentSlot.Production);
  282. }
  283. [Fact]
  284. [Trait(Category.Service, Category.Network)]
  285. [Trait(Category.AcceptanceType, Category.CheckIn)]
  286. public void SetAzureReservedIPAssociationSimpleStaging()
  287. {
  288. SetAzureReservedIPAssociationSimpleBySlot(DeploymentSlot.Staging);
  289. }
  290. private void SetAzureReservedIPAssociationSimpleBySlot(DeploymentSlot slot)
  291. {
  292. SetAzureReservedIPAssociationCmdlet setassociation = new SetAzureReservedIPAssociationCmdlet(testClientProvider)
  293. {
  294. ReservedIPName = ReservedIPName,
  295. ServiceName = ServiceName,
  296. Slot = slot.ToString(),
  297. CommandRuntime = mockCommandRuntime,
  298. };
  299. // Action
  300. setassociation.ExecuteCmdlet();
  301. // Assert
  302. computeClientMock.Verify(
  303. c => c.Deployments.GetBySlotAsync(
  304. ServiceName,
  305. slot,
  306. It.IsAny<CancellationToken>()),
  307. Times.Once);
  308. networkingClientMock.Verify(
  309. c => c.ReservedIPs.AssociateAsync(
  310. ReservedIPName,
  311. It.Is<NetworkReservedIPMobilityParameters>(
  312. p =>
  313. string.Equals(p.ServiceName, ServiceName) &&
  314. string.IsNullOrEmpty(p.VirtualIPName) && string.Equals(p.DeploymentName, DeploymentName)),
  315. It.IsAny<CancellationToken>()),
  316. Times.Once());
  317. Assert.Equal(1, mockCommandRuntime.OutputPipeline.Count);
  318. Assert.Equal("Succeeded", ((ManagementOperationContext) mockCommandRuntime.OutputPipeline[0]).OperationStatus);
  319. }
  320. [Fact]
  321. [Trait(Category.Service, Category.Network)]
  322. [Trait(Category.AcceptanceType, Category.CheckIn)]
  323. public void SetAzureReservedIPAssociationMultivip()
  324. {
  325. SetAzureReservedIPAssociationCmdlet setassociation = new SetAzureReservedIPAssociationCmdlet(testClientProvider)
  326. {
  327. ReservedIPName = ReservedIPName,
  328. ServiceName = ServiceName,
  329. VirtualIPName = VipName,
  330. CommandRuntime = mockCommandRuntime,
  331. };
  332. setassociation.ExecuteCmdlet();
  333. // Assert
  334. computeClientMock.Verify(
  335. c => c.Deployments.GetBySlotAsync(
  336. ServiceName,
  337. DeploymentSlot.Production,
  338. It.IsAny<CancellationToken>()),
  339. Times.Once);
  340. networkingClientMock.Verify(
  341. c => c.ReservedIPs.AssociateAsync(
  342. ReservedIPName,
  343. It.Is<NetworkReservedIPMobilityParameters>(
  344. p =>
  345. string.Equals(p.ServiceName, ServiceName) &&
  346. string.Equals(p.VirtualIPName, VipName) && string.Equals(p.DeploymentName, DeploymentName)),
  347. It.IsAny<CancellationToken>()),
  348. Times.Once());
  349. Assert.Equal(1, mockCommandRuntime.OutputPipeline.Count);
  350. Assert.Equal("Succeeded", ((ManagementOperationContext)mockCommandRuntime.OutputPipeline[0]).OperationStatus);
  351. }
  352. }
  353. }