-
Notifications
You must be signed in to change notification settings - Fork 8
/
stateEngine.ps1
2944 lines (2563 loc) · 110 KB
/
stateEngine.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
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
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
########################################################################################
## ___________ _____________ ___ .____ .___ _________ _____
## \_ _____// _____/\ \/ / | | | |/ _____/ / _ \
## | __)_ \_____ \ \ / ______ | | | |\_____ \ / /_\ \
## | \/ \ / \ /_____/ | |___| |/ \/ | \
## /_______ /_______ //___/\ \ |_______ \___/_______ /\____|__ /
## \/ \/ \_/ \/ \/ \/
########################################################################################
## ESX-LISA is an automation testing framework based on github.com/LIS/lis-test
## project. In order to support ESX, ESX-LISA uses PowerCLI to automate all
## aspects of vSphere maagement, including network, storage, VM, guest OS and
## more. This framework automates the tasks required to test the
## Redhat Enterprise Linux Server on WMware ESX Server.
########################################################################################
## Revision:
## v1.0.0 - xiaofwan - 11/25/2016 - Fork from github.com/LIS/lis-test.
## Incorporate VMware PowerCLI with framework
## v1.1.0 - xiaofwan - 11/28/2016 - Merge ApplyCheckpoint codes
## Merge bug fix from LISA
## v1.2.0 - xiaofwan - 12/29/2016 - Fix snapshot checking issue found by @xuli.
## v1.3.0 - xiaofwan - 01/9/2017 - Add new feature: snapshot auto-create if there's
## no snapshot found in VM.
## v1.4.0 - xiaofwan - 01/25/2017 - Add a new result status - Skipped, which marks
## test case not applicable in current scenario.
## v1.5.0 - xiaofwan - 01/25/2017 - $vm.testCaseResults only contains "Passed",
## "Failed", "Skipped", "Aborted", and "none".
## v1.6.0 - xiaofwan - 02/3/2017 - Add test case running time support.
## v1.7.0 - xiaofwan - 02/3/2017 - $True will be $true and $False will be $false.
## v1.8.0 - xiaofwan - 02/4/2017 - Test result can be exported as JUnix XML file.
## v1.9.0 - xiaofwan - 02/21/2017 - ESX host version, kernel and firmware version
## are visable in XML result.
## v2.0.0 - xiaofwan - 02/21/2017 - Iteration related code has been removed.
## v2.1.0 - xiaofwan - 02/21/2017 - Add test case running date and time in XML.
## v2.2.0 - xiaofwan - 02/21/2017 - Add SetRunningTime in ForceShutDown to support
## time calculation in force shut down scenario.
## v2.3.0 - xiaofwan - 02/28/2017 - Remove summary log from emailSummary.
########################################################################################
<#
.Synopsis
Functions that make up the Lisa state engine.
.Description
This PowerShell script implements the state engine which
moves test VMs through the various states required to perform
a test on a Linux VM. Not all states are visited by each
VM. The test case definition will result in some states
being skipped.
A fairly complete list of states a VM might progress through
would include the following. The below descriptions are not
complete. The intent is to give the reader an understanding
of what is done in each state and the possible state transitions.
SystemDown
- Make sure the VM is stopped.
- Update the current test.
- If no more tests set currentState to Finished
- If test case has a setup script
set currentState to RunSetupScript
- else
set currentState to StartSystem
RunSetupScript
- Run the setup secript (to reconfigure the VM)
- Set currentState to StartSystem
StartSystem
- Start the VM
- Make sure the VM transitions to state Running
- Set currentState to SystemStarting
SystemStarting
- Get the VMs IP address
- Test port 22 (the SSH port) with a 5 second timeout
- If VM is listening on port 22
set currentState to SystemUp
SlowSystemStarting
Enter this state only if SystemStarting state timed out.
- Continue testing port 22
DiagnoseHungSystem
Enter this state only if SlowSystemStarting state timed out.
- Log error that tests will not be performed on this VM.
- Set currentTest to done
- Set currentState to ForceShutdown
SystemUp
- Send a simple command to the VM via SSH and accept any prompts for server key
- Set currentState to PushTestFiles
PushTestFiles
- Create a constants.sh file and populate with all test parameters
- Push the constants.sh file to VM using SSH
- Tell the VM to run dos2unix on the constants.sh file
- Push the test script to the VM
- Tell the VM to run dos2unix on the test script file
- Tell the VM to chmod 755 testScript
- If test case has a pretest script
set currentState to RunPreTestScript
else
set currentState to StartTtest
RunPreTestScript
- Verify test case lists a pretest script
- Run the PowerShell pretest script in a separate PowerShell context
- set currentState to StartTest
StartTest
- Create a Linux command to run the test case script
- write the command to a file named runtest.sh
- copy runtest.sh file to VM
- Tell VM to chmod 755 runtest.sh
- Tell VM to run dos2unix on runtest.sh
- Tell VM to start atd daemon
- send command "at -f runtest.sh now" to VM
This runs test script with both STDOUT and STDERR logged
and allows the SSH connection to be closed. This is needed
so this script can process other VMs in parallel
- set currentState to TestStarting
TestStarting
- test if the file ~/state.txt was created on the VM
- if state.txt exists
set currentState to TestRunning
TestRunning
- Copy ~/state.txt from VM using SSH
- if contents of state.txt is not "TestRunning"
set currentState to CollectLogFiles
CollectLogFiles
- Use state.txt to mark status of test case to completed, aborted, failed
- Copy log file from VM and save in Lisa test directory
Note: The saved logfile will be named: <vmName>_<testCaseName>.log
This is required since the test run may have multiple VMs and
each VM may run the same test cases.
- Delete state.txt on the VM
- If test case has a posttest script
Set currentState to RunPostTestScript
else
Set currentState to DetermineReboot
RunPostTestScript
- Verify test case lists a posttest script
- Run the PowerShell posttest script in a separate PowerShell context
- Set currentState to DetermineReboot
DetermineReboot
- Determine if we need to reboot the VM before the next test
- if reboot required
Set currentState to ShutdownSystem
else
Update currentTest
Set currentState to SystemUp
ShutdownSystem
- Ask the VM to shutdown
- Set currentState to ShuttingDown
ShuttingDown
- If timeout in this state
Set currentState to ForceShutdown
- If VM in Off state
If currentTest has a CleanupScript
Set currentState to RunCleanupScript
else
Set currentState to SystemDown
ForceShutDown
- If VM in Off state
If currentTest has a CleanupScript
Set currentState to RunCleanupScript
else
Set currentState to SystemDown
else
Stop the VM
- If we timeout in this state
Log the error
Mark the VM as disabled (set state to Disabled)
RunCleanUpScript
- Run the cleanup secript (to undo configuration changes)
- Set currentState to SystemDown
.Link
None.
#>
# Source the other files we need
. .\utilFunctions.ps1 | out-null
. .\OSAbstractions.ps1
# Constants
# States a VM can be in
New-Variable SystemDown -value "SystemDown" -option ReadOnly
New-Variable ApplyCheckpoint -value "ApplyCheckpoint" -option ReadOnly
New-variable RunSetupScript -value "RunSetupScript" -option ReadOnly
New-Variable StartSystem -value "StartSystem" -option ReadOnly
New-Variable SystemStarting -value "SystemStarting" -option ReadOnly
New-Variable SlowSystemStarting -value "SlowSystemStarting" -option ReadOnly
New-Variable DiagnoseHungSystem -value "DiagnoseHungSystem" -option ReadOnly
New-Variable SystemUp -value "SystemUp" -option ReadOnly
New-Variable PushTestFiles -value "PushTestFiles" -option ReadOnly
New-Variable RunPreTestScript -value "RunPreTestScript" -option ReadOnly
New-Variable StartTest -value "StartTest" -option ReadOnly
New-Variable TestStarting -value "TestStarting" -option ReadOnly
New-Variable TestRunning -value "TestRunning" -option ReadOnly
New-Variable CollectLogFiles -value "CollectLogFiles" -option ReadOnly
New-Variable RunPostTestScript -value "RunPostTestScript" -option ReadOnly
New-Variable DetermineReboot -value "DetermineReboot" -option ReadOnly
New-Variable ShutdownSystem -value "ShutdownSystem" -option ReadOnly
New-Variable ShuttingDown -value "ShuttingDown" -option ReadOnly
New-Variable ForceShutDown -value "ForceShutDown" -option ReadOnly
New-variable RunCleanUpScript -value "RunCleanUpScript" -option ReadOnly
New-Variable StartPS1Test -value "StartPS1Test" -option ReadOnly
New-Variable PS1TestRunning -value "PS1TestRunning" -option ReadOnly
New-Variable PS1TestCompleted -value "PS1TestCompleted" -option ReadOnly
New-Variable Finished -value "Finished" -option ReadOnly
New-Variable Disabled -value "Disabled" -option ReadOnly
# test completion codes
New-Variable TestCompleted -value "TestCompleted" -option ReadOnly
New-Variable TestSkipped -value "TestSkipped" -option ReadOnly
New-Variable TestAborted -value "TestAborted" -option ReadOnly
New-Variable TestFailed -value "TestFailed" -option ReadOnly
# test result codes
New-Variable Passed -value "Passed" -option ReadOnly
New-Variable Skipped -value "Skipped" -option ReadOnly
New-Variable Aborted -value "Aborted" -option ReadOnly
New-Variable Failed -value "Failed" -option ReadOnly
# Supported OSs
New-Variable LinuxOS -value "Linux" -option ReadOnly
New-Variable FreeBSDOS -value "FreeBSD" -option ReadOnly
# Import vmware.vimautomation.core module if it does not exist.
PowerCLIImport
# Connect with VSphere VI Server if connnect does not exist.
ConnectToVIServer $env:ENVVISIPADDR `
$env:ENVVISUSERNAME `
$env:ENVVISPASSWORD `
$env:ENVVISPROTOCOL
# Generate an JUnit formated XML object to store case results.
$testResult = GetJUnitXML
########################################################################################
# RunICTests()
########################################################################################
function RunICTests([XML] $xmlConfig) {
<#
.Synopsis
Start tests running on the test VMs.
.Description
Reset all VMs to a known state of stopped.
Add any additional any missing "required" XML elements to each
vm definition. Initialize the e-mail message that may be sent
on test completion.
.Parameter xmlConfig
XML document driving the test.
.Example
RunICTests $xmlData
#>
if (-not $xmlConfig -or $xmlConfig -isnot [XML]) {
LogMsg 0 "Error : RunICTests received an bad xmlConfig parameter - terminating LISA"
return
}
LogMsg 9 "Info : RunICTests($($vm.vmName))"
# Verify the Putty utilities exist. Without them, we cannot talk to the Linux VM.
if (-not (Test-Path -Path ".\bin\pscp.exe")) {
LogMsg 0 "Error : The putty utility .\bin\pscp.exe does not exist"
return
}
if (-not (Test-Path -Path ".\bin\plink.exe")) {
LogMsg 0 "Error : The putty utility .\bin\plink.exe does not exist"
return
}
# Reset each VM to a known state
foreach ($vm in $xmlConfig.config.VMs.vm) {
LogMsg 5 "Info : RunICTests() processing VM $($vm.vmName)"
# Add the state related xml elements to each VM xml node
$xmlElementsToAdd = @("currentTest", "stateTimeStamp", "caseStartTime", "state", "emailSummary", "jobID", "testCaseResults", "isRebooted")
foreach ($element in $xmlElementsToAdd) {
if (-not $vm.${element}) {
$newElement = $xmlConfig.CreateElement($element)
$newElement.set_InnerText("none")
$results = $vm.AppendChild($newElement)
}
}
$newElement = $xmlConfig.CreateElement("individualResults")
$newElement.set_InnerText("");
$vm.AppendChild($newElement);
# Add test suite and test date time into test result XML
SetTimeStamp $testStartTime.toString()
SetResultSuite $vm.suite
#
# Add some information to the email summary text
# such as PowerCLI version, vCenter version, ESXi host info.
#
$vm.emailSummary = "VM : $($vm.vmName)<br />"
$outGetCliVer = Get-PowerCLIVersion
$vm.emailSummary += " PowerCLI : $($outGetCliVer.UserFriendlyVersion) <br />"
$outGlobalVar = $global:DefaultVIServer
$vm.emailSummary += " vCenter : version $($outGlobalVar.Version) build $($outGlobalVar.Build) <br />"
# Verify the ESXi serer is on and connected.
$vmhostOut = Get-VMHost -Name $vm.hvServer
if (-not $vmhostOut) {
LogMsg 0 "Error : Run PowerCLI with error $vmhostOut"
return
}
if (-not ($vmhostOut.connectionstate -eq 'Connected' -and $vmhostout.PowerState -eq 'PoweredOn')) {
LogMsg 0 "Error : ESXi host $($vm.hvServer) is poweredOff or not connected with vCenter."
return
}
$vm.emailSummary += " Suite : running at ESXi host $($vm.hvServer) <br />"
$vm.emailSummary += " Host : $($vm.hvServer) with ESXi $($vmhostOut.Version) build $($vmhostOut.Build)<br />"
$vm.emailSummary += "<br /><br />"
# Add ESX host version into result XML
SetESXVersion "$($vmhostOut.Version) build $($vmhostOut.Build)"
# Make sure the VM actually exists
$vmObj = Get-VM -Name $vm.vmName -Location $vmhostOut
if (-not $vmObj) {
LogMsg 0 "Warn : The VM $($vm.vmName) does not exist"
LogMsg 0 "Warn : Tests will not be run on $($vm.vmName)"
UpdateState $vm $Disabled
$vm.emailSummary += " The virtual machine $($vm.vmName) does not exist.<br />"
$vm.emailSummary += " No tests were run on $($vm.vmName)<br />"
continue
}
else {
LogMsg 10 "Info : Resetting vm $($vm.vmName)"
ResetVM $vm $xmlConfig
}
}
#
# All VMs should be either in a ShutDown state, or disabled. If that is not the case
# we have a problem...
#
foreach ($vm in $xmlConfig.config.VMs.vm) {
if ($vm.state -ne $Disabled -and $vm.state -ne $SystemDown) {
LogMsg 0 "Error : RunICTests - $($vm.vmName) is not in a shutdown state"
LogMsg 0 "Error : The VM cannot be put into a stopped state"
LogMsg 0 "Error : Tests will not be run on $($vm.vmName)"
$vm.emailSummary += " The VM could not be stopped. It has been disabled.<br />"
$vm.emailSummary += " No tests were run on this VM`<br />"
UpdateState $vm $Disabled
}
}
# run the state engine
DoStateMachine $xmlConfig
}
########################################################################################
# ResetVM()
########################################################################################
function ResetVM([System.Xml.XmlElement] $vm, [XML] $xmlData) {
<#
.Synopsis
Make sure the test VM is stopped
.Description
Stop the test VM and then reset it to a snapshot.
This ensures the VM starts the test run in a
known good state.
.Parameter vm
XML element representing the test VM
.Parameter xmlData
XML document driving the test.
.Example
ResetVM $vm
#>
if (-not $vm -or $vm -isnot [System.Xml.XmlElement]) {
LogMsg 0 "Error : ResetVM was passed an bad VM object"
return
}
LogMsg 9 "Info : ResetVM( $($vm.vmName) )"
$vmObj = Get-VMHost -Name $vm.hvServer | Get-VM -Name $vm.vmName
if (-not $vmObj) {
LogMsg 0 "Error : ResetVM cannot find the VM $($vm.vmName)"
$vm.emailSummary += "VM $($vm.vmName) cannot be found - no tests run on VM<br />"
UpdateState $vm $Disabled
return
}
# If the VM is not stopped, try to stop it
if ($vmObj.PowerState -ne "PoweredOff") {
LogMsg 3 "Info : $($vm.vmName) is not in a stopped state - stopping VM"
$outStopVm = Stop-VM -VM $vmObj -Confirm:$false -Kill
if ($outStopVm -eq $false -or $outStopVm.PowerState -ne "PoweredOff") {
LogMsg 0 "Error : ResetVM is unable to stop VM $($vm.vmName). VM has been disabled"
$vm.emailSummary += "Unable to stop VM. VM was disabled and no tests run<br />"
UpdateState $vm $Disabled
return
}
}
#
# Reset the VM to a snapshot to put the VM in a known state. The default name is
# ICABase. This can be overridden by the global.defaultSnapshot in the global section
# and then by the vmSnapshotName in the VM definition.
#
$snapshotName = "ICABase"
if ($xmlData.config.global.defaultSnapshot) {
$snapshotName = $xmlData.config.global.defaultSnapshot
LogMsg 5 "Info : $($vm.vmName) Over-riding default snapshotName from global section to $snapshotName"
}
if ($vm.vmSnapshotName) {
$snapshotName = $vm.vmSnapshotName
LogMsg 5 "Info : $($vm.vmName) Over-riding default snapshotName from VM section to $snapshotName"
}
# Find the snapshot we need and apply the snapshot
$snapshotFound = $false
$vmObj = Get-VMHost -Name $vm.hvServer | Get-VM -Name $vm.vmName
$snapsOut = Get-Snapshot -VM $vmObj
if ($snapsOut) {
foreach ($s in $snapsOut) {
if ($s.Name -eq $snapshotName) {
LogMsg 3 "Info : $($vm.vmName) is being reset to snapshot $($s.Name)"
$setsnapOut = Set-VM -VM $vmObj -Snapshot $s -Confirm:$false
if ($setsnapOut) {
$snapshotFound = $true
break
}
else {
LogMsg 0 "Error : ResetVM is unable to revert VM $($vm.vmName) to snapshot $($s.Name). VM has been disabled"
$vm.emailSummary += "Unable to revert snapshot. VM was disabled and no tests run<br />"
UpdateState $vm $Disabled
return
}
}
}
}
# Make sure the snapshot left the VM in a stopped state.
if ($snapshotFound) {
# If a VM is in the Suspended (Saved) state after applying the snapshot,
# the following will handle this case
$vmObj = Get-VMHost -Name $vm.hvServer | Get-VM -Name $vm.vmName
if ($vmObj) {
if ($vmObj.PowerState -eq "Suspended") {
LogMsg 3 "Info : $($vm.vmName) - resetting to a stopped state after restoring a snapshot"
$stopvmOut = Stop-VM -VM $vmObj -Confirm:$false -Kill
if ($stopvmOut -or $stopvmOut.PowerState -ne "PoweredOff") {
LogMsg 0 "Error : ResetVM is unable to stop VM $($vm.vmName). VM has been disabled"
$vm.emailSummary += "Unable to stop VM. VM was disabled and no tests run<br />"
UpdateState $vm $Disabled
return
}
}
}
else {
LogMsg 0 "Error : ResetVM cannot find the VM $($vm.vmName)"
$vm.emailSummary += "VM $($vm.vmName) cannot be found - no tests run on VM<br />"
UpdateState $vm $Disabled
return
}
}
else {
LogMsg 0 "Warn : There's no snapshot with name $snapshotName found in VM $($vm.vmName). Making a new one now."
$newSnap = New-Snapshot -VM $vmObj -Name $snapshotName
if ($newSnap) {
$snapshotFound = $true
LogMsg 3 "Info : $($vm.vmName) made a snapshot $snapshotName."
}
else {
LogMsg 0 "Error : ResetVM is unable to make snapshot for VM $($vm.vmName)."
$vm.emailSummary += "Unable to make snapshot. VM was disabled and no tests run<br />"
UpdateState $vm $Disabled
return
}
}
# Update the state, and state transition timestamp,
UpdateState $vm $SystemDown
}
########################################################################################
# DoStateMachine()
########################################################################################
function DoStateMachine([XML] $xmlConfig) {
<#
.Synopsis
Main function of the state machine.
.Description
Move each VM through the various states required
to run a test on a VM.
.Parameter xmlConfig
XML document driving the test.
.Example
DoStateMachine $xmlData
#>
LogMsg 9 "Info : Entering DoStateMachine()"
$done = $false
while (! $done) {
$done = $true # Assume we are done
foreach ( $vm in $xmlConfig.config.VMs.vm ) {
switch ($vm.state) {
$SystemDown {
DoSystemDown $vm $xmlConfig
$done = $false
}
$ApplyCheckpoint {
DoApplyCheckpoint $vm $xmlConfig
$done = $false
}
$RunSetupScript {
DoRunSetupScript $vm $xmlConfig
$done = $false
}
$StartSystem {
DoStartSystem $vm $xmlConfig
$done = $false
}
$SystemStarting {
DoSystemStarting $vm $xmlConfig
$done = $false
}
$SlowSystemStarting {
DoSlowSystemStarting $vm $xmlConfig
$done = $false
}
$DiagNoseHungSystem {
DoDiagnoseHungSystem $vm $xmlConfig
$done = $false
}
$SystemUp {
DoSystemUp $vm $xmlConfig
$done = $false
}
$PushTestFiles {
DoPushTestFiles $vm $xmlConfig
$done = $false
}
$RunPreTestScript {
DoRunPreTestScript $vm $xmlConfig
$done = $false
}
$WaitForDependencyVM {
DoWaitForDependencyVM $vm $xmlConfig
$done = $false
}
$StartTest {
DoStartTest $vm $xmlConfig
$done = $false
}
$TestStarting {
DoTestStarting $vm $xmlConfig
$done = $false
}
$TestRunning {
DoTestRunning $vm $xmlConfig
$done = $false
}
$CollectLogFiles {
DoCollectLogFiles $vm $xmlConfig
$done = $false
}
$RunPostTestScript {
DoRunPostTestScript $vm $xmlConfig
$done = $false
}
$DetermineReboot {
DoDetermineReboot $vm $xmlConfig
$done = $false
}
$ShutdownSystem {
if (NoShutDownCheck) {
UpdateState $vm $Finished
}else {
DoShutdownSystem $vm $xmlConfig
$done = $false
}
}
$ShuttingDown {
DoShuttingDown $vm $xmlConfig
$done = $false
}
$RunCleanupScript {
DoRunCleanUpScript $vm $xmlConfig
$done = $false
}
$ForceShutDown {
DoForceShutDown $vm $xmlConfig
$done = $false
}
$StartPS1Test {
DoStartPS1Test $vm $xmlConfig
$done = $false
}
$PS1TestRunning {
DoPS1TestRunning $vm $xmlConfig
$done = $false
}
$PS1TestCompleted {
DoPS1TestCompleted $vm $xmlConfig
$done = $false
}
$Finished {
DoFinished $vm $xmlConfig
}
$Disabled {
DoDisabled $vm $xmlConfig
}
default: {
LogMsg 0 "Error : State machine encountered an undefined state for VM $($vm.vmName), State = $($vm.state)"
$vm.currentTest = "done"
UpdateState $vm $ForceShutDown
}
}
}
Start-Sleep -m 100
}
LogMsg 5 "Info : DoStateMachine() exiting"
}
########################################################################################
# DoSystemDown()
########################################################################################
function DoSystemDown([System.Xml.XmlElement] $vm, [XML] $xmlData) {
<#
.Synopsis
Ensure the VM is stopped and update some VM attributes.
.Description
Update the VMs currentTest. Transition to RunSetupScript if the currentTest
defines a setup script. Otherwise, transition to StartSystem
.Parameter vm
XML Element representing the VM under test.
.Parameter xmlData
XML document driving the test.
.Example
DoSystemDown $testVM $xmlData
#>
if (-not $vm -or $vm -isnot [System.Xml.XmlElement]) {
LogMsg 0 "Error : DoSystemDown received an bad VM parameter"
return
}
LogMsg 9 "Info : Entering DoSystemDown( $($vm.vmName) )"
if (-not $xmlData -or $xmlData -isnot [XML]) {
LogMsg 0 "Error : DoSystemDown received a null or bad xmlData parameter - VM $($vm.vmName) disabled"
$vm.emailSummary += " DoSystemDown received a null xmlData parameter - VM disabled<br />"
$vm.currentTest = "done"
UpdateState $vm $Disabled
}
# Make sure the VM is stopped
$vmObj = Get-VMHost -Name $vm.hvServer | Get-VM -Name $vm.vmName
if (-not $vmObj) {
LogMsg 0 "Error : SystemDown cannot find the VM $($vm.vmName)"
$vm.emailSummary += "VM $($vm.vmName) cannot be found - no tests run on VM<br />"
UpdateState $vm $Disabled
return
}
else {
if ($vmObj.PowerState -ne "PoweredOff") {
LogMsg 0 "Error : $($vm.vmName) entered SystemDown in a non-stopped state`n The VM will be disabled"
$vm.emailSummary += " SystemDown found the VM in a non-stopped state - disabling VM<br />"
$vm.currentTest = "done"
UpdateState $vm $ForceShutdown
return
}
}
# Update the VMs current test
UpdateCurrentTest $vm $xmlData
# Mark current test the first case or after rebooted
$vm.isRebooted = $true.ToString()
$vm.caseStartTime = [DateTime]::Now.ToString()
if ($($vm.currentTest) -eq "done") {
UpdateState $vm $Finished
}
else {
UpdateState $vm $ApplyCheckpoint
}
}
########################################################################################
# DoApplyCheckpoint()
########################################################################################
function DoApplyCheckpoint([System.Xml.XmlElement] $vm, [XML] $xmlData) {
<#
.Synopsis
Apply checkpoint to let VM go into a know status
if RevertDefaultSnapshot=True.
.Description
Apply checkpoint if RevertDefaultSnapshot=True. Then transition
to RunSetupScript if the currentTest defines a setup script.
Otherwise, transition to StartSystem. If RevertDefaultSnapshot=False
or not configured, there's no checkpoint restoring applied in VM
and do state transition instead.
.Parameter vm
XML Element representing the VM under test.
.Parameter xmlData
XML document for the test.
.Example
DoSystemDown $testVM $xmlData
#>
if (-not $vm -or $vm -isnot [System.Xml.XmlElement]) {
LogMsg 0 "Error: DoApplyCheckpoint received an bad VM parameter"
return
}
LogMsg 9 "Info : Entering DoApplyCheckpoint( $($vm.vmName) )"
if (-not $xmlData -or $xmlData -isnot [XML]) {
LogMsg 0 "Error: DoApplyCheckpoint received a null or bad xmlData parameter - VM $($vm.vmName) disabled"
$vm.emailSummary += " DoApplyCheckpoint received a null xmlData parameter - VM disabled<br />"
$vm.currentTest = "done"
UpdateState $vm $Disabled
}
$testData = GetTestData $vm.currentTest $xmlData
if ($testData -is [System.Xml.XmlElement]) {
# Do not need to recover from RevertDefaultSnapshot
if (-not $testData.RevertDefaultSnapshot -or $testData.RevertDefaultSnapshot -eq "False") {
LogMsg 9 "Info : noCheckpoint is not configured or set to True."
if (-not (VerifyTestResourcesExist $vm $testData)) {
# One or more resources used by the VM or test case does not exist - fail the test
$testName = $testData.testName
$vm.emailSummary += (" Test {0, -25} : {1}<br />" -f ${testName}, "Failed")
$vm.emailSummary += " Missing resources<br />"
$vm.currentTest = "done"
UpdateState $vm $Disabled
}
}
# Case requires a fresh new state VM to run.
else {
# Reset the VM to a snapshot to put the VM in a known state. The default name is
# ICABase. This can be overridden by the global.defaultSnapshot in the global section
# and then by the vmSnapshotName in the VM definition.
$snapshotName = "ICABase"
if ($xmlData.config.global.defaultSnapshot) {
$snapshotName = $xmlData.config.global.defaultSnapshot
LogMsg 5 "Info : $($vm.vmName) Over-riding default snapshotName from global section to $snapshotName"
}
if ($vm.vmSnapshotName) {
$snapshotName = $vm.vmSnapshotName
LogMsg 5 "Info : $($vm.vmName) Over-riding default snapshotName from VM section to $snapshotName"
}
# Find the snapshot we need and apply the snapshot
$snapshotFound = $false
$vmObj = Get-VMHost -Name $vm.hvServer | Get-VM -Name $vm.vmName
$snapsOut = Get-Snapshot -VM $vmObj
if ($snapsOut) {
foreach ($s in $snapsOut) {
if ($s.Name -eq $snapshotName) {
LogMsg 3 "Info : $($vm.vmName) is being reset to snapshot $($s.Name)"
$setsnapOut = Set-VM -VM $vmObj -Snapshot $s -Confirm:$false
if ($setsnapOut) {
$snapshotFound = $true
break
}
else {
LogMsg 0 "Error : ApplyCheckpoint is unable to revert VM $($vm.vmName) to snapshot $($s.Name). VM has been disabled"
$vm.emailSummary += "Unable to revert snapshot. VM was disabled and no tests run<br />"
UpdateState $vm $Disabled
return
}
}
}
}
# Make sure the snapshot left the VM in a stopped state.
if ($snapshotFound) {
# If a VM is in the Suspended (Saved) state after applying the snapshot,
# the following will handle this case
$vmObj = Get-VMHost -Name $vm.hvServer | Get-VM -Name $vm.vmName
if ($vmObj) {
if ($vmObj.PowerState -eq "Suspended") {
LogMsg 3 "Info : $($vm.vmName) - resetting to a stopped state after restoring a snapshot"
$stopvmOut = Stop-VM -VM $vmObj -Confirm:$false -Kill
if ($stopvmOut -or $stopvmOut.PowerState -ne "PoweredOff") {
LogMsg 0 "Error : ApplyCheckpoint is unable to stop VM $($vm.vmName). VM has been disabled"
$vm.emailSummary += "Unable to stop VM. VM was disabled and no tests run<br />"
UpdateState $vm $Disabled
return
}
}
}
else {
LogMsg 0 "Error : ApplyCheckpoint cannot find the VM $($vm.vmName)"
$vm.emailSummary += "VM $($vm.vmName) cannot be found - no tests run on VM<br />"
UpdateState $vm $Disabled
return
}
}
else {
LogMsg 0 "Warn : $($vm.vmName) does not have a snapshot named $snapshotName."
}
}
}
else {
LogMsg 0 "Error: No test data for test $($vm.currentTest) in the .xml file`n $($vm.vmName) has been disabled"
$vm.emailSummary += "No test data found for test $($vm.currentTest)<br />"
$vm.currentTest = "done"
UpdateState $vm $Disabled
}
if ($vm.preStartConfig -or $testData.setupScript) {
UpdateState $vm $RunSetupScript
}
else {
UpdateState $vm $StartSystem
}
}
########################################################################################
# DoRunSetupScript()
########################################################################################
function DoRunSetupScript([System.Xml.XmlElement] $vm, [XML] $xmlData) {
<#
.Synopsis
Run a setup script to reconfigure a VM.
.Description
If the currentTest has a setup script defined, run the
setup script to reconfigure the VM.
.Parameter vm
XML Element representing the VM under test.
.Parameter xmlData
XML document driving the test.
.Example
DoRunSetupScript $testVM $xmlData
#>
if (-not $vm -or $vm -isnot [System.Xml.XmlElement]) {
LogMsg 0 "Error : DoRunSetupScript() was passed an bad VM object"
return
}
LogMsg 9 "Info : DoRunSetupScript( $($vm.vmName) )"
if (-not $xmlData -or $xmlData -isnot [XML]) {
LogMsg 0 "Error : DoRunSetupScript received a null or bad xmlData parameter - terminating VM"
$vm.currentTest = "done"
UpdateState $vm $Disabled
}
#run preStartScript if has
if ($vm.preStartConfig) {
if ($vm.preStartConfig.File) {
foreach ($preStartScript in $vm.preStartConfig.file) {
LogMsg 3 "Info : $($vm.vmName) running preStartConfig script '${preStartScript}' "
$sts = RunPSScript $vm $preStartScript $xmlData "preStartConfig"
if (-not $sts) {
LogMsg 0 "Error : VM $($vm.vmName) preStartConfig script: $preStartScript failed"
}
}
}
else {
# original syntax of <preStartConfig>.\setupscripts\Config-VM.ps1</preStartConfig>
LogMsg 3 "Info : $($vm.vmName) - starting preStart script $($vm.preStartConfig)"
$sts = RunPSScript $vm $($vm.preStartConfig) $xmlData "preStartConfig"
if (-not $sts) {
LogMsg 0 "Error : VM $($vm.vmName) preStartConfig script: $($vm.preStartConfig) failed"
}
}
}
# Run setup script if one is specified (this setup Script is defined in testcase level)
$testData = GetTestData $($vm.currentTest) $xmlData
if ($testData -is [System.Xml.XmlElement]) {
$testName = $testData.testName
$abortOnError = $true
if ($testData.onError -eq "Continue") {
$abortOnError = $false
}
if ($testData.setupScript) {
if ($testData.setupScript.File) {
foreach ($script in $testData.setupScript.File) {
LogMsg 3 "Info : $($vm.vmName) - running setup script '${script}'"
if (-not (RunPSScript $vm $script $xmlData "Setup" $logfile)) {
# If the setup script fails, fail the test. If <OnError>
# is continue, continue on to the next test in the suite.
# Otherwise, terminate testing.
LogMsg 0 "Error : VM $($vm.vmName) setup script ${script} for test ${testName} failed"
# $vm.emailSummary += (" Test {0, -25} : {1}<br />" -f ${testName}, "Failed - setup script failed")
if ($abortOnError) {
$vm.currentTest = "done"
UpdateState $vm $finished
return
}
else {
UpdateState $vm $PS1TestCompleted
return
}
}
}
}
else {
# the older, single setup script syntax
LogMsg 3 "Info : $($vm.vmName) - running single setup script '$($testData.setupScript)'"
if (-not (RunPSScript $vm $($testData.setupScript) $xmlData "Setup" $logfile)) {
# If the setup script fails, fail the test. If <OnError>
# is continue, continue on to the next test in the suite.
# Otherwise, terminate testing.
LogMsg 0 "Error : VM $($vm.vmName) setup script $($testData.setupScript) for test ${testName} failed"
#$vm.emailSummary += (" Test {0, -25} : {1}<br />" -f ${testName}, "Failed - setup script failed")
if ($abortOnError) {
$vm.currentTest = "done"
UpdateState $vm $finished
return
}
else {
UpdateState $vm $PS1TestCompleted
return
}
}
}
}
else {
LogMsg 9 "INFO : $($vm.vmName) does not have setup script defined for test $($vm.currentTest)"
}
UpdateState $vm $StartSystem
}
else {
LogMsg 0 "Error : $($vm.vmName) could not find test data for $($vm.currentTest)`n The VM $($vm.vmName) will be disabled"
$vm.emailSummary += "Test $($vm.currentTest) : Aborted (no test data)<br />"
$vm.currentTest = "done"
UpdateState $vm $Disabled
}
}