2022-12-02 00:21:38 +01:00
{ usePodman ? false , pkgs , lib ? pkgs . lib , . . . }:
2019-01-02 09:54:46 +01:00
let
# To make some prebuilt derivations available in the vm
2019-03-05 19:41:54 +01:00
preEval = modules : import ../../src/nix/eval-composition.nix {
inherit modules ;
2019-01-02 09:54:46 +01:00
inherit pkgs ;
} ;
2021-05-26 16:57:56 +02:00
inherit ( lib )
2021-10-28 21:59:01 +02:00
concatMapStringsSep
2021-05-26 16:57:56 +02:00
optionalAttrs
optionalString
;
haveSystemd = usePodman || pkgs . arionTestingFlags . dockerSupportsSystemd ;
2021-10-28 21:59:01 +02:00
concatPathLines = paths : concatMapStringsSep " \n " ( x : " ${ x } " ) paths ;
2019-01-02 09:54:46 +01:00
in
{
name = " a r i o n - t e s t " ;
2022-04-07 22:55:22 +02:00
nodes . machine = { pkgs , lib , . . . }: {
2019-01-02 09:54:46 +01:00
environment . systemPackages = [
pkgs . arion
2021-05-26 16:57:56 +02:00
] ++ lib . optional usePodman pkgs . docker ;
virtualisation . docker . enable = ! usePodman ;
virtualisation . podman = optionalAttrs usePodman {
enable = true ;
dockerSocket . enable = true ;
} ;
2019-01-02 09:54:46 +01:00
# no caches, because no internet
nix . binaryCaches = lib . mkForce [ ] ;
2019-01-27 01:08:51 +01:00
# FIXME: Sandbox seems broken with current version of NixOS test
# w/ writable store. Error:
# machine# error: linking '/nix/store/7r8z2zvhwda85pgpdn5hzzz6hs1njklc-stdenv-linux.drv.chroot/nix/store/6v3y7s4q4wd16hsw393gjpxvcf9159bv-patch-shebangs.sh' to '/nix/store/6v3y7s4q4wd16hsw393gjpxvcf9159bv-patch-shebangs.sh': Operation not permitted
#
# There should be no reason why arion can't run without
# sandboxing, so please re-enable.
nix . useSandbox = false ;
2019-01-02 09:54:46 +01:00
virtualisation . writableStore = true ;
2021-10-28 21:59:01 +02:00
# Switch to virtualisation.additionalPaths when dropping all NixOS <= 21.05.
environment . etc . " e x t r a - p a t h s - f o r - t e s t " . text = concatPathLines [
2019-01-02 09:54:46 +01:00
# Pre-build the image because we don't want to build the world
# in the vm.
2019-10-03 21:30:14 +02:00
( preEval [ ../../examples/minimal/arion-compose.nix ] ) . config . out . dockerComposeYaml
( preEval [ ../../examples/full-nixos/arion-compose.nix ] ) . config . out . dockerComposeYaml
( preEval [ ../../examples/nixos-unit/arion-compose.nix ] ) . config . out . dockerComposeYaml
2021-01-21 23:53:37 +01:00
( preEval [ ../../examples/traefik/arion-compose.nix ] ) . config . out . dockerComposeYaml
2019-01-27 01:08:51 +01:00
pkgs . stdenv
2019-01-02 09:54:46 +01:00
] ;
2020-03-02 11:13:47 +01:00
2020-09-05 23:02:21 +02:00
virtualisation . memorySize = 1024 ;
2021-05-26 16:57:56 +02:00
virtualisation . diskSize = 8000 ;
2019-01-02 09:54:46 +01:00
} ;
testScript = ''
2021-01-21 23:54:53 +01:00
machine . fail ( " c u r l - - f a i l l o c a l h o s t : 8 0 0 0 " )
2020-03-02 11:13:47 +01:00
machine . succeed ( " d o c k e r - - v e r s i o n " )
# Tests
# - arion up
# - arion down
# - examples/minimal
with subtest ( " m i n i m a l " ) :
machine . succeed (
" r m - r f w o r k & & c p - f r T ${ ../../examples/minimal } w o r k & & c d w o r k & & N I X _ P A T H = n i x p k g s = ' ${ pkgs . path } ' a r i o n u p - d "
)
2021-01-21 23:54:53 +01:00
machine . wait_until_succeeds ( " c u r l - - f a i l l o c a l h o s t : 8 0 0 0 " )
2020-03-02 11:13:47 +01:00
machine . succeed (
" c d w o r k & & N I X _ P A T H = n i x p k g s = ' ${ pkgs . path } ' a r i o n d o w n "
)
2021-01-21 23:54:53 +01:00
machine . wait_until_fails ( " c u r l - - f a i l l o c a l h o s t : 8 0 0 0 " )
2020-03-02 11:13:47 +01:00
2021-05-31 15:14:24 +02:00
# Tests
# - running same image again doesn't require a `docker load`
with subtest ( " d o c k e r l o a d o n l y o n c e " ) :
# We assume image loading relies on the `docker images` and `docker load` commands, so this should fail
machine . fail (
" e x p o r t R E A L _ D O C K E R = $ ( w h i c h d o c k e r ) ; r m - r f w o r k & & c p - f r T ${ ../../examples/minimal } w o r k & & c d w o r k & & N I X _ P A T H = n i x p k g s = ' ${ pkgs . path } ' P A T H = \" ${ pkgs . writeScriptBin " d o c k e r " ''
#!${pkgs.runtimeShell} -eu
echo 1 > & 2 " T h i s f a i l u r e i s e x p e c t e d . A r g s w e r e " " $ @ "
echo " $ @ " > /tmp/docker-args
exit 1
'' } / b i n : $P A T H \ " a r i o n u p - d "
)
machine . succeed (
" e x p o r t R E A L _ D O C K E R = $ ( w h i c h d o c k e r ) ; r m - r f w o r k & & c p - f r T ${ ../../examples/minimal } w o r k & & c d w o r k & & N I X _ P A T H = n i x p k g s = ' ${ pkgs . path } ' P A T H = \" ${ pkgs . writeScriptBin " d o c k e r " ''
#!${pkgs.runtimeShell} -eu
case $ 1 in
load )
echo 1 > & 2 " a r i o n m u s t n o t d o c k e r l o a d w h e n u p p i n g t h e s a m e d e p l o y m e n t f o r t h e s e c o n d t i m e "
exit 1
; ;
images )
echo 1 > & 2 " e x e c i n g d o c k e r t o l i s t i m a g e s "
exec $ REAL_DOCKER " $ @ "
; ;
* )
echo 1 > & 2 " U n k n o w n d o c k e r i n v o c a t i o n . T h i s m a y b e a s h o r t c o m i n g o f t h i s d o c k e r m o c k . "
echo 1 > & 2 " I n v o c a t i o n : d o c k e r " " $ @ "
; ;
esac
'' } / b i n : $P A T H \ " a r i o n u p - d "
)
machine . wait_until_succeeds ( " c u r l - - f a i l l o c a l h o s t : 8 0 0 0 " )
machine . succeed (
" c d w o r k & & N I X _ P A T H = n i x p k g s = ' ${ pkgs . path } ' a r i o n d o w n "
)
machine . wait_until_fails ( " c u r l - - f a i l l o c a l h o s t : 8 0 0 0 " )
2021-04-20 10:31:09 +02:00
# Tests
# - examples/flake
# This _test_ doesn't work because flake-compat fetches the github
# tarballs without sha256 and/or Nix doesn't consult the store before
# downloading.
# See https://github.com/edolstra/flake-compat/pull/12
# with subtest("flake"):
# machine.succeed(
# "rm -rf work && cp -frT ''${../../examples/flake} work && cd work && NIX_PATH= arion up -d"
# )
# machine.wait_until_succeeds("curl --fail localhost:8000")
# machine.succeed("cd work && NIX_PATH= arion down")
# machine.wait_until_fails("curl --fail localhost:8000")
2021-05-26 16:57:56 +02:00
$ { optionalString haveSystemd ''
2020-03-02 11:13:47 +01:00
# Tests
# - arion exec
# - examples/full-nixos
with subtest ( " f u l l - n i x o s " ) :
machine . succeed (
" r m - r f w o r k & & c p - f r T ${ ../../examples/full-nixos } w o r k & & c d w o r k & & N I X _ P A T H = n i x p k g s = ' ${ pkgs . path } ' a r i o n u p - d "
)
2021-01-21 23:54:53 +01:00
machine . wait_until_succeeds ( " c u r l - - f a i l l o c a l h o s t : 8 0 0 0 " )
2020-03-02 11:13:47 +01:00
machine . succeed (
" " "
set - eux - o pipefail
cd work
export NIX_PATH = nixpkgs = ' $ { pkgs . path } '
echo ' target = world ; echo Hello $ target ; exit' \
| script ' arion exec webserver' \
| grep ' Hello world'
" " "
) ,
machine . succeed (
" c d w o r k & & N I X _ P A T H = n i x p k g s = ' ${ pkgs . path } ' a r i o n d o w n "
)
2021-01-21 23:54:53 +01:00
machine . wait_until_fails ( " c u r l - - f a i l l o c a l h o s t : 8 0 0 0 " )
2021-05-26 16:57:56 +02:00
'' }
2020-03-02 11:13:47 +01:00
# Tests
# - examples/nixos-unit
with subtest ( " n i x o s - u n i t " ) :
machine . succeed (
" r m - r f w o r k & & c p - f r T ${ ../../examples/nixos-unit } w o r k & & c d w o r k & & N I X _ P A T H = n i x p k g s = ' ${ pkgs . path } ' a r i o n u p - d "
)
2021-01-21 23:54:53 +01:00
machine . wait_until_succeeds ( " c u r l - - f a i l l o c a l h o s t : 8 0 0 0 " )
2020-03-02 11:13:47 +01:00
machine . succeed (
" c d w o r k & & N I X _ P A T H = n i x p k g s = ' ${ pkgs . path } ' a r i o n d o w n "
)
2021-01-21 23:54:53 +01:00
machine . wait_until_fails ( " c u r l - - f a i l l o c a l h o s t : 8 0 0 0 " )
2021-01-21 23:53:37 +01:00
# Tests
# - examples/traefik
# - labels
with subtest ( " t r a e f i k " ) :
machine . succeed (
" r m - r f w o r k & & c p - f r T ${ ../../examples/traefik } w o r k & & c d w o r k & & N I X _ P A T H = n i x p k g s = ' ${ pkgs . path } ' a r i o n u p - d "
)
2021-01-21 23:54:53 +01:00
machine . wait_until_succeeds ( " c u r l - - f a i l n i x - d o c s . l o c a l h o s t " )
2021-01-21 23:53:37 +01:00
machine . succeed (
" c d w o r k & & N I X _ P A T H = n i x p k g s = ' ${ pkgs . path } ' a r i o n d o w n "
)
2021-01-21 23:54:53 +01:00
machine . wait_until_fails ( " c u r l - - f a i l n i x - d o c s . l o c a l h o s t " )
2019-01-02 09:54:46 +01:00
'' ;
}