PageRenderTime 28ms CodeModel.GetById 0ms RepoModel.GetById 0ms app.codeStats 0ms

/src/ServiceManagement/Network/Commands.Network.Test/NetworkSecurityGroups/RemoveNetworkSecurityGroupAssociationTests.cs

https://gitlab.com/jslee1/azure-powershell
C# | 417 lines | 339 code | 49 blank | 29 comment | 0 complexity | e0ff53ea345fcba5d83c2086e10d902f 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 Hyak.Common;
  15. using Microsoft.WindowsAzure.Commands.ServiceManagement.Network.NetworkSecurityGroup.Subnet;
  16. using Microsoft.WindowsAzure.Commands.Common.Test.Mocks;
  17. using Microsoft.WindowsAzure.Commands.ServiceManagement.Model;
  18. using Microsoft.WindowsAzure.Commands.Utilities.Common;
  19. using Microsoft.WindowsAzure.Management;
  20. using Microsoft.WindowsAzure.Management.Compute;
  21. using Microsoft.WindowsAzure.Management.Compute.Models;
  22. using Microsoft.WindowsAzure.Management.Network;
  23. using Moq;
  24. using System.Threading;
  25. using System.Threading.Tasks;
  26. using Microsoft.WindowsAzure.Commands.ScenarioTest;
  27. using Xunit;
  28. namespace Microsoft.WindowsAzure.Commands.ServiceManagement.Network.Test.NetworkSecurityGroups
  29. {
  30. public class RemoveNetworkSecurityGroupAssociationTests
  31. {
  32. private const string ServiceName = "serviceName";
  33. private const string DeploymentName = "deploymentName";
  34. private const string RoleName = "roleName";
  35. private const string NetworkInterfaceName = "networkInterfaceName";
  36. private const string NetworkSecurityGroupName = "networkSecurityGroupName";
  37. private const string CurrentNetworkSecurityGroup = "currentNetworkSecurityGroup";
  38. private const string VirtualNetworkName = "virtualNetworkName";
  39. private const string SubnetName = "subnetName";
  40. private PersistentVMRoleContext VM = new PersistentVMRoleContext()
  41. {
  42. // these are the only 2 properties being used in the cmdlet
  43. Name = RoleName,
  44. DeploymentName = DeploymentName,
  45. };
  46. private CloudException AlreadySetException = new CloudException("already set exception")
  47. {
  48. Error = new CloudError()
  49. {
  50. Code = "BadRequest",
  51. Message = "is already mapped to network security group"
  52. }
  53. };
  54. private MockCommandRuntime mockCommandRuntime;
  55. private RemoveAzureNetworkSecurityGroupAssociation cmdlet;
  56. private NetworkClient client;
  57. private Mock<INetworkManagementClient> networkingClientMock;
  58. private Mock<IComputeManagementClient> computeClientMock;
  59. private Mock<IManagementClient> managementClientMock;
  60. public RemoveNetworkSecurityGroupAssociationTests()
  61. {
  62. this.networkingClientMock = new Mock<INetworkManagementClient>();
  63. this.computeClientMock = new Mock<IComputeManagementClient>();
  64. this.managementClientMock = new Mock<IManagementClient>();
  65. this.mockCommandRuntime = new MockCommandRuntime();
  66. this.client = new NetworkClient(
  67. networkingClientMock.Object,
  68. computeClientMock.Object,
  69. managementClientMock.Object,
  70. mockCommandRuntime);
  71. this.computeClientMock
  72. .Setup(c => c.Deployments.GetBySlotAsync(ServiceName, DeploymentSlot.Production, It.IsAny<CancellationToken>()))
  73. .Returns(Task.Factory.StartNew(() => new DeploymentGetResponse()
  74. {
  75. Name = DeploymentName
  76. }));
  77. this.networkingClientMock
  78. .Setup(c =>
  79. c.NetworkSecurityGroups.RemoveFromSubnetAsync(
  80. VirtualNetworkName,
  81. SubnetName,
  82. NetworkSecurityGroupName,
  83. It.IsAny<CancellationToken>()))
  84. .Returns(Task.Factory.StartNew(() => new Azure.OperationStatusResponse()));
  85. this.networkingClientMock
  86. .Setup(c =>
  87. c.NetworkSecurityGroups.RemoveFromRoleAsync(
  88. ServiceName,
  89. DeploymentName,
  90. RoleName,
  91. NetworkSecurityGroupName,
  92. It.IsAny<CancellationToken>()))
  93. .Returns(Task.Factory.StartNew(() => new Azure.OperationStatusResponse()));
  94. this.networkingClientMock
  95. .Setup(c =>
  96. c.NetworkSecurityGroups.RemoveFromNetworkInterfaceAsync(
  97. ServiceName,
  98. DeploymentName,
  99. RoleName,
  100. NetworkInterfaceName,
  101. NetworkSecurityGroupName,
  102. It.IsAny<CancellationToken>()))
  103. .Returns(Task.Factory.StartNew(() => new Azure.OperationStatusResponse()));
  104. }
  105. [Fact]
  106. [Trait(Category.Service, Category.Network)]
  107. [Trait(Category.AcceptanceType, Category.CheckIn)]
  108. public void RemoveNSGFromSubnet()
  109. {
  110. // Setup
  111. cmdlet = new RemoveAzureNetworkSecurityGroupAssociation()
  112. {
  113. Name = NetworkSecurityGroupName,
  114. VirtualNetworkName = VirtualNetworkName,
  115. SubnetName = SubnetName,
  116. CommandRuntime = mockCommandRuntime,
  117. Client = this.client
  118. };
  119. cmdlet.SetParameterSet(RemoveAzureNetworkSecurityGroupAssociation.RemoveNetworkSecurityGroupAssociationFromSubnet);
  120. // Action
  121. cmdlet.ExecuteCmdlet();
  122. // Assert
  123. #region Never called
  124. computeClientMock.Verify(
  125. c => c.Deployments.GetBySlotAsync(
  126. It.IsAny<string>(),
  127. It.IsAny<DeploymentSlot>(),
  128. It.IsAny<CancellationToken>()),
  129. Times.Never);
  130. networkingClientMock.Verify(
  131. c => c.NetworkSecurityGroups.RemoveFromRoleAsync(
  132. It.IsAny<string>(),
  133. It.IsAny<string>(),
  134. It.IsAny<string>(),
  135. It.IsAny<string>(),
  136. It.IsAny<CancellationToken>()),
  137. Times.Never);
  138. networkingClientMock.Verify(
  139. c => c.NetworkSecurityGroups.RemoveFromNetworkInterfaceAsync(
  140. It.IsAny<string>(),
  141. It.IsAny<string>(),
  142. It.IsAny<string>(),
  143. It.IsAny<string>(),
  144. It.IsAny<string>(),
  145. It.IsAny<CancellationToken>()),
  146. Times.Never);
  147. #endregion
  148. networkingClientMock.Verify(
  149. c => c.NetworkSecurityGroups.RemoveFromSubnetAsync(
  150. cmdlet.VirtualNetworkName,
  151. cmdlet.SubnetName,
  152. NetworkSecurityGroupName,
  153. It.IsAny<CancellationToken>()),
  154. Times.Once);
  155. Assert.Equal(0, mockCommandRuntime.OutputPipeline.Count);
  156. }
  157. [Fact]
  158. [Trait(Category.Service, Category.Network)]
  159. [Trait(Category.AcceptanceType, Category.CheckIn)]
  160. public void RemoveNSGFromVMRole()
  161. {
  162. // Setup
  163. cmdlet = new RemoveAzureNetworkSecurityGroupAssociation()
  164. {
  165. Name = NetworkSecurityGroupName,
  166. ServiceName = ServiceName,
  167. VM = VM,
  168. CommandRuntime = mockCommandRuntime,
  169. Client = this.client
  170. };
  171. cmdlet.SetParameterSet(RemoveAzureNetworkSecurityGroupAssociation.RemoveNetworkSecurityGroupAssociationFromIaaSRole);
  172. // Action
  173. cmdlet.ExecuteCmdlet();
  174. // Assert
  175. #region Never called
  176. computeClientMock.Verify(
  177. c => c.Deployments.GetBySlotAsync(
  178. It.IsAny<string>(),
  179. It.IsAny<DeploymentSlot>(),
  180. It.IsAny<CancellationToken>()),
  181. Times.Never);
  182. networkingClientMock.Verify(
  183. c => c.NetworkSecurityGroups.RemoveFromSubnetAsync(
  184. It.IsAny<string>(),
  185. It.IsAny<string>(),
  186. It.IsAny<string>(),
  187. It.IsAny<CancellationToken>()),
  188. Times.Never);
  189. networkingClientMock.Verify(
  190. c => c.NetworkSecurityGroups.RemoveFromNetworkInterfaceAsync(
  191. It.IsAny<string>(),
  192. It.IsAny<string>(),
  193. It.IsAny<string>(),
  194. It.IsAny<string>(),
  195. It.IsAny<string>(),
  196. It.IsAny<CancellationToken>()),
  197. Times.Never);
  198. #endregion
  199. networkingClientMock.Verify(
  200. c => c.NetworkSecurityGroups.RemoveFromRoleAsync(
  201. cmdlet.ServiceName,
  202. DeploymentName,
  203. RoleName,
  204. NetworkSecurityGroupName,
  205. It.IsAny<CancellationToken>()),
  206. Times.Once);
  207. Assert.Equal(0, mockCommandRuntime.OutputPipeline.Count);
  208. }
  209. [Fact]
  210. [Trait(Category.Service, Category.Network)]
  211. [Trait(Category.AcceptanceType, Category.CheckIn)]
  212. public void RemoveNSGFromVMNic()
  213. {
  214. // Setup
  215. cmdlet = new RemoveAzureNetworkSecurityGroupAssociation()
  216. {
  217. Name = NetworkSecurityGroupName,
  218. ServiceName = ServiceName,
  219. VM = VM,
  220. NetworkInterfaceName = NetworkInterfaceName,
  221. CommandRuntime = mockCommandRuntime,
  222. Client = this.client
  223. };
  224. cmdlet.SetParameterSet(RemoveAzureNetworkSecurityGroupAssociation.RemoveNetworkSecurityGroupAssociationFromIaaSRole);
  225. // Action
  226. cmdlet.ExecuteCmdlet();
  227. // Assert
  228. #region Never called
  229. computeClientMock.Verify(
  230. c => c.Deployments.GetBySlotAsync(
  231. It.IsAny<string>(),
  232. It.IsAny<DeploymentSlot>(),
  233. It.IsAny<CancellationToken>()),
  234. Times.Never);
  235. networkingClientMock.Verify(
  236. c => c.NetworkSecurityGroups.RemoveFromSubnetAsync(
  237. It.IsAny<string>(),
  238. It.IsAny<string>(),
  239. It.IsAny<string>(),
  240. It.IsAny<CancellationToken>()),
  241. Times.Never);
  242. networkingClientMock.Verify(
  243. c => c.NetworkSecurityGroups.RemoveFromRoleAsync(
  244. It.IsAny<string>(),
  245. It.IsAny<string>(),
  246. It.IsAny<string>(),
  247. It.IsAny<string>(),
  248. It.IsAny<CancellationToken>()),
  249. Times.Never);
  250. #endregion
  251. networkingClientMock.Verify(
  252. c => c.NetworkSecurityGroups.RemoveFromNetworkInterfaceAsync(
  253. cmdlet.ServiceName,
  254. DeploymentName,
  255. RoleName,
  256. NetworkInterfaceName,
  257. NetworkSecurityGroupName,
  258. It.IsAny<CancellationToken>()),
  259. Times.Once);
  260. Assert.Equal(0, mockCommandRuntime.OutputPipeline.Count);
  261. }
  262. [Fact]
  263. [Trait(Category.Service, Category.Network)]
  264. [Trait(Category.AcceptanceType, Category.CheckIn)]
  265. public void RemoveNSGFromVMRoleByName()
  266. {
  267. // Setup
  268. cmdlet = new RemoveAzureNetworkSecurityGroupAssociation()
  269. {
  270. Name = NetworkSecurityGroupName,
  271. ServiceName = ServiceName,
  272. RoleName = RoleName,
  273. CommandRuntime = mockCommandRuntime,
  274. Client = this.client
  275. };
  276. cmdlet.SetParameterSet(RemoveAzureNetworkSecurityGroupAssociation.RemoveNetworkSecurityGroupAssociationFromPaaSRole);
  277. // Action
  278. cmdlet.ExecuteCmdlet();
  279. // Assert
  280. #region Never called
  281. networkingClientMock.Verify(
  282. c => c.NetworkSecurityGroups.RemoveFromSubnetAsync(
  283. It.IsAny<string>(),
  284. It.IsAny<string>(),
  285. It.IsAny<string>(),
  286. It.IsAny<CancellationToken>()),
  287. Times.Never);
  288. networkingClientMock.Verify(
  289. c => c.NetworkSecurityGroups.RemoveFromNetworkInterfaceAsync(
  290. It.IsAny<string>(),
  291. It.IsAny<string>(),
  292. It.IsAny<string>(),
  293. It.IsAny<string>(),
  294. It.IsAny<string>(),
  295. It.IsAny<CancellationToken>()),
  296. Times.Never);
  297. #endregion
  298. computeClientMock.Verify(
  299. c => c.Deployments.GetBySlotAsync(
  300. It.IsAny<string>(),
  301. DeploymentSlot.Production,
  302. It.IsAny<CancellationToken>()),
  303. Times.Once);
  304. networkingClientMock.Verify(
  305. c => c.NetworkSecurityGroups.RemoveFromRoleAsync(
  306. cmdlet.ServiceName,
  307. DeploymentName,
  308. RoleName,
  309. NetworkSecurityGroupName,
  310. It.IsAny<CancellationToken>()),
  311. Times.Once);
  312. Assert.Equal(0, mockCommandRuntime.OutputPipeline.Count);
  313. }
  314. [Fact]
  315. [Trait(Category.Service, Category.Network)]
  316. [Trait(Category.AcceptanceType, Category.CheckIn)]
  317. public void RemoveNSGFromVMNicByName()
  318. {
  319. // Setup
  320. cmdlet = new RemoveAzureNetworkSecurityGroupAssociation()
  321. {
  322. Name = NetworkSecurityGroupName,
  323. ServiceName = ServiceName,
  324. RoleName = RoleName,
  325. NetworkInterfaceName = NetworkInterfaceName,
  326. CommandRuntime = mockCommandRuntime,
  327. Client = this.client
  328. };
  329. cmdlet.SetParameterSet(RemoveAzureNetworkSecurityGroupAssociation.RemoveNetworkSecurityGroupAssociationFromPaaSRole);
  330. // Action
  331. cmdlet.ExecuteCmdlet();
  332. // Assert
  333. #region Never called
  334. networkingClientMock.Verify(
  335. c => c.NetworkSecurityGroups.RemoveFromSubnetAsync(
  336. It.IsAny<string>(),
  337. It.IsAny<string>(),
  338. It.IsAny<string>(),
  339. It.IsAny<CancellationToken>()),
  340. Times.Never);
  341. networkingClientMock.Verify(
  342. c => c.NetworkSecurityGroups.RemoveFromRoleAsync(
  343. It.IsAny<string>(),
  344. It.IsAny<string>(),
  345. It.IsAny<string>(),
  346. It.IsAny<string>(),
  347. It.IsAny<CancellationToken>()),
  348. Times.Never);
  349. #endregion
  350. computeClientMock.Verify(
  351. c => c.Deployments.GetBySlotAsync(
  352. It.IsAny<string>(),
  353. DeploymentSlot.Production,
  354. It.IsAny<CancellationToken>()),
  355. Times.Once);
  356. networkingClientMock.Verify(
  357. c => c.NetworkSecurityGroups.RemoveFromNetworkInterfaceAsync(
  358. cmdlet.ServiceName,
  359. DeploymentName,
  360. RoleName,
  361. NetworkInterfaceName,
  362. NetworkSecurityGroupName,
  363. It.IsAny<CancellationToken>()),
  364. Times.Once);
  365. Assert.Equal(0, mockCommandRuntime.OutputPipeline.Count);
  366. }
  367. }
  368. }