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 Microsoft . Azure . Commands . Network . Routes ;
17
+ using Microsoft . WindowsAzure . Commands . Common . Test . Mocks ;
18
+ using Microsoft . WindowsAzure . Commands . ServiceManagement . Model ;
19
+ using Microsoft . WindowsAzure . Commands . Utilities . Common ;
20
+ using Microsoft . WindowsAzure . Management ;
21
+ using Microsoft . WindowsAzure . Management . Compute ;
22
+ using Microsoft . WindowsAzure . Management . Compute . Models ;
23
+ using Microsoft . WindowsAzure . Management . Network ;
24
+ using Microsoft . WindowsAzure . Management . Network . Models ;
25
+ using Moq ;
26
+ using System . Threading ;
27
+ using System . Threading . Tasks ;
28
+ using Xunit ;
29
+
30
+ namespace Microsoft . Azure . Commands . Network . Test . Routes
31
+ {
32
+ public class GetEffectiveRouteTests
33
+ {
34
+ private const string ServiceName = "serviceName" ;
35
+ private const string DeploymentName = "deploymentName" ;
36
+ private const string RoleInstanceName = "roleInstanceName" ;
37
+ private const string NetworkInterfaceName = "networkInterfaceName" ;
38
+
39
+ private PersistentVMRoleContext VM = new PersistentVMRoleContext ( )
40
+ {
41
+ // these are the only 2 properties being used in the cmdlet
42
+ InstanceName = RoleInstanceName ,
43
+ DeploymentName = DeploymentName
44
+ } ;
45
+
46
+ private MockCommandRuntime mockCommandRuntime ;
47
+
48
+ private GetAzureEffectiveRouteTable cmdlet ;
49
+
50
+ private NetworkClient client ;
51
+ private Mock < INetworkManagementClient > networkingClientMock ;
52
+ private Mock < IComputeManagementClient > computeClientMock ;
53
+ private Mock < IManagementClient > managementClientMock ;
54
+
55
+ public GetEffectiveRouteTests ( )
56
+ {
57
+ this . networkingClientMock = new Mock < INetworkManagementClient > ( ) ;
58
+ this . computeClientMock = new Mock < IComputeManagementClient > ( ) ;
59
+ this . managementClientMock = new Mock < IManagementClient > ( ) ;
60
+ this . mockCommandRuntime = new MockCommandRuntime ( ) ;
61
+ this . client = new NetworkClient (
62
+ networkingClientMock . Object ,
63
+ computeClientMock . Object ,
64
+ managementClientMock . Object ,
65
+ mockCommandRuntime ) ;
66
+
67
+ this . computeClientMock
68
+ . Setup ( c => c . Deployments . GetBySlotAsync ( ServiceName , DeploymentSlot . Production , It . IsAny < CancellationToken > ( ) ) )
69
+ . Returns ( Task . Factory . StartNew ( ( ) => new DeploymentGetResponse ( )
70
+ {
71
+ Name = DeploymentName
72
+ } ) ) ;
73
+
74
+ this . networkingClientMock
75
+ . Setup ( c => c . Routes . GetEffectiveRouteTableForRoleInstanceAsync (
76
+ ServiceName ,
77
+ DeploymentName ,
78
+ RoleInstanceName ,
79
+ It . IsAny < CancellationToken > ( ) ) )
80
+ . Returns ( Task . Factory . StartNew ( ( ) => new GetEffectiveRouteTableResponse ( )
81
+ {
82
+ EffectiveRouteTable = new EffectiveRouteTable ( )
83
+ {
84
+ EffectiveRoutes = new List < EffectiveRoute > ( )
85
+ }
86
+ } ) ) ;
87
+
88
+ this . networkingClientMock
89
+ . Setup ( c => c . Routes . GetEffectiveRouteTableForNetworkInterfaceAsync (
90
+ ServiceName ,
91
+ DeploymentName ,
92
+ RoleInstanceName ,
93
+ NetworkInterfaceName ,
94
+ It . IsAny < CancellationToken > ( ) ) )
95
+ . Returns ( Task . Factory . StartNew ( ( ) => new GetEffectiveRouteTableResponse ( )
96
+ {
97
+ EffectiveRouteTable = new EffectiveRouteTable ( )
98
+ {
99
+ EffectiveRoutes = new List < EffectiveRoute > ( )
100
+ }
101
+ } ) ) ;
102
+ }
103
+
104
+ [ Fact ]
105
+ public void GetEffectiveRouteTableOnRoleSucceeds ( )
106
+ {
107
+ GetEffectiveRouteTableForRoleInstance ( ) ;
108
+ }
109
+
110
+ [ Fact ]
111
+ public void GetEffectiveRouteTableOnVMSucceeds ( )
112
+ {
113
+ GetEffectiveRouteTableForVM ( ) ;
114
+ }
115
+
116
+ [ Fact ]
117
+ public void GetEffectiveRouteTableOnVMNicSucceeds ( )
118
+ {
119
+ GetEffectiveRouteTableForVMNic ( ) ;
120
+ }
121
+
122
+ #region helpers
123
+
124
+ private void GetEffectiveRouteTableForRoleInstance ( )
125
+ {
126
+ // Setup
127
+ cmdlet = new GetAzureEffectiveRouteTable
128
+ {
129
+ ServiceName = ServiceName ,
130
+ RoleInstanceName = RoleInstanceName ,
131
+ CommandRuntime = mockCommandRuntime ,
132
+ Client = this . client
133
+ } ;
134
+ cmdlet . SetParameterSet ( GetAzureEffectiveRouteTable . SlotGetEffectiveRouteTableParamSet ) ;
135
+
136
+ // Action
137
+ cmdlet . ExecuteCmdlet ( ) ;
138
+
139
+ // Assert
140
+ computeClientMock . Verify (
141
+ c => c . Deployments . GetBySlotAsync (
142
+ ServiceName ,
143
+ DeploymentSlot . Production ,
144
+ It . IsAny < CancellationToken > ( ) ) ,
145
+ Times . Once ( ) ) ;
146
+
147
+ networkingClientMock . Verify (
148
+ c => c . Routes . GetEffectiveRouteTableForRoleInstanceAsync (
149
+ cmdlet . ServiceName ,
150
+ DeploymentName ,
151
+ cmdlet . RoleInstanceName ,
152
+ It . IsAny < CancellationToken > ( ) ) ,
153
+ Times . Once ( ) ) ;
154
+
155
+ Assert . Equal ( 1 , mockCommandRuntime . OutputPipeline . Count ) ;
156
+ Assert . IsType < EffectiveRouteTable > ( mockCommandRuntime . OutputPipeline [ 0 ] ) ;
157
+ }
158
+
159
+ private void GetEffectiveRouteTableForVM ( )
160
+ {
161
+ // Setup
162
+
163
+ cmdlet = new GetAzureEffectiveRouteTable
164
+ {
165
+ ServiceName = ServiceName ,
166
+ VM = VM ,
167
+ CommandRuntime = mockCommandRuntime ,
168
+ Client = this . client ,
169
+ } ;
170
+ cmdlet . SetParameterSet ( GetAzureEffectiveRouteTable . IaaSGetEffectiveRouteTableParamSet ) ;
171
+
172
+ // Action
173
+ cmdlet . ExecuteCmdlet ( ) ;
174
+
175
+ // Assert
176
+ computeClientMock . Verify (
177
+ c => c . Deployments . GetBySlotAsync (
178
+ ServiceName ,
179
+ DeploymentSlot . Production ,
180
+ It . IsAny < CancellationToken > ( ) ) ,
181
+ Times . Never ( ) ) ;
182
+
183
+ networkingClientMock . Verify (
184
+ c => c . Routes . GetEffectiveRouteTableForRoleInstanceAsync (
185
+ cmdlet . ServiceName ,
186
+ DeploymentName ,
187
+ VM . InstanceName ,
188
+ It . IsAny < CancellationToken > ( ) ) ,
189
+ Times . Once ( ) ) ;
190
+
191
+ Assert . Equal ( 1 , mockCommandRuntime . OutputPipeline . Count ) ;
192
+ Assert . IsType < EffectiveRouteTable > ( mockCommandRuntime . OutputPipeline [ 0 ] ) ;
193
+ }
194
+
195
+ private void GetEffectiveRouteTableForVMNic ( )
196
+ {
197
+ // Setup
198
+ cmdlet = new GetAzureEffectiveRouteTable
199
+ {
200
+ ServiceName = ServiceName ,
201
+ VM = VM ,
202
+ NetworkInterfaceName = NetworkInterfaceName ,
203
+ CommandRuntime = mockCommandRuntime ,
204
+ Client = this . client ,
205
+ } ;
206
+ cmdlet . SetParameterSet ( GetAzureEffectiveRouteTable . IaaSGetEffectiveRouteTableParamSet ) ;
207
+
208
+ // Action
209
+ cmdlet . ExecuteCmdlet ( ) ;
210
+
211
+ // Assert
212
+ computeClientMock . Verify (
213
+ c => c . Deployments . GetBySlotAsync (
214
+ ServiceName ,
215
+ DeploymentSlot . Production ,
216
+ It . IsAny < CancellationToken > ( ) ) ,
217
+ Times . Never ( ) ) ;
218
+
219
+ networkingClientMock . Verify (
220
+ c => c . Routes . GetEffectiveRouteTableForNetworkInterfaceAsync (
221
+ cmdlet . ServiceName ,
222
+ DeploymentName ,
223
+ VM . InstanceName ,
224
+ cmdlet . NetworkInterfaceName ,
225
+ It . IsAny < CancellationToken > ( ) ) ,
226
+ Times . Once ( ) ) ;
227
+
228
+ Assert . Equal ( 1 , mockCommandRuntime . OutputPipeline . Count ) ;
229
+ Assert . IsType < EffectiveRouteTable > ( mockCommandRuntime . OutputPipeline [ 0 ] ) ;
230
+ }
231
+
232
+ #endregion
233
+ }
234
+ }
0 commit comments