nixos machine tests

This commit is contained in:
raf 2023-11-29 00:26:35 +03:00
parent 98a817e65a
commit bc09a7f43f
No known key found for this signature in database
GPG key ID: 02D1DD3FA08B6B29
4 changed files with 165 additions and 0 deletions

71
nix/module.nix Normal file
View file

@ -0,0 +1,71 @@
self: {
config,
pkgs,
lib,
...
}: let
inherit (lib) mkEnableOption mkOption mkIf types;
cfg = config.services.pi-air-quality-monitor;
in {
options.services.pi-air-quality-monitor = {
enable = mkEnableOption "pi-air-quality-monitor";
package = mkOption {
type = types.package;
default = self.packages.${pkgs.system}.pi-air-quality-monitor;
};
openFirewall = mkOption {
type = types.bool;
default = true;
description = "Whether to open the firewall for the server";
};
settings = {
port = mkOption {
type = types.int;
default = 8080;
description = "Port to run the server on";
};
user = mkOption {
type = types.str;
default = "pi-aqm";
description = "User to run the server as";
};
group = mkOption {
type = types.str;
default = "pi-aqm";
description = "Group to run the server as";
};
};
};
config = mkIf config.services.pi-air-quality-monitor.enable {
networking.firewall.allowedTCPPorts = [cfg.settings.port];
users = {
users.pi-aqm = {
isSystemUser = true;
group = "pi-aqm";
home = "/var/lib/pi-aqm";
createHome = true;
};
groups.pi-aqm = {};
};
systemd.services."pi-air-quality-monitor" = {
description = "An air quality monitoring service with a Raspberry Pi and a SDS011 sensor";
wantedBy = ["multi-user.target"];
after = ["network.target"];
serviceConfig = {
Type = "simple";
User = cfg.settings.user;
Group = cfg.settings.group;
WorkingDirectory = "/var/lib/pi-aqm";
ExecStart = "${lib.getExe cfg.package}";
Restart = "always";
};
};
};
}

View file

@ -0,0 +1,56 @@
{
nixosTest,
self,
...
}:
nixosTest {
name = "basic";
nodes = {
client = {pkgs, ...}: {
imports = [../profiles/test-setup.nix];
environment.systemPackages = with pkgs; [
netcat
];
};
server = {pkgs, ...}: {
imports = [
../profiles/test-setup.nix
self.nixosModules.pi-air-quality-monitor
];
users.users.test = {
isNormalUser = true;
extraGroups = ["wheel"];
packages = with pkgs; [
tree
];
};
services.pi-air-quality-monitor = {
enable = true;
openFirewall = true;
settings = {
port = 8080;
user = "pi-aqm";
group = "pi-aqm";
};
};
system.stateVersion = "23.11";
};
};
testScript = ''
server.wait_for_unit("default.target")
server.succeed("ls -lah /dev/ttyUSB0")
server.succeed('systemctl status pi-air-quality-monitor | grep \"Active: active (running)\" || return 0')
#server.succeed('nc -vz server 8080')
#client.wait_for_unit("default.target")
#client.succeed("nc -vz server 8080")
'';
}

26
nix/tests/default.nix Normal file
View file

@ -0,0 +1,26 @@
{
self,
config,
inputs,
lib,
...
}: {
perSystem = {
pkgs,
self',
...
}: let
callPackage = lib.callPackageWith (pkgs
// {
inherit (config.flake) nixosModules;
inherit inputs;
});
in {
packages.test = self'.checks.basic.driverInteractive;
checks = {
basic = callPackage ./checks/basic.nix {inherit self;};
default = self'.checks.basic;
};
};
}

View file

@ -0,0 +1,12 @@
{
virtualisation = {
cores = 2;
memorySize = 1024;
qemu.options = [
"-nographic"
"-vga none -enable-kvm"
"-device virtio-gpu-pci,xres=720,yres=1440"
"-serial pty"
];
};
}