at 23.11-pre 2.6 kB view raw
1{ pkgs, makeTest }: 2 3with pkgs.lib; 4 5{ 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 indentLines = str: concatLines (map (s: " " + s) (splitString "\n" str)); 21 in makeTest { 22 name = "ec2-" + name; 23 nodes = {}; 24 testScript = '' 25 import os 26 import subprocess 27 import tempfile 28 29 image_dir = os.path.join( 30 os.environ.get("TMPDIR", tempfile.gettempdir()), "tmp", "vm-state-machine" 31 ) 32 os.makedirs(image_dir, mode=0o700, exist_ok=True) 33 disk_image = os.path.join(image_dir, "machine.qcow2") 34 subprocess.check_call( 35 [ 36 "qemu-img", 37 "create", 38 "-f", 39 "qcow2", 40 "-F", 41 "qcow2", 42 "-o", 43 "backing_file=${image}", 44 disk_image, 45 ] 46 ) 47 subprocess.check_call(["qemu-img", "resize", disk_image, "10G"]) 48 49 # Note: we use net=169.0.0.0/8 rather than 50 # net=169.254.0.0/16 to prevent dhcpcd from getting horribly 51 # confused. (It would get a DHCP lease in the 169.254.* 52 # range, which it would then configure and promptly delete 53 # again when it deletes link-local addresses.) Ideally we'd 54 # turn off the DHCP server, but qemu does not have an option 55 # to do that. 56 start_command = ( 57 "qemu-kvm -m 1024" 58 + " -device virtio-net-pci,netdev=vlan0" 59 + " -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}'" 60 + f" -drive file={disk_image},if=virtio,werror=report" 61 + " $QEMU_OPTS" 62 ) 63 64 machine = create_machine({"startCommand": start_command}) 65 try: 66 '' + indentLines script + '' 67 finally: 68 machine.shutdown() 69 ''; 70 71 inherit meta; 72 }; 73}