at 23.05-pre 5.1 kB view raw
1{ system ? builtins.currentSystem, 2 config ? {}, 3 pkgs ? import ../.. { inherit system config; } 4}: 5 6with import ../lib/testing-python.nix { inherit system pkgs; }; 7with pkgs.lib; 8 9with import common/ec2.nix { inherit makeTest pkgs; }; 10 11let 12 imageCfg = (import ../lib/eval-config.nix { 13 inherit system; 14 modules = [ 15 ../maintainers/scripts/ec2/amazon-image.nix 16 ../modules/testing/test-instrumentation.nix 17 ../modules/profiles/qemu-guest.nix 18 { 19 ec2.hvm = true; 20 21 # Hack to make the partition resizing work in QEMU. 22 boot.initrd.postDeviceCommands = mkBefore '' 23 ln -s vda /dev/xvda 24 ln -s vda1 /dev/xvda1 25 ''; 26 27 # In a NixOS test the serial console is occupied by the "backdoor" 28 # (see testing/test-instrumentation.nix) and is incompatible with 29 # the configuration in virtualisation/amazon-image.nix. 30 systemd.services."serial-getty@ttyS0".enable = mkForce false; 31 32 # Needed by nixos-rebuild due to the lack of network 33 # access. Determined by trial and error. 34 system.extraDependencies = with pkgs; ( [ 35 # Needed for a nixos-rebuild. 36 busybox 37 cloud-utils 38 desktop-file-utils 39 libxslt.bin 40 mkinitcpio-nfs-utils 41 stdenv 42 stdenvNoCC 43 texinfo 44 unionfs-fuse 45 xorg.lndir 46 47 # These are used in the configure-from-userdata tests 48 # for EC2. Httpd and valgrind are requested by the 49 # configuration. 50 apacheHttpd 51 apacheHttpd.doc 52 apacheHttpd.man 53 valgrind.doc 54 ]); 55 } 56 ]; 57 }).config; 58 image = "${imageCfg.system.build.amazonImage}/${imageCfg.amazonImage.name}.vhd"; 59 60 sshKeys = import ./ssh-keys.nix pkgs; 61 snakeOilPrivateKey = sshKeys.snakeOilPrivateKey.text; 62 snakeOilPrivateKeyFile = pkgs.writeText "private-key" snakeOilPrivateKey; 63 snakeOilPublicKey = sshKeys.snakeOilPublicKey; 64 65in { 66 boot-ec2-nixops = makeEc2Test { 67 name = "nixops-userdata"; 68 inherit image; 69 sshPublicKey = snakeOilPublicKey; # That's right folks! My user's key is also the host key! 70 71 userData = '' 72 SSH_HOST_ED25519_KEY_PUB:${snakeOilPublicKey} 73 SSH_HOST_ED25519_KEY:${replaceStrings ["\n"] ["|"] snakeOilPrivateKey} 74 ''; 75 script = '' 76 machine.start() 77 machine.wait_for_file("/etc/ec2-metadata/user-data") 78 machine.wait_for_unit("sshd.service") 79 80 machine.succeed("grep unknown /etc/ec2-metadata/ami-manifest-path") 81 82 # We have no keys configured on the client side yet, so this should fail 83 machine.fail("ssh -o BatchMode=yes localhost exit") 84 85 # Let's install our client private key 86 machine.succeed("mkdir -p ~/.ssh") 87 88 machine.copy_from_host_via_shell( 89 "${snakeOilPrivateKeyFile}", "~/.ssh/id_ed25519" 90 ) 91 machine.succeed("chmod 600 ~/.ssh/id_ed25519") 92 93 # We haven't configured the host key yet, so this should still fail 94 machine.fail("ssh -o BatchMode=yes localhost exit") 95 96 # Add the host key; ssh should finally succeed 97 machine.succeed( 98 "echo localhost,127.0.0.1 ${snakeOilPublicKey} > ~/.ssh/known_hosts" 99 ) 100 machine.succeed("ssh -o BatchMode=yes localhost exit") 101 102 # Test whether the root disk was resized. 103 blocks, block_size = map(int, machine.succeed("stat -c %b:%S -f /").split(":")) 104 GB = 1024 ** 3 105 assert 9.7 * GB <= blocks * block_size <= 10 * GB 106 107 # Just to make sure resizing is idempotent. 108 machine.shutdown() 109 machine.start() 110 machine.wait_for_file("/etc/ec2-metadata/user-data") 111 ''; 112 }; 113 114 boot-ec2-config = makeEc2Test { 115 name = "config-userdata"; 116 meta.broken = true; # amazon-init wants to download from the internet while building the system 117 inherit image; 118 sshPublicKey = snakeOilPublicKey; 119 120 # ### https://nixos.org/channels/nixos-unstable nixos 121 userData = '' 122 { pkgs, ... }: 123 124 { 125 imports = [ 126 <nixpkgs/nixos/modules/virtualisation/amazon-image.nix> 127 <nixpkgs/nixos/modules/testing/test-instrumentation.nix> 128 <nixpkgs/nixos/modules/profiles/qemu-guest.nix> 129 ]; 130 environment.etc.testFile = { 131 text = "whoa"; 132 }; 133 134 networking.hostName = "ec2-test-vm"; # required by services.httpd 135 136 services.httpd = { 137 enable = true; 138 adminAddr = "test@example.org"; 139 virtualHosts.localhost.documentRoot = "''${pkgs.valgrind.doc}/share/doc/valgrind/html"; 140 }; 141 networking.firewall.allowedTCPPorts = [ 80 ]; 142 } 143 ''; 144 script = '' 145 machine.start() 146 147 # amazon-init must succeed. if it fails, make the test fail 148 # immediately instead of timing out in wait_for_file. 149 machine.wait_for_unit("amazon-init.service") 150 151 machine.wait_for_file("/etc/testFile") 152 assert "whoa" in machine.succeed("cat /etc/testFile") 153 154 machine.wait_for_unit("httpd.service") 155 assert "Valgrind" in machine.succeed("curl http://localhost") 156 ''; 157 }; 158}