Skip to content

Commit f008bda

Browse files
author
Ed Munoz
committed
Add unit tests for *AzureNetworkSecurityGroupAssociation
1 parent 8b56714 commit f008bda

File tree

8 files changed

+1186
-50
lines changed

8 files changed

+1186
-50
lines changed

src/ServiceManagement/Network/Commands.Network.Test/Commands.Network.Test.csproj

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

0 commit comments

Comments
 (0)