python310Packages.pydeconz: 104 -> 105
[NixPkgs.git] / nixos / tests / common / ec2.nix
blob64b0a91ac1f721986fb4aac1b4b6c7b2abe070c0
1 { pkgs, makeTest }:
3 with pkgs.lib;
6   makeEc2Test = { name, image, userData, script, hostname ? "ec2-instance", sshPublicKey ? null, meta ? {} }:
7     let
8       metaData = pkgs.stdenv.mkDerivation {
9         name = "metadata";
10         buildCommand = ''
11           mkdir -p $out/1.0/meta-data
12           ln -s ${pkgs.writeText "userData" userData} $out/1.0/user-data
13           echo "${hostname}" > $out/1.0/meta-data/hostname
14           echo "(unknown)" > $out/1.0/meta-data/ami-manifest-path
15         '' + optionalString (sshPublicKey != null) ''
16           mkdir -p $out/1.0/meta-data/public-keys/0
17           ln -s ${pkgs.writeText "sshPublicKey" sshPublicKey} $out/1.0/meta-data/public-keys/0/openssh-key
18         '';
19       };
20     in makeTest {
21       name = "ec2-" + name;
22       nodes = {};
23       testScript = ''
24         import os
25         import subprocess
26         import tempfile
28         image_dir = os.path.join(
29             os.environ.get("TMPDIR", tempfile.gettempdir()), "tmp", "vm-state-machine"
30         )
31         os.makedirs(image_dir, mode=0o700, exist_ok=True)
32         disk_image = os.path.join(image_dir, "machine.qcow2")
33         subprocess.check_call(
34             [
35                 "qemu-img",
36                 "create",
37                 "-f",
38                 "qcow2",
39                 "-o",
40                 "backing_file=${image}",
41                 disk_image,
42             ]
43         )
44         subprocess.check_call(["qemu-img", "resize", disk_image, "10G"])
46         # Note: we use net=169.0.0.0/8 rather than
47         # net=169.254.0.0/16 to prevent dhcpcd from getting horribly
48         # confused. (It would get a DHCP lease in the 169.254.*
49         # range, which it would then configure and prompty delete
50         # again when it deletes link-local addresses.) Ideally we'd
51         # turn off the DHCP server, but qemu does not have an option
52         # to do that.
53         start_command = (
54             "qemu-kvm -m 1024"
55             + " -device virtio-net-pci,netdev=vlan0"
56             + " -netdev 'user,id=vlan0,net=169.0.0.0/8,guestfwd=tcp:169.254.169.254:80-cmd:${pkgs.micro-httpd}/bin/micro_httpd ${metaData}'"
57             + f" -drive file={disk_image},if=virtio,werror=report"
58             + " $QEMU_OPTS"
59         )
61         machine = create_machine({"startCommand": start_command})
62       '' + script;
64       inherit meta;
65     };