summaryrefslogtreecommitdiffstats
path: root/nixos/tests/common/ec2.nix
blob: f28e094d455b40a6a5f67e99372dafcf2fdd50fb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
{ pkgs, makeTest }:

with pkgs.lib;

let
  imdsServer = import ./imds-server.nix { inherit pkgs; };
in
{
  inherit imdsServer;

  makeEc2Test =
    {
      name,
      image,
      userData ? null,
      script,
      hostname ? "ec2-instance",
      sshPublicKey ? null,
      meta ? { },
    }:
    let
      metaData = pkgs.stdenv.mkDerivation {
        name = "metadata";
        buildCommand = ''
          mkdir -p $out/1.0/meta-data
          ${optionalString (
            userData != null
          ) "ln -s ${pkgs.writeText "userData" userData} $out/1.0/user-data"}
          ${optionalString (userData == null) "touch $out/1.0/user-data"}
          echo "${hostname}" > $out/1.0/meta-data/hostname
          echo "(unknown)" > $out/1.0/meta-data/ami-manifest-path
          echo "i-1234567890abcdef0" > $out/1.0/meta-data/instance-id
        ''
        + optionalString (sshPublicKey != null) ''
          mkdir -p $out/1.0/meta-data/public-keys/0
          ln -s ${pkgs.writeText "sshPublicKey" sshPublicKey} $out/1.0/meta-data/public-keys/0/openssh-key
        '';
      };
      indentLines = str: concatLines (map (s: "  " + s) (splitString "\n" str));
    in
    makeTest {
      name = "ec2-" + name;
      nodes = { };
      testScript = ''
        import os
        import subprocess
        import tempfile

        image_dir = os.path.join(
            os.environ.get("TMPDIR", tempfile.gettempdir()), "tmp", "vm-state-machine"
        )
        os.makedirs(image_dir, mode=0o700, exist_ok=True)
        disk_image = os.path.join(image_dir, "machine.qcow2")
        QEMU_BIN = "${pkgs.qemu}"
        QEMU_IMG = f"{QEMU_BIN}/bin/qemu-img"
        subprocess.check_call(
            [
                QEMU_IMG,
                "create",
                "-f",
                "qcow2",
                "-F",
                "qcow2",
                "-o",
                "backing_file=${image}",
                disk_image,
            ]
        )
        subprocess.check_call([QEMU_IMG, "resize", disk_image, "10G"])

        # Note: we use net=169.0.0.0/8 rather than
        # net=169.254.0.0/16 to prevent dhcpcd from getting horribly
        # confused. (It would get a DHCP lease in the 169.254.*
        # range, which it would then configure and promptly delete
        # again when it deletes link-local addresses.) Ideally we'd
        # turn off the DHCP server, but qemu does not have an option
        # to do that.
        start_command = (
            f"{QEMU_BIN}/bin/qemu-kvm -m 1024"
            + " -device virtio-net-pci,netdev=vlan0"
            + " -netdev 'user,id=vlan0,net=169.0.0.0/8,guestfwd=tcp:169.254.169.254:80-cmd:${getExe imdsServer} ${metaData}'"
            + f" -drive file={disk_image},if=virtio,werror=report"
            + " $QEMU_OPTS"
        )

        machine = create_machine(start_command)
        try:
      ''
      + indentLines script
      + ''
        finally:
          machine.shutdown()
      '';

      inherit meta;
    };
}