Skip to content

Commit 276c53a

Browse files
committed
test: remove docker tests from lib
1 parent 0f11150 commit 276c53a

11 files changed

+41
-136
lines changed

tests/README.md

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,27 @@
11
# Tests
22

3-
This directory contains tests that are executed against a built NixOS-WSL "legacy" tarball.
3+
This directory contains tests that are executed against a built NixOS-WSL tarball.
44
The tests are written using the [Pester](https://pester.dev/) testing framework.
55

66
## Execute Tests
77

8-
The tests can be executed on both Windows or Linux.
9-
10-
### Windows
8+
The tests can only be executed on Windows. Support for running the tests in an emulated WSL environment through docker has been removed.
119

1210
Make sure that you are able to run a distro in WSL2 before trying to run the tests.
1311
Please note that the tests are not compatible with Windows PowerShell, but require the new [PowerShell Core](https://apps.microsoft.com/store/detail/powershell/9MZ1SNWT0N5D?hl=en-us&gl=us).
1412

15-
### Linux
16-
17-
Running the tests requires Docker and PowerShell to be installed on your system. Make sure that the user you are running the tests as has permissions to run docker containers and that it is possible to access the internet from inside docker containers.
18-
1913
### Running the Tests
2014

2115
If you haven't already, [install Pester](https://pester.dev/docs/introduction/installation/).
22-
The tests require a "legacy" `nixos-wsl.tar.gz` to be present in the current working directory, which can be built with
23-
`sudo nix run .#nixosConfigurations.legacy.config.system.build.tarballBuilder -- nixos-wsl.tar.gz`.
16+
The tests require a "modern" `nixos-wsl.tar.gz` to be present in the current working directory, which can be built with
17+
`sudo nix run .#nixosConfigurations.modern.config.system.build.tarballBuilder -- nixos-wsl.tar.gz`.
2418

2519
Once everything is in place, run the test by running the following in PowerShell at the root of this repo:
2620

2721
```powershell
2822
Invoke-Pester -Output Detailed ./tests
2923
```
3024

31-
3225
## Writing Test
3326

3427
Please refer to [the Pester documentation](https://pester.dev/docs/quick-start) on how to write new tests.
@@ -42,10 +35,10 @@ BeforeAll {
4235
}
4336
```
4437

45-
- `Install-Distro`: Creates a new NixOS-WSL instance, automatically selecting the appropriate runtime (WSL or Docker) for the host OS. Returns a new `Distro` object
38+
- `[Distro]::new()`: Creates a new NixOS-WSL instance.
4639
- A Distro object has the following methods:
47-
- `Launch($command)`: Runs the specified command inside the container. Returns the command output
48-
- `GetPath($path)`: Returns the path inside the container, that points to the specified file on the host.
40+
- `Launch($command)`: Runs the specified command inside the distro. Returns the command output
41+
- `GetPath($path)`: Returns the path inside the distro, that points to the specified file on the host.
4942
- `InstallConfig($path)`: Installs a nix-file as the systems `configuration.nix`.
50-
- `Shutdown()`: End all processes running in the container
51-
- `Uninstall()`: Stop and then delete the container from the system. This should be called in an AfterEach or AfterAll block, so that the test does not leave it on the system.
43+
- `Shutdown()`: End all processes running in the distro
44+
- `Uninstall()`: Stop and then delete the distro from the system. This should be called in an AfterEach or AfterAll block, so that the test does not leave it on the system.

tests/basic-functionality.Tests.ps1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ BeforeAll {
44

55
Describe "Basic Functionality" {
66
BeforeAll {
7-
$distro = Install-Distro
7+
$distro = [Distro]::new()
88
}
99

1010
It "is possible to run a command in the container" {

tests/docker/docker.Tests.ps1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ BeforeAll {
44

55
Describe "Docker (native)" {
66
BeforeAll {
7-
$distro = Install-Distro
7+
$distro = [Distro]::new()
88
try {
99
$distro.InstallConfig("$PSScriptRoot/docker-native.nix")
1010
}

tests/exit-codes.Tests.ps1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ BeforeAll {
44

55
Describe "Exit Codes" {
66
BeforeAll {
7-
$distro = Install-Distro
7+
$distro = [Distro]::new()
88
}
99

1010
It "should return 0 when running true" {

tests/lib/Dockerfile

Lines changed: 0 additions & 2 deletions
This file was deleted.

tests/lib/lib.ps1

Lines changed: 24 additions & 110 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
if ($PSVersionTable.PSEdition -ne 'Core') {
22
throw "The tests are not compatible with Windows PowerShell, please use PowerShell Core instead"
33
}
4+
if ($IsWindows -eq $false) {
5+
throw "Support for tests in an emulated WSL environment on Linux has been removed"
6+
}
47

58
function Remove-Escapes {
69
param(
@@ -15,13 +18,31 @@ function Remove-Escapes {
1518
# Implementation-independent base class
1619
class Distro {
1720
[string]$id
21+
[string]$tempdir
22+
23+
Distro() {
24+
$tarball = $this.FindTarball()
25+
26+
$this.id = $(New-Guid).ToString()
27+
$this.tempdir = Join-Path $([System.IO.Path]::GetTempPath()) $this.id
28+
New-Item -ItemType Directory $this.tempdir
1829

19-
[string]Launch() {
20-
throw "Not implemented"
30+
& wsl.exe --import $this.id $this.tempdir $tarball --version 2 | Write-Host
31+
if ($LASTEXITCODE -ne 0) {
32+
throw "Failed to import distro"
33+
}
34+
& wsl.exe --list -q | Should -Contain $this.id
35+
}
36+
37+
[Array]Launch([string]$command) {
38+
Write-Host "> $command"
39+
$result = @()
40+
Invoke-Expression "wsl.exe -d $($this.id) --% $command" | Tee-Object -Variable result | Write-Host
41+
return $result | Remove-Escapes
2142
}
2243

2344
[string]GetPath([string]$path) {
24-
throw "Not implemented"
45+
return $this.Launch("wslpath $($path -replace "\\", "/")") | Select-Object -Last 1
2546
}
2647

2748
[string]FindTarball() {
@@ -48,101 +69,6 @@ class Distro {
4869
Write-Host "Config installed successfully"
4970
}
5071

51-
[void]Shutdown() {
52-
throw "Not implemented"
53-
}
54-
55-
[void]Uninstall() {
56-
throw "Not implemented"
57-
}
58-
}
59-
60-
# Emulates a WSL distro using a Docker container (for Linux hosts)
61-
class DockerDistro : Distro {
62-
static [string]$hostMount = "/mnt/c"
63-
static [string]$imageName = "local:nixos-wsl"
64-
65-
static [bool]$imageCreated = $false
66-
67-
DockerDistro() {
68-
$tarball = $this.FindTarball()
69-
70-
if (!([DockerDistro]::imageCreated)) {
71-
# Build docker image from the tarball
72-
$tmpdir = $(mktemp -d)
73-
Copy-Item $PSScriptRoot/Dockerfile $tmpdir
74-
Copy-Item $tarball $tmpdir
75-
docker build -t $([DockerDistro]::imageName) $tmpdir | Write-Host
76-
Remove-Item $tmpdir -Recurse -Force
77-
78-
[DockerDistro]::imageCreated = $true
79-
}
80-
81-
$this.id = $(New-Guid).ToString()
82-
83-
docker run -di --privileged --volume "/:$([DockerDistro]::hostMount)" --name $this.id $([DockerDistro]::imageName) "/bin/sh" | Out-Null
84-
if ($LASTEXITCODE -ne 0) {
85-
throw "Failed to launch container"
86-
}
87-
88-
$this.Launch("sudo nix-channel --update")
89-
}
90-
91-
[Array]Launch([string]$command) {
92-
Write-Host "> $command"
93-
$result = @()
94-
docker exec -t $this.id /bin/syschdemd -c $command | Tee-Object -Variable result | Write-Host
95-
return $result | Remove-Escapes
96-
}
97-
98-
[string]GetPath([string]$path) {
99-
return [DockerDistro]::hostMount + $(readlink -f $path)
100-
}
101-
102-
[void]Shutdown() {
103-
docker restart $this.id # Restart instead of stop so that exec can still be used
104-
if ($LASTEXITCODE -ne 0) {
105-
throw "Failed to stop container"
106-
}
107-
}
108-
109-
[void]Uninstall() {
110-
docker rm -f $this.id
111-
if ($LASTEXITCODE -ne 0) {
112-
throw "Failed to remove container"
113-
}
114-
}
115-
}
116-
117-
# A real WSL distro (for Windows hosts)
118-
class WslDistro : Distro {
119-
[string]$tempdir
120-
121-
WslDistro() {
122-
$tarball = $this.FindTarball()
123-
124-
$this.id = $(New-Guid).ToString()
125-
$this.tempdir = Join-Path $([System.IO.Path]::GetTempPath()) $this.id
126-
New-Item -ItemType Directory $this.tempdir
127-
128-
& wsl.exe --import $this.id $this.tempdir $tarball --version 2 | Write-Host
129-
if ($LASTEXITCODE -ne 0) {
130-
throw "Failed to import distro"
131-
}
132-
& wsl.exe --list -q | Should -Contain $this.id
133-
}
134-
135-
[Array]Launch([string]$command) {
136-
Write-Host "> $command"
137-
$result = @()
138-
Invoke-Expression "wsl.exe -d $($this.id) --% $command" | Tee-Object -Variable result | Write-Host
139-
return $result | Remove-Escapes
140-
}
141-
142-
[string]GetPath([string]$path) {
143-
return $this.Launch("wslpath $($path -replace "\\", "/")") | Select-Object -Last 1
144-
}
145-
14672
[void]Shutdown() {
14773
& wsl.exe -t $this.id
14874
if ($LASTEXITCODE -ne 0) {
@@ -158,15 +84,3 @@ class WslDistro : Distro {
15884
Remove-Item $this.tempdir -Recurse -Force
15985
}
16086
}
161-
162-
# Auto-select the implementation to use
163-
function Install-Distro() {
164-
if ($IsWindows) {
165-
Write-Host "Detected Windows host, using WSL2"
166-
return [WslDistro]::new()
167-
}
168-
else {
169-
Write-Host "Detected Linux host, using Docker"
170-
return [DockerDistro]::new()
171-
}
172-
}

tests/login-shell/login-shell.Tests.ps1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ BeforeAll {
44

55
Describe "Login Shell" {
66
BeforeAll {
7-
$distro = Install-Distro
7+
$distro = [Distro]::new()
88
}
99

1010
if ($IsWindows) {

tests/shells.Tests.ps1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ BeforeAll {
44

55
Describe "Shells" {
66
BeforeAll {
7-
$distro = Install-Distro
7+
$distro = [Distro]::new()
88

99
function Add-ShellTest([string]$package, [string]$executable, [string]$command) {
1010
$temp = New-TemporaryFile

tests/systemd-services.Tests.ps1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ BeforeAll {
44

55
Describe "Systemd Services" {
66
BeforeAll {
7-
$distro = Install-Distro
7+
$distro = [Distro]::new()
88
}
99

1010
It "should boot" {

tests/systemd-user.Tests.ps1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ BeforeAll {
44

55
Describe "Systemd User Daemon" {
66
BeforeAll {
7-
$distro = Install-Distro
7+
$distro = [Distro]::new()
88
}
99

1010
It "should be possible to connect to the user daemon" {

0 commit comments

Comments
 (0)