This repository was archived by the owner on Sep 17, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRun-Tests.psm1
More file actions
187 lines (136 loc) · 5.25 KB
/
Run-Tests.psm1
File metadata and controls
187 lines (136 loc) · 5.25 KB
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
##############################################################################
#
# Script to run Nunit tests on a remote machine
#
# Uses FastNunit.exe to deal with spinning up as many users
# as possible.
#
##############################################################################
function Get-ScriptDirectory {
$invocation = (Get-Variable MyInvocation -Scope 1).Value
$script = [IO.FileInfo] $invocation.MyCommand.Path
if ([IO.File]::Exists($script)) {
Return (Split-Path $script.Fullname)
} else {
return $null
}
}
$scriptPath = Get-ScriptDirectory
Write-Host "Script residing at $scriptPath"
<#
.Synopsis
Run a bunch of Nunit tests on our big ol' server.
.Description
Packages up dependencies, then copies them to a share on the test server
before running a job to execute the tests.
.Parameter TestAssembly
The assembly the tests are located in.
.Parameter Category (optional)
The category of tests to run, as defined in Nunit.
.Parameter ResultsFileName
The filename to store the results in.
.Parameter AdditionalFiles (optional)
Any additional files that the tests depend on, assembly dependencies will be automatically resolved
so this list should be additional non-code dependencies, i.e. config files.
.Example
# Run all the tests in a given assembly and save to results.xml
Run-Tests -TestAsssembly Nunit.Tests.dll -ResultsFileName results.xml
.Example
# Run all tests in a category
Run-Tests -TestAsssembly Nunit.Tests.dll -Category sanity -ResultsFileName results.xml
.Example
# Run all tests with a couple of additional files
Run-Tests -TestAssembly Nunit.Tests.dll -ResultsFileName results.xml -AdditionalFiles Nunit.Tests.dll.config,Additional.Data.xml
#>
function Run-Tests() {
param ($TestAssembly, $Category, $ResultsFileName, $AdditionalFiles)
Write-Host "Importing credentials management."
. "$scriptPath\Store-Credentials.ps1"
Write-Host "Importing stored credentials."
$credentials = Import-PSCredential
# If not already enabled, turn on PSRemoting
$remoteHost = "btmgsrvhpv02.brislabs.com"
$hosts = Get-Item WsMan:\localhost\Client\TrustedHosts
if ( $hosts.Value -notcontains $remoteHost )
{
Write-Host "Enabling PSRemoting..."
winrm quickconfig
winrm set winrm/config/client '@{TrustedHosts="btmgsrvhpv02.brislabs.com"}'
}
# Create a unique Id for the job.
$testId = [System.Guid]::NewGuid().ToString()
Write-Host "Started Job: $testId"
$credentials = Import-PSCredential
$zipFileName = "input.zip"
#run packager to build a zip
Write-Host "Packaging up assembly and dependent files."
if ([String]::IsNullOrEmpty($AdditionalFiles))
{
Invoke-Expression "$scriptPath\Packager.exe -a $TestAssembly -o $zipFileName"
}
else
{
$files = ([String]::Join(",", $AdditionalFiles))
Write-Host "Additional files added to zip:"
Write-Host $files
$command = "$scriptPath\Packager.exe -a $TestAssembly -o $zipFileName -f $files"
Write-Host $command
Invoke-Expression $command
}
$drive = New-PSDrive -Name J -PSProvider FileSystem -Root "\\$remoteHost\Jobs" -Credential $credentials
$dir = New-Item "J:\$testId" -ItemType Directory
Copy-Item $zipFileName -Destination "J:\$testId"
# Copy FastNunit and all dependencies (packager could do this, a standalone exe would be nice though.)
Copy-Item "$scriptPath\FastNunit.exe" -Destination "J:\$testId"
"$scriptPath\FSharp.Core.dll", `
"$scriptPath\nunit.framework.dll", `
"$scriptPath\CommandLine.dll", `
"$scriptPath\FSharp.Data.dll", `
"$scriptPath\FSharp.Data.TypeProviders.dll", `
"$scriptPath\Packager.exe" | % { Copy-Item $_ -Destination "J:\$testId" }
Remove-PSDrive -Name J
#run the tests in a remote session:
$results = invoke-command -ComputerName $remoteHost -Credential $credentials -port 80 -ScriptBlock {
param ($ass, $cat, $id, $zip)
# Crack open the zip file - in an ideal world we'd infer the local path
cd "c:\Jobs\$id"
$shell = New-Object -com shell.application
[System.Int32]$yesToAll = 16
$path = (pwd).Path
Write-Host "Unzipping file at $path\$zip"
$zipFile = $shell.namespace("$path\$zip")
$destination = $shell.Namespace("$path")
$destination.CopyHere($zipFile.Items(), $yesToAll)
$resultsFile = "results.xml"
Write-Host "Asking manager to start the tests."
$job = @{
sessionId = $id
path = "$path\FastNunit.exe"
arguments = "-a ""$ass"" -o ""$resultsFile"" -i ""$id"" -w ""$path"""
}
if($cat)
{
$job.Arguments += " -c $cat"
}
$jobText = ConvertTo-Json $job -Compress
$response = Invoke-RestMethod -Uri http://localhost:8080/jobs -Method POST -Body $jobText -ContentType application/json
# TODO: Check the response code and deal appropriately
if ($response -eq $null)
{
throw "Failed to create a new job."
}
$jobPath = "http://localhost:8080/job/$id"
Write-Host "Waiting for the job to finish."
do {
$jobStatus = Invoke-RestMethod -Uri $jobPath
Start-Sleep -Seconds 5
} while ( -not $jobStatus.completed )
Write-Host "Job finished, output:"
Write-Host $jobStatus
Get-Content $resultsFile
} -ArgumentList $TestAssembly, $Category, $testId, $zipFileName
Write-Host "Deleting temporary file $zipFileName"
Remove-Item $zipFileName -Force
Set-Content $ResultsFileName -Value $results
}
export-ModuleMember -Function Run-Tests