99 lines
3.9 KiB
Bash
99 lines
3.9 KiB
Bash
#!/usr/bin/env bash
|
|||
|
|
# Eval and start-script checks for nixosModules.t3code.
|
||
|
|
# Usage: t3code.test.sh
|
||
|
|
set -euo pipefail
|
||
|
|
|
||
|
|
here="$(cd "$(dirname "$0")" && pwd)"
|
||
|
|
repo="$(cd "$here/../.." && pwd)"
|
||
|
|
fail=0
|
||
|
|
|
||
|
|
check() { # name, got, want
|
||
|
|
if [[ "$2" == "$3" ]]; then echo "ok $1"; else echo "FAIL $1: got '$2', want '$3'"; fail=1; fi
|
||
|
|
}
|
||
|
|
contains() { # name, haystack, needle
|
||
|
|
if [[ "$2" == *"$3"* ]]; then echo "ok $1"; else echo "FAIL $1: missing '$3' in '$2'"; fail=1; fi
|
||
|
|
}
|
||
|
|
|
||
|
|
# ev <module-attrs> <expression over `config`>; prints JSON, or the error on stderr.
|
||
|
|
ev() {
|
||
|
|
nix eval --impure --json --expr "
|
||
|
|
let
|
||
|
|
flake = builtins.getFlake \"path:$repo\";
|
||
|
|
system = flake.inputs.nixpkgs.lib.nixosSystem {
|
||
|
|
modules = [
|
||
|
|
flake.nixosModules.t3code
|
||
|
|
({pkgs, ...}: {
|
||
|
|
nixpkgs.hostPlatform = \"x86_64-linux\";
|
||
|
|
nixpkgs.overlays = [(final: prev: {llm-agents.t3code = final.hello;})];
|
||
|
|
boot.loader.grub.enable = false;
|
||
|
|
fileSystems.\"/\" = {device = \"none\"; fsType = \"tmpfs\";};
|
||
|
|
system.stateVersion = \"25.05\";
|
||
|
|
users.users.alice = {isNormalUser = true; home = \"/home/alice\";};
|
||
|
|
})
|
||
|
|
($1)
|
||
|
|
];
|
||
|
|
};
|
||
|
|
config = system.config;
|
||
|
|
in $2"
|
||
|
|
}
|
||
|
|
|
||
|
|
on='{services.netbird.enable = true; m3ta.t3code = {enable = true; user = "alice";};}'
|
||
|
|
dev="sys-subsystem-net-devices-wt0.device"
|
||
|
|
|
||
|
|
check "bindsTo" "$(ev "$on" 'config.systemd.services.t3code.bindsTo')" "[\"$dev\"]"
|
||
|
|
check "wantedBy" "$(ev "$on" 'config.systemd.services.t3code.wantedBy')" "[\"$dev\"]"
|
||
|
|
contains "after device" "$(ev "$on" 'config.systemd.services.t3code.after')" "$dev"
|
||
|
|
contains "after netbird" "$(ev "$on" 'config.systemd.services.t3code.after')" "netbird.service"
|
||
|
|
check "user" "$(ev "$on" 'config.systemd.services.t3code.serviceConfig.User')" '"alice"'
|
||
|
|
check "port env" "$(ev "$on" 'config.systemd.services.t3code.environment.T3CODE_PORT')" '"3773"'
|
||
|
|
check "home env" "$(ev "$on" 'config.systemd.services.t3code.environment.HOME')" '"/home/alice"'
|
||
|
|
check "firewall" "$(ev "$on" 'config.networking.firewall.interfaces.wt0.allowedTCPPorts')" '[3773]'
|
||
|
|
contains "path has user profile" \
|
||
|
|
"$(ev "$on" 'map toString config.systemd.services.t3code.path')" "/etc/profiles/per-user/alice"
|
||
|
|
|
||
|
|
custom='{services.netbird.enable = true; m3ta.t3code = {enable = true; user = "alice"; port = 4000; interface = "nb-x";};}'
|
||
|
|
check "custom port" "$(ev "$custom" 'config.networking.firewall.interfaces."nb-x".allowedTCPPorts')" '[4000]'
|
||
|
|
contains "custom interface" "$(ev "$custom" 'config.systemd.services.t3code.bindsTo')" 'nb\\x2dx.device'
|
||
|
|
|
||
|
|
check "disabled has no service" \
|
||
|
|
"$(ev '{services.netbird.enable = true;}' 'config.systemd.services ? t3code')" "false"
|
||
|
|
|
||
|
|
err="$(ev '{m3ta.t3code = {enable = true; user = "alice";};}' 'config.system.build.toplevel.drvPath' 2>&1 || true)"
|
||
|
|
contains "netbird assertion" "$err" "m3ta.t3code requires services.netbird.enable"
|
||
|
|
|
||
|
|
err="$(ev '{services.netbird.enable = true; m3ta.t3code.enable = true;}' 'config.systemd.services.t3code.serviceConfig.User' 2>&1 || true)"
|
||
|
|
contains "user is required" "$err" "m3ta.t3code.user"
|
||
|
|
|
||
|
|
# ── Start script, with stubs for ip and t3 ──
|
||
|
|
stubs="$(mktemp -d)"
|
||
|
|
trap 'rm -rf "$stubs"' EXIT
|
||
|
|
cat >"$stubs/t3" <<'EOF'
|
||
|
|
#!/usr/bin/env bash
|
||
|
|
echo "t3 $*"
|
||
|
|
EOF
|
||
|
|
chmod +x "$stubs/t3"
|
||
|
|
|
||
|
|
run() { # ip stub body
|
||
|
|
cat >"$stubs/ip" <<EOF
|
||
|
|
#!/usr/bin/env bash
|
||
|
|
$1
|
||
|
|
EOF
|
||
|
|
chmod +x "$stubs/ip"
|
||
|
|
PATH="$stubs:$PATH" T3CODE_ADDR_TIMEOUT=1 bash "$here/t3code-serve.sh" wt0 "$stubs/t3" 2>&1
|
||
|
|
}
|
||
|
|
|
||
|
|
check "binds the interface address" \
|
||
|
|
"$(run 'echo "5: wt0 inet 100.1.2.3/16 brd 100.1.255.255 scope global wt0"')" "t3 serve --host 100.1.2.3"
|
||
|
|
|
||
|
|
set +e
|
||
|
|
out="$(run 'exit 0')"; rc=$?
|
||
|
|
set -e
|
||
|
|
check "no address exits 1" "$rc" "1"
|
||
|
|
if [[ "$out" == *"t3 serve"* ]]; then echo "FAIL no address must not start t3: $out"; fail=1; else echo "ok no address does not start t3"; fi
|
||
|
|
|
||
|
|
set +e
|
||
|
|
out="$(run 'echo "Device does not exist" >&2; exit 1')"; rc=$?
|
||
|
|
set -e
|
||
|
|
check "missing interface exits 1" "$rc" "1"
|
||
|
|
|
||
|
|
exit "$fail"
|