PageRenderTime 60ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://gitlab.com/jslee1/azure-powershell
C# | 1043 lines | 867 code | 127 blank | 49 comment | 0 complexity | d92ae577570d4ebed9d7ba78b4291591 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.Association;
  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 Microsoft.WindowsAzure.Management.Network.Models;
  24. using Moq;
  25. using System.Threading;
  26. using System.Threading.Tasks;
  27. using Microsoft.WindowsAzure.Commands.ScenarioTest;
  28. using Xunit;
  29. namespace Microsoft.WindowsAzure.Commands.ServiceManagement.Network.Test.NetworkSecurityGroups
  30. {
  31. public class SetNetworkSecurityGroupAssociationTests
  32. {
  33. private const string ServiceName = "serviceName";
  34. private const string DeploymentName = "deploymentName";
  35. private const string RoleName = "roleName";
  36. private const string NetworkInterfaceName = "networkInterfaceName";
  37. private const string NetworkSecurityGroupName = "networkSecurityGroupName";
  38. private const string CurrentNetworkSecurityGroup = "currentNetworkSecurityGroup";
  39. private const string VirtualNetworkName = "virtualNetworkName";
  40. private const string SubnetName = "subnetName";
  41. private PersistentVMRoleContext VM = new PersistentVMRoleContext()
  42. {
  43. // these are the only 2 properties being used in the cmdlet
  44. Name = RoleName,
  45. DeploymentName = DeploymentName,
  46. };
  47. private CloudException AlreadySetException = new CloudException("already set exception")
  48. {
  49. Error = new CloudError()
  50. {
  51. Code = "BadRequest",
  52. Message = "is already mapped to network security group"
  53. }
  54. };
  55. private MockCommandRuntime mockCommandRuntime;
  56. private SetAzureNetworkSecurityGroupAssociation cmdlet;
  57. private NetworkClient client;
  58. private Mock<INetworkManagementClient> networkingClientMock;
  59. private Mock<IComputeManagementClient> computeClientMock;
  60. private Mock<IManagementClient> managementClientMock;
  61. public SetNetworkSecurityGroupAssociationTests()
  62. {
  63. this.networkingClientMock = new Mock<INetworkManagementClient>();
  64. this.computeClientMock = new Mock<IComputeManagementClient>();
  65. this.managementClientMock = new Mock<IManagementClient>();
  66. this.mockCommandRuntime = new MockCommandRuntime();
  67. this.client = new NetworkClient(
  68. networkingClientMock.Object,
  69. computeClientMock.Object,
  70. managementClientMock.Object,
  71. mockCommandRuntime);
  72. this.computeClientMock
  73. .Setup(c => c.Deployments.GetBySlotAsync(ServiceName, DeploymentSlot.Production, It.IsAny<CancellationToken>()))
  74. .Returns(Task.Factory.StartNew(() => new DeploymentGetResponse()
  75. {
  76. Name = DeploymentName
  77. }));
  78. this.networkingClientMock
  79. .Setup(c =>
  80. c.NetworkSecurityGroups.AddToSubnetAsync(
  81. VirtualNetworkName,
  82. SubnetName,
  83. It.IsAny<NetworkSecurityGroupAddAssociationParameters>(),
  84. It.IsAny<CancellationToken>()))
  85. .Returns(Task.Factory.StartNew(() => new Azure.OperationStatusResponse()));
  86. this.networkingClientMock
  87. .Setup(c =>
  88. c.NetworkSecurityGroups.AddToRoleAsync(
  89. ServiceName,
  90. DeploymentName,
  91. RoleName,
  92. It.IsAny<NetworkSecurityGroupAddAssociationParameters>(),
  93. It.IsAny<CancellationToken>()))
  94. .Returns(Task.Factory.StartNew(() => new Azure.OperationStatusResponse()));
  95. this.networkingClientMock
  96. .Setup(c =>
  97. c.NetworkSecurityGroups.AddToNetworkInterfaceAsync(
  98. ServiceName,
  99. DeploymentName,
  100. RoleName,
  101. NetworkInterfaceName,
  102. It.IsAny<NetworkSecurityGroupAddAssociationParameters>(),
  103. It.IsAny<CancellationToken>()))
  104. .Returns(Task.Factory.StartNew(() => new Azure.OperationStatusResponse()));
  105. this.networkingClientMock
  106. .Setup(c =>
  107. c.NetworkSecurityGroups.GetForSubnetAsync(
  108. VirtualNetworkName,
  109. SubnetName,
  110. It.IsAny<CancellationToken>()))
  111. .Returns(Task.Factory.StartNew(() =>
  112. new NetworkSecurityGroupGetAssociationResponse()
  113. {
  114. Name = CurrentNetworkSecurityGroup
  115. }));
  116. this.networkingClientMock
  117. .Setup(c =>
  118. c.NetworkSecurityGroups.GetForRoleAsync(
  119. ServiceName,
  120. DeploymentName,
  121. RoleName,
  122. It.IsAny<CancellationToken>()))
  123. .Returns(Task.Factory.StartNew(() =>
  124. new NetworkSecurityGroupGetAssociationResponse()
  125. {
  126. Name = CurrentNetworkSecurityGroup
  127. }));
  128. this.networkingClientMock
  129. .Setup(c =>
  130. c.NetworkSecurityGroups.GetForNetworkInterfaceAsync(
  131. ServiceName,
  132. DeploymentName,
  133. RoleName,
  134. NetworkInterfaceName,
  135. It.IsAny<CancellationToken>()))
  136. .Returns(Task.Factory.StartNew(() =>
  137. new NetworkSecurityGroupGetAssociationResponse()
  138. {
  139. Name = CurrentNetworkSecurityGroup
  140. }));
  141. }
  142. #region No previous NSG association is set
  143. [Fact]
  144. [Trait(Category.Service, Category.Network)]
  145. [Trait(Category.AcceptanceType, Category.CheckIn)]
  146. public void SetNSGOnSubnetNoPreviousNSG()
  147. {
  148. // Setup
  149. cmdlet = new SetAzureNetworkSecurityGroupAssociation
  150. {
  151. Name = NetworkSecurityGroupName,
  152. VirtualNetworkName = VirtualNetworkName,
  153. SubnetName = SubnetName,
  154. CommandRuntime = mockCommandRuntime,
  155. Client = this.client
  156. };
  157. cmdlet.SetParameterSet(SetAzureNetworkSecurityGroupAssociation.AddNetworkSecurityGroupAssociationToSubnet);
  158. // Action
  159. cmdlet.ExecuteCmdlet();
  160. // Assert
  161. #region Never called
  162. computeClientMock.Verify(
  163. c => c.Deployments.GetBySlotAsync(
  164. It.IsAny<string>(),
  165. It.IsAny<DeploymentSlot>(),
  166. It.IsAny<CancellationToken>()),
  167. Times.Never);
  168. networkingClientMock.Verify(
  169. c => c.NetworkSecurityGroups.AddToRoleAsync(
  170. It.IsAny<string>(),
  171. It.IsAny<string>(),
  172. It.IsAny<string>(),
  173. It.IsAny<NetworkSecurityGroupAddAssociationParameters>(),
  174. It.IsAny<CancellationToken>()),
  175. Times.Never);
  176. networkingClientMock.Verify(
  177. c => c.NetworkSecurityGroups.AddToNetworkInterfaceAsync(
  178. It.IsAny<string>(),
  179. It.IsAny<string>(),
  180. It.IsAny<string>(),
  181. It.IsAny<string>(),
  182. It.IsAny<NetworkSecurityGroupAddAssociationParameters>(),
  183. It.IsAny<CancellationToken>()),
  184. Times.Never);
  185. #endregion
  186. networkingClientMock.Verify(
  187. c => c.NetworkSecurityGroups.AddToSubnetAsync(
  188. cmdlet.VirtualNetworkName,
  189. cmdlet.SubnetName,
  190. It.Is<NetworkSecurityGroupAddAssociationParameters>(
  191. parameters => string.Equals(NetworkSecurityGroupName, parameters.Name)),
  192. It.IsAny<CancellationToken>()),
  193. Times.Once);
  194. Assert.Equal(0, mockCommandRuntime.OutputPipeline.Count);
  195. }
  196. [Fact]
  197. [Trait(Category.Service, Category.Network)]
  198. [Trait(Category.AcceptanceType, Category.CheckIn)]
  199. public void SetNSGOnVMRoleNoPreviousNSG()
  200. {
  201. // Setup
  202. cmdlet = new SetAzureNetworkSecurityGroupAssociation
  203. {
  204. Name = NetworkSecurityGroupName,
  205. ServiceName = ServiceName,
  206. VM = VM,
  207. CommandRuntime = mockCommandRuntime,
  208. Client = this.client
  209. };
  210. cmdlet.SetParameterSet(SetAzureNetworkSecurityGroupAssociation.AddNetworkSecurityGroupAssociationToIaaSRole);
  211. // Action
  212. cmdlet.ExecuteCmdlet();
  213. // Assert
  214. #region Never called
  215. computeClientMock.Verify(
  216. c => c.Deployments.GetBySlotAsync(
  217. ServiceName,
  218. DeploymentSlot.Production,
  219. It.IsAny<CancellationToken>()),
  220. Times.Never);
  221. networkingClientMock.Verify(
  222. c => c.NetworkSecurityGroups.AddToNetworkInterfaceAsync(
  223. It.IsAny<string>(),
  224. It.IsAny<string>(),
  225. It.IsAny<string>(),
  226. It.IsAny<string>(),
  227. It.IsAny<NetworkSecurityGroupAddAssociationParameters>(),
  228. It.IsAny<CancellationToken>()),
  229. Times.Never);
  230. networkingClientMock.Verify(
  231. c => c.NetworkSecurityGroups.AddToSubnetAsync(
  232. It.IsAny<string>(),
  233. It.IsAny<string>(),
  234. It.IsAny<NetworkSecurityGroupAddAssociationParameters>(),
  235. It.IsAny<CancellationToken>()),
  236. Times.Never);
  237. #endregion
  238. networkingClientMock.Verify(
  239. c => c.NetworkSecurityGroups.AddToRoleAsync(
  240. ServiceName,
  241. DeploymentName,
  242. RoleName,
  243. It.Is<NetworkSecurityGroupAddAssociationParameters>(
  244. parameters => string.Equals(NetworkSecurityGroupName, parameters.Name)),
  245. It.IsAny<CancellationToken>()),
  246. Times.Once);
  247. Assert.Equal(0, mockCommandRuntime.OutputPipeline.Count);
  248. }
  249. [Fact]
  250. [Trait(Category.Service, Category.Network)]
  251. [Trait(Category.AcceptanceType, Category.CheckIn)]
  252. public void SetNSGOnVMNicNoPreviousNSG()
  253. {
  254. // Setup
  255. cmdlet = new SetAzureNetworkSecurityGroupAssociation
  256. {
  257. Name = NetworkSecurityGroupName,
  258. ServiceName = ServiceName,
  259. VM = VM,
  260. NetworkInterfaceName = NetworkInterfaceName,
  261. CommandRuntime = mockCommandRuntime,
  262. Client = this.client
  263. };
  264. cmdlet.SetParameterSet(SetAzureNetworkSecurityGroupAssociation.AddNetworkSecurityGroupAssociationToIaaSRole);
  265. // Action
  266. cmdlet.ExecuteCmdlet();
  267. // Assert
  268. #region Never called
  269. computeClientMock.Verify(
  270. c => c.Deployments.GetBySlotAsync(
  271. ServiceName,
  272. DeploymentSlot.Production,
  273. It.IsAny<CancellationToken>()),
  274. Times.Never);
  275. networkingClientMock.Verify(
  276. c => c.NetworkSecurityGroups.AddToRoleAsync(
  277. It.IsAny<string>(),
  278. It.IsAny<string>(),
  279. It.IsAny<string>(),
  280. It.IsAny<NetworkSecurityGroupAddAssociationParameters>(),
  281. It.IsAny<CancellationToken>()),
  282. Times.Never);
  283. networkingClientMock.Verify(
  284. c => c.NetworkSecurityGroups.AddToSubnetAsync(
  285. It.IsAny<string>(),
  286. It.IsAny<string>(),
  287. It.IsAny<NetworkSecurityGroupAddAssociationParameters>(),
  288. It.IsAny<CancellationToken>()),
  289. Times.Never);
  290. #endregion
  291. networkingClientMock.Verify(
  292. c => c.NetworkSecurityGroups.AddToNetworkInterfaceAsync(
  293. ServiceName,
  294. DeploymentName,
  295. RoleName,
  296. NetworkInterfaceName,
  297. It.Is<NetworkSecurityGroupAddAssociationParameters>(
  298. parameters => string.Equals(NetworkSecurityGroupName, parameters.Name)),
  299. It.IsAny<CancellationToken>()),
  300. Times.Once);
  301. Assert.Equal(0, mockCommandRuntime.OutputPipeline.Count);
  302. }
  303. [Fact]
  304. [Trait(Category.Service, Category.Network)]
  305. [Trait(Category.AcceptanceType, Category.CheckIn)]
  306. public void SetNSGOnRoleByNameNoPreviousNSG()
  307. {
  308. // Setup
  309. cmdlet = new SetAzureNetworkSecurityGroupAssociation
  310. {
  311. Name = NetworkSecurityGroupName,
  312. ServiceName = ServiceName,
  313. RoleName = RoleName,
  314. CommandRuntime = mockCommandRuntime,
  315. Client = this.client
  316. };
  317. cmdlet.SetParameterSet(SetAzureNetworkSecurityGroupAssociation.AddNetworkSecurityGroupAssociationToPaaSRole);
  318. // Action
  319. cmdlet.ExecuteCmdlet();
  320. // Assert
  321. computeClientMock.Verify(
  322. c => c.Deployments.GetBySlotAsync(
  323. ServiceName,
  324. DeploymentSlot.Production,
  325. It.IsAny<CancellationToken>()),
  326. Times.Once);
  327. #region Never called
  328. networkingClientMock.Verify(
  329. c => c.NetworkSecurityGroups.AddToNetworkInterfaceAsync(
  330. It.IsAny<string>(),
  331. It.IsAny<string>(),
  332. It.IsAny<string>(),
  333. It.IsAny<string>(),
  334. It.IsAny<NetworkSecurityGroupAddAssociationParameters>(),
  335. It.IsAny<CancellationToken>()),
  336. Times.Never);
  337. networkingClientMock.Verify(
  338. c => c.NetworkSecurityGroups.AddToSubnetAsync(
  339. It.IsAny<string>(),
  340. It.IsAny<string>(),
  341. It.IsAny<NetworkSecurityGroupAddAssociationParameters>(),
  342. It.IsAny<CancellationToken>()),
  343. Times.Never);
  344. #endregion
  345. networkingClientMock.Verify(
  346. c => c.NetworkSecurityGroups.AddToRoleAsync(
  347. ServiceName,
  348. DeploymentName,
  349. RoleName,
  350. It.Is<NetworkSecurityGroupAddAssociationParameters>(
  351. parameters => string.Equals(NetworkSecurityGroupName, parameters.Name)),
  352. It.IsAny<CancellationToken>()),
  353. Times.Once);
  354. Assert.Equal(0, mockCommandRuntime.OutputPipeline.Count);
  355. }
  356. [Fact]
  357. [Trait(Category.Service, Category.Network)]
  358. [Trait(Category.AcceptanceType, Category.CheckIn)]
  359. public void SetNSGOnNicByNameNoPreviousNSG()
  360. {
  361. // Setup
  362. cmdlet = new SetAzureNetworkSecurityGroupAssociation
  363. {
  364. Name = NetworkSecurityGroupName,
  365. ServiceName = ServiceName,
  366. RoleName = RoleName,
  367. NetworkInterfaceName = NetworkInterfaceName,
  368. CommandRuntime = mockCommandRuntime,
  369. Client = this.client
  370. };
  371. cmdlet.SetParameterSet(SetAzureNetworkSecurityGroupAssociation.AddNetworkSecurityGroupAssociationToPaaSRole);
  372. // Action
  373. cmdlet.ExecuteCmdlet();
  374. // Assert
  375. #region Never called
  376. networkingClientMock.Verify(
  377. c => c.NetworkSecurityGroups.AddToRoleAsync(
  378. It.IsAny<string>(),
  379. It.IsAny<string>(),
  380. It.IsAny<string>(),
  381. It.IsAny<NetworkSecurityGroupAddAssociationParameters>(),
  382. It.IsAny<CancellationToken>()),
  383. Times.Never);
  384. networkingClientMock.Verify(
  385. c => c.NetworkSecurityGroups.AddToSubnetAsync(
  386. It.IsAny<string>(),
  387. It.IsAny<string>(),
  388. It.IsAny<NetworkSecurityGroupAddAssociationParameters>(),
  389. It.IsAny<CancellationToken>()),
  390. Times.Never);
  391. #endregion
  392. computeClientMock.Verify(
  393. c => c.Deployments.GetBySlotAsync(
  394. ServiceName,
  395. DeploymentSlot.Production,
  396. It.IsAny<CancellationToken>()),
  397. Times.Once);
  398. networkingClientMock.Verify(
  399. c => c.NetworkSecurityGroups.AddToNetworkInterfaceAsync(
  400. ServiceName,
  401. DeploymentName,
  402. RoleName,
  403. NetworkInterfaceName,
  404. It.Is<NetworkSecurityGroupAddAssociationParameters>(
  405. parameters => string.Equals(NetworkSecurityGroupName, parameters.Name)),
  406. It.IsAny<CancellationToken>()),
  407. Times.Once);
  408. Assert.Equal(0, mockCommandRuntime.OutputPipeline.Count);
  409. }
  410. #endregion
  411. #region A previous NSG association is set
  412. [Fact]
  413. [Trait(Category.Service, Category.Network)]
  414. [Trait(Category.AcceptanceType, Category.CheckIn)]
  415. public void SetNSGOnSubnetWithPreviousNSG()
  416. {
  417. // Setup
  418. this.networkingClientMock
  419. .Setup(c =>
  420. c.NetworkSecurityGroups.AddToSubnetAsync(
  421. VirtualNetworkName,
  422. SubnetName,
  423. It.IsAny<NetworkSecurityGroupAddAssociationParameters>(),
  424. It.IsAny<CancellationToken>()))
  425. .ThrowsAsync(AlreadySetException);
  426. this.networkingClientMock
  427. .Setup(c =>
  428. c.NetworkSecurityGroups.RemoveFromSubnetAsync(
  429. VirtualNetworkName,
  430. SubnetName,
  431. CurrentNetworkSecurityGroup,
  432. It.IsAny<CancellationToken>()))
  433. .Returns(() =>
  434. {
  435. // after we remove the current association, Set shouldn't throw an exception any more
  436. this.networkingClientMock
  437. .Setup(c =>
  438. c.NetworkSecurityGroups.AddToSubnetAsync(
  439. VirtualNetworkName,
  440. SubnetName,
  441. It.IsAny<NetworkSecurityGroupAddAssociationParameters>(),
  442. It.IsAny<CancellationToken>()))
  443. .Returns(Task.Factory.StartNew(() => new Azure.OperationStatusResponse()));
  444. return Task.Factory.StartNew(() => new Azure.OperationStatusResponse());
  445. });
  446. cmdlet = new SetAzureNetworkSecurityGroupAssociation
  447. {
  448. Name = NetworkSecurityGroupName,
  449. VirtualNetworkName = VirtualNetworkName,
  450. SubnetName = SubnetName,
  451. CommandRuntime = mockCommandRuntime,
  452. Client = this.client
  453. };
  454. cmdlet.SetParameterSet(SetAzureNetworkSecurityGroupAssociation.AddNetworkSecurityGroupAssociationToSubnet);
  455. // Action
  456. cmdlet.ExecuteCmdlet();
  457. // Assert
  458. #region Never called
  459. computeClientMock.Verify(
  460. c => c.Deployments.GetBySlotAsync(
  461. It.IsAny<string>(),
  462. It.IsAny<DeploymentSlot>(),
  463. It.IsAny<CancellationToken>()),
  464. Times.Never);
  465. networkingClientMock.Verify(
  466. c => c.NetworkSecurityGroups.AddToRoleAsync(
  467. It.IsAny<string>(),
  468. It.IsAny<string>(),
  469. It.IsAny<string>(),
  470. It.IsAny<NetworkSecurityGroupAddAssociationParameters>(),
  471. It.IsAny<CancellationToken>()),
  472. Times.Never);
  473. networkingClientMock.Verify(
  474. c => c.NetworkSecurityGroups.AddToNetworkInterfaceAsync(
  475. It.IsAny<string>(),
  476. It.IsAny<string>(),
  477. It.IsAny<string>(),
  478. It.IsAny<string>(),
  479. It.IsAny<NetworkSecurityGroupAddAssociationParameters>(),
  480. It.IsAny<CancellationToken>()),
  481. Times.Never);
  482. #endregion
  483. networkingClientMock.Verify(
  484. c => c.NetworkSecurityGroups.AddToSubnetAsync(
  485. cmdlet.VirtualNetworkName,
  486. cmdlet.SubnetName,
  487. It.Is<NetworkSecurityGroupAddAssociationParameters>(
  488. parameters => string.Equals(NetworkSecurityGroupName, parameters.Name)),
  489. It.IsAny<CancellationToken>()),
  490. Times.Exactly(2));
  491. networkingClientMock.Verify(
  492. c => c.NetworkSecurityGroups.RemoveFromSubnetAsync(
  493. cmdlet.VirtualNetworkName,
  494. cmdlet.SubnetName,
  495. CurrentNetworkSecurityGroup,
  496. It.IsAny<CancellationToken>()),
  497. Times.Once);
  498. networkingClientMock.Verify(
  499. c => c.NetworkSecurityGroups.GetForSubnetAsync(
  500. cmdlet.VirtualNetworkName,
  501. cmdlet.SubnetName,
  502. It.IsAny<CancellationToken>()),
  503. Times.Once);
  504. Assert.Equal(0, mockCommandRuntime.OutputPipeline.Count);
  505. }
  506. [Fact]
  507. [Trait(Category.Service, Category.Network)]
  508. [Trait(Category.AcceptanceType, Category.CheckIn)]
  509. public void SetNSGOnVMRoleWithPreviousNSG()
  510. {
  511. // Setup
  512. this.networkingClientMock
  513. .Setup(c =>
  514. c.NetworkSecurityGroups.AddToRoleAsync(
  515. ServiceName,
  516. DeploymentName,
  517. RoleName,
  518. It.IsAny<NetworkSecurityGroupAddAssociationParameters>(),
  519. It.IsAny<CancellationToken>()))
  520. .ThrowsAsync(AlreadySetException);
  521. this.networkingClientMock
  522. .Setup(c =>
  523. c.NetworkSecurityGroups.RemoveFromRoleAsync(
  524. ServiceName,
  525. DeploymentName,
  526. RoleName,
  527. CurrentNetworkSecurityGroup,
  528. It.IsAny<CancellationToken>()))
  529. .Returns(() =>
  530. {
  531. // after we remove the current association, Set shouldn't throw an exception any more
  532. this.networkingClientMock
  533. .Setup(c =>
  534. c.NetworkSecurityGroups.AddToRoleAsync(
  535. ServiceName,
  536. DeploymentName,
  537. RoleName,
  538. It.IsAny<NetworkSecurityGroupAddAssociationParameters>(),
  539. It.IsAny<CancellationToken>()))
  540. .Returns(Task.Factory.StartNew(() => new Azure.OperationStatusResponse()));
  541. return Task.Factory.StartNew(() => new Azure.OperationStatusResponse());
  542. });
  543. cmdlet = new SetAzureNetworkSecurityGroupAssociation
  544. {
  545. ServiceName = ServiceName,
  546. VM = VM,
  547. Name = NetworkSecurityGroupName,
  548. CommandRuntime = mockCommandRuntime,
  549. Client = this.client
  550. };
  551. cmdlet.SetParameterSet(SetAzureNetworkSecurityGroupAssociation.AddNetworkSecurityGroupAssociationToIaaSRole);
  552. // Action
  553. cmdlet.ExecuteCmdlet();
  554. // Assert
  555. #region Never called
  556. computeClientMock.Verify(
  557. c => c.Deployments.GetBySlotAsync(
  558. It.IsAny<string>(),
  559. It.IsAny<DeploymentSlot>(),
  560. It.IsAny<CancellationToken>()),
  561. Times.Never);
  562. networkingClientMock.Verify(
  563. c => c.NetworkSecurityGroups.AddToSubnetAsync(
  564. It.IsAny<string>(),
  565. It.IsAny<string>(),
  566. It.IsAny<NetworkSecurityGroupAddAssociationParameters>(),
  567. It.IsAny<CancellationToken>()),
  568. Times.Never);
  569. networkingClientMock.Verify(
  570. c => c.NetworkSecurityGroups.AddToNetworkInterfaceAsync(
  571. It.IsAny<string>(),
  572. It.IsAny<string>(),
  573. It.IsAny<string>(),
  574. It.IsAny<string>(),
  575. It.IsAny<NetworkSecurityGroupAddAssociationParameters>(),
  576. It.IsAny<CancellationToken>()),
  577. Times.Never);
  578. #endregion
  579. networkingClientMock.Verify(
  580. c => c.NetworkSecurityGroups.AddToRoleAsync(
  581. cmdlet.ServiceName,
  582. DeploymentName,
  583. RoleName,
  584. It.Is<NetworkSecurityGroupAddAssociationParameters>(
  585. parameters => string.Equals(NetworkSecurityGroupName, parameters.Name)),
  586. It.IsAny<CancellationToken>()),
  587. Times.Exactly(2));
  588. networkingClientMock.Verify(
  589. c => c.NetworkSecurityGroups.RemoveFromRoleAsync(
  590. cmdlet.ServiceName,
  591. DeploymentName,
  592. RoleName,
  593. CurrentNetworkSecurityGroup,
  594. It.IsAny<CancellationToken>()),
  595. Times.Once);
  596. networkingClientMock.Verify(
  597. c => c.NetworkSecurityGroups.GetForRoleAsync(
  598. cmdlet.ServiceName,
  599. DeploymentName,
  600. RoleName,
  601. It.IsAny<CancellationToken>()),
  602. Times.Once);
  603. Assert.Equal(0, mockCommandRuntime.OutputPipeline.Count);
  604. }
  605. [Fact]
  606. [Trait(Category.Service, Category.Network)]
  607. [Trait(Category.AcceptanceType, Category.CheckIn)]
  608. public void SetNSGOnVMNicWithPreviousNSG()
  609. {
  610. // Setup
  611. this.networkingClientMock
  612. .Setup(c =>
  613. c.NetworkSecurityGroups.AddToNetworkInterfaceAsync(
  614. ServiceName,
  615. DeploymentName,
  616. RoleName,
  617. NetworkInterfaceName,
  618. It.IsAny<NetworkSecurityGroupAddAssociationParameters>(),
  619. It.IsAny<CancellationToken>()))
  620. .ThrowsAsync(AlreadySetException);
  621. this.networkingClientMock
  622. .Setup(c =>
  623. c.NetworkSecurityGroups.RemoveFromNetworkInterfaceAsync(
  624. ServiceName,
  625. DeploymentName,
  626. RoleName,
  627. NetworkInterfaceName,
  628. CurrentNetworkSecurityGroup,
  629. It.IsAny<CancellationToken>()))
  630. .Returns(() =>
  631. {
  632. // after we remove the current association, Set shouldn't throw an exception any more
  633. this.networkingClientMock
  634. .Setup(c =>
  635. c.NetworkSecurityGroups.AddToNetworkInterfaceAsync(
  636. ServiceName,
  637. DeploymentName,
  638. RoleName,
  639. NetworkInterfaceName,
  640. It.IsAny<NetworkSecurityGroupAddAssociationParameters>(),
  641. It.IsAny<CancellationToken>()))
  642. .Returns(Task.Factory.StartNew(() => new Azure.OperationStatusResponse()));
  643. return Task.Factory.StartNew(() => new Azure.OperationStatusResponse());
  644. });
  645. cmdlet = new SetAzureNetworkSecurityGroupAssociation
  646. {
  647. ServiceName = ServiceName,
  648. VM = VM,
  649. NetworkInterfaceName = NetworkInterfaceName,
  650. Name = NetworkSecurityGroupName,
  651. CommandRuntime = mockCommandRuntime,
  652. Client = this.client
  653. };
  654. cmdlet.SetParameterSet(SetAzureNetworkSecurityGroupAssociation.AddNetworkSecurityGroupAssociationToIaaSRole);
  655. // Action
  656. cmdlet.ExecuteCmdlet();
  657. // Assert
  658. #region Never called
  659. computeClientMock.Verify(
  660. c => c.Deployments.GetBySlotAsync(
  661. It.IsAny<string>(),
  662. It.IsAny<DeploymentSlot>(),
  663. It.IsAny<CancellationToken>()),
  664. Times.Never);
  665. networkingClientMock.Verify(
  666. c => c.NetworkSecurityGroups.AddToSubnetAsync(
  667. It.IsAny<string>(),
  668. It.IsAny<string>(),
  669. It.IsAny<NetworkSecurityGroupAddAssociationParameters>(),
  670. It.IsAny<CancellationToken>()),
  671. Times.Never);
  672. networkingClientMock.Verify(
  673. c => c.NetworkSecurityGroups.AddToRoleAsync(
  674. It.IsAny<string>(),
  675. It.IsAny<string>(),
  676. It.IsAny<string>(),
  677. It.IsAny<NetworkSecurityGroupAddAssociationParameters>(),
  678. It.IsAny<CancellationToken>()),
  679. Times.Never);
  680. #endregion
  681. networkingClientMock.Verify(
  682. c => c.NetworkSecurityGroups.AddToNetworkInterfaceAsync(
  683. cmdlet.ServiceName,
  684. DeploymentName,
  685. RoleName,
  686. NetworkInterfaceName,
  687. It.Is<NetworkSecurityGroupAddAssociationParameters>(
  688. parameters => string.Equals(NetworkSecurityGroupName, parameters.Name)),
  689. It.IsAny<CancellationToken>()),
  690. Times.Exactly(2));
  691. networkingClientMock.Verify(
  692. c => c.NetworkSecurityGroups.RemoveFromNetworkInterfaceAsync(
  693. cmdlet.ServiceName,
  694. DeploymentName,
  695. RoleName,
  696. NetworkInterfaceName,
  697. CurrentNetworkSecurityGroup,
  698. It.IsAny<CancellationToken>()),
  699. Times.Once);
  700. networkingClientMock.Verify(
  701. c => c.NetworkSecurityGroups.GetForNetworkInterfaceAsync(
  702. cmdlet.ServiceName,
  703. DeploymentName,
  704. RoleName,
  705. NetworkInterfaceName,
  706. It.IsAny<CancellationToken>()),
  707. Times.Once);
  708. Assert.Equal(0, mockCommandRuntime.OutputPipeline.Count);
  709. }
  710. [Fact]
  711. [Trait(Category.Service, Category.Network)]
  712. [Trait(Category.AcceptanceType, Category.CheckIn)]
  713. public void SetNSGOnRoleByNameWithPreviousNSG()
  714. {
  715. // Setup
  716. this.networkingClientMock
  717. .Setup(c =>
  718. c.NetworkSecurityGroups.AddToRoleAsync(
  719. ServiceName,
  720. DeploymentName,
  721. RoleName,
  722. It.IsAny<NetworkSecurityGroupAddAssociationParameters>(),
  723. It.IsAny<CancellationToken>()))
  724. .ThrowsAsync(AlreadySetException);
  725. this.networkingClientMock
  726. .Setup(c =>
  727. c.NetworkSecurityGroups.RemoveFromRoleAsync(
  728. ServiceName,
  729. DeploymentName,
  730. RoleName,
  731. CurrentNetworkSecurityGroup,
  732. It.IsAny<CancellationToken>()))
  733. .Returns(() =>
  734. {
  735. // after we remove the current association, Set shouldn't throw an exception any more
  736. this.networkingClientMock
  737. .Setup(c =>
  738. c.NetworkSecurityGroups.AddToRoleAsync(
  739. ServiceName,
  740. DeploymentName,
  741. RoleName,
  742. It.IsAny<NetworkSecurityGroupAddAssociationParameters>(),
  743. It.IsAny<CancellationToken>()))
  744. .Returns(Task.Factory.StartNew(() => new Azure.OperationStatusResponse()));
  745. return Task.Factory.StartNew(() => new Azure.OperationStatusResponse());
  746. });
  747. cmdlet = new SetAzureNetworkSecurityGroupAssociation
  748. {
  749. ServiceName = ServiceName,
  750. RoleName = RoleName,
  751. Name = NetworkSecurityGroupName,
  752. CommandRuntime = mockCommandRuntime,
  753. Client = this.client
  754. };
  755. cmdlet.SetParameterSet(SetAzureNetworkSecurityGroupAssociation.AddNetworkSecurityGroupAssociationToPaaSRole);
  756. // Action
  757. cmdlet.ExecuteCmdlet();
  758. // Assert
  759. #region Never called
  760. networkingClientMock.Verify(
  761. c => c.NetworkSecurityGroups.AddToSubnetAsync(
  762. It.IsAny<string>(),
  763. It.IsAny<string>(),
  764. It.IsAny<NetworkSecurityGroupAddAssociationParameters>(),
  765. It.IsAny<CancellationToken>()),
  766. Times.Never);
  767. networkingClientMock.Verify(
  768. c => c.NetworkSecurityGroups.AddToNetworkInterfaceAsync(
  769. It.IsAny<string>(),
  770. It.IsAny<string>(),
  771. It.IsAny<string>(),
  772. It.IsAny<string>(),
  773. It.IsAny<NetworkSecurityGroupAddAssociationParameters>(),
  774. It.IsAny<CancellationToken>()),
  775. Times.Never);
  776. #endregion
  777. computeClientMock.Verify(
  778. c => c.Deployments.GetBySlotAsync(
  779. It.IsAny<string>(),
  780. It.IsAny<DeploymentSlot>(),
  781. It.IsAny<CancellationToken>()),
  782. Times.Once);
  783. networkingClientMock.Verify(
  784. c => c.NetworkSecurityGroups.AddToRoleAsync(
  785. cmdlet.ServiceName,
  786. DeploymentName,
  787. RoleName,
  788. It.Is<NetworkSecurityGroupAddAssociationParameters>(
  789. parameters => string.Equals(NetworkSecurityGroupName, parameters.Name)),
  790. It.IsAny<CancellationToken>()),
  791. Times.Exactly(2));
  792. networkingClientMock.Verify(
  793. c => c.NetworkSecurityGroups.RemoveFromRoleAsync(
  794. cmdlet.ServiceName,
  795. DeploymentName,
  796. RoleName,
  797. CurrentNetworkSecurityGroup,
  798. It.IsAny<CancellationToken>()),
  799. Times.Once);
  800. networkingClientMock.Verify(
  801. c => c.NetworkSecurityGroups.GetForRoleAsync(
  802. cmdlet.ServiceName,
  803. DeploymentName,
  804. RoleName,
  805. It.IsAny<CancellationToken>()),
  806. Times.Once);
  807. Assert.Equal(0, mockCommandRuntime.OutputPipeline.Count);
  808. }
  809. [Fact]
  810. [Trait(Category.Service, Category.Network)]
  811. [Trait(Category.AcceptanceType, Category.CheckIn)]
  812. public void SetNSGOnNicByNameWithPreviousNSG()
  813. {
  814. // Setup
  815. this.networkingClientMock
  816. .Setup(c =>
  817. c.NetworkSecurityGroups.AddToNetworkInterfaceAsync(
  818. ServiceName,
  819. DeploymentName,
  820. RoleName,
  821. NetworkInterfaceName,
  822. It.IsAny<NetworkSecurityGroupAddAssociationParameters>(),
  823. It.IsAny<CancellationToken>()))
  824. .ThrowsAsync(AlreadySetException);
  825. this.networkingClientMock
  826. .Setup(c =>
  827. c.NetworkSecurityGroups.RemoveFromNetworkInterfaceAsync(
  828. ServiceName,
  829. DeploymentName,
  830. RoleName,
  831. NetworkInterfaceName,
  832. CurrentNetworkSecurityGroup,
  833. It.IsAny<CancellationToken>()))
  834. .Returns(() =>
  835. {
  836. // after we remove the current association, Set shouldn't throw an exception any more
  837. this.networkingClientMock
  838. .Setup(c =>
  839. c.NetworkSecurityGroups.AddToNetworkInterfaceAsync(
  840. ServiceName,
  841. DeploymentName,
  842. RoleName,
  843. NetworkInterfaceName,
  844. It.IsAny<NetworkSecurityGroupAddAssociationParameters>(),
  845. It.IsAny<CancellationToken>()))
  846. .Returns(Task.Factory.StartNew(() => new Azure.OperationStatusResponse()));
  847. return Task.Factory.StartNew(() => new Azure.OperationStatusResponse());
  848. });
  849. cmdlet = new SetAzureNetworkSecurityGroupAssociation
  850. {
  851. ServiceName = ServiceName,
  852. RoleName = RoleName,
  853. NetworkInterfaceName = NetworkInterfaceName,
  854. Name = NetworkSecurityGroupName,
  855. CommandRuntime = mockCommandRuntime,
  856. Client = this.client
  857. };
  858. cmdlet.SetParameterSet(SetAzureNetworkSecurityGroupAssociation.AddNetworkSecurityGroupAssociationToPaaSRole);
  859. // Action
  860. cmdlet.ExecuteCmdlet();
  861. // Assert
  862. #region Never called
  863. networkingClientMock.Verify(
  864. c => c.NetworkSecurityGroups.AddToSubnetAsync(
  865. It.IsAny<string>(),
  866. It.IsAny<string>(),
  867. It.IsAny<NetworkSecurityGroupAddAssociationParameters>(),
  868. It.IsAny<CancellationToken>()),
  869. Times.Never);
  870. networkingClientMock.Verify(
  871. c => c.NetworkSecurityGroups.AddToRoleAsync(
  872. It.IsAny<string>(),
  873. It.IsAny<string>(),
  874. It.IsAny<string>(),
  875. It.IsAny<NetworkSecurityGroupAddAssociationParameters>(),
  876. It.IsAny<CancellationToken>()),
  877. Times.Never);
  878. #endregion
  879. networkingClientMock.Verify(
  880. c => c.NetworkSecurityGroups.AddToNetworkInterfaceAsync(
  881. cmdlet.ServiceName,
  882. DeploymentName,
  883. RoleName,
  884. NetworkInterfaceName,
  885. It.Is<NetworkSecurityGroupAddAssociationParameters>(
  886. parameters => string.Equals(NetworkSecurityGroupName, parameters.Name)),
  887. It.IsAny<CancellationToken>()),
  888. Times.Exactly(2));
  889. networkingClientMock.Verify(
  890. c => c.NetworkSecurityGroups.RemoveFromNetworkInterfaceAsync(
  891. cmdlet.ServiceName,
  892. DeploymentName,
  893. RoleName,
  894. NetworkInterfaceName,
  895. CurrentNetworkSecurityGroup,
  896. It.IsAny<CancellationToken>()),
  897. Times.Once);
  898. networkingClientMock.Verify(
  899. c => c.NetworkSecurityGroups.GetForNetworkInterfaceAsync(
  900. cmdlet.ServiceName,
  901. DeploymentName,
  902. RoleName,
  903. NetworkInterfaceName,
  904. It.IsAny<CancellationToken>()),
  905. Times.Once);
  906. computeClientMock.Verify(
  907. c => c.Deployments.GetBySlotAsync(
  908. It.IsAny<string>(),
  909. It.IsAny<DeploymentSlot>(),
  910. It.IsAny<CancellationToken>()),
  911. Times.Once);
  912. Assert.Equal(0, mockCommandRuntime.OutputPipeline.Count);
  913. }
  914. #endregion
  915. }
  916. }