tests/nixos/remote-builds*: Inline module + format

(cherry picked from commit 5167351efbee5c5a7390510eb720c31c6976f4d9)
Change-Id: I0caba23b589ed428d08895d7b8f0c22532bd259e
This commit is contained in:
eldritch horrors 2024-03-07 03:02:22 +01:00
parent 097acae35b
commit f10e673272
2 changed files with 149 additions and 151 deletions

View file

@ -28,96 +28,95 @@ let
in in
{ {
name = lib.mkDefault "remote-builds-ssh-ng"; options = {
builders.config = lib.mkOption {
# TODO expand module shorthand syntax instead of use imports type = lib.types.deferredModule;
imports = [{ description = ''
options = { Configuration to add to the builder nodes.
builders.config = lib.mkOption { '';
type = lib.types.deferredModule; default = { };
description = ''
Configuration to add to the builder nodes.
'';
default = { };
};
}; };
}]; };
nodes = config = {
{ builder = name = lib.mkDefault "remote-builds-ssh-ng";
{ config, pkgs, ... }:
{
imports = [ test.config.builders.config ];
services.openssh.enable = true;
virtualisation.writableStore = true;
nix.settings.sandbox = true;
nix.settings.substituters = lib.mkForce [ ];
};
client = nodes =
{ config, lib, pkgs, ... }: { builder =
{ nix.settings.max-jobs = 0; # force remote building { config, pkgs, ... }:
nix.distributedBuilds = true; {
nix.buildMachines = imports = [ test.config.builders.config ];
[ { hostName = "builder"; services.openssh.enable = true;
sshUser = "root";
sshKey = "/root/.ssh/id_ed25519";
system = "i686-linux";
maxJobs = 1;
protocol = "ssh-ng";
}
];
virtualisation.writableStore = true; virtualisation.writableStore = true;
virtualisation.additionalPaths = [ config.system.build.extraUtils ]; nix.settings.sandbox = true;
nix.settings.substituters = lib.mkForce [ ]; nix.settings.substituters = lib.mkForce [ ];
programs.ssh.extraConfig = "ConnectTimeout 30";
}; };
};
testScript = { nodes }: '' client =
# fmt: off { config, lib, pkgs, ... }:
import subprocess { nix.settings.max-jobs = 0; # force remote building
nix.distributedBuilds = true;
nix.buildMachines =
[ { hostName = "builder";
sshUser = "root";
sshKey = "/root/.ssh/id_ed25519";
system = "i686-linux";
maxJobs = 1;
protocol = "ssh-ng";
}
];
virtualisation.writableStore = true;
virtualisation.additionalPaths = [ config.system.build.extraUtils ];
nix.settings.substituters = lib.mkForce [ ];
programs.ssh.extraConfig = "ConnectTimeout 30";
};
};
start_all() testScript = { nodes }: ''
# fmt: off
import subprocess
# Create an SSH key on the client. start_all()
subprocess.run([
"${hostPkgs.openssh}/bin/ssh-keygen", "-t", "ed25519", "-f", "key", "-N", ""
], capture_output=True, check=True)
client.succeed("mkdir -p -m 700 /root/.ssh")
client.copy_from_host("key", "/root/.ssh/id_ed25519")
client.succeed("chmod 600 /root/.ssh/id_ed25519")
# Install the SSH key on the builder. # Create an SSH key on the client.
client.wait_for_unit("network.target") subprocess.run([
builder.succeed("mkdir -p -m 700 /root/.ssh") "${hostPkgs.openssh}/bin/ssh-keygen", "-t", "ed25519", "-f", "key", "-N", ""
builder.copy_from_host("key.pub", "/root/.ssh/authorized_keys") ], capture_output=True, check=True)
builder.wait_for_unit("sshd") client.succeed("mkdir -p -m 700 /root/.ssh")
client.succeed(f"ssh -o StrictHostKeyChecking=no {builder.name} 'echo hello world'") client.copy_from_host("key", "/root/.ssh/id_ed25519")
client.succeed("chmod 600 /root/.ssh/id_ed25519")
# Perform a build # Install the SSH key on the builder.
out = client.succeed("nix-build ${expr nodes.client.config 1} 2> build-output") client.wait_for_unit("network.target")
builder.succeed("mkdir -p -m 700 /root/.ssh")
builder.copy_from_host("key.pub", "/root/.ssh/authorized_keys")
builder.wait_for_unit("sshd")
client.succeed(f"ssh -o StrictHostKeyChecking=no {builder.name} 'echo hello world'")
# Verify that the build was done on the builder # Perform a build
builder.succeed(f"test -e {out.strip()}") out = client.succeed("nix-build ${expr nodes.client 1} 2> build-output")
# Print the build log, prefix the log lines to avoid nix intercepting lines starting with @nix # Verify that the build was done on the builder
buildOutput = client.succeed("sed -e 's/^/build-output:/' build-output") builder.succeed(f"test -e {out.strip()}")
print(buildOutput)
# Make sure that we get the expected build output # Print the build log, prefix the log lines to avoid nix intercepting lines starting with @nix
client.succeed("grep -qF Hello build-output") buildOutput = client.succeed("sed -e 's/^/build-output:/' build-output")
print(buildOutput)
# We don't want phase reporting in the build output # Make sure that we get the expected build output
client.fail("grep -qF '@nix' build-output") client.succeed("grep -qF Hello build-output")
# Get the log file # We don't want phase reporting in the build output
client.succeed(f"nix-store --read-log {out.strip()} > log-output") client.fail("grep -qF '@nix' build-output")
# Prefix the log lines to avoid nix intercepting lines starting with @nix
logOutput = client.succeed("sed -e 's/^/log-file:/' log-output")
print(logOutput)
# Check that we get phase reporting in the log file # Get the log file
client.succeed("grep -q '@nix {\"action\":\"setPhase\",\"phase\":\"buildPhase\"}' log-output") client.succeed(f"nix-store --read-log {out.strip()} > log-output")
''; # Prefix the log lines to avoid nix intercepting lines starting with @nix
logOutput = client.succeed("sed -e 's/^/log-file:/' log-output")
print(logOutput)
# Check that we get phase reporting in the log file
client.succeed("grep -q '@nix {\"action\":\"setPhase\",\"phase\":\"buildPhase\"}' log-output")
'';
};
} }

View file

@ -37,90 +37,89 @@ let
in in
{ {
name = lib.mkDefault "remote-builds"; options = {
builders.config = lib.mkOption {
type = lib.types.deferredModule;
description = ''
Configuration to add to the builder nodes.
'';
default = { };
};
};
# TODO expand module shorthand syntax instead of use imports config = {
imports = [{ name = lib.mkDefault "remote-builds";
options = {
builders.config = lib.mkOption { nodes =
type = lib.types.deferredModule; { builder1 = builder;
description = '' builder2 = builder;
Configuration to add to the builder nodes.
''; client =
default = { }; { config, lib, pkgs, ... }:
{ nix.settings.max-jobs = 0; # force remote building
nix.distributedBuilds = true;
nix.buildMachines =
[ { hostName = "builder1";
sshUser = "root";
sshKey = "/root/.ssh/id_ed25519";
system = "i686-linux";
maxJobs = 1;
}
{ hostName = "builder2";
sshUser = "root";
sshKey = "/root/.ssh/id_ed25519";
system = "i686-linux";
maxJobs = 1;
}
];
virtualisation.writableStore = true;
virtualisation.additionalPaths = [ config.system.build.extraUtils ];
nix.settings.substituters = lib.mkForce [ ];
programs.ssh.extraConfig = "ConnectTimeout 30";
};
}; };
};
}];
nodes = testScript = { nodes }: ''
{ builder1 = builder; # fmt: off
builder2 = builder; import subprocess
client = start_all()
{ config, lib, pkgs, ... }:
{ nix.settings.max-jobs = 0; # force remote building
nix.distributedBuilds = true;
nix.buildMachines =
[ { hostName = "builder1";
sshUser = "root";
sshKey = "/root/.ssh/id_ed25519";
system = "i686-linux";
maxJobs = 1;
}
{ hostName = "builder2";
sshUser = "root";
sshKey = "/root/.ssh/id_ed25519";
system = "i686-linux";
maxJobs = 1;
}
];
virtualisation.writableStore = true;
virtualisation.additionalPaths = [ config.system.build.extraUtils ];
nix.settings.substituters = lib.mkForce [ ];
programs.ssh.extraConfig = "ConnectTimeout 30";
};
};
testScript = { nodes }: '' # Create an SSH key on the client.
# fmt: off subprocess.run([
import subprocess "${hostPkgs.openssh}/bin/ssh-keygen", "-t", "ed25519", "-f", "key", "-N", ""
], capture_output=True, check=True)
client.succeed("mkdir -p -m 700 /root/.ssh")
client.copy_from_host("key", "/root/.ssh/id_ed25519")
client.succeed("chmod 600 /root/.ssh/id_ed25519")
start_all() # Install the SSH key on the builders.
client.wait_for_unit("network.target")
for builder in [builder1, builder2]:
builder.succeed("mkdir -p -m 700 /root/.ssh")
builder.copy_from_host("key.pub", "/root/.ssh/authorized_keys")
builder.wait_for_unit("sshd")
client.succeed(f"ssh -o StrictHostKeyChecking=no {builder.name} 'echo hello world'")
# Create an SSH key on the client. # Perform a build and check that it was performed on the builder.
subprocess.run([ out = client.succeed(
"${hostPkgs.openssh}/bin/ssh-keygen", "-t", "ed25519", "-f", "key", "-N", "" "nix-build ${expr nodes.client 1} 2> build-output",
], capture_output=True, check=True) "grep -q Hello build-output"
client.succeed("mkdir -p -m 700 /root/.ssh") )
client.copy_from_host("key", "/root/.ssh/id_ed25519") builder1.succeed(f"test -e {out}")
client.succeed("chmod 600 /root/.ssh/id_ed25519")
# Install the SSH key on the builders. # And a parallel build.
client.wait_for_unit("network.target") paths = client.succeed(r'nix-store -r $(nix-instantiate ${expr nodes.client 2})\!out $(nix-instantiate ${expr nodes.client 3})\!out')
for builder in [builder1, builder2]: out1, out2 = paths.split()
builder.succeed("mkdir -p -m 700 /root/.ssh") builder1.succeed(f"test -e {out1} -o -e {out2}")
builder.copy_from_host("key.pub", "/root/.ssh/authorized_keys") builder2.succeed(f"test -e {out1} -o -e {out2}")
builder.wait_for_unit("sshd")
client.succeed(f"ssh -o StrictHostKeyChecking=no {builder.name} 'echo hello world'")
# Perform a build and check that it was performed on the builder. # And a failing build.
out = client.succeed( client.fail("nix-build ${expr nodes.client 5}")
"nix-build ${expr nodes.client.config 1} 2> build-output",
"grep -q Hello build-output"
)
builder1.succeed(f"test -e {out}")
# And a parallel build. # Test whether the build hook automatically skips unavailable builders.
paths = client.succeed(r'nix-store -r $(nix-instantiate ${expr nodes.client.config 2})\!out $(nix-instantiate ${expr nodes.client.config 3})\!out') builder1.block()
out1, out2 = paths.split() client.succeed("nix-build ${expr nodes.client 4}")
builder1.succeed(f"test -e {out1} -o -e {out2}") '';
builder2.succeed(f"test -e {out1} -o -e {out2}") };
# And a failing build.
client.fail("nix-build ${expr nodes.client.config 5}")
# Test whether the build hook automatically skips unavailable builders.
builder1.block()
client.succeed("nix-build ${expr nodes.client.config 4}")
'';
} }