master
ps1 168 lines 6.87 KB
Raw
1 <#
2 .SYNOPSIS
3 Sets up a given Machine for running WSL tests.
4 .PARAMETER VMName
5 Name of the VM or Computer to set-up for testing.
6 .PARAMETER ComputerName
7 Name of the Computer to set-up for testing.
8 .PARAMETER IPv6Addr
9 IPv6 address of the computer to set-up for testing.
10 .PARAMETER Credential
11 A credential object to be used for authenticating with New-PSSession.
12 .PARAMETER Username
13 Username on the given VM to set-up the tests under.
14 .PARAMETER Password
15 Password associated with the Username, if needed.
16 .PARAMETER ArtifactFolder
17 Specify when using cmake -S ArtifactFolder.
18 .PARAMETER BuildType
19 The type of build to create when compiling the WSL source code. Defaults to "Debug".
20 .PARAMETER MakeTrusted
21 An optional switch to add the destination to the TrustedHosts list.
22 .PARAMETER SkipEnableFeatures
23 Skip enabling optional Windows features necessary for some tests.
24 .PARAMETER RemoteFolder
25 Absolute Path to a folder on the VM to copy requisite files to. Defaults to "C:\Package".
26 .PARAMETER TaefFolder
27 Absolute Path to a folder on the VM to copy taef binaries. Defaults to "C:\Taef".
28 .PARAMETER SkipDistro
29 Skip copying over the distro.
30 .PARAMETER TestDistroPath
31 Path to the distro image to import and use for testing, if needed. Auto filled if left empty.
32 .PARAMETER TestDataPath
33 Path to the test data folder to be copied to the VM, if needed. Auto filled if left empty.
34 #>
35
36 [CmdletBinding(PositionalBinding=$False, DefaultParameterSetName='vm')]
37 param (
38 [Parameter(Mandatory = $true, ParameterSetName='vm')][string]$VMName,
39 [Parameter(Mandatory = $true, ParameterSetName='hostname')][string]$ComputerName,
40 [Parameter(Mandatory = $true, ParameterSetName='ipv6')][string]$IPv6Addr,
41 [System.Management.Automation.PSCredential]$Credential,
42 [string]$Username,
43 [string]$Password,
44 [string]$ArtifactFolder,
45 [ValidateSet("Debug", "Release")][string]$BuildType = "Debug",
46 [switch]$MakeTrusted,
47 [switch]$SkipEnableFeatures,
48 [string]$RemoteFolder = "C:\Package",
49 [string]$TaefFolder = "C:\Taef",
50 [switch]$SkipDistro,
51 [string]$TestDistroPath,
52 [string]$TestDataPath
53 )
54
55 $ErrorActionPreference = "Stop"
56 Set-StrictMode -Version Latest
57
58 if ($Credential -eq $null) {
59 if ([string]::IsNullOrEmpty($Username)) {
60 $Credential = Get-Credential
61 }
62 else {
63 if ([string]::IsNullOrEmpty($Password)) {
64 $SecurePassword = New-Object System.Security.SecureString
65 }
66 else {
67 $SecurePassword = ConvertTo-SecureString "$Password" -AsPlainText -Force
68 }
69 $Credential = New-Object System.Management.Automation.PSCredential("$Username", $SecurePassword)
70 }
71 }
72
73 if ($PSCmdlet.ParameterSetName -eq 'ipv6') {
74 $ComputerName = $Ipv6Addr.Replace(":", "-")+".ipv6-literal.net"
75 }
76
77 if (![string]::IsNullOrEmpty($ComputerName) -and $MakeTrusted) {
78 Set-Item -Path WSMan:\localhost\Client\TrustedHosts -Concatenate -Value $ComputerName
79 }
80
81 $Session = switch ($PSCmdlet.ParameterSetName) {
82 "vm" {
83 New-PSSession -VMName $VMName -Credential $Credential
84 }
85 default {
86 New-PSSession `
87 -Authentication Negotiate `
88 -ComputerName $ComputerName `
89 -Credential $Credential
90 }
91 }
92 $Arch = Invoke-Command -Session $Session {$env:PROCESSOR_ARCHITECTURE}
93 $Platform = switch ($Arch) {
94 "AMD64" {"X64"}
95 "ARM64" {"arm64"}
96 default { throw "Architecture $Arch unknown" }
97 }
98
99 if ([string]::IsNullOrEmpty($TestDistroPath)) {
100 $TestDistroVersion = (Select-Xml -Path "$PSScriptRoot\..\..\packages.config" -XPath '/packages/package[@id=''Microsoft.WSL.TestDistro'']/@version').Node.Value
101 $TestDistroPath = "$PSScriptRoot\..\..\packages\Microsoft.WSL.TestDistro.$TestDistroVersion\$Platform\test_distro.tar.xz"
102 }
103
104 if ([string]::IsNullOrEmpty($TestDataPath)) {
105 $TestDataVersion = (Select-Xml -Path "$PSScriptRoot\..\..\packages.config" -XPath '/packages/package[@id=''Microsoft.WSL.TestData'']/@version').Node.Value
106 $TestDataPath = "$PSScriptRoot\..\..\packages\Microsoft.WSL.TestData.$TestDataVersion\$Platform"
107 }
108
109 if ([string]::IsNullOrEmpty($ArtifactFolder)) {
110 $ArtifactFolder = "$PSScriptRoot/../.."
111 }
112
113 $Bin = "$((Get-ItemProperty -Path $ArtifactFolder).FullName)/bin/$Platform/$BuildType"
114
115 if (!$SkipEnableFeatures) {
116 $Reboot = Invoke-Command -Session $Session -ScriptBlock {
117 $RestartNeeded = $false
118
119 $OptionalFeatureNames = @(
120 "VirtualMachinePlatform", # Needed to run V2 tests
121 "Microsoft-Hyper-V-Management-PowerShell", # Needed for mount tests
122 "Microsoft-Windows-Subsystem-Linux") # Needed to run V1 tests
123
124 foreach ($Name in $OptionalFeatureNames) {
125 if ((Get-WindowsOptionalFeature -Online -FeatureName $Name).State -ne "Enabled") {
126 $RestartNeeded = (Enable-WindowsOptionalFeature -Online -All -NoRestart -FeatureName $Name -WarningAction SilentlyContinue).RestartNeeded -or $RestartNeeded
127 }
128 }
129
130 return $RestartNeeded
131 }
132 }
133 else {
134 $Reboot = $false
135 }
136
137 Invoke-Command -Session $Session -ArgumentList $RemoteFolder -ScriptBlock {
138 New-Item -ItemType Directory -Path "$Using:RemoteFolder" -Force
139 }
140 Copy-Item -ToSession $Session -Path "$Bin/installer.msix" -Destination $RemoteFolder -Force
141 Copy-Item -ToSession $Session -Path "$Bin/wsltests.dll" -Destination $RemoteFolder -Force
142 Copy-Item -ToSession $Session -Path "$Bin/wslcsdk.dll" -Destination $RemoteFolder -Force
143 Copy-Item -ToSession $Session -Path "$Bin/testplugin.dll" -Destination $RemoteFolder -Force
144 Copy-Item -ToSession $Session -Path "$PSScriptRoot/test-setup.ps1" -Destination $RemoteFolder -Force
145 Copy-Item -ToSession $Session -Path "$PSScriptRoot/run-tests.ps1" -Destination $RemoteFolder -Force
146 Copy-Item -ToSession $Session -Path "$PSScriptRoot/../../test/linux/unit_tests" -Destination $RemoteFolder -Recurse -Force
147
148 if (!$SkipDistro) {
149 Copy-Item -ToSession $Session -Path $TestDistroPath -Destination "$RemoteFolder/test_distro.tar.gz" -Force
150 }
151
152 Copy-Item -ToSession $Session -Path $TestDataPath -Destination "$RemoteFolder/test_data" -Recurse -Force
153
154 $taefVersion = (Select-Xml -Path "$PSScriptRoot\..\..\packages.config" -XPath '/packages/package[@id=''Microsoft.Taef'']/@version').Node.Value
155 $taefPackage = "$ArtifactFolder/packages/Microsoft.Taef.$taefVersion/build/Binaries/$Platform"
156 Copy-Item -ToSession $Session -Path "$taefPackage" -Destination $TaefFolder -Recurse -Force
157 Invoke-Command -Session $Session -ArgumentList $RemoteFolder -ScriptBlock {
158 $path = [System.Environment]::GetEnvironmentVariable("PATH", [EnvironmentVariableTarget]::User) -split ";"
159 if ($path -notcontains $using:TaefFolder) {
160 $path += $using:TaefFolder
161 [System.Environment]::SetEnvironmentVariable("PATH", $path -join ";", [EnvironmentVariableTarget]::User)
162 }
163 }
164
165
166 if ($Reboot) {
167 Invoke-Command -Session $Session -ScriptBlock { Start-Process shutdown -ArgumentList "-r","-t 0" }
168 }