-
Notifications
You must be signed in to change notification settings - Fork 15
/
LISAv2-Framework.psm1
268 lines (229 loc) · 9.25 KB
/
LISAv2-Framework.psm1
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
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the Apache License.
using Module "TestControllers\AzureController.psm1"
using Module "TestControllers\HyperVController.psm1"
using Module "TestControllers\OLController.psm1"
using Module "TestControllers\WSLController.psm1"
$here = Split-Path -Parent $MyInvocation.MyCommand.Path
Get-ChildItem (Join-Path $here "Libraries") -Recurse | Where-Object { $_.FullName.EndsWith(".psm1") } | `
ForEach-Object { Import-Module $_.FullName -Force -Global -DisableNameChecking }
# Get inital variable list
$initialGlobalVarList = Get-Variable -Scope "Global" | Select-Object -ExpandProperty Name
function Start-LISAv2 {
[CmdletBinding()]
Param(
[string] $ParametersFile = "",
# [Required]
[ValidateSet('Azure','HyperV','OLQ','WSL' ,'OLVM',IgnoreCase = $false)]
[string] $TestPlatform = "",
# [Required] for Azure.
[string] $TestLocation="",
[string] $ARMImageName = "",
[string] $StorageAccount="",
# [Required] for Two Hosts HyperV
[string] $DestinationOsVHDPath="",
# [Required] Common for HyperV and Azure.
[string] $RGIdentifier = "",
[string] $OsVHD = "", #... [Azure: Required only if -ARMImageName is not provided.]
#... [HyperV: Mandatory]
#... [OLVM: Mandatory]
#... [WSL: Mandatory, which can be the URL of the distro, or the path to the distro file on the local host]
[string] $TestCategory = "",
[string] $TestArea = "",
[string] $TestTag = "",
[string] $TestNames="",
[string] $TestPriority="",
# [Optional] Exclude the tests from being executed. (Comma separated values)
[string] $ExcludeTests = "",
# [Optional] Enable kernel code coverage
[switch] $EnableCodeCoverage,
# [Optional] Parameters for Image preparation before running tests.
[string] $CustomKernel = "",
[string] $CustomLIS,
# [Optional] Parameters for changing framework behavior.
[int] $TestIterations = 1,
[string] $XMLSecretFile = "",
[switch] $EnableTelemetry,
[switch] $UseExistingRG,
# [Optional] Parameters for setting TiPCluster=ClusterId;TipSessionId=SessionId;DiskType=Managed/Unmanaged;Networking=SRIOV/Synthetic.
[string] $CustomParameters = "",
# [Optional] Parameters for Overriding VM Configuration.
[string] $CustomTestParameters = "",
[string] $OverrideVMSize = "",
[ValidateSet('Default','Keep','Delete',IgnoreCase = $true)]
[string] $ResourceCleanup,
[switch] $DeployVMPerEachTest,
[string] $VMGeneration = "",
[string] $ResultDBTable = "",
[string] $ResultDBTestTag = "",
[switch] $ExitWithZero
)
PROCESS {
try {
# Generate test log file
$testId = New-TestId
$logFileName = "LISAv2-Test-${testId}.log"
Set-Variable -Name "LogFileName" -Value $logFileName -Scope Global -Force
Set-Variable -Name "TestId" -Value $testId -Scope Global -Force
Write-LogInfo "Autogenerated test ID: $testId"
# Get git information
# Set initial value
$currentBranch = $null
$CommitId = $null
try {
git branch | foreach {
if($_ -match "^\*(.*)"){
$currentBranch = $matches[1]
Write-LogInfo "Found branch information and updated with $currentBranch"
}
}
$CommitId = git log --format="%ai`t%H`t%an`t%ae`t%s" -n 1 | ConvertFrom-String -Delimiter "`t"
$CommitId = $CommitId.P2
} catch {
Write-LogInfo "This may not be in Git repo"
}
# Prepare the workspace
$maxDirLength = 32
$workingDirectory = (Get-Location).Path
if ($workingDirectory.Length -gt $maxDirLength) {
$originalWorkingDirectory = $workingDirectory
$workingDirectory = Move-ToNewWorkingSpace $originalWorkingDirectory | `
Select-Object -Last 1
}
Set-Variable -Name "WorkingDirectory" -Value $workingDirectory -Scope Global
# Prepare log folder
$testTimestamp = Get-Date -Format 'yyyy-dd-MM-HH-mm-ss-ffff'
$logDir = Join-Path $workingDirectory "TestResults\${testTimestamp}"
New-Item -ItemType "Directory" -Path $logDir -Force | Out-Null
Write-LogInfo "Logging directory: $logDir"
Set-Variable -Name "LogDir" -Value $logDir -Scope Global -Force
# Import parameters from file if ParametersFile parameter is given
# and set them as global variables
$paramTable = @{}
if ($ParametersFile) {
$paramTable = Import-TestParameters -ParametersFile $ParametersFile
}
# Processing parameters provided in the command runtime
$paramList = (Get-Command -Name $PSCmdlet.MyInvocation.InvocationName).Parameters
foreach ($paramName in $paramList.Keys) {
$paramValue = (Get-Variable -Name $paramName -ErrorAction "SilentlyContinue").Value
if ($paramValue) {
if ($paramTable.ContainsKey($paramName)) {
Write-LogInfo "Overwriting parameter $paramName value: $($paramTable[$paramName]) -> $paramValue"
$paramTable[$paramName] = $paramValue
} else {
Write-LogInfo "Setting parameter: $paramName = $paramValue"
$paramTable.Add($paramName, $paramValue)
}
}
}
# Validate test platform, and select test controller of the platform
$supportedPlatforms = @("Azure", "HyperV", "OLQ", "WSL", "OLVM")
if ($paramTable.ContainsKey("TestPlatform")) {
$testPlatform = $paramTable["TestPlatform"]
}
if ($testPlatform) {
if ($supportedPlatforms.contains($testPlatform)) {
if ($testPlatform.StartsWith('OL')) {
$testController = New-Object -TypeName "OLController" -ArgumentList $testPlatform
}
else {
$testController = New-Object -TypeName $testPlatform"Controller"
}
} else {
throw "$testPlatform is not yet supported."
}
} else {
throw "'TestPlatform' parameter is not provided."
}
# Validate the test parameters.
$testController.ParseAndValidateParameters($paramTable)
# Handle the secrets file
if ($env:Azure_Secrets_File) {
$XMLSecretFile = $env:Azure_Secrets_File
Write-LogInfo "The Secrets file is defined by an environment variable."
}
$testController.PrepareTestEnvironment($XMLSecretFile)
# Validate all the XML files and then import test cases from them for test
Validate-XmlFiles -ParentFolder $workingDirectory
$testController.LoadTestCases($workingDirectory, $CustomTestParameters)
# Create report folder
$reportFolder = Join-Path $workingDirectory "Report"
if (!(Test-Path $reportFolder)) {
New-Item -ItemType "Directory" $reportFolder | Out-Null
}
$TestReportXml = Join-Path "$reportFolder" "LISAv2_TestReport_$testId-junit.xml"
# Create result folder
$TestResultsDir = "$WorkingDirectory\TestResults"
if (!(Test-Path $TestResultsDir)) {
New-Item -ItemType "Directory" $TestResultsDir | Out-Null
}
# Run test
$testController.RunTest($TestReportXml,$TestIterations,$false)
Write-LogInfo "Test $global:testId finished"
# Output text summary
$plainTextSummary = $testController.TestSummary.GetPlainTextSummary()
Write-LogInfo $plainTextSummary
if ( $currentBranch ) {
Write-LogInfo "Current Git Branch: $currentBranch"
Write-LogInfo "Current Git Commit ID: $CommitId"
}
# Zip the test log folder
$zipFile = "$TestPlatform"
if ( $TestCategory ) { $zipFile += "-$TestCategory" }
if ( $TestArea ) { $zipFile += "-$TestArea" }
if ( $TestTag ) { $zipFile += "-$($TestTag)" }
if ( $TestPriority ) { $zipFile += "-$($TestPriority)" }
$zipFile += "-$testId-TestLogs.zip"
$zipFile = $zipFile.Replace("*", "All")
$zipFilePath = Join-Path (Get-Location).Path $zipFile
New-ZipFile -zipFileName $zipFilePath -sourceDir $LogDir
if (Test-Path -Path $TestReportXml) {
Write-LogInfo "Analyzing results.."
$results = $null
try {
$results = [xml](Get-Content $TestReportXml -ErrorAction SilentlyContinue)
} catch {
throw "Could not parse test report results"
}
$testSuiteresults = $results.testsuites.testsuite
if (($testSuiteresults.failures -eq 0) `
-and ($testSuiteresults.errors -eq 0) `
-and ($testSuiteresults.tests -gt 0)) {
$ExitCode = 0
} else {
$ExitCode = 1
}
} else {
Write-LogErr "Summary file: $TestReportXml does not exist. Exiting with exit code 1"
$ExitCode = 1
}
} catch {
$line = $_.InvocationInfo.ScriptLineNumber
$script_name = ($_.InvocationInfo.ScriptName).Replace($PWD,".")
$ErrorMessage = $_.Exception.Message
Write-LogErr "EXCEPTION: $ErrorMessage"
Write-LogErr "Source: Line $line in script $script_name."
$ExitCode = 1
} finally {
if ($OriginalWorkingDirectory) {
Move-BackToOriginalWorkingSpace $WorkingDirectory $OriginalWorkingDirectory $ExitCode
}
if ( $ExitWithZero -and ($ExitCode -ne 0) ) {
Write-LogInfo "Forcefully exiting with exit code 0 as ExitWithZero flag was set to $true"
$ExitCode = 0
}
Write-LogInfo "LISAv2 exit code: $ExitCode"
# Remove all varibles that cannot be found in the initial list
$finalGlobalVarList = Get-Variable -Exclude ("ExitCode", "LASTEXITCODE") -Scope "Global" | Select-Object -ExpandProperty Name
$removableGlobalVarList = (Compare-Object $initialGlobalVarList $finalGlobalVarList).InputObject
Get-Variable -Include $removableGlobalVarList -Scope "Global" | Remove-Variable -Scope "Global" `
-Force -ErrorAction SilentlyContinue
if ($ExitCode -ne 0) {
throw "LISAv2 failed with exit code: $ExitCode"
}
}
}
}
New-Alias -Name Run-LISAv2 -Value Start-LISAv2 -Force
Export-ModuleMember -Function * -Alias *