1{ pkgs, ... }:
2let
3 inherit (pkgs) lib;
4
5 tests = {
6 # TODO: uncomment stable
7 # - Blocked on https://github.com/NixOS/nixpkgs/issues/138584 which has a
8 # PR in staging: https://github.com/NixOS/nixpkgs/pull/139986
9 # - Alternatively, blocked on a NixOps 2 release
10 # https://github.com/NixOS/nixops/issues/1242
11 # stable = testsLegacyNetwork { nixopsPkg = pkgs.nixops; };
12 unstable = testsForPackage { nixopsPkg = pkgs.nixops_unstable_minimal; };
13
14 # inherit testsForPackage;
15 };
16
17 testsForPackage =
18 args:
19 lib.recurseIntoAttrs {
20 legacyNetwork = testLegacyNetwork args;
21 passthru.override = args': testsForPackage (args // args');
22 };
23
24 testLegacyNetwork =
25 { nixopsPkg, ... }:
26 pkgs.testers.nixosTest ({
27 name = "nixops-legacy-network";
28 nodes = {
29 deployer =
30 {
31 config,
32 lib,
33 nodes,
34 pkgs,
35 ...
36 }:
37 {
38 imports = [ ../../modules/installer/cd-dvd/channel.nix ];
39 environment.systemPackages = [ nixopsPkg ];
40 nix.settings.substituters = lib.mkForce [ ];
41 users.users.person.isNormalUser = true;
42 virtualisation.writableStore = true;
43 virtualisation.additionalPaths = [
44 pkgs.hello
45 pkgs.figlet
46 ];
47 virtualisation.memorySize = 2048;
48
49 # TODO: make this efficient, https://github.com/NixOS/nixpkgs/issues/180529
50 system.includeBuildDependencies = true;
51 };
52 server =
53 { lib, ... }:
54 {
55 imports = [ ./legacy/base-configuration.nix ];
56 };
57 };
58
59 testScript =
60 { nodes }:
61 let
62 deployerSetup = pkgs.writeScript "deployerSetup" ''
63 #!${pkgs.runtimeShell}
64 set -eux -o pipefail
65 cp --no-preserve=mode -r ${./legacy} unicorn
66 cp --no-preserve=mode ${../ssh-keys.nix} unicorn/ssh-keys.nix
67 mkdir -p ~/.ssh
68 cp ${snakeOilPrivateKey} ~/.ssh/id_ed25519
69 chmod 0400 ~/.ssh/id_ed25519
70 '';
71 serverNetworkJSON = pkgs.writeText "server-network.json" (
72 builtins.toJSON nodes.server.system.build.networkConfig
73 );
74 in
75 ''
76 import shlex
77
78 def deployer_do(cmd):
79 cmd = shlex.quote(cmd)
80 return deployer.succeed(f"su person -l -c {cmd} &>/dev/console")
81
82 start_all()
83
84 deployer_do("cat /etc/hosts")
85
86 deployer_do("${deployerSetup}")
87 deployer_do("cp ${serverNetworkJSON} unicorn/server-network.json")
88
89 # Establish that ssh works, regardless of nixops
90 # Easy way to accept the server host key too.
91 server.wait_for_open_port(22)
92 deployer.wait_for_unit("network.target")
93
94 # Put newlines on console, to flush the console reader's line buffer
95 # in case nixops' last output did not end in a newline, as is the case
96 # with a status line (if implemented?)
97 deployer.succeed("while sleep 60s; do echo [60s passed]; done >&2 &")
98
99 deployer_do("cd ~/unicorn; ssh -oStrictHostKeyChecking=accept-new root@server echo hi")
100
101 # Create and deploy
102 deployer_do("cd ~/unicorn; nixops create")
103
104 deployer_do("cd ~/unicorn; nixops deploy --confirm")
105
106 deployer_do("cd ~/unicorn; nixops ssh server 'hello | figlet'")
107 '';
108 });
109
110 inherit (import ../ssh-keys.nix pkgs) snakeOilPrivateKey snakeOilPublicKey;
111
112in
113tests