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

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

https://gitlab.com/jslee1/azure-powershell
C# | 433 lines | 360 code | 47 blank | 26 comment | 0 complexity | 253079713499577d75e30c60becb704a 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 System.Collections.Generic;
  15. using System.Linq;
  16. using System.Management.Automation;
  17. using System.Threading;
  18. using System.Threading.Tasks;
  19. using Hyak.Common;
  20. using Microsoft.WindowsAzure.Commands.ServiceManagement.Network.NetworkSecurityGroup.Association;
  21. using Microsoft.WindowsAzure.Commands.ServiceManagement.Network.NetworkSecurityGroup.Model;
  22. using Microsoft.WindowsAzure.Commands.Common.Test.Mocks;
  23. using Microsoft.WindowsAzure.Commands.ScenarioTest;
  24. using Microsoft.WindowsAzure.Commands.ServiceManagement.Model;
  25. using Microsoft.WindowsAzure.Commands.Utilities.Common;
  26. using Microsoft.WindowsAzure.Management.Compute;
  27. using Microsoft.WindowsAzure.Management.Network;
  28. using Microsoft.WindowsAzure.Management;
  29. using Models = Microsoft.WindowsAzure.Management.Network.Models;
  30. using Moq;
  31. using Xunit;
  32. namespace Microsoft.WindowsAzure.Commands.ServiceManagement.Network.Test.NetworkSecurityGroups
  33. {
  34. public class GetNetworkSecurityGroupAssociationTests
  35. {
  36. private const string ServiceName = "serviceName";
  37. private const string DeploymentName = "deploymentName";
  38. private const string RoleName = "roleName";
  39. private const string NetworkInterfaceName = "networkInterfaceName";
  40. private const string NetworkSecurityGroupName = "networkSecurityGroupName";
  41. private const string NSGLocation = "usnorth";
  42. private const string NSGLabel = "My NSG label";
  43. private const string VirtualNetworkName = "virtualNetworkName";
  44. private const string SubnetName = "subnetName";
  45. private PersistentVMRoleContext VM = new PersistentVMRoleContext()
  46. {
  47. // these are the only 2 properties being used in the cmdlet
  48. Name = RoleName,
  49. DeploymentName = DeploymentName,
  50. };
  51. private IList<Models.NetworkSecurityRule> Rules = new List<Models.NetworkSecurityRule>()
  52. {
  53. new Models.NetworkSecurityRule()
  54. {
  55. Name = "rule1",
  56. Action = "Allow",
  57. Type = "Inbound",
  58. Protocol = "TCP",
  59. IsDefault = true,
  60. DestinationAddressPrefix = "*",
  61. DestinationPortRange = "*",
  62. SourceAddressPrefix = "*",
  63. SourcePortRange = "*",
  64. Priority = 100
  65. },
  66. };
  67. private MockCommandRuntime mockCommandRuntime;
  68. private GetAzureNetworkSecurityGroupAssociation cmdlet;
  69. private NetworkClient client;
  70. private Mock<INetworkManagementClient> networkingClientMock;
  71. private Mock<IComputeManagementClient> computeClientMock;
  72. private Mock<IManagementClient> managementClientMock;
  73. public GetNetworkSecurityGroupAssociationTests()
  74. {
  75. this.networkingClientMock = new Mock<INetworkManagementClient>();
  76. this.computeClientMock = new Mock<IComputeManagementClient>();
  77. this.managementClientMock = new Mock<IManagementClient>();
  78. this.mockCommandRuntime = new MockCommandRuntime();
  79. this.client = new NetworkClient(
  80. networkingClientMock.Object,
  81. computeClientMock.Object,
  82. managementClientMock.Object,
  83. mockCommandRuntime);
  84. this.networkingClientMock
  85. .Setup(c =>
  86. c.NetworkSecurityGroups.GetForSubnetAsync(
  87. VirtualNetworkName,
  88. SubnetName,
  89. It.IsAny<CancellationToken>()))
  90. .Returns(Task.Factory.StartNew(() =>
  91. new Models.NetworkSecurityGroupGetAssociationResponse()
  92. {
  93. Name = NetworkSecurityGroupName
  94. }));
  95. this.networkingClientMock
  96. .Setup(c =>
  97. c.NetworkSecurityGroups.GetForRoleAsync(
  98. ServiceName,
  99. DeploymentName,
  100. RoleName,
  101. It.IsAny<CancellationToken>()))
  102. .Returns(Task.Factory.StartNew(() =>
  103. new Models.NetworkSecurityGroupGetAssociationResponse()
  104. {
  105. Name = NetworkSecurityGroupName
  106. }));
  107. this.networkingClientMock
  108. .Setup(c =>
  109. c.NetworkSecurityGroups.GetForNetworkInterfaceAsync(
  110. ServiceName,
  111. DeploymentName,
  112. RoleName,
  113. NetworkInterfaceName,
  114. It.IsAny<CancellationToken>()))
  115. .Returns(Task.Factory.StartNew(() =>
  116. new Models.NetworkSecurityGroupGetAssociationResponse()
  117. {
  118. Name = NetworkSecurityGroupName
  119. }));
  120. this.networkingClientMock
  121. .Setup(c =>
  122. c.NetworkSecurityGroups.GetAsync(
  123. NetworkSecurityGroupName,
  124. null,
  125. It.IsAny<CancellationToken>()))
  126. .Returns(Task.Factory.StartNew(() =>
  127. new Models.NetworkSecurityGroupGetResponse()
  128. {
  129. Name = NetworkSecurityGroupName,
  130. Location = NSGLocation,
  131. Label = NSGLabel
  132. }));
  133. this.networkingClientMock
  134. .Setup(c =>
  135. c.NetworkSecurityGroups.GetAsync(
  136. NetworkSecurityGroupName,
  137. "Full",
  138. It.IsAny<CancellationToken>()))
  139. .Returns(Task.Factory.StartNew(() =>
  140. new Models.NetworkSecurityGroupGetResponse()
  141. {
  142. Name = NetworkSecurityGroupName,
  143. Location = NSGLocation,
  144. Label = NSGLabel,
  145. Rules = Rules
  146. }));
  147. }
  148. #region No Details
  149. [Fact]
  150. [Trait(Category.Service, Category.Network)]
  151. [Trait(Category.AcceptanceType, Category.CheckIn)]
  152. public void GetNSGForSubnetNoDetails()
  153. {
  154. // Setup
  155. cmdlet = new GetAzureNetworkSecurityGroupAssociation
  156. {
  157. VirtualNetworkName = VirtualNetworkName,
  158. SubnetName = SubnetName,
  159. CommandRuntime = mockCommandRuntime,
  160. Client = this.client
  161. };
  162. cmdlet.SetParameterSet(GetAzureNetworkSecurityGroupAssociation.GetNetworkSecurityGroupAssociationForSubnet);
  163. // Action
  164. cmdlet.ExecuteCmdlet();
  165. networkingClientMock.Verify(
  166. c => c.NetworkSecurityGroups.GetForSubnetAsync(
  167. VirtualNetworkName,
  168. SubnetName,
  169. It.IsAny<CancellationToken>()),
  170. Times.Once);
  171. networkingClientMock.Verify(
  172. c => c.NetworkSecurityGroups.GetAsync(
  173. NetworkSecurityGroupName,
  174. null,
  175. It.IsAny<CancellationToken>()),
  176. Times.Once);
  177. Assert.Equal(1, mockCommandRuntime.OutputPipeline.Count);
  178. Assert.IsType<SimpleNetworkSecurityGroup>(mockCommandRuntime.OutputPipeline.Single());
  179. var nsg = (SimpleNetworkSecurityGroup)(mockCommandRuntime.OutputPipeline.Single());
  180. Assert.Equal(NetworkSecurityGroupName, nsg.Name);
  181. Assert.Equal(NSGLocation, nsg.Location);
  182. Assert.Equal(NSGLabel, nsg.Label);
  183. }
  184. [Fact]
  185. [Trait(Category.Service, Category.Network)]
  186. [Trait(Category.AcceptanceType, Category.CheckIn)]
  187. public void GetNSGForVMRoleNoDetails()
  188. {
  189. // Setup
  190. cmdlet = new GetAzureNetworkSecurityGroupAssociation
  191. {
  192. VM = VM,
  193. ServiceName = ServiceName,
  194. CommandRuntime = mockCommandRuntime,
  195. Client = this.client
  196. };
  197. cmdlet.SetParameterSet(GetAzureNetworkSecurityGroupAssociation.GetNetworkSecurityGroupAssociationForIaaSRole);
  198. // Action
  199. cmdlet.ExecuteCmdlet();
  200. networkingClientMock.Verify(
  201. c => c.NetworkSecurityGroups.GetForRoleAsync(
  202. ServiceName,
  203. DeploymentName,
  204. RoleName,
  205. It.IsAny<CancellationToken>()),
  206. Times.Once);
  207. networkingClientMock.Verify(
  208. c => c.NetworkSecurityGroups.GetAsync(
  209. NetworkSecurityGroupName,
  210. null,
  211. It.IsAny<CancellationToken>()),
  212. Times.Once);
  213. Assert.Equal(1, mockCommandRuntime.OutputPipeline.Count);
  214. Assert.IsType<SimpleNetworkSecurityGroup>(mockCommandRuntime.OutputPipeline.Single());
  215. var nsg = (SimpleNetworkSecurityGroup)(mockCommandRuntime.OutputPipeline.Single());
  216. Assert.Equal(NetworkSecurityGroupName, nsg.Name);
  217. Assert.Equal(NSGLocation, nsg.Location);
  218. Assert.Equal(NSGLabel, nsg.Label);
  219. }
  220. [Fact]
  221. [Trait(Category.Service, Category.Network)]
  222. [Trait(Category.AcceptanceType, Category.CheckIn)]
  223. public void GetNSGForVMNicNoDetails()
  224. {
  225. // Setup
  226. cmdlet = new GetAzureNetworkSecurityGroupAssociation
  227. {
  228. VM = VM,
  229. ServiceName = ServiceName,
  230. NetworkInterfaceName = NetworkInterfaceName,
  231. CommandRuntime = mockCommandRuntime,
  232. Client = this.client
  233. };
  234. cmdlet.SetParameterSet(GetAzureNetworkSecurityGroupAssociation.GetNetworkSecurityGroupAssociationForIaaSRole);
  235. // Action
  236. cmdlet.ExecuteCmdlet();
  237. networkingClientMock.Verify(
  238. c => c.NetworkSecurityGroups.GetForNetworkInterfaceAsync(
  239. ServiceName,
  240. DeploymentName,
  241. RoleName,
  242. NetworkInterfaceName,
  243. It.IsAny<CancellationToken>()),
  244. Times.Once);
  245. networkingClientMock.Verify(
  246. c => c.NetworkSecurityGroups.GetAsync(
  247. NetworkSecurityGroupName,
  248. null,
  249. It.IsAny<CancellationToken>()),
  250. Times.Once);
  251. Assert.Equal(1, mockCommandRuntime.OutputPipeline.Count);
  252. Assert.IsType<SimpleNetworkSecurityGroup>(mockCommandRuntime.OutputPipeline.Single());
  253. var nsg = (SimpleNetworkSecurityGroup)(mockCommandRuntime.OutputPipeline.Single());
  254. Assert.Equal(NetworkSecurityGroupName, nsg.Name);
  255. Assert.Equal(NSGLocation, nsg.Location);
  256. Assert.Equal(NSGLabel, nsg.Label);
  257. }
  258. #endregion
  259. #region With Details
  260. [Fact]
  261. [Trait(Category.Service, Category.Network)]
  262. [Trait(Category.AcceptanceType, Category.CheckIn)]
  263. public void GetNSGForSubnetWithDetails()
  264. {
  265. // Setup
  266. cmdlet = new GetAzureNetworkSecurityGroupAssociation
  267. {
  268. VirtualNetworkName = VirtualNetworkName,
  269. SubnetName = SubnetName,
  270. CommandRuntime = mockCommandRuntime,
  271. Detailed = new SwitchParameter(true),
  272. Client = this.client
  273. };
  274. cmdlet.SetParameterSet(GetAzureNetworkSecurityGroupAssociation.GetNetworkSecurityGroupAssociationForSubnet);
  275. // Action
  276. cmdlet.ExecuteCmdlet();
  277. networkingClientMock.Verify(
  278. c => c.NetworkSecurityGroups.GetForSubnetAsync(
  279. VirtualNetworkName,
  280. SubnetName,
  281. It.IsAny<CancellationToken>()),
  282. Times.Once);
  283. networkingClientMock.Verify(
  284. c => c.NetworkSecurityGroups.GetAsync(
  285. NetworkSecurityGroupName,
  286. "Full",
  287. It.IsAny<CancellationToken>()),
  288. Times.Once);
  289. Assert.Equal(1, mockCommandRuntime.OutputPipeline.Count);
  290. Assert.IsType<NetworkSecurityGroupWithRules>(mockCommandRuntime.OutputPipeline.Single());
  291. var nsg = (NetworkSecurityGroupWithRules)(mockCommandRuntime.OutputPipeline.Single());
  292. Assert.Equal(NetworkSecurityGroupName, nsg.Name);
  293. Assert.Equal(NSGLabel, nsg.Label);
  294. Assert.Equal(NSGLocation, nsg.Location);
  295. Assert.NotEmpty(nsg.Rules);
  296. Assert.Equal(Rules.First().Name, nsg.Rules.First().Name);
  297. Assert.Equal(Rules.First().Action, nsg.Rules.First().Action);
  298. Assert.Equal(Rules.First().Protocol, nsg.Rules.First().Protocol);
  299. }
  300. [Fact]
  301. [Trait(Category.Service, Category.Network)]
  302. [Trait(Category.AcceptanceType, Category.CheckIn)]
  303. public void GetNSGForVMRoleDetails()
  304. {
  305. // Setup
  306. cmdlet = new GetAzureNetworkSecurityGroupAssociation
  307. {
  308. VM = VM,
  309. ServiceName = ServiceName,
  310. CommandRuntime = mockCommandRuntime,
  311. Detailed = new SwitchParameter(true),
  312. Client = this.client
  313. };
  314. cmdlet.SetParameterSet(GetAzureNetworkSecurityGroupAssociation.GetNetworkSecurityGroupAssociationForIaaSRole);
  315. // Action
  316. cmdlet.ExecuteCmdlet();
  317. networkingClientMock.Verify(
  318. c => c.NetworkSecurityGroups.GetForRoleAsync(
  319. ServiceName,
  320. DeploymentName,
  321. RoleName,
  322. It.IsAny<CancellationToken>()),
  323. Times.Once);
  324. networkingClientMock.Verify(
  325. c => c.NetworkSecurityGroups.GetAsync(
  326. NetworkSecurityGroupName,
  327. "Full",
  328. It.IsAny<CancellationToken>()),
  329. Times.Once);
  330. Assert.Equal(1, mockCommandRuntime.OutputPipeline.Count);
  331. Assert.IsType<NetworkSecurityGroupWithRules>(mockCommandRuntime.OutputPipeline.Single());
  332. var nsg = (NetworkSecurityGroupWithRules)(mockCommandRuntime.OutputPipeline.Single());
  333. Assert.Equal(NetworkSecurityGroupName, nsg.Name);
  334. Assert.Equal(NSGLabel, nsg.Label);
  335. Assert.Equal(NSGLocation, nsg.Location);
  336. Assert.NotEmpty(nsg.Rules);
  337. Assert.Equal(Rules.First().Name, nsg.Rules.First().Name);
  338. Assert.Equal(Rules.First().Action, nsg.Rules.First().Action);
  339. Assert.Equal(Rules.First().Protocol, nsg.Rules.First().Protocol);
  340. }
  341. [Fact]
  342. [Trait(Category.Service, Category.Network)]
  343. [Trait(Category.AcceptanceType, Category.CheckIn)]
  344. public void GetNSGForVMNicDetails()
  345. {
  346. // Setup
  347. cmdlet = new GetAzureNetworkSecurityGroupAssociation
  348. {
  349. VM = VM,
  350. ServiceName = ServiceName,
  351. NetworkInterfaceName = NetworkInterfaceName,
  352. CommandRuntime = mockCommandRuntime,
  353. Detailed = new SwitchParameter(true),
  354. Client = this.client
  355. };
  356. cmdlet.SetParameterSet(GetAzureNetworkSecurityGroupAssociation.GetNetworkSecurityGroupAssociationForIaaSRole);
  357. // Action
  358. cmdlet.ExecuteCmdlet();
  359. networkingClientMock.Verify(
  360. c => c.NetworkSecurityGroups.GetForNetworkInterfaceAsync(
  361. ServiceName,
  362. DeploymentName,
  363. RoleName,
  364. NetworkInterfaceName,
  365. It.IsAny<CancellationToken>()),
  366. Times.Once);
  367. networkingClientMock.Verify(
  368. c => c.NetworkSecurityGroups.GetAsync(
  369. NetworkSecurityGroupName,
  370. "Full",
  371. It.IsAny<CancellationToken>()),
  372. Times.Once);
  373. Assert.Equal(1, mockCommandRuntime.OutputPipeline.Count);
  374. Assert.IsType<NetworkSecurityGroupWithRules>(mockCommandRuntime.OutputPipeline.Single());
  375. var nsg = (NetworkSecurityGroupWithRules)(mockCommandRuntime.OutputPipeline.Single());
  376. Assert.Equal(NetworkSecurityGroupName, nsg.Name);
  377. Assert.Equal(NSGLabel, nsg.Label);
  378. Assert.Equal(NSGLocation, nsg.Location);
  379. Assert.NotEmpty(nsg.Rules);
  380. Assert.Equal(Rules.First().Name, nsg.Rules.First().Name);
  381. Assert.Equal(Rules.First().Action, nsg.Rules.First().Action);
  382. Assert.Equal(Rules.First().Protocol, nsg.Rules.First().Protocol);
  383. }
  384. #endregion
  385. }
  386. }