|
| 1 | +# SPDX-FileCopyrightText: 2024 Serokell <https://serokell.io/> |
| 2 | +# |
| 3 | +# SPDX-License-Identifier: MPL-2.0 |
| 4 | + |
| 5 | +{ pkgs , inputs , ... }: |
| 6 | +let |
| 7 | + inherit (pkgs) system lib; |
| 8 | + |
| 9 | + inherit (import "${pkgs.path}/nixos/tests/ssh-keys.nix" pkgs) snakeOilPrivateKey; |
| 10 | + |
| 11 | + # Include all build dependencies to be able to build profiles offline |
| 12 | + allDrvOutputs = pkg: pkgs.runCommand "allDrvOutputs" { refs = pkgs.writeReferencesToFile pkg.drvPath; } '' |
| 13 | + touch $out |
| 14 | + while read ref; do |
| 15 | + case $ref in |
| 16 | + *.drv) |
| 17 | + cat $ref >>$out |
| 18 | + ;; |
| 19 | + esac |
| 20 | + done <$refs |
| 21 | + ''; |
| 22 | + |
| 23 | + mkTest = { name ? "", user ? "root", isLocal ? true, deployArgs }: let |
| 24 | + nodes = { |
| 25 | + server = { nodes, ... }: { |
| 26 | + imports = [ |
| 27 | + ./server.nix |
| 28 | + (import ./common.nix { inherit inputs pkgs; }) |
| 29 | + ]; |
| 30 | + virtualisation.additionalPaths = lib.optionals (!isLocal) [ |
| 31 | + pkgs.hello |
| 32 | + pkgs.figlet |
| 33 | + (allDrvOutputs nodes.server.system.build.toplevel) |
| 34 | + pkgs.deploy-rs.deploy-rs |
| 35 | + ]; |
| 36 | + }; |
| 37 | + client = { nodes, ... }: { |
| 38 | + imports = [ (import ./common.nix { inherit inputs pkgs; }) ]; |
| 39 | + environment.systemPackages = [ pkgs.deploy-rs.deploy-rs ]; |
| 40 | + virtualisation.additionalPaths = lib.optionals isLocal [ |
| 41 | + pkgs.hello |
| 42 | + pkgs.figlet |
| 43 | + (allDrvOutputs nodes.server.system.build.toplevel) |
| 44 | + ]; |
| 45 | + }; |
| 46 | + }; |
| 47 | + |
| 48 | + flakeInputs = '' |
| 49 | + deploy-rs.url = "${../..}"; |
| 50 | + deploy-rs.inputs.utils.follows = "utils"; |
| 51 | + deploy-rs.inputs.flake-compat.follows = "flake-compat"; |
| 52 | +
|
| 53 | + nixpkgs.url = "${inputs.nixpkgs}"; |
| 54 | + utils.url = "${inputs.utils}"; |
| 55 | + utils.inputs.systems.follows = "systems"; |
| 56 | + systems.url = "${inputs.utils.inputs.systems}"; |
| 57 | + flake-compat.url = "${inputs.flake-compat}"; |
| 58 | + flake-compat.flake = false; |
| 59 | + ''; |
| 60 | + |
| 61 | + flake = builtins.toFile "flake.nix" |
| 62 | + (lib.replaceStrings [ "##inputs##" ] [ flakeInputs ] (builtins.readFile ./deploy-flake.nix)); |
| 63 | + |
| 64 | + in pkgs.nixosTest { |
| 65 | + inherit nodes name; |
| 66 | + |
| 67 | + testScript = { nodes }: let |
| 68 | + serverNetworkJSON = pkgs.writeText "server-network.json" |
| 69 | + (builtins.toJSON nodes.server.system.build.networkConfig); |
| 70 | + in '' |
| 71 | + start_all() |
| 72 | +
|
| 73 | + # Prepare |
| 74 | + client.succeed("mkdir tmp && cd tmp") |
| 75 | + client.succeed("cp ${flake} ./flake.nix") |
| 76 | + client.succeed("cp ${./server.nix} ./server.nix") |
| 77 | + client.succeed("cp ${./common.nix} ./common.nix") |
| 78 | + client.succeed("cp ${serverNetworkJSON} ./network.json") |
| 79 | + client.succeed("nix flake lock") |
| 80 | +
|
| 81 | +
|
| 82 | + # Setup SSH key |
| 83 | + client.succeed("mkdir -m 700 /root/.ssh") |
| 84 | + client.succeed('cp --no-preserve=mode ${snakeOilPrivateKey} /root/.ssh/id_ed25519') |
| 85 | + client.succeed("chmod 600 /root/.ssh/id_ed25519") |
| 86 | +
|
| 87 | + # Test SSH connection |
| 88 | + server.wait_for_open_port(22) |
| 89 | + client.wait_for_unit("network.target") |
| 90 | + client.succeed( |
| 91 | + "ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no server 'echo hello world' >&2", |
| 92 | + timeout=30 |
| 93 | + ) |
| 94 | +
|
| 95 | + # Make sure the hello and figlet packages are missing |
| 96 | + server.fail("su ${user} -l -c 'hello | figlet'") |
| 97 | +
|
| 98 | + # Deploy to the server |
| 99 | + client.succeed("deploy ${deployArgs}") |
| 100 | +
|
| 101 | + # Make sure packages are present after deployment |
| 102 | + server.succeed("su ${user} -l -c 'hello | figlet' >&2") |
| 103 | + ''; |
| 104 | + }; |
| 105 | +in { |
| 106 | + # Deployment with client-side build |
| 107 | + local-build = mkTest { |
| 108 | + name = "local-build"; |
| 109 | + deployArgs = "-s .#server -- --offline"; |
| 110 | + }; |
| 111 | + # Deployment with server-side build |
| 112 | + remote-build = mkTest { |
| 113 | + name = "remote-build"; |
| 114 | + isLocal = false; |
| 115 | + deployArgs = "-s .#server --remote-build -- --offline"; |
| 116 | + }; |
| 117 | + # Deployment with overridden options |
| 118 | + options-overriding = mkTest { |
| 119 | + name = "options-overriding"; |
| 120 | + deployArgs = lib.concatStrings [ |
| 121 | + "-s .#server-override" |
| 122 | + " --hostname server --profile-user root --ssh-user root --sudo 'sudo -u'" |
| 123 | + " --ssh-opts='-o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null'" |
| 124 | + " --confirm-timeout 30 --activation-timeout 30" |
| 125 | + " -- --offline" |
| 126 | + ]; |
| 127 | + }; |
| 128 | + # User profile deployment |
| 129 | + profile = mkTest { |
| 130 | + name = "profile"; |
| 131 | + user = "deploy"; |
| 132 | + deployArgs = "-s .#profile -- --offline"; |
| 133 | + }; |
| 134 | +} |
0 commit comments