-
Notifications
You must be signed in to change notification settings - Fork 9
/
Wsl.Tests.ps1
437 lines (362 loc) · 20.1 KB
/
Wsl.Tests.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
# When adding new functionality, you should:
# - Write tests for the new functionality.
# - Run the tests on both PowerShell Core and Windows PowerShell.
# - Run the tests against the latest WSL store version.
# - Run the tests against the oldest supported WSL inbox version, which should be the oldest Windows
# version still in mainstream support.
# - See https://learn.microsoft.com/lifecycle/products/windows-10-home-and-pro
#
# These tests are not designed to run in PowerShell on Linux inside WSL; only run them directly in
# Windows.
#Requires -Modules @{ ModuleName="Pester"; ModuleVersion="5.0" }
param(
[Parameter(Mandatory=$false)][string]$TestDistroPath
)
BeforeDiscovery {
Import-Module "$PSScriptRoot/Wsl.psd1" -Force
$wslVersion = (Get-WslVersion).Wsl
}
BeforeAll {
function Test-Distro($Distro, [string]$Name, [string]$Version, [string]$State, [string]$BasePath, [string]$VhdFile = "ext4.vhdx", [Switch]$Default)
{
$Distro | Should -Not -BeNullOrEmpty
$Distro.Name | Should -Be $Name
$Distro.Version | Should -Be $Version
$Distro.State | Should -Be $State
$Distro.Guid | Should -Not -BeNullOrEmpty
if (-not $BasePath) {
$BasePath = "$TestDrive\wsl\$Name"
}
$Distro.BasePath | Should -Be $BasePath
$Distro.FileSystemPath | Should -Be "\\wsl.localhost\$Name"
$Distro.Default | Should -Be $Default
if ($Distro.Version -eq 2) {
$Distro.VhdPath | Should -Be (Join-Path $BasePath $VhdFile)
} else {
$Distro.VhdPath | Should -BeNullOrEmpty
}
}
function Test-DistroEqual($Expected, $Actual)
{
$Expected | Should -Not -BeNullOrEmpty
$Actual | Should -Not -BeNullOrEmpty
$Actual.Name | Should -Be $Expected.Name
$Actual.Version | Should -Be $Expected.Version
$Actual.State | Should -Be $Expected.State
$Actual.Guid | Should -Be $Expected.Guid
$Actual.BasePath | Should -Be $Expected.BasePath
$Actual.FileSystemPath | Should -Be $Expected.FileSystemPath
$Actual.Default | Should -Be $Expected.Default
$Actual.VhdPath | Should -Be $Expected.VhdPath
}
Write-Warning "These tests should be run on a machine with no existing distributions."
# This check for arm64 requires cross-platform PowerShell; you can still run the tests with
# Windows PowerShell using the -TestDistroPath parameter.
if ([System.Runtime.InteropServices.RuntimeInformation]::OSArchitecture -eq "Arm64") {
$testDistroUrl = "https://dl-cdn.alpinelinux.org/alpine/v3.18/releases/aarch64/alpine-minirootfs-3.18.3-aarch64.tar.gz"
} else {
$testDistroUrl = "https://dl-cdn.alpinelinux.org/alpine/v3.18/releases/x86_64/alpine-minirootfs-3.18.3-x86_64.tar.gz"
}
$testDistroFile = "TestDrive:/wslps_test.tar.gz"
if ($TestDistroPath) {
Copy-Item $TestDistroPath $testDistroFile
} else {
Invoke-WebRequest $testDistroUrl -OutFile $testDistroFile
}
New-Item TestDrive:/wsl -ItemType Directory | Out-Null
$originalWslUtf8 = $env:WSL_UTF8
if (Test-Path env:WSL_UTF8) {
Remove-Item env:WSL_UTF8
}
}
AfterAll {
try {
Remove-WslDistribution "wslps_*"
} catch {
# Don't care about exceptions here.
}
$env:WSL_UTF8 = $originalWslUtf8
}
Describe "WslManagementPS" {
It "Can list with no distributions" {
Get-WslDistribution | Should -BeNullOrEmpty
}
It "Can accept empty collections as pipes" {
# Use the filter string to avoid affecting other distributions if someone ran this test on a
# system that has distributions by mistake.
Get-WslDistribution "wslps_*" | Remove-WslDistribution
Get-WslDistribution "wslps_*" | Stop-WslDistribution
Get-WslDistribution "wslps_*" | Export-WslDistribution -Destination "TestDrive:/wsl"
Get-WslDistribution "wslps_*" | Invoke-WslCommand "echo foo"
Get-WslDistribution "wslps_*" | Set-WslDistribution -Version 2
}
It "Throws when removing a non-existing distribution" {
{ Remove-WslDistribution "wslps_bogus" } | Should -Throw "There is no distribution with the name 'wslps_bogus'."
}
# A bunch of the below tests depend on this one, so if this one fails, expect more to fail.
It "Can import and export distributions" {
$distro = Import-WslDistribution $testDistroFile -Destination "TestDrive:/wsl" -Version 1
Test-Distro $distro "wslps_test" 1 "Stopped" -Default
Test-DistroEqual $distro (Get-WslDistribution "wslps_test")
# Already exists
{ Import-WslDistribution $testDistroFile -Destination "TestDrive:/" -Version 1 } | Should -Throw
# Specific name
$distro = Import-WslDistribution $testDistroFile -Name "wslps_test2" -Destination "TestDrive:/wsl" -Version 2
Test-Distro $distro "wslps_test2" 2 "Stopped"
Test-DistroEqual $distro (Get-WslDistribution "wslps_test2")
# Raw destination
New-Item "TestDrive:/wsl/raw" -ItemType Directory
$distro = Import-WslDistribution $testDistroFile -Name "wslps_raw" -Destination "TestDrive:/wsl/raw" -RawDestination -Version 2
Test-Distro $distro "wslps_raw" 2 "Stopped" "$TestDrive\wsl\raw"
Test-DistroEqual $distro (Get-WslDistribution "wslps_raw")
# Export single distro to file.
$exported = Export-WslDistribution "wslps_test2" "TestDrive:/wslps_exported.tar.gz"
$exported.FullName | Should -Be "$TestDrive\wslps_exported.tar.gz"
Test-Path "TestDrive:/wslps_exported.tar.gz" -PathType Leaf | Should -Be $true
$exported.Length | Should -Not -Be (Get-Item (Get-WslDistribution "wslps_test2").VhdPath).Length
# Export multiple distros using wildcards
New-Item "TestDrive:/exported" -ItemType Container
Export-WslDistribution "wslps_test*" "TestDrive:/exported" | Out-Null
Test-Path "TestDrive:/exported/wslps_test.tar.gz" -PathType Leaf | Should -Be $true
Test-Path "TestDrive:/exported/wslps_test2.tar.gz" -PathType Leaf | Should -Be $true
(Get-Item "TestDrive:/exported/wslps_test2.tar.gz").Length | Should -Not -Be (Get-Item (Get-WslDistribution "wslps_test2").VhdPath).Length
"TestDrive:/exported/wslps_raw.tar.gz" | Should -Not -Exist
# Export multiple distros using pipeline
Remove-Item "TestDrive:/exported/*"
Get-WslDistribution -Version 2 | Export-WslDistribution -Destination "TestDrive:/exported"
Test-Path "TestDrive:/exported/wslps_test2.tar.gz" -PathType Leaf | Should -Be $true
Test-Path "TestDrive:/exported/wslps_raw.tar.gz" -PathType Leaf | Should -Be $true
"TestDrive:/exported/wslps_test.tar.gz" | Should -Not -Exist
# Export non-existant
{ Export-WslDistribution "wslps_bogus" "TestDrive:/exported" } | Should -Throw "There is no distribution with the name 'wslps_bogus'."
Remove-WslDistribution "wslps_test2","wslps_raw"
# Import multiple distributions using pipeline
$distros = Get-ChildItem "TestDrive:/exported" | Import-WslDistribution -Destination "TestDrive:/wsl" -Version 2
$distros | Should -HaveCount 2
Test-Distro $distros[0] "wslps_raw" 2 "Stopped"
Test-Distro $distros[1] "wslps_test2" 2 "Stopped"
# Use the default version
$distro = Import-WslDistribution $testDistroFile -Name wslps_default -Destination "TestDrive:/wsl"
$version = Get-WslVersion
Test-Distro $distro "wslps_default" $version.DefaultDistroVersion "Stopped"
{ Remove-WslDistribution "wslps_default" } | Should -Not -Throw
}
It "Can import and export VHDs" -Skip:($wslVersion -lt ([Version]::new(0, 58))) {
try {
Stop-Wsl # Otherwise export may fail due to files in use.
$exported = Export-WslDistribution "wslps_test2" "TestDrive:/exported" -Format "Vhd"
$exported.FullName | Should -Be "$TestDrive\exported\wslps_test2.vhdx"
"TestDrive:/exported/wslps_test2.vhdx" | Should -Exist
$vhdSize = (Get-Item (Get-WslDistribution "wslps_test2").VhdPath).Length
$exported.Length | Should -Be $vhdSize
# Auto export as VHD based on extension
$exported = Export-WslDistribution "wslps_test2" "TestDrive:/exported/test.vhdx"
$exported.FullName | Should -Be "$TestDrive\exported\test.vhdx"
$exported.Length | Should -Be $vhdSize
# -Format overrides extension.
$exported = Export-WslDistribution "wslps_test2" "TestDrive:/exported/test2.vhdx" -Format "Tar"
$exported.FullName | Should -Be "$TestDrive\exported\test2.vhdx"
$exported.Length | Should -Not -Be $vhdSize
# Import VHDs
$distro = Import-WslDistribution "TestDrive:/exported/wslps_test2.vhdx" "TestDrive:/wsl" "wslps_vhd1"
Test-Distro $distro "wslps_vhd1" 2 "Stopped"
$distro = Import-WslDistribution -InPlace "TestDrive:/exported/wslps_test2.vhdx" "wslps_vhd2"
Test-Distro $distro "wslps_vhd2" 2 "Stopped" "$TestDrive\exported" "wslps_test2.vhdx"
# -Format overrides extension on import
$distro = Import-WslDistribution "TestDrive:/exported/test2.vhdx" "TestDrive:/wsl" "wslps_vhd3" -Format "tar"
Test-Distro $distro "wslps_vhd3" 2 "Stopped"
} finally {
try { Remove-WslDistribution "wslps_vhd*" } catch {}
}
}
It "Can list distributions" {
# Invoke a command to start one of the distros.
Invoke-WslCommand "echo foo" "wslps_test2" *> $null
$distros = Get-WslDistribution
$distros | Should -HaveCount 3
Test-Distro ($distros | Where-Object { $_.Name -eq "wslps_test" }) "wslps_test" 1 "Stopped" -Default
Test-Distro ($distros | Where-Object { $_.Name -eq "wslps_raw" }) "wslps_raw" 2 "Stopped"
Test-Distro ($distros | Where-Object { $_.Name -eq "wslps_test2" }) "wslps_test2" 2 "Running"
$distros = Get-WslDistribution -Default
$distros | Should -HaveCount 1
Test-Distro ($distros | Where-Object { $_.Name -eq "wslps_test" }) "wslps_test" 1 "Stopped" -Default
$distros = Get-WslDistribution -Version 2
$distros | Should -HaveCount 2
Test-Distro ($distros | Where-Object { $_.Name -eq "wslps_raw" }) "wslps_raw" 2 "Stopped"
Test-Distro ($distros | Where-Object { $_.Name -eq "wslps_test2" }) "wslps_test2" 2 "Running"
$distros = Get-WslDistribution -State Running
$distros | Should -HaveCount 1
Test-Distro ($distros | Where-Object { $_.Name -eq "wslps_test2" }) "wslps_test2" 2 "Running"
$distros = Get-WslDistribution -State Stopped -Version 2
$distros | Should -HaveCount 1
Test-Distro ($distros | Where-Object { $_.Name -eq "wslps_raw" }) "wslps_raw" 2 "Stopped"
}
It "Can list online distributions" {
# Tests that there are online distributions available
$distrosOnline = Get-WslDistributionOnline
$distrosOnline | Should -Not -BeNullOrEmpty
}
It "Has no missing Name or FriendlyName values" {
# Tests that there are no null or empty values in either Name or FriendlyName
Get-WslDistributionOnline | ForEach-Object {
$_.Name | Should -Not -BeNullOrEmpty
$_.FriendlyName | Should -Not -BeNullOrEmpty
}
}
It "Supports WSL_UTF8" -Skip:($wslVersion -lt ([Version]::new(0, 64))) {
$env:WSL_UTF8 = "1"
try {
$distros = Get-WslDistribution
$distros | Should -HaveCount 3
Test-Distro ($distros | Where-Object { $_.Name -eq "wslps_test" }) "wslps_test" 1 "Stopped" -Default
Test-Distro ($distros | Where-Object { $_.Name -eq "wslps_raw" }) "wslps_raw" 2 "Stopped"
Test-Distro ($distros | Where-Object { $_.Name -eq "wslps_test2" }) "wslps_test2" 2 "Running"
} finally {
Remove-Item env:WSL_UTF8
}
}
It "Can stop distributions" {
# Invoke a command to start the distros.
Invoke-WslCommand "echo foo" "wslps_*" *> $null
(Get-WslDistribution "wslps_test").State | Should -Be "Running"
(Get-WslDistribution "wslps_test2").State | Should -Be "Running"
(Get-WslDistribution "wslps_raw").State | Should -Be "Running"
# Wildcards
Stop-WslDistribution "wslps_test*"
(Get-WslDistribution "wslps_test").State | Should -Be "Stopped"
(Get-WslDistribution "wslps_test2").State | Should -Be "Stopped"
(Get-WslDistribution "wslps_raw").State | Should -Be "Running"
# Exact name and passthru
$distro = Stop-WslDistribution "wslps_raw" -Passthru
Test-Distro $distro "wslps_raw" 2 "Stopped"
# Pipeline input
Invoke-WslCommand "echo foo" "wslps_*" *> $null
(Get-WslDistribution "wslps_test").State | Should -Be "Running"
(Get-WslDistribution "wslps_test2").State | Should -Be "Running"
(Get-WslDistribution "wslps_raw").State | Should -Be "Running"
Get-WslDistribution -Version 2 | Stop-WslDistribution
(Get-WslDistribution "wslps_test").State | Should -Be "Running"
(Get-WslDistribution "wslps_test2").State | Should -Be "Stopped"
(Get-WslDistribution "wslps_raw").State | Should -Be "Stopped"
# Non-existent
{ Stop-WslDistribution "wslps_bogus" } | Should -Throw "There is no distribution with the name 'wslps_bogus'."
}
It "Can run commands" {
# Default distro
# Redirect stderr to avoid printing warnings if the current directory can't be translated.
Invoke-WslCommand "whoami" 2> $null | Should -Be "root"
$uncPrefix = "wsl.localhost"
# Specific distro
$output = Invoke-WslCommand "cd; wslpath -w ." "wslps_raw" 2> $null
if ($output -eq "\\wsl$\wslps_raw\root") {
# Older WSL versions use this as the prefix.
$uncPrefix = "wsl$"
} else {
$output | Should -Be "\\wsl.localhost\wslps_raw\root"
}
# Wildcards
$output = Invoke-WslCommand "cd; wslpath -w ." "wslps_test*" 2> $null
$output | Should -HaveCount 2
$output | Should -Contain "\\$uncPrefix\wslps_test\root"
$output | Should -Contain "\\$uncPrefix\wslps_test2\root"
# Pipeline input
$output = Get-WslDistribution -Version 2 | Invoke-WslCommand "cd; wslpath -w ." 2> $null
$output | Should -HaveCount 2
$output | Should -Contain "\\$uncPrefix\wslps_raw\root"
$output | Should -Contain "\\$uncPrefix\wslps_test2\root"
# Set starting directory
Invoke-WslCommand "pwd" -WorkingDirectory "~" 2> $null | Should -Be "/root"
if ((Get-WslVersion).Wsl -gt [Version]::new(0, 56, 1)) {
Invoke-WslCommand "mkdir ~/foo" 2> $null
Invoke-WslCommand "pwd" -WorkingDirectory "~/foo" 2> $null | Should -Be "/root/foo"
}
Invoke-WslCommand "pwd" -WorkingDirectory "/etc" 2> $null | Should -Be "/etc"
Invoke-WslCommand "pwd" -WorkingDirectory "C:\Windows" 2> $null | Should -Be "/mnt/c/windows"
Invoke-WslCommand "pwd" -WorkingDirectory "C:\" 2> $null | Should -Be "/mnt/c"
$testPath = Invoke-WslCommand "wslpath '$TestDrive\wsl'" 2> $null
Invoke-WslCommand "pwd" -WorkingDirectory "TestDrive:/wsl" 2> $null | Should -Be $testPath
# Raw command
Invoke-WslCommand -RawCommand -- echo foo`; whoami | Should -Be "foo","root"
# Shell type (only tests if the argument is accepted; testing it if had any effect is not
# trivial).
if ((Get-WslVersion).Wsl -gt [Version]::new(0, 61, 4)) {
Invoke-WslCommand "echo foo" -ShellType none 2> $null | Should -Be "foo"
Invoke-WslCommand "echo foo" -ShellType login 2> $null | Should -Be "foo"
Invoke-WslCommand -ShellType Standard -RawCommand -- echo foo 2> $null | Should -Be "foo"
}
# System distribution
if ((Get-WslVersion).Wsl -gt [Version]::new(0, 47, 1)) {
Invoke-WslCommand "whoami" "wslps_test2" -System 2> $null | Should -Be "wslg"
Invoke-WslCommand -DistributionName "wslps_test2" -System -RawCommand whoami 2> $null | Should -Be "wslg"
}
# Non-existent
{ Invoke-WslCommand "whoami" "wslps_bogus" } | Should -Throw "There is no distribution with the name 'wslps_bogus'."
}
It "Can set distribution properties" {
# Specific distro
$distro = Set-WslDistribution "wslps_test" -Version 2 -Passthru
Test-Distro $distro "wslps_test" 2 "Stopped" -Default
# Wildcards
$distros = Set-WslDistribution "wslps_test*" -Version 1 -Passthru
Test-Distro $distros[0] "wslps_test" 1 "Stopped" -Default
Test-Distro $distros[1] "wslps_test2" 1 "Stopped"
# Pipeline input
Get-WslDistribution -Version 1 | Set-WslDistribution -Version 2
Get-WslDistribution -Version 2 | Should -HaveCount 3
# Change default
Set-WslDistribution "wslps_test2" -Default
(Get-WslDistribution -Default).Name | Should -Be "wslps_test2"
# Non-existent
{ Set-WslDistribution "wslps_bogus" -Version 2 } | Should -Throw "There is no distribution with the name 'wslps_bogus'."
}
It "Can return version information" {
$version = Get-WslVersion
if ($version.Wsl) {
$packageVersion = (Get-AppxPackage -Name "MicrosoftCorporationII.WindowsSubsystemforLinux").Version
$version.Wsl | Should -Be $packageVersion
$kernelVersion = Invoke-WslCommand "uname -r" 2> $null
$index = $kernelVersion.IndexOf("-")
if ($index -ge 0) {
$kernelVersion = $kernelVersion.Substring(0, $index)
}
$kernelVersion = [Version]::Parse($kernelVersion)
$version.Kernel | Should -Be $kernelVersion
$version.WslG | Should -BeGreaterThan ([Version]::new())
$version.Msrdc | Should -BeGreaterThan ([Version]::new())
$version.Direct3D | Should -BeGreaterThan ([Version]::new())
$version.DXCore | Should -BeGreaterThan ([Version]::new())
} else {
$version.Wsl | Should -BeNullOrEmpty
$version.Kernel | Should -BeNullOrEmpty
$version.WslG | Should -BeNullOrEmpty
$version.Msrdc | Should -BeNullOrEmpty
$version.Direct3D | Should -BeNullOrEmpty
$version.DXCore | Should -BeNullOrEmpty
}
# Environment.OSVersion always has 0 as the revision so don't compare it.
$version.Windows.Major | Should -Be ([Environment]::OSVersion.Version.Major)
$version.Windows.Minor | Should -Be ([Environment]::OSVersion.Version.Minor)
$version.Windows.Build | Should -Be ([Environment]::OSVersion.Version.Build)
$version.DefaultDistroVersion -eq 1 -or $version.DefaultDistroVersion -eq 2 | Should -BeTrue
}
It "Can stop WSL" {
# Invoke a command to start the distros.
Invoke-WslCommand "echo foo" "wslps_*" *> $null
Stop-WSL
Get-WslDistribution -State "Stopped" | Should -HaveCount 3
# Can't really test if the utility VM was stopped.
}
It "Can remove distributions" {
# Remove explicit name was tested in import/export test.
# Wildcards
Remove-WslDistribution "wslps_test*"
$distros = Get-WslDistribution
$distros | Should -HaveCount 1
$distros[0].Name | Should -Be "wslps_raw"
# Pipeline input
Get-WslDistribution | Remove-WslDistribution
Get-WslDistribution | Should -BeNullOrEmpty
# Non-existent
{ Remove-WslDistribution "wslps_bogus" } | Should -Throw "There is no distribution with the name 'wslps_bogus'."
}
}