| 1 | { pkgs }: |
| 2 | pkgs.testers.runNixOSTest { |
| 3 | name = "nomad-upgrade"; |
| 4 | globalTimeout = 600; |
| 5 | nodes.machine = { lib, ... }: { |
| 6 | virtualisation.memorySize = 2048; |
| 7 | services.nomad = { |
| 8 | enable = true; |
| 9 | package = pkgs.nomad_1_11; |
| 10 | enableDocker = false; |
| 11 | dropPrivileges = false; |
| 12 | settings = { |
| 13 | bind_addr = "127.0.0.1"; |
| 14 | advertise = { http = "127.0.0.1"; rpc = "127.0.0.1"; serf = "127.0.0.1"; }; |
| 15 | server = { enabled = true; bootstrap_expect = 1; }; |
| 16 | client.enabled = true; |
| 17 | acl.enabled = true; |
| 18 | plugin.raw_exec.config.enabled = true; |
| 19 | }; |
| 20 | }; |
| 21 | specialisation.patched.configuration.services.nomad.package = lib.mkForce pkgs.nomad; |
| 22 | environment.systemPackages = [ pkgs.python3 ]; |
| 23 | }; |
| 24 | testScript = '' |
| 25 | import json |
| 26 | start_all() |
| 27 | machine.wait_for_unit("nomad.service") |
| 28 | machine.wait_until_succeeds("umask 077; nomad acl bootstrap -json > /run/bootstrap.json", timeout=60) |
| 29 | machine.succeed("python3 -c \"import json,pathlib; pathlib.Path('/run/nomad-token').write_text(json.load(open('/run/bootstrap.json'))['SecretID'])\"; chmod 600 /run/nomad-token") |
| 30 | machine.succeed("NOMAD_TOKEN=$(cat /run/nomad-token) nomad var put nomad/jobs/fixture proof=synthetic") |
| 31 | job = {"ID": "fixture", "Name": "fixture", "Type": "service", "Datacenters": ["dc1"], "TaskGroups": [{"Name": "fixture", "Count": 1, "Tasks": [{"Name": "fixture", "Driver": "raw_exec", "Config": {"command": "${pkgs.coreutils}/bin/sleep", "args": ["infinity"]}, "Resources": {"CPU": 100, "MemoryMB": 32}}]}]} |
| 32 | machine.succeed("cat > /run/fixture.json <<'EOF'\n" + json.dumps(job) + "\nEOF") |
| 33 | machine.succeed("NOMAD_TOKEN=$(cat /run/nomad-token) nomad job run -json -detach /run/fixture.json") |
| 34 | machine.wait_until_succeeds("NOMAD_TOKEN=$(cat /run/nomad-token) nomad job allocs -json fixture | python3 -c \"import json,sys; a=json.load(sys.stdin); assert len(a) == 1 and a[0]['ClientStatus'] == 'running'\"", timeout=60) |
| 35 | allocation = machine.succeed("NOMAD_TOKEN=$(cat /run/nomad-token) nomad job allocs -json fixture") |
| 36 | machine.succeed("NOMAD_TOKEN=$(cat /run/nomad-token) nomad operator snapshot save /run/before.snap") |
| 37 | machine.succeed("/run/current-system/specialisation/patched/bin/switch-to-configuration test") |
| 38 | machine.wait_for_unit("nomad.service") |
| 39 | machine.wait_until_succeeds("NOMAD_TOKEN=$(cat /run/nomad-token) nomad status", timeout=60) |
| 40 | assert machine.succeed("nomad version").splitlines()[0] == "Nomad v2.1.1" |
| 41 | machine.succeed("NOMAD_TOKEN=$(cat /run/nomad-token) nomad var get -out=json nomad/jobs/fixture | python3 -c \"import json,sys; assert json.load(sys.stdin)['Items'] == {'proof':'synthetic'}\"") |
| 42 | after = machine.succeed("NOMAD_TOKEN=$(cat /run/nomad-token) nomad job allocs -json fixture") |
| 43 | assert {a['ID'] for a in json.loads(allocation)} == {a['ID'] for a in json.loads(after)} |
| 44 | assert all(a['ClientStatus'] == 'running' for a in json.loads(after)) |
| 45 | machine.succeed("NOMAD_TOKEN=$(cat /run/nomad-token) nomad operator snapshot save /run/after.snap") |
| 46 | print("Nomad upgrade preserved ACLs, variables, the running allocation, and snapshot access") |
| 47 | ''; |
| 48 | } |