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