Watch
0
0
Fork
You've already forked hyperhive
0

ci: type-check the semi-spread and swarm-builder-offline test scripts with stand-in values

This commit is contained in:
atlas 2026-11-13 08:13:51 +01:00
commit de43dac33c

View file

@ -74,6 +74,96 @@ let
tokenFile = "/run/builder-offline/forge-token";
caBundle = "${tls}/ca.pem";
# The script is a function of the fixtures it names, so the type check can
# build it with stand-in paths and not evaluate the agent toplevel.
scriptWith =
{
toplevel,
hyperhiveRepo,
metaRepo,
}:
''
import re
import secrets
CA = "${caBundle}"
API = "${forgeUrl}/api/v1"
TOPLEVEL = "${toplevel}"
# What nix prints for a derivation it builds or plans to build.
BUILD_PATTERN = r"building '|will be built"
# The forge admin's password, made for this run.
password = secrets.token_hex(16)
def forge_api(method, path, body, auth):
return machine.succeed(
f"curl --fail --silent --cacert {CA} -X {method} {API}{path} "
f"-H 'Content-Type: application/json' {auth} -d '{body}'"
)
machine.start()
machine.wait_for_unit("forgejo.service")
machine.wait_for_open_port(${toString forgePort})
with subtest("the forge holds both repos, private, at the fixtures' commits"):
machine.succeed(
"su -l forgejo -c 'GITEA_WORK_DIR=/var/lib/forgejo forgejo admin user create "
f"--admin --username ops --password {password} --email ops@${forgeHost} "
"--must-change-password=false'"
)
token = machine.succeed(
f"curl --fail --silent --cacert {CA} -X POST {API}/users/ops/tokens "
f"-u ops:{password} -H 'Content-Type: application/json' "
"-d '{\"name\":\"fixtures\",\"scopes\":[\"all\"]}' | jq -r .sha1"
).strip()
auth = f"-H 'Authorization: token {token}'"
for org, repo, fixture in [
("hyperhive", "hyperhive", "${hyperhiveRepo}"),
("internal", "meta", "${metaRepo}"),
]:
forge_api("POST", "/orgs", f'{{"username":"{org}","visibility":"private"}}', auth)
forge_api("POST", f"/orgs/{org}/repos", f'{{"name":"{repo}","private":true,"default_branch":"main"}}', auth)
remote = f"https://ops:{token}@${forgeAuthority}/{org}/{repo}.git"
machine.succeed(f"cp -r {fixture}/repo.git /tmp/{repo}.git && chmod -R u+w /tmp/{repo}.git")
machine.succeed(f"GIT_SSL_CAINFO={CA} git -C /tmp/{repo}.git push {remote} main")
pushed = machine.succeed(f"GIT_SSL_CAINFO={CA} git ls-remote {remote} refs/heads/main").split()[0]
assert pushed == machine.succeed(f"cat {fixture}/rev").strip(), (repo, pushed)
# The forge asks for a login: the builder's credential helper has to supply it.
machine.fail(f"GIT_SSL_CAINFO={CA} GIT_TERMINAL_PROMPT=0 git ls-remote ${forgeUrl}/internal/meta.git")
machine.succeed(
f"install -d -m 0755 /run/builder-offline && printf %s {token} > ${tokenFile} "
"&& chown builder ${tokenFile} && chmod 0400 ${tokenFile}"
)
with subtest("the toplevel is in the store before the build"):
machine.succeed(f"nix --extra-experimental-features nix-command path-info {TOPLEVEL}")
# The control for the lookup: a path that is not there is not found.
machine.fail("nix --extra-experimental-features nix-command path-info /nix/store/00000000000000000000000000000000-absent")
with subtest("the builder's build finishes with nothing built"):
status, _ = machine.execute(
"runuser -u builder -- timeout 600 /run/current-system/sw/bin/meta-build >/tmp/out 2>/tmp/err"
)
out = machine.succeed("cat /tmp/out").strip()
err = machine.succeed("cat /tmp/err")
assert status == 0, f"nix exited {status}:\n{err}"
assert out == TOPLEVEL, f"printed {out}, prebuilt {TOPLEVEL}"
assert not re.search(BUILD_PATTERN, err), err
with subtest("control: a derivation that does build is found by the same pattern"):
machine.succeed("grep -x 'max-jobs = 0' /etc/nix/nix.conf")
machine.succeed(
"cp --remove-destination $(readlink -f /etc/nix/nix.conf) /etc/nix/nix.conf "
"&& sed -i 's/^max-jobs = 0$/max-jobs = 1/' /etc/nix/nix.conf "
"&& systemctl stop nix-daemon.socket nix-daemon.service "
"&& systemctl start nix-daemon.socket"
)
machine.succeed(
"NIX_CONFIG='experimental-features = nix-command' "
"nix build --no-link --print-build-logs --impure -f ${control} 2>/tmp/control.err"
)
assert re.search(BUILD_PATTERN, machine.succeed("cat /tmp/control.err"))
'';
in
pkgs.testers.runNixOSTest {
name = "swarm-builder-offline";
@ -175,85 +265,12 @@ pkgs.testers.runNixOSTest {
];
};
testScript = ''
import re
import secrets
testScript = scriptWith { inherit toplevel hyperhiveRepo metaRepo; };
CA = "${caBundle}"
API = "${forgeUrl}/api/v1"
TOPLEVEL = "${toplevel}"
# What nix prints for a derivation it builds or plans to build.
BUILD_PATTERN = r"building '|will be built"
# The forge admin's password, made for this run.
password = secrets.token_hex(16)
def forge_api(method, path, body, auth):
return machine.succeed(
f"curl --fail --silent --cacert {CA} -X {method} {API}{path} "
f"-H 'Content-Type: application/json' {auth} -d '{body}'"
)
machine.start()
machine.wait_for_unit("forgejo.service")
machine.wait_for_open_port(${toString forgePort})
with subtest("the forge holds both repos, private, at the fixtures' commits"):
machine.succeed(
"su -l forgejo -c 'GITEA_WORK_DIR=/var/lib/forgejo forgejo admin user create "
f"--admin --username ops --password {password} --email ops@${forgeHost} "
"--must-change-password=false'"
)
token = machine.succeed(
f"curl --fail --silent --cacert {CA} -X POST {API}/users/ops/tokens "
f"-u ops:{password} -H 'Content-Type: application/json' "
"-d '{\"name\":\"fixtures\",\"scopes\":[\"all\"]}' | jq -r .sha1"
).strip()
auth = f"-H 'Authorization: token {token}'"
for org, repo, fixture in [
("hyperhive", "hyperhive", "${hyperhiveRepo}"),
("internal", "meta", "${metaRepo}"),
]:
forge_api("POST", "/orgs", f'{{"username":"{org}","visibility":"private"}}', auth)
forge_api("POST", f"/orgs/{org}/repos", f'{{"name":"{repo}","private":true,"default_branch":"main"}}', auth)
remote = f"https://ops:{token}@${forgeAuthority}/{org}/{repo}.git"
machine.succeed(f"cp -r {fixture}/repo.git /tmp/{repo}.git && chmod -R u+w /tmp/{repo}.git")
machine.succeed(f"GIT_SSL_CAINFO={CA} git -C /tmp/{repo}.git push {remote} main")
pushed = machine.succeed(f"GIT_SSL_CAINFO={CA} git ls-remote {remote} refs/heads/main").split()[0]
assert pushed == machine.succeed(f"cat {fixture}/rev").strip(), (repo, pushed)
# The forge asks for a login: the builder's credential helper has to supply it.
machine.fail(f"GIT_SSL_CAINFO={CA} GIT_TERMINAL_PROMPT=0 git ls-remote ${forgeUrl}/internal/meta.git")
machine.succeed(
f"install -d -m 0755 /run/builder-offline && printf %s {token} > ${tokenFile} "
"&& chown builder ${tokenFile} && chmod 0400 ${tokenFile}"
)
with subtest("the toplevel is in the store before the build"):
machine.succeed(f"nix --extra-experimental-features nix-command path-info {TOPLEVEL}")
# The control for the lookup: a path that is not there is not found.
machine.fail("nix --extra-experimental-features nix-command path-info /nix/store/00000000000000000000000000000000-absent")
with subtest("the builder's build finishes with nothing built"):
status, _ = machine.execute(
"runuser -u builder -- timeout 600 /run/current-system/sw/bin/meta-build >/tmp/out 2>/tmp/err"
)
out = machine.succeed("cat /tmp/out").strip()
err = machine.succeed("cat /tmp/err")
assert status == 0, f"nix exited {status}:\n{err}"
assert out == TOPLEVEL, f"printed {out}, prebuilt {TOPLEVEL}"
assert not re.search(BUILD_PATTERN, err), err
with subtest("control: a derivation that does build is found by the same pattern"):
machine.succeed("grep -x 'max-jobs = 0' /etc/nix/nix.conf")
machine.succeed(
"cp --remove-destination $(readlink -f /etc/nix/nix.conf) /etc/nix/nix.conf "
"&& sed -i 's/^max-jobs = 0$/max-jobs = 1/' /etc/nix/nix.conf "
"&& systemctl stop nix-daemon.socket nix-daemon.service "
"&& systemctl start nix-daemon.socket"
)
machine.succeed(
"NIX_CONFIG='experimental-features = nix-command' "
"nix build --no-link --print-build-logs --impure -f ${control} 2>/tmp/control.err"
)
assert re.search(BUILD_PATTERN, machine.succeed("cat /tmp/control.err"))
'';
# What `nix/vm-test-typecheck.nix` checks: the same text with stand-in paths.
passthru.typecheckScript = scriptWith {
toplevel = "/nix/store/stub";
hyperhiveRepo = "/nix/store/stub";
metaRepo = "/nix/store/stub";
};
}