Skip to content

Commit

Permalink
add testing VM - needs x86_64 builder
Browse files Browse the repository at this point in the history
  • Loading branch information
lostbean committed Feb 1, 2024
1 parent d5f4eae commit 2e92527
Show file tree
Hide file tree
Showing 5 changed files with 107 additions and 43 deletions.
6 changes: 5 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -124,4 +124,8 @@ Cargo.lock
# Lerna debug
lerna-debug.log

result
# Nix outputs
result

# Nix VM disk image
nixos.qcow2
15 changes: 15 additions & 0 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -86,5 +86,20 @@
architecture = "arm64";
config.Cmd = [ "${server}/bin/linux_arm64/server" ];
};

testingVMs = import ./internal_testsuites/vm_modules.nix {
inherit nixpkgs;
engine_image = container.image.arm64;
};

packages.x86_64-linux.linuxVM =
testingVMs.nixosConfigurations.linuxVM.config.system.build.vm;

packages.aarch64-darwin.darwinVM =
testingVMs.nixosConfigurations.darwinVM.config.system.build.vm;

packages.aarch64-darwin.testVM =
import ./internal_testsuites/vm_tests.nix
(self.inputs // { inherit pkgs nixpkgs; });
});
}
49 changes: 7 additions & 42 deletions internal_testsuites/flake.nix
Original file line number Diff line number Diff line change
@@ -1,46 +1,11 @@
{
outputs = { self, nixpkgs, }: {
nixosModules.vm = { ... }: {
# Make VM output to the terminal instead of a separate window
virtualisation.vmVariant.virtualisation.graphics = false;
};

nixosModules.base = { pkgs, ... }: {
system.stateVersion = "23.11";

# Configure networking
networking.useDHCP = false;
networking.interfaces.eth0.useDHCP = true;
outputs = { self, nixpkgs, }:
let vms = import ./vm_modules.nix { inherit nixpkgs; };
in {
packages.x86_64-linux.linuxVM =
vms.nixosConfigurations.linuxVM.config.system.build.vm;

# Create user "test"
services.getty.autologinUser = "tester";
users.users.test.isNormalUser = true;

# Enable passwordless ‘sudo’ for the "test" user
users.users.test.extraGroups = [ "wheel" ];
security.sudo.wheelNeedsPassword = false;
packages.aarch64-darwin.darwinVM =
vms.nixosConfigurations.darwinVM.config.system.build.vm;
};

nixosConfigurations.linuxVM = nixpkgs.lib.nixosSystem {
system = "x86_64-linux";
modules = [ self.nixosModules.base self.nixosModules.vm ];
};
packages.x86_64-linux.linuxVM =
self.nixosConfigurations.linuxVM.config.system.build.vm;

nixosConfigurations.darwinVM = nixpkgs.lib.nixosSystem {
system = "aarch64-linux";
modules = [
self.nixosModules.base
self.nixosModules.vm
{
virtualisation.vmVariant.virtualisation.host.pkgs =
nixpkgs.legacyPackages.aarch64-darwin;
}
];
};
packages.aarch64-darwin.darwinVM =
self.nixosConfigurations.darwinVM.config.system.build.vm;

};
}
50 changes: 50 additions & 0 deletions internal_testsuites/vm_modules.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
{ nixpkgs, engine_image, ... }:
let
nixosModulesVM = { ... }: {
# Make VM output to the terminal instead of a separate window
virtualisation.vmVariant.virtualisation.graphics = false;
};

nixosModulesBase = { pkgs, ... }: {
system.stateVersion = "23.11";

# Configure networking
networking.useDHCP = false;
networking.interfaces.eth0.useDHCP = true;

# Create user "tester"
services.getty.autologinUser = "tester";
users.users.tester.isNormalUser = true;

# setup k3s
services.k3s.enable = true;
services.k3s.role = "server";
environment.systemPackages = [ pkgs.k3s ];

# setup docker
virtualisation.docker.enable = true;

# Enable passwordless ‘sudo’ for the "tester" user
users.users.tester.extraGroups = [ "wheel" "docker" ];
security.sudo.wheelNeedsPassword = false;
};

in {
nixosConfigurations.linuxVM = nixpkgs.lib.nixosSystem {
system = "x86_64-linux";
modules = [ nixosModulesBase nixosModulesVM ];
};

nixosConfigurations.darwinVM = nixpkgs.lib.nixosSystem {
system = "aarch64-linux";
modules = [
nixosModulesBase
nixosModulesVM
{
virtualisation.vmVariant.virtualisation.host.pkgs =
nixpkgs.legacyPackages.aarch64-darwin;
}
];
};

}
30 changes: 30 additions & 0 deletions internal_testsuites/vm_tests.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
{ nixpkgs, pkgs, ... }:
let nixos-lib = import (nixpkgs + "/nixos/lib") { };
in nixos-lib.runTest rec {
name = "demo-test";

hostPkgs = import nixpkgs { system = pkgs.stdenv.hostPlatform.system; };

node = pkgs.lib.optionalAttrs pkgs.stdenv.isDarwin {
pkgs = import nixpkgs {
system = builtins.replaceStrings [ "darwin" ] [ "linux" ]
pkgs.stdenv.hostPlatform.system;
};
};

nodes.machine = { config, pkgs, ... }: {
users.users.alice = {
isNormalUser = true;
extraGroups = [ "wheel" ];
packages = with pkgs; [ tree ];
};
system.stateVersion = "23.11";
virtualisation.vmVariant.virtualisation.graphics = false;
};

testScript = ''
machine.wait_for_unit("default.target")
machine.succeed("su -- alice -c 'which firefox'")
machine.fail("su -- root -c 'which firefox'")
'';
}

0 comments on commit 2e92527

Please sign in to comment.