/
FC_MultipathDetection.ps1
313 lines (267 loc) · 8.77 KB
/
FC_MultipathDetection.ps1
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
########################################################################
#
# Linux on Hyper-V and Azure Test Code, ver. 1.0.0
# Copyright (c) Microsoft Corporation
#
# All rights reserved.
# 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
#
# THIS CODE IS PROVIDED *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS
# OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION
# ANY IMPLIED WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR
# PURPOSE, MERCHANTABLITY OR NON-INFRINGEMENT.
#
# See the Apache Version 2.0 License for specific language governing
# permissions and limitations under the License.
#
########################################################################
<#
.Synopsis
Verify that VM sees all the FC disks attached in MPIO scenario.
.Description
This script counts the number of connected fibre channel.
Pushes a script to count the number of disks visible inside the VM
and compares the results.
A typical test case definition for this test script would look
similar to the following:
<test>
<testName>FC_disks_multipath_detection</testName>
<setupscript>setupscripts\FC_AddFibreChannelHba.ps1</setupscript>
<testScript>setupscripts\FC_MultipathDetection.ps1</testScript>
<timeout>10600</timeout>
<testParams>
<param>snapshotname=ICABase</param>
<param>TC_COVERED=FC-xx</param>
<param>vSANName=FC_NAME</param>
</testParams>
<onError>Continue</onError>
<noReboot>False</noReboot>
</test>
.Parameter vmName
Name of the VM to perform the test with.
.Parameter hvServer
Name of the Hyper-V server hosting the VM.
.Parameter testParams
.Example
setupScripts\FC_MultipathDetection.ps1 -vmName "myVm" -hvServer "localhost" -TestParams "TC_COVERED=FC-08"
#>
param([string] $vmName, [string] $hvServer, [string] $testParams)
$remoteScript = "FC_multipath_detect.sh"
$summaryLog = "${vmName}_summary.log"
$retVal = $False
######################################################################
#
# Helper function to execute command on remote machine.
#
#######################################################################
function Execute ([string] $command)
{
.\bin\plink.exe -i ssh\${sshKey} root@${ipv4} $command
return $?
}
#######################################################################
# Runs a remote script on the VM an returns the log.
#######################################################################
function RunRemoteScript($remoteScript)
{
$retValue = $False
$stateFile = "state.txt"
$TestCompleted = "TestCompleted"
$TestAborted = "TestAborted"
$TestRunning = "TestRunning"
$TestFailed ="TestFailed"
$timeout = 6000
"./${remoteScript} > ${remoteScript}.log" | out-file -encoding ASCII -filepath runtest.sh
.\bin\pscp -i ssh\${sshKey} .\runtest.sh root@${ipv4}:
if (-not $?)
{
Write-Output "ERROR: Unable to copy runtest.sh to the VM"
return $False
}
.\bin\pscp -i ssh\${sshKey} .\remote-scripts\ica\${remoteScript} root@${ipv4}:
if (-not $?)
{
Write-Output "ERROR: Unable to copy ${remoteScript} to the VM"
return $False
}
.\bin\plink.exe -i ssh\${sshKey} root@${ipv4} "dos2unix ${remoteScript} 2> /dev/null"
if (-not $?)
{
Write-Output "ERROR: Unable to run dos2unix on ${remoteScript}"
return $False
}
.\bin\plink.exe -i ssh\${sshKey} root@${ipv4} "dos2unix runtest.sh 2> /dev/null"
if (-not $?)
{
Write-Output "ERROR: Unable to run dos2unix on runtest.sh"
return $False
}
.\bin\plink.exe -i ssh\${sshKey} root@${ipv4} "chmod +x ${remoteScript} 2> /dev/null"
if (-not $?)
{
Write-Output "ERROR: Unable to chmod +x ${remoteScript}"
return $False
}
.\bin\plink.exe -i ssh\${sshKey} root@${ipv4} "chmod +x runtest.sh 2> /dev/null"
if (-not $?)
{
Write-Output "ERROR: Unable to chmod +x runtest.sh " -
return $False
}
# Run the script on the vm
.\bin\plink.exe -i ssh\${sshKey} root@${ipv4} "./runtest.sh"
# Return the state file
while ($timeout -ne 0 )
{
.\bin\pscp -q -i ssh\${sshKey} root@${ipv4}:${stateFile} . #| out-null
$sts = $?
if ($sts)
{
if (test-path $stateFile)
{
$contents = Get-Content -Path $stateFile
if ($null -ne $contents)
{
if ($contents -eq $TestCompleted)
{
Write-Output "Info : state file contains Testcompleted"
$retValue = $True
break
}
if ($contents -eq $TestAborted)
{
Write-Output "Info : State file contains TestAborted failed. "
break
}
if ($contents -eq $TestFailed)
{
Write-Output "Test failed."
break
}
#Start-Sleep -s 1
$timeout--
if ($timeout -eq 0)
{
Write-Output "Error : Timed out on Test Running , Exiting test execution."
break
}
}
else
{
Write-Output "Warn : state file is empty"
break
}
}
else
{
Write-Host "Warn : ssh reported success, but state file was not copied"
break
}
}
else #
{
Write-Output "Error : pscp exit status = $sts"
Write-Output "Error : unable to pull state.txt from VM."
break
}
}
# Get the logs
$remoteScriptLog = $remoteScript+".log"
bin\pscp -q -i ssh\${sshKey} root@${ipv4}:${remoteScriptLog} .
$sts = $?
if ($sts)
{
if (test-path $remoteScriptLog)
{
$contents = Get-Content -Path $remoteScriptLog
Write-Output $contents | Tee-Object -Append -file $summaryLog
if ($null -ne $contents)
{
if ($null -ne ${TestLogDir})
{
move "${remoteScriptLog}" "${TestLogDir}\${remoteScriptLog}"
}
else
{
Write-Output "INFO: $remoteScriptLog is copied in ${rootDir}"
}
}
else
{
Write-Output "Warn: $remoteScriptLog is empty"
}
}
else
{
Write-Output "Warn: ssh reported success, but $remoteScriptLog file was not copied"
}
}
# Cleanup
del state.txt -ErrorAction "SilentlyContinue"
del runtest.sh -ErrorAction "SilentlyContinue"
return $retValue
}
#######################################################################
#
# Main script body
#
#######################################################################
$retVal = $false
#
# Check input arguments
#
if ($vmName -eq $null) {
"Error: VM name is null"
return $retVal
}
if ($hvServer -eq $null) {
"Error: hvServer is null"
return $retVal
}
$params = $testParams.Split(";")
foreach ($p in $params) {
$fields = $p.Split("=")
switch ($fields[0].Trim()) {
"sshKey" { $sshKey = $fields[1].Trim() }
"ipv4" { $ipv4 = $fields[1].Trim() }
"rootdir" { $rootDir = $fields[1].Trim() }
"TC_COVERED" { $TC_COVERED = $fields[1].Trim() }
"TestLogDir" { $TestLogDir = $fields[1].Trim() }
default {}
}
}
if ($null -eq $sshKey) {
"Error: Test parameter sshKey was not specified"
return $False
}
if ($null -eq $ipv4) {
"Error: Test parameter ipv4 was not specified"
return $False
}
#
# Change the working directory to where we need to be
#
if (-not (Test-Path $rootDir)) {
"Error: The directory `"${rootDir}`" does not exist!"
return $False
}
Write-output "This script covers test case: ${TC_COVERED}" | Tee-Object -Append -file $summaryLog
cd $rootDir
#
# Source the TCUtils.ps1 file
#
. .\setupscripts\TCUtils.ps1
$fcDisks = Get-Disk | Where-Object -FilterScript {$_.BusType -Eq "Fibre Channel"}
$fcCount = $fcDisks.Length
#set fcCount in constants.sh
$cmd="echo `"expectedCount=$($fcCount)`" >> ~/constants.sh";
$result = Execute($cmd);
if (-not $result) {
Write-Error -Message "Error: Unable to submit ${cmd} to vm" -ErrorAction SilentlyContinue
return $False
}
$sts = RunRemoteScript $remoteScript
return $sts