at 25.11-pre 2.7 kB view raw
1{ pkgs, makeTest }: 2 3with pkgs.lib; 4 5{ 6 makeEc2Test = 7 { 8 name, 9 image, 10 userData, 11 script, 12 hostname ? "ec2-instance", 13 sshPublicKey ? null, 14 meta ? { }, 15 }: 16 let 17 metaData = pkgs.stdenv.mkDerivation { 18 name = "metadata"; 19 buildCommand = 20 '' 21 mkdir -p $out/1.0/meta-data 22 ln -s ${pkgs.writeText "userData" userData} $out/1.0/user-data 23 echo "${hostname}" > $out/1.0/meta-data/hostname 24 echo "(unknown)" > $out/1.0/meta-data/ami-manifest-path 25 '' 26 + optionalString (sshPublicKey != null) '' 27 mkdir -p $out/1.0/meta-data/public-keys/0 28 ln -s ${pkgs.writeText "sshPublicKey" sshPublicKey} $out/1.0/meta-data/public-keys/0/openssh-key 29 ''; 30 }; 31 indentLines = str: concatLines (map (s: " " + s) (splitString "\n" str)); 32 in 33 makeTest { 34 name = "ec2-" + name; 35 nodes = { }; 36 testScript = 37 '' 38 import os 39 import subprocess 40 import tempfile 41 42 image_dir = os.path.join( 43 os.environ.get("TMPDIR", tempfile.gettempdir()), "tmp", "vm-state-machine" 44 ) 45 os.makedirs(image_dir, mode=0o700, exist_ok=True) 46 disk_image = os.path.join(image_dir, "machine.qcow2") 47 subprocess.check_call( 48 [ 49 "qemu-img", 50 "create", 51 "-f", 52 "qcow2", 53 "-F", 54 "qcow2", 55 "-o", 56 "backing_file=${image}", 57 disk_image, 58 ] 59 ) 60 subprocess.check_call(["qemu-img", "resize", disk_image, "10G"]) 61 62 # Note: we use net=169.0.0.0/8 rather than 63 # net=169.254.0.0/16 to prevent dhcpcd from getting horribly 64 # confused. (It would get a DHCP lease in the 169.254.* 65 # range, which it would then configure and promptly delete 66 # again when it deletes link-local addresses.) Ideally we'd 67 # turn off the DHCP server, but qemu does not have an option 68 # to do that. 69 start_command = ( 70 "qemu-kvm -m 1024" 71 + " -device virtio-net-pci,netdev=vlan0" 72 + " -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}'" 73 + f" -drive file={disk_image},if=virtio,werror=report" 74 + " $QEMU_OPTS" 75 ) 76 77 machine = create_machine(start_command) 78 try: 79 '' 80 + indentLines script 81 + '' 82 finally: 83 machine.shutdown() 84 ''; 85 86 inherit meta; 87 }; 88}