nixosTests.ec2: Port tests that depend on common/ec2.nix

This commit is contained in:
Jacek Galowicz 2020-02-02 09:48:11 +01:00 committed by Florian Klink
parent 4555623f39
commit c32369676b
3 changed files with 141 additions and 120 deletions

View File

@ -20,13 +20,27 @@ with pkgs.lib;
in makeTest { in makeTest {
name = "ec2-" + name; name = "ec2-" + name;
nodes = {}; nodes = {};
testScript = testScript = ''
'' import os
my $imageDir = ($ENV{'TMPDIR'} // "/tmp") . "/vm-state-machine"; import subprocess
mkdir $imageDir, 0700;
my $diskImage = "$imageDir/machine.qcow2"; image_dir = os.path.join(
system("qemu-img create -f qcow2 -o backing_file=${image} $diskImage") == 0 or die; os.environ.get("TMPDIR", tempfile.gettempdir()), "tmp", "vm-state-machine"
system("qemu-img resize $diskImage 10G") == 0 or die; )
os.makedirs(image_dir, mode=0o700, exist_ok=True)
disk_image = os.path.join(image_dir, "machine.qcow2")
subprocess.check_call(
[
"qemu-img",
"create",
"-f",
"qcow2",
"-o",
"backing_file=${image}",
disk_image,
]
)
subprocess.check_call(["qemu-img", "resize", disk_image, "10G"])
# Note: we use net=169.0.0.0/8 rather than # Note: we use net=169.0.0.0/8 rather than
# net=169.254.0.0/16 to prevent dhcpcd from getting horribly # net=169.254.0.0/16 to prevent dhcpcd from getting horribly
@ -35,15 +49,15 @@ with pkgs.lib;
# again when it deletes link-local addresses.) Ideally we'd # again when it deletes link-local addresses.) Ideally we'd
# turn off the DHCP server, but qemu does not have an option # turn off the DHCP server, but qemu does not have an option
# to do that. # to do that.
my $startCommand = "qemu-kvm -m 1024"; start_command = (
$startCommand .= " -device virtio-net-pci,netdev=vlan0"; "qemu-kvm -m 1024"
$startCommand .= " -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}'"; + " -device virtio-net-pci,netdev=vlan0"
$startCommand .= " -drive file=$diskImage,if=virtio,werror=report"; + " -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}'"
$startCommand .= " \$QEMU_OPTS"; + f" -drive file={disk_image},if=virtio,werror=report"
+ " $QEMU_OPTS"
)
my $machine = createMachine({ startCommand => $startCommand }); machine = create_machine({"startCommand": start_command})
'' + script;
${script}
'';
}; };
} }

View File

@ -3,51 +3,50 @@
pkgs ? import ../.. { inherit system config; } pkgs ? import ../.. { inherit system config; }
}: }:
with import ../lib/testing.nix { inherit system pkgs; }; with import ../lib/testing-python.nix { inherit system pkgs; };
with pkgs.lib; with pkgs.lib;
with import common/ec2.nix { inherit makeTest pkgs; }; with import common/ec2.nix { inherit makeTest pkgs; };
let let
imageCfg = imageCfg = (import ../lib/eval-config.nix {
(import ../lib/eval-config.nix {
inherit system; inherit system;
modules = [ modules = [
../maintainers/scripts/ec2/amazon-image.nix ../maintainers/scripts/ec2/amazon-image.nix
../modules/testing/test-instrumentation.nix ../modules/testing/test-instrumentation.nix
../modules/profiles/qemu-guest.nix ../modules/profiles/qemu-guest.nix
{ ec2.hvm = true; {
ec2.hvm = true;
# Hack to make the partition resizing work in QEMU. # Hack to make the partition resizing work in QEMU.
boot.initrd.postDeviceCommands = mkBefore boot.initrd.postDeviceCommands = mkBefore ''
''
ln -s vda /dev/xvda ln -s vda /dev/xvda
ln -s vda1 /dev/xvda1 ln -s vda1 /dev/xvda1
''; '';
# Needed by nixos-rebuild due to the lack of network # Needed by nixos-rebuild due to the lack of network
# access. Determined by trial and error. # access. Determined by trial and error.
system.extraDependencies = system.extraDependencies = with pkgs; ( [
with pkgs; (
[
# Needed for a nixos-rebuild. # Needed for a nixos-rebuild.
busybox busybox
stdenv
stdenvNoCC
mkinitcpio-nfs-utils
unionfs-fuse
cloud-utils cloud-utils
desktop-file-utils desktop-file-utils
texinfo
libxslt.bin libxslt.bin
mkinitcpio-nfs-utils
stdenv
stdenvNoCC
texinfo
unionfs-fuse
xorg.lndir xorg.lndir
# These are used in the configure-from-userdata tests # These are used in the configure-from-userdata tests
# for EC2. Httpd and valgrind are requested by the # for EC2. Httpd and valgrind are requested by the
# configuration. # configuration.
apacheHttpd apacheHttpd.doc apacheHttpd.man valgrind.doc apacheHttpd
] apacheHttpd.doc
); apacheHttpd.man
valgrind.doc
]);
} }
]; ];
}).config; }).config;
@ -55,6 +54,7 @@ let
sshKeys = import ./ssh-keys.nix pkgs; sshKeys = import ./ssh-keys.nix pkgs;
snakeOilPrivateKey = sshKeys.snakeOilPrivateKey.text; snakeOilPrivateKey = sshKeys.snakeOilPrivateKey.text;
snakeOilPrivateKeyFile = pkgs.writeText "private-key" snakeOilPrivateKey;
snakeOilPublicKey = sshKeys.snakeOilPublicKey; snakeOilPublicKey = sshKeys.snakeOilPublicKey;
in { in {
@ -68,38 +68,41 @@ in {
SSH_HOST_ED25519_KEY:${replaceStrings ["\n"] ["|"] snakeOilPrivateKey} SSH_HOST_ED25519_KEY:${replaceStrings ["\n"] ["|"] snakeOilPrivateKey}
''; '';
script = '' script = ''
$machine->start; machine.start()
$machine->waitForFile("/etc/ec2-metadata/user-data"); machine.wait_for_file("/etc/ec2-metadata/user-data")
$machine->waitForUnit("sshd.service"); machine.wait_for_unit("sshd.service")
$machine->succeed("grep unknown /etc/ec2-metadata/ami-manifest-path"); machine.succeed("grep unknown /etc/ec2-metadata/ami-manifest-path")
# We have no keys configured on the client side yet, so this should fail # We have no keys configured on the client side yet, so this should fail
$machine->fail("ssh -o BatchMode=yes localhost exit"); machine.fail("ssh -o BatchMode=yes localhost exit")
# Let's install our client private key # Let's install our client private key
$machine->succeed("mkdir -p ~/.ssh"); machine.succeed("mkdir -p ~/.ssh")
$machine->succeed("echo '${snakeOilPrivateKey}' > ~/.ssh/id_ed25519"); machine.copy_from_host_via_shell(
$machine->succeed("chmod 600 ~/.ssh/id_ed25519"); "${snakeOilPrivateKeyFile}", "~/.ssh/id_ed25519"
)
machine.succeed("chmod 600 ~/.ssh/id_ed25519")
# We haven't configured the host key yet, so this should still fail # We haven't configured the host key yet, so this should still fail
$machine->fail("ssh -o BatchMode=yes localhost exit"); machine.fail("ssh -o BatchMode=yes localhost exit")
# Add the host key; ssh should finally succeed # Add the host key; ssh should finally succeed
$machine->succeed("echo localhost,127.0.0.1 ${snakeOilPublicKey} > ~/.ssh/known_hosts"); machine.succeed(
$machine->succeed("ssh -o BatchMode=yes localhost exit"); "echo localhost,127.0.0.1 ${snakeOilPublicKey} > ~/.ssh/known_hosts"
)
machine.succeed("ssh -o BatchMode=yes localhost exit")
# Test whether the root disk was resized. # Test whether the root disk was resized.
my $blocks = $machine->succeed("stat -c %b -f /"); blocks, block_size = map(int, machine.succeed("stat -c %b:%S -f /").split(":"))
my $bsize = $machine->succeed("stat -c %S -f /"); GB = 1024 ** 3
my $size = $blocks * $bsize; assert 9.7 * GB <= blocks * block_size <= 10 * GB
die "wrong free space $size" if $size < 9.7 * 1024 * 1024 * 1024 || $size > 10 * 1024 * 1024 * 1024;
# Just to make sure resizing is idempotent. # Just to make sure resizing is idempotent.
$machine->shutdown; machine.shutdown()
$machine->start; machine.start()
$machine->waitForFile("/etc/ec2-metadata/user-data"); machine.wait_for_file("/etc/ec2-metadata/user-data")
''; '';
}; };
@ -133,17 +136,17 @@ in {
} }
''; '';
script = '' script = ''
$machine->start; machine.start()
# amazon-init must succeed. if it fails, make the test fail # amazon-init must succeed. if it fails, make the test fail
# immediately instead of timing out in waitForFile. # immediately instead of timing out in wait_for_file.
$machine->waitForUnit('amazon-init.service'); machine.wait_for_unit("amazon-init.service")
$machine->waitForFile("/etc/testFile"); machine.wait_for_file("/etc/testFile")
$machine->succeed("cat /etc/testFile | grep -q 'whoa'"); assert "whoa" in machine.succeed("cat /etc/testFile")
$machine->waitForUnit("httpd.service"); machine.wait_for_unit("httpd.service")
$machine->succeed("curl http://localhost | grep Valgrind"); assert "Valgrind" in machine.succeed("curl http://localhost")
''; '';
}; };
} }

View File

@ -3,14 +3,13 @@
pkgs ? import ../.. { inherit system config; } pkgs ? import ../.. { inherit system config; }
}: }:
with import ../lib/testing.nix { inherit system pkgs; }; with import ../lib/testing-python.nix { inherit system pkgs; };
with pkgs.lib; with pkgs.lib;
with import common/ec2.nix { inherit makeTest pkgs; }; with import common/ec2.nix { inherit makeTest pkgs; };
let let
image = image = (import ../lib/eval-config.nix {
(import ../lib/eval-config.nix {
inherit system; inherit system;
modules = [ modules = [
../maintainers/scripts/openstack/openstack-image.nix ../maintainers/scripts/openstack/openstack-image.nix
@ -27,6 +26,7 @@ let
sshKeys = import ./ssh-keys.nix pkgs; sshKeys = import ./ssh-keys.nix pkgs;
snakeOilPrivateKey = sshKeys.snakeOilPrivateKey.text; snakeOilPrivateKey = sshKeys.snakeOilPrivateKey.text;
snakeOilPrivateKeyFile = pkgs.writeText "private-key" snakeOilPrivateKey;
snakeOilPublicKey = sshKeys.snakeOilPublicKey; snakeOilPublicKey = sshKeys.snakeOilPublicKey;
in { in {
@ -39,32 +39,36 @@ in {
SSH_HOST_ED25519_KEY:${replaceStrings ["\n"] ["|"] snakeOilPrivateKey} SSH_HOST_ED25519_KEY:${replaceStrings ["\n"] ["|"] snakeOilPrivateKey}
''; '';
script = '' script = ''
$machine->start; machine.start()
$machine->waitForFile("/etc/ec2-metadata/user-data"); machine.wait_for_file("/etc/ec2-metadata/user-data")
$machine->waitForUnit("sshd.service"); machine.wait_for_unit("sshd.service")
$machine->succeed("grep unknown /etc/ec2-metadata/ami-manifest-path"); machine.succeed("grep unknown /etc/ec2-metadata/ami-manifest-path")
# We have no keys configured on the client side yet, so this should fail # We have no keys configured on the client side yet, so this should fail
$machine->fail("ssh -o BatchMode=yes localhost exit"); machine.fail("ssh -o BatchMode=yes localhost exit")
# Let's install our client private key # Let's install our client private key
$machine->succeed("mkdir -p ~/.ssh"); machine.succeed("mkdir -p ~/.ssh")
$machine->succeed("echo '${snakeOilPrivateKey}' > ~/.ssh/id_ed25519"); machine.copy_from_host_via_shell(
$machine->succeed("chmod 600 ~/.ssh/id_ed25519"); "${snakeOilPrivateKeyFile}", "~/.ssh/id_ed25519"
)
machine.succeed("chmod 600 ~/.ssh/id_ed25519")
# We haven't configured the host key yet, so this should still fail # We haven't configured the host key yet, so this should still fail
$machine->fail("ssh -o BatchMode=yes localhost exit"); machine.fail("ssh -o BatchMode=yes localhost exit")
# Add the host key; ssh should finally succeed # Add the host key; ssh should finally succeed
$machine->succeed("echo localhost,127.0.0.1 ${snakeOilPublicKey} > ~/.ssh/known_hosts"); machine.succeed(
$machine->succeed("ssh -o BatchMode=yes localhost exit"); "echo localhost,127.0.0.1 ${snakeOilPublicKey} > ~/.ssh/known_hosts"
)
machine.succeed("ssh -o BatchMode=yes localhost exit")
# Just to make sure resizing is idempotent. # Just to make sure resizing is idempotent.
$machine->shutdown; machine.shutdown()
$machine->start; machine.start()
$machine->waitForFile("/etc/ec2-metadata/user-data"); machine.wait_for_file("/etc/ec2-metadata/user-data")
''; '';
}; };
@ -86,9 +90,9 @@ in {
} }
''; '';
script = '' script = ''
$machine->start; machine.start()
$machine->waitForFile("/etc/testFile"); machine.wait_for_file("/etc/testFile")
$machine->succeed("cat /etc/testFile | grep -q 'whoa'"); assert "whoa" in machine.succeed("cat /etc/testFile")
''; '';
}; };
} }