2015-05-27 07:12:26 +01:00
|
|
|
{ system ? builtins.currentSystem }:
|
|
|
|
|
|
|
|
with import ../lib/testing.nix { inherit system; };
|
|
|
|
with pkgs.lib;
|
|
|
|
|
|
|
|
let
|
|
|
|
image =
|
|
|
|
(import ../lib/eval-config.nix {
|
|
|
|
inherit system;
|
|
|
|
modules = [
|
2015-09-28 12:42:24 +01:00
|
|
|
../maintainers/scripts/ec2/amazon-image.nix
|
2016-02-02 18:03:13 +00:00
|
|
|
../modules/testing/test-instrumentation.nix
|
|
|
|
../modules/profiles/qemu-guest.nix
|
|
|
|
{ ec2.hvm = true;
|
2015-09-28 19:25:10 +01:00
|
|
|
|
|
|
|
# Hack to make the partition resizing work in QEMU.
|
|
|
|
boot.initrd.postDeviceCommands = mkBefore
|
|
|
|
''
|
|
|
|
ln -s vda /dev/xvda
|
|
|
|
ln -s vda1 /dev/xvda1
|
|
|
|
'';
|
2016-03-30 21:21:56 +01:00
|
|
|
|
|
|
|
# Needed by nixos-rebuild due to the lack of network
|
|
|
|
# access. Mostly copied from
|
|
|
|
# modules/profiles/installation-device.nix.
|
|
|
|
system.extraDependencies =
|
2017-02-27 16:51:36 +00:00
|
|
|
with pkgs; [
|
|
|
|
stdenv busybox perlPackages.ArchiveCpio unionfs-fuse mkinitcpio-nfs-utils
|
|
|
|
|
|
|
|
# These are used in the configure-from-userdata tests for EC2. Httpd and valgrind are requested
|
|
|
|
# directly by the configuration we set, and libxslt.bin is used indirectly as a build dependency
|
|
|
|
# of the derivation for dbus configuration files.
|
|
|
|
apacheHttpd valgrind.doc libxslt.bin
|
2016-03-30 21:21:56 +01:00
|
|
|
];
|
2015-09-28 12:42:24 +01:00
|
|
|
}
|
2015-05-27 07:12:26 +01:00
|
|
|
];
|
|
|
|
}).config.system.build.amazonImage;
|
|
|
|
|
|
|
|
makeEc2Test = { name, userData, script, hostname ? "ec2-instance", sshPublicKey ? null }:
|
|
|
|
let
|
|
|
|
metaData = pkgs.stdenv.mkDerivation {
|
|
|
|
name = "metadata";
|
|
|
|
buildCommand = ''
|
|
|
|
mkdir -p $out/1.0/meta-data
|
2016-03-30 20:48:12 +01:00
|
|
|
ln -s ${pkgs.writeText "userData" userData} $out/1.0/user-data
|
2015-05-27 07:12:26 +01:00
|
|
|
echo "${hostname}" > $out/1.0/meta-data/hostname
|
2016-02-02 18:03:13 +00:00
|
|
|
echo "(unknown)" > $out/1.0/meta-data/ami-manifest-path
|
2015-05-27 07:12:26 +01:00
|
|
|
'' + optionalString (sshPublicKey != null) ''
|
|
|
|
mkdir -p $out/1.0/meta-data/public-keys/0
|
|
|
|
ln -s ${pkgs.writeText "sshPublicKey" sshPublicKey} $out/1.0/meta-data/public-keys/0/openssh-key
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
in makeTest {
|
|
|
|
name = "ec2-" + name;
|
|
|
|
nodes = {};
|
|
|
|
testScript =
|
|
|
|
''
|
2015-09-28 12:42:24 +01:00
|
|
|
my $imageDir = ($ENV{'TMPDIR'} // "/tmp") . "/vm-state-machine";
|
|
|
|
mkdir $imageDir, 0700;
|
|
|
|
my $diskImage = "$imageDir/machine.qcow2";
|
2016-03-30 20:48:12 +01:00
|
|
|
system("qemu-img create -f qcow2 -o backing_file=${image}/nixos.qcow2 $diskImage") == 0 or die;
|
2015-09-28 19:25:10 +01:00
|
|
|
system("qemu-img resize $diskImage 10G") == 0 or die;
|
2015-09-28 12:42:24 +01:00
|
|
|
|
|
|
|
# Note: we use net=169.0.0.0/8 rather than
|
|
|
|
# net=169.254.0.0/16 to prevent dhcpcd from getting horribly
|
|
|
|
# confused. (It would get a DHCP lease in the 169.254.*
|
|
|
|
# range, which it would then configure and prompty delete
|
|
|
|
# again when it deletes link-local addresses.) Ideally we'd
|
|
|
|
# turn off the DHCP server, but qemu does not have an option
|
|
|
|
# to do that.
|
2016-02-02 18:03:13 +00:00
|
|
|
my $startCommand = "qemu-kvm -m 768 -net nic,vlan=0,model=virtio -net 'user,vlan=0,net=169.0.0.0/8,guestfwd=tcp:169.254.169.254:80-cmd:${pkgs.micro-httpd}/bin/micro_httpd ${metaData}'";
|
2015-09-28 12:42:24 +01:00
|
|
|
$startCommand .= " -drive file=$diskImage,if=virtio,werror=report";
|
2015-05-27 07:12:26 +01:00
|
|
|
$startCommand .= " \$QEMU_OPTS";
|
|
|
|
|
|
|
|
my $machine = createMachine({ startCommand => $startCommand });
|
2015-09-28 12:42:24 +01:00
|
|
|
|
2015-05-27 07:12:26 +01:00
|
|
|
${script}
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2015-09-28 12:42:24 +01:00
|
|
|
snakeOilPrivateKey = ''
|
|
|
|
-----BEGIN OPENSSH PRIVATE KEY-----
|
|
|
|
b3BlbnNzaC1rZXktdjEAAAAABG5vbmUAAAAEbm9uZQAAAAAAAAABAAAAMwAAAAtzc2gtZW
|
|
|
|
QyNTUxOQAAACDEPmwZv5dDPrMUaq0dDP+6eBTTe+QNrz14KBEIdhHd1QAAAJDufJ4S7nye
|
|
|
|
EgAAAAtzc2gtZWQyNTUxOQAAACDEPmwZv5dDPrMUaq0dDP+6eBTTe+QNrz14KBEIdhHd1Q
|
|
|
|
AAAECgwbDlYATM5/jypuptb0GF/+zWZcJfoVIFBG3LQeRyGsQ+bBm/l0M+sxRqrR0M/7p4
|
|
|
|
FNN75A2vPXgoEQh2Ed3VAAAADEVDMiB0ZXN0IGtleQE=
|
|
|
|
-----END OPENSSH PRIVATE KEY-----
|
|
|
|
'';
|
|
|
|
|
|
|
|
snakeOilPublicKey = "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIMQ+bBm/l0M+sxRqrR0M/7p4FNN75A2vPXgoEQh2Ed3V EC2 test key";
|
|
|
|
|
2015-05-27 07:12:26 +01:00
|
|
|
in {
|
2015-04-10 05:06:52 +01:00
|
|
|
boot-ec2-nixops = makeEc2Test {
|
2015-05-27 07:12:26 +01:00
|
|
|
name = "nixops-userdata";
|
|
|
|
sshPublicKey = snakeOilPublicKey; # That's right folks! My user's key is also the host key!
|
|
|
|
|
|
|
|
userData = ''
|
2015-09-28 12:42:24 +01:00
|
|
|
SSH_HOST_ED25519_KEY_PUB:${snakeOilPublicKey}
|
|
|
|
SSH_HOST_ED25519_KEY:${replaceStrings ["\n"] ["|"] snakeOilPrivateKey}
|
2015-05-27 07:12:26 +01:00
|
|
|
'';
|
|
|
|
script = ''
|
|
|
|
$machine->start;
|
2016-03-30 20:48:12 +01:00
|
|
|
$machine->waitForFile("/etc/ec2-metadata/user-data");
|
2015-05-27 07:12:26 +01:00
|
|
|
$machine->waitForUnit("sshd.service");
|
|
|
|
|
2016-02-02 18:03:13 +00:00
|
|
|
$machine->succeed("grep unknown /etc/ec2-metadata/ami-manifest-path");
|
|
|
|
|
2015-05-27 07:12:26 +01:00
|
|
|
# We have no keys configured on the client side yet, so this should fail
|
|
|
|
$machine->fail("ssh -o BatchMode=yes localhost exit");
|
|
|
|
|
|
|
|
# Let's install our client private key
|
|
|
|
$machine->succeed("mkdir -p ~/.ssh");
|
2015-09-28 12:42:24 +01:00
|
|
|
|
|
|
|
$machine->succeed("echo '${snakeOilPrivateKey}' > ~/.ssh/id_ed25519");
|
|
|
|
$machine->succeed("chmod 600 ~/.ssh/id_ed25519");
|
2015-05-27 07:12:26 +01:00
|
|
|
|
|
|
|
# We haven't configured the host key yet, so this should still fail
|
|
|
|
$machine->fail("ssh -o BatchMode=yes localhost exit");
|
|
|
|
|
|
|
|
# Add the host key; ssh should finally succeed
|
|
|
|
$machine->succeed("echo localhost,127.0.0.1 ${snakeOilPublicKey} > ~/.ssh/known_hosts");
|
|
|
|
$machine->succeed("ssh -o BatchMode=yes localhost exit");
|
|
|
|
|
2015-09-28 19:25:10 +01:00
|
|
|
# Test whether the root disk was resized.
|
|
|
|
my $blocks = $machine->succeed("stat -c %b -f /");
|
|
|
|
my $bsize = $machine->succeed("stat -c %S -f /");
|
|
|
|
my $size = $blocks * $bsize;
|
|
|
|
die "wrong free space $size" if $size < 9.7 * 1024 * 1024 * 1024 || $size > 10 * 1024 * 1024 * 1024;
|
|
|
|
|
|
|
|
# Just to make sure resizing is idempotent.
|
2015-05-27 07:12:26 +01:00
|
|
|
$machine->shutdown;
|
2015-09-28 19:25:10 +01:00
|
|
|
$machine->start;
|
2016-03-30 20:48:12 +01:00
|
|
|
$machine->waitForFile("/etc/ec2-metadata/user-data");
|
2015-05-27 07:12:26 +01:00
|
|
|
'';
|
|
|
|
};
|
2015-04-10 05:06:52 +01:00
|
|
|
|
|
|
|
boot-ec2-config = makeEc2Test {
|
|
|
|
name = "config-userdata";
|
|
|
|
sshPublicKey = snakeOilPublicKey;
|
|
|
|
|
2016-01-07 03:25:56 +00:00
|
|
|
# ### http://nixos.org/channels/nixos-unstable nixos
|
2015-04-10 05:06:52 +01:00
|
|
|
userData = ''
|
2017-02-27 16:51:36 +00:00
|
|
|
{ pkgs, ... }:
|
|
|
|
|
2015-04-10 05:06:52 +01:00
|
|
|
{
|
|
|
|
imports = [
|
|
|
|
<nixpkgs/nixos/modules/virtualisation/amazon-image.nix>
|
|
|
|
<nixpkgs/nixos/modules/testing/test-instrumentation.nix>
|
2016-03-30 20:48:12 +01:00
|
|
|
<nixpkgs/nixos/modules/profiles/qemu-guest.nix>
|
2015-04-10 05:06:52 +01:00
|
|
|
];
|
|
|
|
environment.etc.testFile = {
|
|
|
|
text = "whoa";
|
|
|
|
};
|
2017-02-27 16:51:36 +00:00
|
|
|
|
|
|
|
services.httpd = {
|
|
|
|
enable = true;
|
|
|
|
adminAddr = "test@example.org";
|
|
|
|
documentRoot = "${pkgs.valgrind.doc}/share/doc/valgrind/html";
|
|
|
|
};
|
|
|
|
networking.firewall.allowedTCPPorts = [ 80 ];
|
2015-04-10 05:06:52 +01:00
|
|
|
}
|
|
|
|
'';
|
|
|
|
script = ''
|
|
|
|
$machine->start;
|
|
|
|
$machine->waitForFile("/etc/testFile");
|
|
|
|
$machine->succeed("cat /etc/testFile | grep -q 'whoa'");
|
2017-02-27 16:51:36 +00:00
|
|
|
|
|
|
|
$machine->waitForUnit("httpd.service");
|
|
|
|
$machine->succeed("curl http://localhost | grep Valgrind");
|
2015-04-10 05:06:52 +01:00
|
|
|
'';
|
|
|
|
};
|
2015-05-27 07:12:26 +01:00
|
|
|
}
|