Skip to content

Commit

Permalink
Add kernel tests
Browse files Browse the repository at this point in the history
This commit adds kernel tests running on x86_64 for 5.15, 6.0, 6.2,
and 6.6 using vmtest[0]. The main goal is to ensure that the BPF
programs load fine for various kernels.

The original plan was to have a nix derivation that would do everything:
1) download the kernels 2) build vmtest 3) build lightswitch 4) generate
the vmtest configuration, and finally 5) run vmtest with said configuration.
I attempted to do this in this branch [1] but I was taking too long and
decided for this approach that gets us 80% of the value for 20% of the work.

The downsides for this approach are:

1) As the vmtest config file is not generated on the fly, the kernel
   paths have to be hardcoded. This is not as bad as it might seem
because nix guarantees these paths to be the same accross machines, but
it's still a bit kludgy;
2) There is an expectation that lightswitch in dev mode has been built
   before runnign the vmtests;

How to run the kernel tests
===========================

```
nix develop
cargo build
vmtest
```

Notes
=====

We still require qemu-guest-agen to be running in the base OS (nix or
not) which is not ideal, but a proper fix might be quite involved.

Test Plan
=========

Forced a panic to make sure the kernel tests would fail.

- [0]: https://github.com/danobi/vmtest
- [1]: https://github.com/javierhonduco/lightswitch/commits/wip-vmtests
  • Loading branch information
javierhonduco committed Apr 24, 2024
1 parent c5b8525 commit ae589c3
Show file tree
Hide file tree
Showing 6 changed files with 137 additions and 0 deletions.
File renamed without changes.
34 changes: 34 additions & 0 deletions .github/workflows/vmtests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
name: vmtests
on:
pull_request:
push:

concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true

jobs:
vmtests:
runs-on: ubuntu-22.04
permissions:
id-token: write
contents: read

steps:
- uses: actions/checkout@main
- uses: DeterminateSystems/nix-installer-action@main
- uses: DeterminateSystems/magic-nix-cache-action@main

- name: Install system dependencies
run: |
export DEBIAN_FRONTEND=noninteractive
sudo apt update && sudo apt -y install --no-install-recommends qemu-system-x86 qemu-guest-agent
- name: Set up nix dev env
run: nix develop --command echo 0

- name: Build `lightswitch`
run: nix develop --ignore-environment --command bash -c 'cargo build'

- name: Run kernel tests
run: nix develop --command 'vmtest'
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/target
flame.svg
src/bpf/*_skel.rs
.vmtest.log
5 changes: 5 additions & 0 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,11 @@
# snapshot testing plugin binary
cargo-insta
# ocamlPackages.magic-trace
(import ./vm.nix { inherit pkgs; }).vmtest
(import ./vm.nix { inherit pkgs; }).kernel_5_15
(import ./vm.nix { inherit pkgs; }).kernel_6_0
(import ./vm.nix { inherit pkgs; }).kernel_6_2
(import ./vm.nix { inherit pkgs; }).kernel_6_6
];

LIBCLANG_PATH = lib.makeLibraryPath [ llvmPackages_16.libclang ];
Expand Down
78 changes: 78 additions & 0 deletions vm.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
{ pkgs }:
{
kernel_5_15 = pkgs.stdenv.mkDerivation {
name = "download-kernel-5.15";
src = pkgs.fetchurl {
url = "https://github.com/danobi/vmtest/releases/download/test_assets/bzImage-v5.15-fedora38";
hash = "sha256-nq8W72vuNKCgO1OS6aJtAfg7AjHavRZ7WAkP7X6V610=";
};
dontUnpack = true;
installPhase = ''
mkdir -p $out
cp -r $src $out/bzImage
'';
};

kernel_6_0 = pkgs.stdenv.mkDerivation {
name = "download-kernel-6.0";
src = pkgs.fetchurl {
url = "https://github.com/danobi/vmtest/releases/download/test_assets/bzImage-v6.0-fedora38";
hash = "sha256-ZBBQ0yVUn+Isd2b+a32oMEbNo8T1v46P3rEtZ+1j9Ic=";
};
dontUnpack = true;
installPhase = ''
mkdir -p $out
cp -r $src $out/bzImage
'';
};

kernel_6_2 = pkgs.stdenv.mkDerivation {
name = "download-kernel-6.2";
src = pkgs.fetchurl {
url = "https://github.com/danobi/vmtest/releases/download/test_assets/bzImage-v6.2-fedora38";
hash = "sha256-YO2HEIWTuEEJts9JrW3V7UVR7t4J3+8On+tjdELa2m8=";
};
dontUnpack = true;
installPhase = ''
mkdir -p $out
cp -r $src $out/bzImage
'';
};

kernel_6_6 = pkgs.stdenv.mkDerivation {
name = "download-kernel-6.6";
src = pkgs.fetchurl {
url = "https://github.com/danobi/vmtest/releases/download/test_assets/bzImage-v6.6-fedora38";
hash = "sha256-6Fu16SPBITP0sI3lapkckZna6GKBn2hID038itt82jA=";
};
dontUnpack = true;
installPhase = ''
mkdir -p $out
cp -r $src $out/bzImage
'';
};

vmtest = pkgs.rustPlatform.buildRustPackage {
name = "vmtest";
src = pkgs.fetchFromGitHub {
owner = "danobi";
repo = "vmtest";
rev = "51f11bf301fea054342996802a16ed21fb5054f4";
sha256 = "sha256-qtTq0dnDHi1ITfQzKrXz+1dRMymAFBivWpjXntD09+A=";
};
cargoHash = "sha256-SHjjCWz4FVVk1cczkMltRVEB3GK8jz2tVABNSlSZiUc=";
# nativeCheckInputs = [ pkgs.qemu ];

# There are some errors trying to access `/build/source/tests/*`.
doCheck = false;

meta = with pkgs.lib; {
description = "Helps run tests in virtual machines";
homepage = "https://github.com/danobi/vmtest/";
license = licenses.asl20;
mainProgram = "";
maintainers = with maintainers; [ ];
platforms = platforms.linux;
};
};
}
19 changes: 19 additions & 0 deletions vmtest.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
[[target]]
name = "Fedora 5.15"
kernel = "/nix/store/cg9wq48zz2dbvayh7sigr5smaf4dwcxp-download-kernel-5.15/bzImage"
command = "/mnt/vmtest/target/debug/lightswitch --duration 0"

[[target]]
name = "Fedora 6.0"
kernel = "/nix/store/p8xc9gqdkpfjpmcb9ja2gx6g6bd9cby7-download-kernel-6.0/bzImage"
command = "/mnt/vmtest/target/debug/lightswitch --duration 0"

[[target]]
name = "Fedora 6.2"
kernel = "/nix/store/79wzcqy12fncdf6pk1fj627ggh7v0nc7-download-kernel-6.2/bzImage"
command = "/mnt/vmtest/target/debug/lightswitch --duration 0"

[[target]]
name = "Fedora 6.6"
kernel = "/nix/store/77ixckavs2qidx1pglmlxsj6bfvjqijb-download-kernel-6.6/bzImage"
command = "/mnt/vmtest/target/debug/lightswitch --duration 0"

0 comments on commit ae589c3

Please sign in to comment.