-
-
Notifications
You must be signed in to change notification settings - Fork 120
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
Add CI Tests #161
Merged
Merged
Add CI Tests #161
Changes from all commits
Commits
Show all changes
35 commits
Select commit
Hold shift + click to select a range
136cc14
use systemd-run instead of machinectl
nzbr 8cfef03
fix systemd user sessions by launching through runuser
nzbr aff4bf6
skip mounting binfmt_misc if the kernel lacks support for it
nzbr 14c3025
link syschdemd/installer to static location
nzbr 6ab9a9e
make check happy
nzbr abe56b5
Merge branch 'main' into tests
nzbr c376de7
test tests
nzbr 1ff0201
add a simple installer test
nzbr fc1fb4f
add second test for exit codes
nzbr 40493e5
Merge branch 'main' into exit-code
nzbr 3bda6c1
Merge branch 'exit-code' into tests
nzbr b0aed50
make tests responsible for checking exit code
nzbr 1bc49d8
fix: add which to path
nzbr 54a2f75
Merge branch 'exit-code' into tests
nzbr 91d0353
overhaul actions workflow
nzbr 3bcd5ba
move checks out of flake.nix
nzbr 2c8f69b
use GUID for image name
nzbr c7ec43a
Split flake checks into matrix job
nzbr 101423b
Check for side-effects
nzbr d75cc32
reformat powershell scripts
nzbr 1a5804b
extend basic test
nzbr da98e33
use pester for tests
nzbr a20184d
"fix" (purposefully) failing test
nzbr dc51fdd
Merge remote-tracking branch 'nix-community/main' into tests
nzbr df1bde2
rename lib -> init
nzbr da0b3e5
Add test for systemd --user
nzbr 55f218d
Add test for docker-native
nzbr c6cd480
move release to separate workflow
nzbr 3bb60fd
change downstream workflow path
nzbr 1f5db11
switch to a class
nzbr f9025b2
Test running with different user shells
nzbr 31d07f2
Add lib implementation for Windows
nzbr 4b244b1
Add documentation for the tests
nzbr d36ef47
readme: docker permissions
nzbr 13a9adb
remove empty files
nzbr File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
root = true | ||
|
||
[*] | ||
indent_style = space | ||
indent_size = 2 | ||
charset = utf-8 | ||
trim_trailing_whitespace = true | ||
insert_final_newline = true | ||
|
||
[*.md] | ||
trim_trailing_whitespace = false |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,78 +1,103 @@ | ||
name: 'Build NixOS WSL tarball' | ||
name: 'CI' | ||
|
||
on: [push, pull_request, release] | ||
on: | ||
push: {} | ||
pull_request: {} | ||
workflow_call: {} | ||
|
||
jobs: | ||
build: | ||
find-tests: | ||
name: Find Tests 🔍 | ||
runs-on: ubuntu-latest | ||
outputs: | ||
tests: ${{ steps.tests.outputs.tests }} | ||
checks: ${{ steps.checks.outputs.checks }} | ||
steps: | ||
- name: Checkout | ||
uses: actions/checkout@v2 | ||
uses: actions/checkout@v3 | ||
with: | ||
# Nix Flakes doesn't work on shallow clones | ||
fetch-depth: 0 | ||
|
||
- name: Install nix | ||
uses: cachix/install-nix-action@v12 | ||
with: | ||
install_url: https://github.com/numtide/nix-flakes-installer/releases/download/nix-2.4pre20201221_9fab14a/install | ||
# Configure Nix to enable flakes | ||
extra_nix_config: | | ||
experimental-features = nix-command flakes | ||
- name: Install nix ❄️ | ||
uses: cachix/install-nix-action@v18 | ||
|
||
- name: Run checks | ||
- name: Find tests 🔍 | ||
id: tests | ||
run: | | ||
nix flake check | ||
find tests -name '*.Tests.ps1' -print0 | perl -pe 's|(.*?)\x0|"\1",|g;s|,$||;s|(.*)|tests=[\1]|' >> $GITHUB_OUTPUT | ||
|
||
- name: Build tarball | ||
- name: Find checks 🔍 | ||
id: checks | ||
run: | | ||
nix build '.#nixosConfigurations.mysystem.config.system.build.tarball' | ||
nix-instantiate --json --eval --strict -E 'with builtins; attrNames (getFlake (toString ./.)).checks.${currentSystem}' | perl -pe 's|(.*)|checks=\1|' >>$GITHUB_OUTPUT | ||
|
||
- name: Upload tarball | ||
uses: actions/upload-artifact@v2 | ||
build: | ||
name: Build 🛠️ | ||
runs-on: ubuntu-latest | ||
steps: | ||
- name: Checkout | ||
uses: actions/checkout@v3 | ||
with: | ||
name: rootfs | ||
path: result/tarball/nixos-wsl-x86_64-linux.tar.gz | ||
fetch-depth: 0 | ||
|
||
- name: Build installer | ||
- name: Install nix ❄️ | ||
uses: cachix/install-nix-action@v18 | ||
|
||
- name: Build installer 🛠️ | ||
run: | | ||
nix build '.#nixosConfigurations.mysystem.config.system.build.installer' | ||
|
||
- name: Upload installer | ||
uses: actions/upload-artifact@v2 | ||
- name: Upload installer 📤 | ||
uses: actions/upload-artifact@v3 | ||
with: | ||
name: installer | ||
path: result/tarball/nixos-wsl-installer.tar.gz | ||
|
||
release: | ||
if: startsWith(github.ref, 'refs/tags/') | ||
needs: build | ||
checks: | ||
name: Flake Check 📋 | ||
needs: | ||
- find-tests | ||
strategy: | ||
fail-fast: false | ||
matrix: | ||
check: ${{ fromJSON(needs.find-tests.outputs.checks) }} | ||
runs-on: ubuntu-latest | ||
steps: | ||
- name: Checkout | ||
uses: actions/checkout@v2 | ||
|
||
- uses: actions/download-artifact@v2 | ||
uses: actions/checkout@v3 | ||
with: | ||
name: rootfs | ||
fetch-depth: 0 | ||
|
||
- uses: actions/download-artifact@v2 | ||
with: | ||
name: installer | ||
- name: Install nix ❄️ | ||
uses: cachix/install-nix-action@v18 | ||
|
||
- name: Generate checksums | ||
- name: Run check 📋 | ||
run: | | ||
for x in *.tar.gz; do | ||
sha256sum $x > ${x}.sha256 | ||
done | ||
nix build -L --impure --expr "with builtins; (getFlake (toString ./.)).checks.\${currentSystem}.${{ matrix.check }}" | ||
|
||
- name: Attach to release | ||
uses: softprops/action-gh-release@v1 | ||
tests: | ||
name: Test 🧪 | ||
needs: | ||
- find-tests | ||
- build | ||
strategy: | ||
fail-fast: false | ||
matrix: | ||
test: ${{ fromJSON(needs.find-tests.outputs.tests) }} | ||
os: | ||
- ubuntu-latest | ||
# - windows-latest # doesn't work due to lack of nested virtualization on the runners, hopefully this will work one day | ||
runs-on: ${{ matrix.os }} | ||
steps: | ||
- name: Checkout | ||
uses: actions/checkout@v3 | ||
|
||
- name: Download installer 📥 | ||
uses: actions/download-artifact@v3 | ||
with: | ||
files: | | ||
nixos-wsl-x86_64-linux.tar.gz | ||
nixos-wsl-x86_64-linux.tar.gz.sha256 | ||
nixos-wsl-installer.tar.gz | ||
nixos-wsl-installer.tar.gz.sha256 | ||
env: | ||
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} | ||
name: installer | ||
|
||
- name: Execute test 🧪 | ||
shell: pwsh | ||
run: | | ||
Invoke-Pester -Output Detailed ${{ matrix.test }} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
name: 'Release' | ||
|
||
on: | ||
push: | ||
tags: [] | ||
|
||
jobs: | ||
build: | ||
name: Build 🛠️ | ||
uses: nix-community/nixos-wsl/.github/workflows/main.yml@main | ||
release: | ||
needs: | ||
- build | ||
name: Create Release 📢 | ||
runs-on: ubuntu-latest | ||
steps: | ||
- name: Download installer 📥 | ||
uses: actions/download-artifact@v3 | ||
with: | ||
name: installer | ||
|
||
- name: Generate checksums 🔑 | ||
run: | | ||
for x in *.tar.gz; do | ||
sha256sum $x > ${x}.sha256 | ||
done | ||
|
||
- name: Attach to release 📦 | ||
uses: softprops/action-gh-release@v1 | ||
with: | ||
files: | | ||
nixos-wsl-installer.tar.gz | ||
nixos-wsl-installer.tar.gz.sha256 | ||
env: | ||
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
{ | ||
"version": "0.2.0", | ||
"configurations": [ | ||
{ | ||
"name": "PowerShell: Invoke Pester", | ||
"type": "PowerShell", | ||
"request": "launch", | ||
"script": "Invoke-Pester -Output Detailed", | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
{ runCommand | ||
, nixpkgs-fmt | ||
, ... | ||
}: | ||
runCommand "check-nixpkgs-fmt" { nativeBuildInputs = [ nixpkgs-fmt ]; } '' | ||
nixpkgs-fmt --check ${./..} | ||
touch $out | ||
'' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
{ runCommand | ||
, shfmt | ||
, ... | ||
}: | ||
runCommand "check-shfmt" { nativeBuildInputs = [ shfmt ]; } '' | ||
shfmt -i 2 -d ${./../scripts}/*.sh | ||
touch $out | ||
'' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
# Test that including the WSL module in a config does not change anything without enabling it | ||
|
||
{ inputs | ||
, system | ||
, emptyFile | ||
, ... | ||
}: | ||
let | ||
configModule = { config, options, ... }: { | ||
fileSystems."/" = { | ||
device = "/dev/sda1"; | ||
fsType = "ext4"; | ||
}; | ||
boot.loader.grub.device = "nodev"; | ||
system.stateVersion = options.system.stateVersion.default; | ||
}; | ||
|
||
cleanConfig = inputs.nixpkgs.lib.nixosSystem { | ||
inherit system; | ||
modules = [ | ||
configModule | ||
]; | ||
}; | ||
wslModuleConfig = inputs.nixpkgs.lib.nixosSystem { | ||
inherit system; | ||
modules = [ | ||
configModule | ||
inputs.self.nixosModules.wsl | ||
]; | ||
}; | ||
in | ||
# Check that both configs evaluate to the same derivation | ||
if cleanConfig.config.system.build.toplevel.outPath == wslModuleConfig.config.system.build.toplevel.outPath | ||
then emptyFile | ||
else throw "The WSL module introduces a side-effect even when not enabled!" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,8 @@ | ||
#!/usr/bin/env bash | ||
set -euxo pipefail | ||
|
||
mount -t binfmt_misc binfmt_misc /proc/sys/fs/binfmt_misc | ||
if grep -q binfmt_misc /proc/filesystems; then | ||
mount -t binfmt_misc binfmt_misc /proc/sys/fs/binfmt_misc | ||
fi | ||
|
||
exec systemd |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
# Tests | ||
|
||
This directory contains tests that are executed against a built installer tarball. | ||
The test are written using the [Pester](https://pester.dev/) testing framework | ||
|
||
## Execute Tests | ||
|
||
The tests can be executed on both Windows or Linux. | ||
|
||
### Windows | ||
|
||
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 `nixos-wsl-installer.tar.gz` to be present in the current working directory or in `./result/tarball`. Refer to the top-level readme on how to build it. | ||
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. | ||
|
||
Put this snippet at the start of your test file to gain access to the following libray functions: | ||
(This assumes that your test is at the root of the `tests` directory) | ||
|
||
```powershell | ||
BeforeAll { | ||
. $PSScriptRoot/lib/lib.ps1 | ||
} | ||
``` | ||
|
||
- `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 | ||
- 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. | ||
- `InstallConfig($path)`: Installs a nix-file as the systems `configuration.nix`. The default configuration is moved to `base.nix`, so that it can be imported by the new config | ||
- `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. |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe use lib.makeBinPath here