Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: Make tests work on Windows with native systemd #466

Merged
merged 13 commits into from
May 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .github/workflows/run_build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ jobs:
config:
- modern
- legacy
- test
steps:
- name: Checkout
uses: actions/checkout@v4
Expand Down
15 changes: 8 additions & 7 deletions .github/workflows/run_tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,7 @@ jobs:
fail-fast: false
matrix:
test: ${{ fromJSON(needs.prepare.outputs.tests) }}
os:
- ubuntu-20.04
# - windows-latest # doesn't work due to lack of nested virtualization on the runners, hopefully this will work one day
runs-on: ${{ matrix.os }}
runs-on: windows-latest
steps:
- name: Checkout
uses: actions/checkout@v4
Expand All @@ -44,9 +41,13 @@ jobs:
- name: Download Tarball 📥
uses: actions/download-artifact@v4
with:
name: tarball-test
name: tarball-modern

- name: Setup WSL 🐧
run: |
wsl --update
wsl --version

- name: Execute Test 🧪
shell: pwsh
run: |
Invoke-Pester -Output Detailed ${{ matrix.test }}
run: Invoke-Pester -Output Detailed ${{ matrix.test }}
4 changes: 3 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
{
"rust-analyzer.linkedProjects": [
"scripts/native-utils/Cargo.toml"
"utils/Cargo.toml"
],
"workbench.colorCustomizations": {
"commandCenter.border": "#e7e7e799",
"sash.hoverBorder": "#6194da",
"titleBar.activeBackground": "#3878d0",
"titleBar.inactiveBackground": "#3878d099",
"titleBar.activeForeground": "#e7e7e7",
"titleBar.inactiveForeground": "#e7e7e799"
},
"peacock.remoteColor": "#3878d0"
}
47 changes: 16 additions & 31 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,22 @@

nixosConfigurations =
let
initialConfig = { config, ... }: {
config = { legacy ? false }: { config, lib, pkgs, ... }: {
wsl.enable = true;

wsl.nativeSystemd = lib.mkIf legacy false;
programs.bash.loginShellInit = "nixos-wsl-welcome";

systemd.tmpfiles.rules =
let
channels = pkgs.runCommand "default-channels" { } ''
mkdir -p $out
ln -s ${pkgs.path} $out/nixos
ln -s ${./.} $out/nixos-wsl
'';
in
[
"L /nix/var/nix/profiles/per-user/root/channels-1-link - - - - ${channels}"
"L /nix/var/nix/profiles/per-user/root/channels - - - - channels-1-link"
];
system.stateVersion = config.system.nixos.release;
};
in
Expand All @@ -41,41 +52,15 @@
system = "x86_64-linux";
modules = [
self.nixosModules.default
initialConfig
(config { })
];
};

legacy = nixpkgs.lib.nixosSystem {
system = "x86_64-linux";
modules = [
self.nixosModules.default
initialConfig
{ wsl.nativeSystemd = false; }
];
};

test = nixpkgs.lib.nixosSystem {
system = "x86_64-linux";
modules = [
self.nixosModules.default
({ config, pkgs, ... }: {
wsl.enable = true;
wsl.nativeSystemd = false;

system.activationScripts.create-test-entrypoint.text =
let
syschdemdProxy = pkgs.writeShellScript "syschdemd-proxy" ''
shell=$(${pkgs.getent}/bin/getent passwd root | ${pkgs.coreutils}/bin/cut -d: -f7)
exec $shell $@
'';
in
''
mkdir -p /bin
ln -sfn ${syschdemdProxy} /bin/syschdemd
'';

system.stateVersion = config.system.nixos.release;
})
(config { legacy = true; })
];
};
};
Expand Down
25 changes: 9 additions & 16 deletions tests/README.md
Original file line number Diff line number Diff line change
@@ -1,34 +1,27 @@
# Tests

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

## Execute Tests

The tests can be executed on both Windows or Linux.

### Windows
The tests can only be executed on Windows. Support for running the tests in an emulated WSL environment through docker has been removed.

Make sure that you are able to run a distro in WSL2 before trying to run the tests.
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).

### Linux

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.

### Running the Tests

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

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

```powershell
Invoke-Pester -Output Detailed ./tests
```


## Writing Test

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

- `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
- `[Distro]::new()`: Creates a new NixOS-WSL instance.
- A Distro object has the following methods:
- `Launch($command)`: Runs the specified command inside the container. Returns the command output
- `GetPath($path)`: Returns the path inside the container, that points to the specified file on the host.
- `Launch($command)`: Runs the specified command inside the distro. Returns the command output
- `GetPath($path)`: Returns the path inside the distro, that points to the specified file on the host.
- `InstallConfig($path)`: Installs a nix-file as the systems `configuration.nix`.
- `Shutdown()`: End all processes running in the container
- `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.
- `Shutdown()`: End all processes running in the distro
- `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.
8 changes: 4 additions & 4 deletions tests/basic-functionality.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ BeforeAll {

Describe "Basic Functionality" {
BeforeAll {
$distro = Install-Distro
$distro = [Distro]::new()
}

It "is possible to run a command in the container" {
Expand All @@ -24,17 +24,17 @@ Describe "Basic Functionality" {
}

It "is possible to use nixos-rebuild" {
$distro.Launch("sudo nixos-rebuild switch")
$distro.Launch("sudo nixos-rebuild switch < /dev/null")
$LASTEXITCODE | Should -Be 0
}

It "is possible to run a command through nix-shell" {
$distro.Launch("nix-shell -p neofetch --command neofetch")
$distro.Launch("nix-shell -p hello --command hello") | Select-Object -Last 1 | Should -BeExactly "Hello, world!"
$LASTEXITCODE | Should -Be 0
}

It "is possible to run a command through nix run" {
$distro.Launch("nix --extra-experimental-features 'nix-command flakes' run nixpkgs#neofetch")
$distro.Launch("nix --extra-experimental-features 'nix-command flakes' run nixpkgs#hello") | Select-Object -Last 1 | Should -BeExactly "Hello, world!"
$LASTEXITCODE | Should -Be 0
}

Expand Down
7 changes: 1 addition & 6 deletions tests/docker/docker-native.nix
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,8 @@
];

wsl.enable = true;
wsl.nativeSystemd = false;

users.users.nixos.extraGroups = [ "docker" ];

virtualisation.docker = {
enable = true;
# Github Actions runners try to use aufs and fail if this is not set explicitly
daemon.settings."storage-driver" = "vfs";
};
virtualisation.docker.enable = true;
}
10 changes: 5 additions & 5 deletions tests/docker/docker.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ BeforeAll {

Describe "Docker (native)" {
BeforeAll {
$distro = Install-Distro
$distro = [Distro]::new()
try {
$distro.InstallConfig("$PSScriptRoot/docker-native.nix")
}
Expand All @@ -15,18 +15,18 @@ Describe "Docker (native)" {
}

It "should be possible to run a docker container" {
$distro.Launch("docker run --rm -it hello-world")
$distro.Launch("docker run --rm -i hello-world")
$LASTEXITCODE | Should -Be 0
}

It "should still be possible to run a docker container after a restart" {
$distro.Shutdown()
$distro.Launch("docker run --rm -it hello-world")
$distro.Launch("docker run --rm -i hello-world")
$LASTEXITCODE | Should -Be 0
}

It "should be possible to connect to the internet from a container" {
$distro.Launch("docker run --rm -it alpine wget -qO- http://www.msftconnecttest.com/connecttest.txt") | Select-Object -Last 1 | Should -BeExactly "Microsoft Connect Test"
$distro.Launch("docker run --rm -i alpine wget -qO- http://www.msftconnecttest.com/connecttest.txt") | Select-Object -Last 1 | Should -BeExactly "Microsoft Connect Test"
$LASTEXITCODE | Should -Be 0
}

Expand All @@ -37,7 +37,7 @@ Describe "Docker (native)" {
$testfilename = "testfile"
$testfile = "${testdir}/${testfilename}"
$distro.Launch("echo $teststring > $testfile")
$distro.Launch("docker run --rm -it -v ${testdir}:/mnt alpine cat /mnt/${testfilename}") | Select-Object -Last 1 | Should -BeExactly $teststring
$distro.Launch("docker run --rm -i -v ${testdir}:/mnt alpine cat /mnt/${testfilename}") | Select-Object -Last 1 | Should -BeExactly $teststring
$LASTEXITCODE | Should -Be 0
}

Expand Down
2 changes: 1 addition & 1 deletion tests/exit-codes.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ BeforeAll {

Describe "Exit Codes" {
BeforeAll {
$distro = Install-Distro
$distro = [Distro]::new()
}

It "should return 0 when running true" {
Expand Down
2 changes: 0 additions & 2 deletions tests/lib/Dockerfile

This file was deleted.

Loading
Loading