forked from Azure/azure-powershell
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVerifiers.cs
407 lines (374 loc) · 15.2 KB
/
Verifiers.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
// ----------------------------------------------------------------------------------
//
// Copyright Microsoft Corporation
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// ----------------------------------------------------------------------------------
using System;
using System.Reflection;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Microsoft.WindowsAzure.Commands.ServiceManagement.Model;
using Microsoft.WindowsAzure.Commands.ServiceManagement.Test.FunctionalTests.ConfigDataInfo;
namespace Microsoft.WindowsAzure.Commands.ServiceManagement.Test.FunctionalTests
{
public class Verify : ServiceManagementTest
{
/// <summary>
///
/// </summary>
/// <param name="vm"></param>
/// <param name="availabilitySetName"></param>
/// <returns></returns>
internal static bool AzureAvailabilitySet(PersistentVM vm, string availabilitySetName)
{
try
{
if (string.IsNullOrEmpty(vm.AvailabilitySetName))
{
Assert.IsTrue(string.IsNullOrEmpty(availabilitySetName));
}
else
{
Assert.IsTrue(vm.AvailabilitySetName.Equals(availabilitySetName, StringComparison.InvariantCultureIgnoreCase));
}
return true;
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
if (e is AssertFailedException)
{
return false;
}
else
{
throw;
}
}
}
/// <summary>
///
/// </summary>
/// <param name="svc"></param>
/// <param name="expThumbprint"></param>
/// <param name="expAlgorithm"></param>
/// <param name="expData"></param>
/// <returns></returns>
internal static bool AzureCertificate(string svc, string expThumbprint, string expAlgorithm, string expData)
{
try
{
CertificateContext result = vmPowershellCmdlets.GetAzureCertificate(svc)[0];
Assert.AreEqual(expThumbprint, result.Thumbprint);
Assert.AreEqual(expAlgorithm, result.ThumbprintAlgorithm);
Assert.AreEqual(expData, result.Data);
Assert.AreEqual(svc, result.ServiceName);
//Assert.AreEqual(expUrl, result.Url);
return true;
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
return false;
}
}
/// <summary>
///
/// </summary>
/// <param name="vm"></param>
/// <param name="expLabel"></param>
/// <param name="expSize"></param>
/// <param name="expLun"></param>
/// <param name="hc"></param>
/// <returns></returns>
internal static bool AzureDataDisk(PersistentVM vm, string expLabel, int expSize, int expLun, HostCaching hc)
{
bool found = false;
foreach (DataVirtualHardDisk disk in vmPowershellCmdlets.GetAzureDataDisk(vm))
{
if (CheckDataDisk(disk, expLabel, expSize, expLun, hc))
{
found = true;
break;
}
}
return found;
}
private static bool CheckDataDisk(DataVirtualHardDisk disk, string expLabel, int expSize, int expLun, HostCaching hc)
{
Console.WriteLine("DataDisk - Name:{0}, Label:{1}, Size:{2}, LUN:{3}, HostCaching: {4}",
disk.DiskName, disk.DiskLabel, disk.LogicalDiskSizeInGB, disk.Lun, disk.HostCaching);
try
{
Assert.AreEqual(expLabel, disk.DiskLabel);
Assert.AreEqual(expSize, disk.LogicalDiskSizeInGB);
Assert.AreEqual(expLun, disk.Lun);
if (disk.HostCaching == null && hc == HostCaching.None || disk.HostCaching == hc.ToString())
{
Console.WriteLine("DataDisk found: {0}", disk.DiskLabel);
}
else
{
Assert.Fail("HostCaching is not matched!");
}
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
if (e is AssertFailedException)
{
return false;
}
else
{
throw;
}
}
return true;
}
/// <summary>
///
/// </summary>
/// <param name="resultSettings"></param>
/// <param name="expDns"></param>
/// <returns></returns>
internal static bool AzureDns(DnsSettings resultSettings, DnsServer expDns)
{
try
{
DnsServerList dnsList = vmPowershellCmdlets.GetAzureDns(resultSettings);
foreach (DnsServer dnsServer in dnsList)
{
Console.WriteLine("DNS Server Name: {0}, DNS Server Address: {1}", dnsServer.Name, dnsServer.Address);
if (MatchDns(expDns, dnsServer))
{
Console.WriteLine("Matched Dns found!");
return true;
}
}
return false;
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
throw;
}
}
private static bool MatchDns(DnsServer expDns, DnsServer actualDns)
{
try
{
Assert.AreEqual(expDns.Name, actualDns.Name);
Assert.AreEqual(expDns.Address, actualDns.Address);
return true;
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
if (e is AssertFailedException)
{
return false;
}
else
{
throw;
}
}
}
/// <summary>
///
/// </summary>
/// <param name="vm"></param>
/// <param name="epInfos"></param>
/// <returns></returns>
internal static bool AzureEndpoint(PersistentVM vm, AzureEndPointConfigInfo[] epInfos)
{
try
{
var serverEndpoints = vmPowershellCmdlets.GetAzureEndPoint(vm);
// List the endpoints found for debugging.
Console.WriteLine("***** Checking for Endpoints **************************************************");
Console.WriteLine("***** Listing Returned Endpoints");
foreach (InputEndpointContext ep in serverEndpoints)
{
Console.WriteLine("Endpoint - Name:{0} Protocol:{1} Port:{2} LocalPort:{3} Vip:{4}", ep.Name, ep.Protocol, ep.Port, ep.LocalPort, ep.Vip);
if (!string.IsNullOrEmpty(ep.LBSetName))
{
Console.WriteLine("\t- LBSetName:{0}", ep.LBSetName);
Console.WriteLine("\t- Probe - Port:{0} Protocol:{1} Interval:{2} Timeout:{3}", ep.ProbePort, ep.ProbeProtocol, ep.ProbeIntervalInSeconds, ep.ProbeTimeoutInSeconds);
}
}
Console.WriteLine("*******************************************************************************");
// Check if the specified endpoints were found.
foreach (AzureEndPointConfigInfo epInfo in epInfos)
{
bool found = false;
foreach (InputEndpointContext ep in serverEndpoints)
{
if (epInfo.CheckInputEndpointContext(ep))
{
found = true;
Console.WriteLine("Endpoint found: {0}", epInfo.EndpointName);
break;
}
}
Assert.IsTrue(found, string.Format("Error: Endpoint '{0}' was not found!", epInfo.EndpointName));
}
return true;
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
return false;
}
}
/// <summary>
///
/// </summary>
/// <param name="vm"></param>
/// <param name="expOS"></param>
/// <param name="expHC"></param>
/// <returns></returns>
internal static bool AzureOsDisk(PersistentVM vm, string expOS, HostCaching expHC)
{
try
{
OSVirtualHardDisk osdisk = vmPowershellCmdlets.GetAzureOSDisk(vm);
Console.WriteLine("OS Disk: Name - {0}, Label - {1}, HostCaching - {2}, OS - {3}", osdisk.DiskName, osdisk.DiskLabel, osdisk.HostCaching, osdisk.OS);
Assert.IsTrue(osdisk.Equals(vm.OSVirtualHardDisk), "OS disk returned is not the same!");
Assert.AreEqual(expOS, osdisk.OS);
Assert.AreEqual(expHC.ToString(), osdisk.HostCaching);
return true;
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
if (e is AssertFailedException)
{
return false;
}
else
{
throw;
}
}
}
/// <summary>
///
/// </summary>
/// <param name="svc"></param>
/// <param name="expLabel"></param>
/// <param name="expLocation"></param>
/// <param name="expAff"></param>
/// <param name="expDescription"></param>
/// <param name="expStatus"></param>
/// <returns></returns>
internal static bool AzureService(string svc, string expLabel, string expLocation = "West US", string expAff = null, string expDescription = null, string expStatus = "Created")
{
try
{
HostedServiceDetailedContext result = vmPowershellCmdlets.GetAzureService(svc);
Assert.AreEqual(expLabel, result.Label);
Assert.AreEqual(expDescription, result.Description);
Assert.AreEqual(expAff, result.AffinityGroup);
Assert.AreEqual(expLocation, result.Location);
Assert.AreEqual(expStatus, result.Status);
Assert.AreEqual(svc, result.ServiceName);
//Assert.AreEqual(expDateCreated, result.DateCreated);
//Assert.AreEqual(expDateModified, result.DateModified);
//Assert.AreEqual(expUrl, result.Url);
return true;
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
return false;
}
}
internal static bool AzureAclConfig(NetworkAclObject expectedAcl, NetworkAclObject actualAcl)
{
for (int i = 0; i < expectedAcl.Rules.Count; i++)
{
Assert.IsTrue(CompareContext(expectedAcl.Rules[i], actualAcl.Rules[i]));
}
return true;
}
internal static bool AzureReservedIP(ReservedIPContext rsvIP, string name, string label, string affname,
string ip, string dep, string svcName, string id)
{
Utilities.PrintContext(rsvIP);
Assert.AreEqual(name, rsvIP.ReservedIPName, "Reserved IP names are not equal!");
Assert.AreEqual(label, rsvIP.Label, "Reserved IP labels are not equal!");
Assert.AreEqual(affname, rsvIP.Location, "Reserved IP affinity groups are not equal!");
if (!string.IsNullOrEmpty(ip))
{
Assert.AreEqual(ip, rsvIP.Address, "Reserved IP addresses are not equal!");
}
Assert.AreEqual(dep, rsvIP.DeploymentName, "Reserved IP deployment names are not equal!");
Assert.AreEqual(svcName, rsvIP.ServiceName, "Reserved IP service names are not equal!");
if (!string.IsNullOrEmpty(id))
{
Assert.AreEqual(id, rsvIP.Id, "Reserved IP IDs are not equal!");
}
return true;
}
internal static bool AzureReservedIPNotInUse(ReservedIPContext rsvIP, string name, string label, string affname,
string id = null)
{
AzureReservedIP(rsvIP, name, label, affname, null, null, null, id);
Assert.AreEqual(false, rsvIP.InUse);
return true;
}
internal static bool AzureReservedIPInUse(ReservedIPContext rsvIP, string name, string label, string affname,
string ip = null, string deploymentName =null, string svcName = null)
{
AzureReservedIP(rsvIP, name, label, affname, ip, deploymentName, svcName, null);
Assert.AreEqual(true, rsvIP.InUse);
return true;
}
private static bool CompareContext<T>(T obj1, T obj2)
{
bool result = true;
Type type = typeof(T);
foreach (PropertyInfo property in type.GetProperties(BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly))
{
string typeName = property.PropertyType.FullName;
if (typeName.Equals("System.String") || typeName.Equals("System.Int32") || typeName.Equals("System.Uri") || typeName.Contains("Nullable"))
{
if (typeName.Contains("System.DateTime"))
{
continue;
}
var obj1Value = property.GetValue(obj1, null);
var obj2Value = property.GetValue(obj2, null);
Console.WriteLine("Expected: {0}", obj1Value);
Console.WriteLine("Acutal: {0}", obj2Value);
if (obj1Value == null)
{
result &= (obj2Value == null);
}
else if (typeName.Contains("System.String"))
{
result &= (string.Compare(obj1Value.ToString(), obj2Value.ToString(), StringComparison.CurrentCultureIgnoreCase) == 0);
}
else
{
result &= (obj1Value.Equals(obj2Value));
}
}
else
{
Console.WriteLine("This type is not compared: {0}", typeName);
}
}
return result;
}
}
}