2
0
Fork 0

assimilate nixos modules

This commit is contained in:
Ehmry - 2020-10-23 22:39:33 +02:00
parent c765ba3b02
commit 3d5b687b4b
19 changed files with 1092 additions and 867 deletions

View File

@ -65,9 +65,7 @@
forAllCrossSystems ({ system, localSystem, crossSystem }:
nixpkgs.lib // (import ./lib {
inherit system localSystem crossSystem;
localPackages = nixpkgs.legacyPackages.${localSystem};
genodepkgs = self;
nixpkgs = nixpkgsFor.${system};
pkgs = self.legacyPackages.${system};
}));
legacyPackages =
@ -140,11 +138,11 @@
checks =
# Checks for continous testing
let tests = import ./tests;
in
with (forAllCrossSystems ({ system, localSystem, crossSystem }:
in with (forAllCrossSystems ({ system, localSystem, crossSystem }:
tests {
flake = self;
inherit system localSystem crossSystem;
pkgs = self.legacyPackages.${system};
} // {
ports = nixpkgsFor.${localSystem}.symlinkJoin {
name = "ports";

View File

@ -1,9 +1,11 @@
{ system, localSystem, crossSystem, genodepkgs, nixpkgs, localPackages }:
{ system, localSystem, crossSystem, pkgs }:
let
nixpkgs = pkgs;
localPackages = pkgs.buildPackages.buildPackages;
thisSystem = builtins.getAttr system;
inherit (nixpkgs) buildPackages;
testPkgs = thisSystem genodepkgs.packages;
testPkgs = pkgs.genodePackages;
dhallCachePrelude = ''
export XDG_CACHE_HOME=$NIX_BUILD_TOP

View File

@ -0,0 +1,27 @@
{ config, pkgs, lib, ... }:
with lib;
let
localPackages = pkgs.buildPackages;
utils = import ../lib {
inherit (config.nixpkgs) system localSystem crossSystem;
inherit pkgs;
};
in {
genode.core = {
prefix = "hw-pc-";
supportedSystems = [ "x86_64-genode" ];
basePackages = with pkgs.genodePackages; [ base-hw-pc rtc_drv ];
};
genode.boot = {
initrd = "${config.genode.boot.image}/image.elf";
image = utils.hwImage "0xffffffc000000000" "0x00200000"
pkgs.genodePackages.base-hw-pc config.system.name { }
config.genode.boot.config;
};
}

View File

@ -0,0 +1,27 @@
{ config, pkgs, lib, ... }:
with lib;
let
localPackages = pkgs.buildPackages;
utils = import ../lib {
inherit (config.nixpkgs) system localSystem crossSystem;
inherit pkgs;
};
in {
genode.core = {
prefix = "hw-virt_qemu";
supportedSystems = [ "aarch64-genode" ];
basePackages = with pkgs.genodePackages; [ base-hw-virt_qemu rtc-dummy ];
};
genode.boot = {
initrd = "${config.genode.boot.image}/image.elf";
image = utils.hwImage "0xffffffc000000000" "0x40000000"
pkgs.genodePackages.base-hw-virt_qemu config.system.name { }
config.genode.boot.config;
};
}

View File

@ -0,0 +1,123 @@
{ config, pkgs, lib, ... }:
with lib;
let localPackages = pkgs.buildPackages;
in {
options.genode = {
core = {
prefix = mkOption {
type = types.str;
example = "hw-pc-";
};
supportedSystems = mkOption {
type = types.listOf types.str;
example = [ "i686-genode" "x86_64-genode" ];
};
basePackages = mkOption { type = types.listOf types.package; };
};
boot = {
kernel = mkOption {
type = types.path;
default = "${pkgs.genodePackages.bender}/bender";
};
initrd = mkOption {
type = types.str;
default = "${pkgs.genodePackages.bender}/bender";
description = "Path to an image or a command-line arguments";
};
config = mkOption {
type = types.str;
description = ''
Dhall boot configuration. See
https://git.sr.ht/~ehmry/dhall-genode/tree/master/Boot/package.dhall
'';
};
image = mkOption {
type = types.path;
description =
"Boot image containing the base component binaries and configuration.";
};
romModules = mkOption {
type = types.attrsOf types.path;
description = "Attr set of initial ROM modules";
};
};
};
config = {
assertions = [{
assertion = builtins.any (s: s == config.nixpkgs.system)
config.genode.core.supportedSystems;
message = "invalid Genode core for this system";
}];
genode.boot.config = let
addManifest = drv:
drv // {
manifest =
localPackages.runCommand "${drv.name}.dhall" { inherit drv; } ''
set -eu
echo -n '[' >> $out
find $drv/ -type f -printf ',{mapKey= "%f",mapValue="%p"}' >> $out
${if builtins.elem "lib" drv.outputs then
''
find ${drv.lib}/ -type f -printf ',{mapKey= "%f",mapValue="%p"}' >> $out''
else
""}
echo -n ']' >> $out
'';
};
mergeManifests = inputs:
localPackages.writeTextFile {
name = "manifest.dhall";
text = with builtins;
let
f = head: input:
if hasAttr "manifest" input then
''
${head}, { mapKey = "${
lib.getName input
}", mapValue = ${input.manifest} }''
else
abort "${input.pname} does not have a manifest";
in (foldl' f "[" inputs) + "]";
};
storeManifest =
mergeManifests (map addManifest config.genode.init.inputs);
storeTarball = localPackages.runCommand "store" { } ''
mkdir -p $out
tar cf "$out/store.tar" --absolute-names ${
toString config.genode.init.inputs
}
'';
manifest = mergeManifests (map addManifest
(config.genode.core.basePackages ++ [ storeTarball ]
++ (with pkgs.genodePackages; [ init vfs cached_fs_rom ])));
in ''
${./store-wrapper.dhall}
(${config.genode.init.config})
$(stat --format '%s' ${storeTarball}/store.tar)
${storeManifest} ${manifest}
'';
};
}

View File

@ -11,7 +11,8 @@ with lib;
config = mkOption {
description = "Dhall configuration of this init instance";
type = types.either types.str types.path;
type = types.nullOr types.str;
default = null;
};
inputs = mkOption {
@ -19,6 +20,15 @@ with lib;
type = types.listOf types.package;
};
children = mkOption {
type = let
childOptions = { name, ... }: {
name = mkOption { type = types.str; };
dhallAttrs = mkOption { type = types.str; };
};
in types.attrsOf (types.submodule childOptions);
};
subinits = mkOption {
type = types.attrsOf (types.submodule ({ config, options, name, ... }: {
options = {

View File

@ -0,0 +1,51 @@
{ config, pkgs, lib, ... }:
with lib; {
options.genode = {
hardware = {
nic = mkOption {
default = { };
example = { eth0.driver = "virtio"; };
description = "The configuration for each Nic service.";
type = let
nicOptions = { name, ... }: {
name = mkOption {
example = "eth0";
type = types.str;
description = "Name of the Nic service.";
};
driver = mkOption { type = types.enum [ "ipxe" "virtio" ]; };
ipStack = mkOption {
type = types.enum [ "lwip" "lxip" ];
default = "lwip";
};
};
in types.attrsOf (types.submodule nicOptions);
};
};
};
config = {
genode.init.children = let
drivers = mapAttrsToList (name: interface: {
name = name + "-nic";
value = { };
}) config.networking.interfaces;
sockets = mapAttrsToList (name: interface: {
name = name + "-sockets";
value = { };
}) config.networking.interfaces;
in builtins.listToAttrs (drivers ++ sockets);
};
}

26
nixos-modules/nova.nix Normal file
View File

@ -0,0 +1,26 @@
{ config, pkgs, lib, ... }:
with lib;
let
localPackages = pkgs.buildPackages;
utils = import ../lib {
inherit (config.nixpkgs) system localSystem crossSystem;
inherit pkgs;
};
in {
genode.core = {
prefix = "nova-";
supportedSystems = [ "x86_64-genode" ];
basePackages = with pkgs.genodePackages; [ base-nova rtc_drv ];
};
genode.boot = {
initrd =
"'${pkgs.genodePackages.NOVA}/hypervisor-x86_64 arg=iommu novpid serial,${config.genode.boot.image}/image.elf'";
image = utils.novaImage config.system.name { } config.genode.boot.config;
};
}

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,159 @@
let Genode =
env:DHALL_GENODE sha256:e90438be23b5100003cf018b783986df67bc6d0e3d35e800677d0d9109ff6aa9
let Prelude = Genode.Prelude
let XML = Prelude.XML
let Init = Genode.Init
let Child = Init.Child
let TextMapType = Prelude.Map.Type Text
let Children = TextMapType Child.Type
let Manifest/Type = TextMapType (TextMapType Text)
let Manifest/toRoutes =
λ(manifest : Manifest/Type) →
Prelude.List.map
(Prelude.Map.Entry Text Text)
Init.ServiceRoute.Type
( λ(entry : Prelude.Map.Entry Text Text) →
{ service =
{ name = "ROM"
, label = Init.LabelSelector.Type.Last entry.mapKey
}
, route =
Init.Route.Type.Child
{ name = "store_rom"
, label = Some entry.mapValue
, diag = None Bool
}
}
)
( Prelude.List.concat
(Prelude.Map.Entry Text Text)
(Prelude.Map.values Text (Prelude.Map.Type Text Text) manifest)
)
let parentROMs =
Prelude.List.map
Text
Init.ServiceRoute.Type
( λ(label : Text) →
{ service =
{ name = "ROM", label = Init.LabelSelector.Type.Last label }
, route =
Init.Route.Type.Parent { label = Some label, diag = None Bool }
}
)
let wrapStore
: Init.Type → Manifest/Type → Child.Type
= λ(init : Init.Type) →
λ(manifest : Manifest/Type) →
Init.toChild
init
Init.Attributes::{
, exitPropagate = True
, resources = Init.Resources::{ ram = Genode.units.MiB 4 }
, routes =
[ Init.ServiceRoute.parent "IO_MEM"
, Init.ServiceRoute.parent "IO_PORT"
, Init.ServiceRoute.parent "IRQ"
, Init.ServiceRoute.parent "VM"
, Init.ServiceRoute.child "Timer" "timer"
, Init.ServiceRoute.child "Rtc" "rtc"
, Init.ServiceRoute.child "File_system" "store_fs"
]
# parentROMs
[ "ld.lib.so"
, "init"
, "platform_info"
, "core_log"
, "kernel_log"
, "vfs"
, "libvfs.so"
, "cached_fs_rom"
]
# Manifest/toRoutes manifest
}
in λ(subinit : Init.Type) →
λ(storeSize : Natural) →
λ(storeManifest : Manifest/Type) →
λ(bootManifest : Manifest/Type) →
Genode.Boot::{
, config = Init::{
, children =
[ { mapKey = "timer"
, mapValue =
Child.flat
Child.Attributes::{
, binary = "timer_drv"
, provides = [ "Timer" ]
}
}
, { mapKey = "rtc"
, mapValue =
Child.flat
Child.Attributes::{
, binary = "rtc_drv"
, provides = [ "Rtc" ]
, routes = [ Init.ServiceRoute.parent "IO_PORT" ]
}
}
, { mapKey = "store_fs"
, mapValue =
Child.flat
Child.Attributes::{
, binary = "vfs"
, config = Init.Config::{
, content =
[ XML.element
{ name = "vfs"
, attributes = XML.emptyAttributes
, content =
[ XML.leaf
{ name = "tar"
, attributes = toMap { name = "store.tar" }
}
]
}
]
, defaultPolicy = Some Init.Config.DefaultPolicy::{
, attributes = toMap { root = "/", writeable = "no" }
}
}
, provides = [ "File_system" ]
}
}
, { mapKey = "store_rom"
, mapValue =
Child.flat
Child.Attributes::{
, binary = "cached_fs_rom"
, provides = [ "ROM" ]
, resources = Init.Resources::{
, ram = storeSize + Genode.units.MiB 1
}
, routes =
[ Init.ServiceRoute.child "File_system" "store_fs" ]
}
}
, { mapKey = "init", mapValue = wrapStore subinit storeManifest }
]
}
, rom =
Genode.BootModules.toRomPaths
( Prelude.List.concat
(Prelude.Map.Entry Text Text)
( Prelude.Map.values
Text
(Prelude.Map.Type Text Text)
bootManifest
)
)
}

View File

@ -1,345 +1,88 @@
{ flake, system, localSystem, crossSystem }:
{ flake, system, localSystem, crossSystem, pkgs }:
let
apps = flake.apps.${system};
localPackages = flake.legacyPackages.${localSystem};
genodepkgs = flake.packages.${system};
lib = flake.lib.${system};
nixpkgs = flake.legacyPackages.${system};
legacyPackages = flake.legacyPackages.${system};
in with import ./lib/build-vms.nix {
inherit flake system localSystem crossSystem;
pkgs = flake.legacyPackages.${system};
localPackages = flake.inputs.nixpkgs.legacyPackages.${localSystem};
modulesPath = "${flake.inputs.nixpkgs}/nixos/modules";
};
with flake.legacyPackages.${system};
let
testingPython = import ./lib/testing-python.nix;
testSpecs = map (p: import p) [
./log.nix
./lighttpd.nix
# ./posix.nix
# ./tox-bootstrapd.nix
# ./vmm_arm.nix
# ./vmm_x86.nix
# ./x86.nix
./x86.nix
]; # TODO ++ (callTest ./solo5);
testPkgs = genodepkgs;
qemu' = localPackages.qemu;
qemuBinary = qemuPkg:
{
aarch64-genode = "${qemuPkg}/bin/qemu-system-aarch64";
x86_64-genode = "${qemuPkg}/bin/qemu-system-x86_64";
}.${genodepkgs.stdenv.hostPlatform.system};
}.${pkgs.stdenv.hostPlatform.system};
# TODO: move the cores into nixos modules
cores = [
{
prefix = "hw-pc-";
testingPython = testingPython {
inherit flake system localSystem crossSystem pkgs;
extraConfigurations = [ ../nixos-modules/base-hw-pc.nix ];
};
specs = [ "x86" "hw" ];
platforms = [ "x86_64-genode" ];
basePackages = [ testPkgs.base-hw-pc ]
++ map testPkgs.genodeSources.depot [ "rtc_drv" ];
makeImage =
lib.hwImage "0xffffffc000000000" "0x00200000" testPkgs.base-hw-pc;
startVM = vmName: image: ''
#! ${localPackages.runtimeShell}
exec ${qemuBinary qemu'} \
-name ${vmName} \
-machine q35 \
-m 384 \
-netdev user,id=net0 \
-device virtio-net-pci,netdev=net0 \
-kernel "${testPkgs.bender}/bender" \
-initrd "${image}/image.elf" \
$QEMU_OPTS \
"$@"
'';
}
{
prefix = "hw-virt_qemu-";
specs = [ "aarch64" "hw" ];
platforms = [ "aarch64-genode" ];
basePackages = with testPkgs; [ base-hw-virt_qemu rtc-dummy ];
makeImage = lib.hwImage "0xffffffc000000000" "0x40000000"
testPkgs.base-hw-virt_qemu;
startVM = vmName: image: ''
#! ${localPackages.runtimeShell}
exec ${qemuBinary qemu'} \
-name ${vmName} \
-M virt,virtualization=true,gic_version=3 \
-cpu cortex-a53 \
-smp 4 \
-m 384 \
-kernel "${image}/image.elf" \
$QEMU_OPTS \
"$@"
'';
}
/* {
prefix = "hw-virt_qemu-";
testingPython = testingPython {
inherit flake system localSystem crossSystem pkgs;
extraConfigurations = [ ../nixos-modules/base-hw-virt_qemu.nix ];
};
specs = [ "aarch64" "hw" ];
platforms = [ "aarch64-genode" ];
startVM = vmName: image: ''
#! ${localPackages.runtimeShell}
exec ${qemuBinary qemu'} \
-name ${vmName} \
-M virt,virtualization=true,gic_version=3 \
-cpu cortex-a53 \
-smp 4 \
-m 384 \
-kernel "${image}/image.elf" \
$QEMU_OPTS \
"$@"
'';
}
*/
{
prefix = "nova-";
testingPython = testingPython {
inherit flake system localSystem crossSystem pkgs;
extraConfigurations = [ ../nixos-modules/nova.nix ];
};
specs = [ "x86" "nova" ];
platforms = [ "x86_64-genode" ];
basePackages = [ testPkgs.base-nova ]
++ map testPkgs.genodeSources.depot [ "rtc_drv" ];
makeImage = lib.novaImage;
startVM = vmName: image: ''
#! ${localPackages.runtimeShell}
exec ${qemuBinary qemu'} \
-name ${vmName} \
-machine q35 \
-m 384 \
-kernel "${testPkgs.bender}/bender" \
-initrd "${testPkgs.NOVA}/hypervisor-x86_64 arg=iommu novpid serial,${image}/image.elf" \
$QEMU_OPTS \
"$@"
'';
}
];
cores' = builtins.filter (core:
builtins.any (x: x == genodepkgs.stdenv.hostPlatform.system) core.platforms)
builtins.any (x: x == pkgs.stdenv.hostPlatform.system) core.platforms)
cores;
testDriver = with localPackages;
let testDriverScript = ./test-driver/test-driver.py;
in stdenv.mkDerivation {
name = "nixos-test-driver";
nativeBuildInputs = [ makeWrapper ];
buildInputs = [ (python3.withPackages (p: [ p.ptpython ])) ];
checkInputs = with python3Packages; [ pylint mypy ];
dontUnpack = true;
preferLocalBuild = true;
doCheck = true;
checkPhase = ''
mypy --disallow-untyped-defs \
--no-implicit-optional \
--ignore-missing-imports ${testDriverScript}
pylint --errors-only ${testDriverScript}
'';
installPhase = ''
mkdir -p $out/bin
cp ${testDriverScript} $out/bin/nixos-test-driver
chmod u+x $out/bin/nixos-test-driver
# TODO: copy user script part into this file (append)
wrapProgram $out/bin/nixos-test-driver \
--prefix PATH : "${lib.makeBinPath [ qemu' coreutils ]}" \
'';
};
defaultTestScript = ''
start_all()
machine.wait_until_serial_output('child "init" exited with exit value 0')
'';
makeTest = with localPackages;
{ prefix, specs, platforms, basePackages, makeImage, startVM }:
{ name ? "unnamed", testScript ? defaultTestScript,
# Skip linting (mainly intended for faster dev cycles)
skipLint ? false, ... }@t:
let
testDriverName = "genode-test-driver-${name}";
# TODO: move buildVM into a nixos module
buildVM = vmName:
{ config, inputs, env ? { }, extraPaths ? [ ] }:
let
storeTarball = localPackages.runCommand "store" { } ''
mkdir -p $out
tar cf "$out/store.tar" --absolute-names ${toString inputs} ${
toString extraPaths
}
'';
addManifest = drv:
drv // {
manifest =
nixpkgs.runCommand "${drv.name}.dhall" { inherit drv; } ''
set -eu
echo -n '[' >> $out
find $drv/ -type f -printf ',{mapKey= "%f",mapValue="%p"}' >> $out
${if builtins.elem "lib" drv.outputs then
''
find ${drv.lib}/ -type f -printf ',{mapKey= "%f",mapValue="%p"}' >> $out''
else
""}
echo -n ']' >> $out
'';
};
storeManifest = lib.mergeManifests (map addManifest inputs);
manifest = lib.mergeManifests (map addManifest (basePackages
++ [ testPkgs.sotest-producer storeTarball ]
++ map testPkgs.genodeSources.depot [
"init"
"vfs"
"cached_fs_rom"
]));
config' = "${
./test-wrapper.dhall
} (${config}) $(stat --format '%s' ${storeTarball}/store.tar) ${storeManifest} ${manifest}";
env' = {
DHALL_GENODE_TEST = "${./test.dhall}";
} // env;
image = makeImage vmName env' config';
startVM' = startVM vmName image;
in {
script = localPackages.writeScriptBin "run-${vmName}-vm" startVM';
config = lib.runDhallCommand (name + ".dhall") env' ''
${apps.dhall.program} <<< "${config'}" > $out
'';
store = storeTarball;
xml = lib.runDhallCommand (name + ".config") env'
''${apps.render-init.program} <<< "(${config'}).config" > $out'';
};
# nodes = lib.mapAttrs buildVM
# (t.nodes or (if t ? machine then { machine = t.machine; } else { }));
nodes = buildVirtualNetwork
(t.nodes or (if t ? machine then { machine = t.machine; } else { }));
testScript' =
# Call the test script with the computed nodes.
if lib.isFunction testScript then
testScript { inherit nodes; }
else
testScript;
vlans = map (m: m.config.virtualisation.vlans) (lib.attrValues nodes);
vms = map (m: m.config.system.build.vm) (lib.attrValues nodes);
# Generate onvenience wrappers for running the test driver
# interactively with the specified network, and for starting the
# VMs from the command line.
driver =
let warn = if skipLint then lib.warn "Linting is disabled!" else lib.id;
in warn (runCommand testDriverName {
buildInputs = [ makeWrapper ];
testScript = testScript';
preferLocalBuild = true;
testName = name;
} ''
mkdir -p $out/bin
echo -n "$testScript" > $out/test-script
${lib.optionalString (!skipLint) ''
${python3Packages.black}/bin/black --check --quiet --diff $out/test-script
''}
ln -s ${testDriver}/bin/nixos-test-driver $out/bin/
vms=($(for i in ${toString vms}; do echo $i/bin/run-*-vm; done))
wrapProgram $out/bin/nixos-test-driver \
--add-flags "''${vms[*]}" \
--run "export testScript=\"\$(${coreutils}/bin/cat $out/test-script)\""
ln -s ${testDriver}/bin/nixos-test-driver $out/bin/nixos-run-vms
wrapProgram $out/bin/nixos-run-vms \
--add-flags "''${vms[*]}" \
--set tests 'start_all(); join_all();'
''); # "
passMeta = drv:
drv
// lib.optionalAttrs (t ? meta) { meta = (drv.meta or { }) // t.meta; };
# Run an automated test suite in the given virtual network.
# `driver' is the script that runs the network.
runTests = driver:
stdenv.mkDerivation {
name = "test-run-${driver.testName}";
buildCommand = ''
mkdir -p $out
LOGFILE=/dev/null tests='exec(os.environ["testScript"])' ${driver}/bin/nixos-test-driver
'';
};
test = passMeta (runTests driver);
nodeNames = builtins.attrNames nodes;
invalidNodeNames =
lib.filter (node: builtins.match "^[A-z_]([A-z0-9_]+)?$" node == null)
nodeNames;
in if lib.length invalidNodeNames > 0 then
throw ''
Cannot create machines out of (${
lib.concatStringsSep ", " invalidNodeNames
})!
All machines are referenced as python variables in the testing framework which will break the
script when special characters are used.
Please stick to alphanumeric chars and underscores as separation.
''
else
test // { inherit nodes driver test; };
testList = let
f = core:
let makeTest' = makeTest core;
in test:
f = core: test:
if (test.constraints or (_: true)) core.specs then {
name = core.prefix + test.name;
value = makeTest' test;
value = core.testingPython.makeTest test;
} else
null;
in lib.lists.crossLists f [ cores' testSpecs ];
in builtins.listToAttrs (builtins.filter (_: _ != null) testList)
/* sotest = let
hwTests = with hw; [ multi posix x86 ];
novaTests = with nova; [ multi posix x86 vmm ];
allTests = hwTests ++ novaTests;
projectCfg.boot_items =
(map (test: {
inherit (test) name;
exec = "bender";
load = [ "${test.name}.image.elf" ];
}) hwTests)
++ (map (test: {
inherit (test) name;
exec = "bender";
load = [ "hypervisor serial novga iommu" test.image.name ];
}) novaTests);
in localPackages.stdenv.mkDerivation {
name = "sotest";
buildCommand = ''
mkdir zip; cd zip
cp "${testPkgs.bender}/bender" bender
cp "${testPkgs.NOVA}/hypervisor-x86_64" hypervisor
${concatStringsSep "\n"
(map (test: "cp ${test.image}/image.elf ${test.name}.image.elf")
allTests)}
mkdir -p $out/nix-support
${localPackages.zip}/bin/zip "$out/binaries.zip" *
cat << EOF > "$out/project.json"
${builtins.toJSON projectCfg}
EOF
echo file sotest-binaries $out/binaries.zip >> "$out/nix-support/hydra-build-products"
echo file sotest-config $out/project.json >> "$out/nix-support/hydra-build-products"
'';
};
*/

View File

@ -1,6 +1,8 @@
{ flake, system, localSystem, crossSystem
{ system, localSystem, crossSystem
# Nixpkgs, for qemu, lib and more
, localPackages, pkgs, modulesPath }:
, pkgs, modulesPath
# NixOS configuration to add to the VMs
, extraConfigurations ? [ ] }:
with pkgs.lib;
with import ./qemu-flags.nix { inherit pkgs; };
@ -9,7 +11,7 @@ rec {
inherit pkgs;
qemu = pkgs.qemu_test;
qemu = pkgs.buildPackages.buildPackages.qemu_test;
# Build a virtual network from an attribute set `{ machine1 =
# config1; ... machineN = configN; }', where `machineX' is the
@ -23,10 +25,11 @@ rec {
import "${modulesPath}/../lib/eval-config.nix" {
inherit system;
modules = configurations;
modules = configurations ++ extraConfigurations;
baseModules = (import "${modulesPath}/module-list.nix") ++ [
"${modulesPath}/virtualisation/qemu-vm.nix"
"${modulesPath}/testing/test-instrumentation.nix" # !!! should only get added for automated test runs
../../nixos-modules/genode-core.nix
../../nixos-modules/genode-init.nix
../../nixos-modules/qemu-vm.nix
{
key = "no-manual";
documentation.nixos.enable = false;
@ -40,10 +43,8 @@ rec {
_module.args.nodes = nodes;
}
{
nixpkgs = {
inherit system crossSystem localSystem;
pkgs = flake.legacyPackages.${system};
};
system.build.qemu = qemu;
nixpkgs = { inherit system crossSystem localSystem pkgs; };
}
];
};

View File

@ -0,0 +1,235 @@
{ flake, system, localSystem, crossSystem, pkgs
# Modules to add to each VM
, extraConfigurations ? [ ] }:
with import ./build-vms.nix {
inherit system localSystem crossSystem pkgs extraConfigurations;
modulesPath = "${flake.inputs.nixpkgs}/nixos/modules";
};
with pkgs.buildPackages.buildPackages;
rec {
inherit pkgs;
testDriver = let testDriverScript = ./test-driver.py;
in stdenv.mkDerivation {
name = "nixos-test-driver";
nativeBuildInputs = [ makeWrapper ];
buildInputs = [ (python3.withPackages (p: [ p.ptpython ])) ];
checkInputs = with python3Packages; [ pylint mypy ];
dontUnpack = true;
preferLocalBuild = true;
doCheck = true;
checkPhase = ''
mypy --disallow-untyped-defs \
--no-implicit-optional \
--ignore-missing-imports ${testDriverScript}
pylint --errors-only ${testDriverScript}
'';
installPhase = ''
mkdir -p $out/bin
cp ${testDriverScript} $out/bin/nixos-test-driver
chmod u+x $out/bin/nixos-test-driver
# TODO: copy user script part into this file (append)
wrapProgram $out/bin/nixos-test-driver \
--prefix PATH : "${
lib.makeBinPath [ qemu_test vde2 netpbm coreutils ]
}" \
'';
};
# Run an automated test suite in the given virtual network.
# `driver' is the script that runs the network.
runTests = driver:
stdenv.mkDerivation {
name = "vm-test-run-${driver.testName}";
requiredSystemFeatures = [ "nixos-test" ];
buildCommand = ''
mkdir -p $out
LOGFILE=/dev/null tests='exec(os.environ["testScript"])' ${driver}/bin/nixos-test-driver
'';
};
defaultTestScript = ''
start_all()
machine.wait_until_serial_output('child "init" exited with exit value 0')
'';
makeTest = { testScript ? defaultTestScript, enableOCR ? false, name ?
"unnamed"
# Skip linting (mainly intended for faster dev cycles)
, skipLint ? false, ... }@t:
let
testDriverName = "genode-test-driver-${name}";
nodes = buildVirtualNetwork
(t.nodes or (if t ? machine then { machine = t.machine; } else { }));
testScript' =
# Call the test script with the computed nodes.
if lib.isFunction testScript then
testScript { inherit nodes; }
else
testScript;
vlans = map (m: m.config.virtualisation.vlans) (lib.attrValues nodes);
vms = map (m: m.config.system.build.vm) (lib.attrValues nodes);
ocrProg = tesseract4.override { enableLanguages = [ "eng" ]; };
imagemagick_tiff = imagemagick_light.override { inherit libtiff; };
# Generate convenience wrappers for running the test driver
# interactively with the specified network, and for starting the
# VMs from the command line.
driver =
let warn = if skipLint then lib.warn "Linting is disabled!" else lib.id;
in warn (runCommand testDriverName {
buildInputs = [ makeWrapper ];
testScript = testScript';
preferLocalBuild = true;
testName = name;
} ''
mkdir -p $out/bin
echo -n "$testScript" > $out/test-script
ln -s ${testDriver}/bin/nixos-test-driver $out/bin/
vms=($(for i in ${toString vms}; do echo $i/bin/run-*-vm; done))
wrapProgram $out/bin/nixos-test-driver \
--add-flags "''${vms[*]}" \
${
lib.optionalString enableOCR
"--prefix PATH : '${ocrProg}/bin:${imagemagick_tiff}/bin'"
} \
--run "export testScript=\"\$(${coreutils}/bin/cat $out/test-script)\"" \
--set VLANS '${toString vlans}'
ln -s ${testDriver}/bin/nixos-test-driver $out/bin/nixos-run-vms
wrapProgram $out/bin/nixos-run-vms \
--add-flags "''${vms[*]}" \
${lib.optionalString enableOCR "--prefix PATH : '${ocrProg}/bin'"} \
--set tests 'start_all(); join_all();' \
--set VLANS '${toString vlans}' \
${
lib.optionalString (builtins.length vms == 1) "--set USE_SERIAL 1"
}
''); # "
passMeta = drv:
drv
// lib.optionalAttrs (t ? meta) { meta = (drv.meta or { }) // t.meta; };
test = passMeta (runTests driver);
nodeNames = builtins.attrNames nodes;
invalidNodeNames =
lib.filter (node: builtins.match "^[A-z_]([A-z0-9_]+)?$" node == null)
nodeNames;
in if lib.length invalidNodeNames > 0 then
throw ''
Cannot create machines out of (${
lib.concatStringsSep ", " invalidNodeNames
})!
All machines are referenced as python variables in the testing framework which will break the
script when special characters are used.
Please stick to alphanumeric chars and underscores as separation.
''
else
test // { inherit nodes driver test; };
runInMachine = { drv, machine, preBuild ? "", postBuild ? "", ... # ???
}:
let
vm = buildVM { } [
machine
{
key = "run-in-machine";
networking.hostName = "client";
nix.readOnlyStore = false;
virtualisation.writableStore = false;
}
];
buildrunner = writeText "vm-build" ''
source $1
${coreutils}/bin/mkdir -p $TMPDIR
cd $TMPDIR
exec $origBuilder $origArgs
'';
testScript = ''
start_all()
client.wait_for_unit("multi-user.target")
${preBuild}
client.succeed("env -i ${bash}/bin/bash ${buildrunner} /tmp/xchg/saved-env >&2")
${postBuild}
client.succeed("sync") # flush all data before pulling the plug
'';
vmRunCommand = writeText "vm-run" ''
xchg=vm-state-client/xchg
${coreutils}/bin/mkdir $out
${coreutils}/bin/mkdir -p $xchg
for i in $passAsFile; do
i2=''${i}Path
_basename=$(${coreutils}/bin/basename ''${!i2})
${coreutils}/bin/cp ''${!i2} $xchg/$_basename
eval $i2=/tmp/xchg/$_basename
${coreutils}/bin/ls -la $xchg
done
unset i i2 _basename
export | ${gnugrep}/bin/grep -v '^xchg=' > $xchg/saved-env
unset xchg
export tests='${testScript}'
${testDriver}/bin/nixos-test-driver ${vm.config.system.build.vm}/bin/run-*-vm
''; # */
in lib.overrideDerivation drv (attrs: {
requiredSystemFeatures = [ "kvm" ];
builder = "${bash}/bin/sh";
args = [ "-e" vmRunCommand ];
origArgs = attrs.args;
origBuilder = attrs.builder;
});
runInMachineWithX = { require ? [ ], ... }@args:
let
client = { ... }: {
inherit require;
imports = [ ../tests/common/auto.nix ];
virtualisation.memorySize = 1024;
services.xserver.enable = true;
test-support.displayManager.auto.enable = true;
services.xserver.displayManager.defaultSession = "none+icewm";
services.xserver.windowManager.icewm.enable = true;
};
in runInMachine ({
machine = client;
preBuild = ''
client.wait_for_x()
'';
} // args);
simpleTest = as: (makeTest as).test;
}

13
tests/lighttpd.nix Normal file
View File

@ -0,0 +1,13 @@
{
name = "lighttpd";
nodes = {
webserver = {
imports = [ ../nixos-modules/hardware.nix ];
services.lighttpd.enable = true;
};
client = {
imports = [ ../nixos-modules/hardware.nix ];
genode.hardware.nic.eth0.driver = "virtio";
};
};
}

View File

@ -1,9 +1,6 @@
{
name = "log";
machine = { config, pkgs, ... }: {
imports = [
../nixos-modules/genode-init.nix
];
genode.init = {
config = ./log.dhall;
inputs = [ (pkgs.genodeSources.depot "test-log") ];

View File

@ -1,4 +1,6 @@
let Test = ./test.dhall ? env:DHALL_GENODE_TEST
let Test =
./test.dhall sha256:00e0b73a23e1f131a2e5af36a34bc85b31b4fb6597ea3772dee9c536929ea166
? env:DHALL_GENODE_TEST sha256:00e0b73a23e1f131a2e5af36a34bc85b31b4fb6597ea3772dee9c536929ea166
let Genode = Test.Genode

View File

@ -1,4 +1,5 @@
let Genode = env:DHALL_GENODE
let Genode =
env:DHALL_GENODE sha256:e90438be23b5100003cf018b783986df67bc6d0e3d35e800677d0d9109ff6aa9
let Prelude = Genode.Prelude

View File

@ -1,13 +1,12 @@
{ pkgs, ... }: {
{
name = "x86";
constraints = builtins.any (spec: spec == "x86");
machine = {
config = ./x86.dhall;
inputs = (map pkgs.genodeSources.depot [
"acpi_drv"
"platform_drv"
"report_rom"
"test-signal"
]) ++ (map pkgs.genodeSources.make [ "test/pci" "test/rtc" ]);
machine = { config, pkgs, ... }: {
genode.init = {
config = ./x86.dhall;
inputs = with pkgs.genodePackages;
[ acpi_drv platform_drv report_rom test-signal ]
++ (map genodeSources.make [ "test/pci" "test/rtc" ]);
};
};
}