Clean up "work" directory before "arion up"

to ensure that the subtests defined in tests/arion-test/default.nix do
not inadvertently re-use the arion-compose.nix, arion-pkgs.nix, etc. set
up during the _previous_ subtest.

Previously, each subtest attempted to clean itself up by doing
the following:

    cd work && [...snip...] && rm -rf work

This removes the directory "work/work", while leaving "work" itself
intact.  Subsequent subtests would then run:

    cp -r ${../../examples/some-example} work

thereby copying the contents of "some-example" into "work/work" rather
than into "work".

As a result, all subtests but the first simply reapplied the Arion
configuration set up by the first subtest, because this configuration
persisted within the "work" directory used as the working directory for
"arion up", etc.

This commit corrects the issue by:

    1. Removing "work" rather than "work/work", and
    2. Adding certain flags to the "cp" invocation to ensure it reliably
       copies files into "work" rather than "work/work": (a) "-f"
       ("--force"), to overwrite destination files if they already
       exist, and (b) "-T" ("--no-target-directory") to copy the
       *contents* of the source directory to "work" rather than copying
       the source directory itself as a subdirectory of "work".

Additionally, this commit factors out code common to all subtests into a
reusable subtest generator coderef.
This commit is contained in:
Matt Schreiber 2020-01-15 14:06:16 -05:00 committed by Robert Hensing
parent 41d3fb490c
commit 5bd7ea2aa3

View file

@ -40,27 +40,22 @@ in
$machine->fail("curl localhost:8000"); $machine->fail("curl localhost:8000");
$machine->succeed("docker --version"); $machine->succeed("docker --version");
subtest "minimal", sub { my $makeSubtest = sub {
$machine->succeed("cp -r ${../../examples/minimal} work && cd work && NIX_PATH=nixpkgs='${pkgs.path}' arion up -d"); my ( $subtestName, $exampleSrc, @codeRefs ) = @_;
subtest $subtestName => sub {
$machine->succeed("rm -rf work && cp -frT $exampleSrc work && cd work && NIX_PATH=nixpkgs='${pkgs.path}' arion up -d");
$machine->waitUntilSucceeds("curl localhost:8000"); $machine->waitUntilSucceeds("curl localhost:8000");
$machine->succeed("cd work && NIX_PATH=nixpkgs='${pkgs.path}' arion down && rm -rf work"); $_->() for @codeRefs;
$machine->succeed("cd work && NIX_PATH=nixpkgs='${pkgs.path}' arion down");
$machine->waitUntilFails("curl localhost:8000"); $machine->waitUntilFails("curl localhost:8000");
}; };
};
subtest "full-nixos", sub { $makeSubtest->("minimal", "${../../examples/minimal}");
$machine->succeed("cp -r ${../../examples/full-nixos} work && cd work && NIX_PATH=nixpkgs='${pkgs.path}' arion up -d"); $makeSubtest->("full-nixos", "${../../examples/full-nixos}", sub {
$machine->waitUntilSucceeds("curl localhost:8000");
# Also test exec with defaultExec
$machine->succeed("cd work && export NIX_PATH=nixpkgs='${pkgs.path}' && (echo 'nix run -f ~/h/arion arion -c arion exec webserver'; echo 'target=world; echo Hello \$target'; echo exit) | script /dev/null | grep 'Hello world'"); $machine->succeed("cd work && export NIX_PATH=nixpkgs='${pkgs.path}' && (echo 'nix run -f ~/h/arion arion -c arion exec webserver'; echo 'target=world; echo Hello \$target'; echo exit) | script /dev/null | grep 'Hello world'");
$machine->succeed("cd work && NIX_PATH=nixpkgs='${pkgs.path}' arion down && rm -rf work"); });
$machine->waitUntilFails("curl localhost:8000"); $makeSubtest->("nixos-unit", "${../../examples/nixos-unit}");
};
subtest "nixos-unit", sub {
$machine->succeed("cp -r ${../../examples/nixos-unit} work && cd work && NIX_PATH=nixpkgs='${pkgs.path}' arion up -d");
$machine->waitUntilSucceeds("curl localhost:8000");
$machine->succeed("cd work && NIX_PATH=nixpkgs='${pkgs.path}' arion down && rm -rf work");
$machine->waitUntilFails("curl localhost:8000");
};
''; '';
} }