2021-02-24 14:10:22 +01:00
# Test for cntr tool
{ system ? builtins . currentSystem , config ? { }
, pkgs ? import ../.. { inherit system config ; } , lib ? pkgs . lib } :
let
inherit ( import ../lib/testing-python.nix { inherit system pkgs ; } ) makeTest ;
mkOCITest = backend :
makeTest {
name = " c n t r - ${ backend } " ;
2021-09-22 08:43:58 +02:00
meta = { maintainers = with lib . maintainers ; [ sorki mic92 ] ; } ;
2021-02-24 14:10:22 +01:00
nodes = {
$ { backend } = { pkgs , . . . }: {
environment . systemPackages = [ pkgs . cntr ] ;
virtualisation . oci-containers = {
inherit backend ;
containers . nginx = {
image = " n g i n x - c o n t a i n e r " ;
2024-08-24 04:38:27 +02:00
imageStream = pkgs . dockerTools . examples . nginxStream ;
2021-02-24 14:10:22 +01:00
ports = [ " 8 1 8 1 : 8 0 " ] ;
} ;
} ;
} ;
} ;
testScript = ''
start_all ( )
$ { backend } . wait_for_unit ( " ${ backend } - n g i n x . s e r v i c e " )
2022-03-03 07:01:44 +01:00
$ { backend } . wait_for_open_port ( 8181 )
# For some reason, the cntr command hangs when run without the &.
# As such, we have to do some messy things to ensure we check the exitcode and output in a race-condition-safe manner
$ { backend } . execute (
" ( c n t r a t t a c h - t ${ backend } n g i n x s h - - - c ' c u r l l o c a l h o s t | g r e p H e l l o ' > / t m p / r e s u l t ; e c h o $ ? > / t m p / e x i t c o d e ; t o u c h / t m p / d o n e ) & "
2021-02-24 14:10:22 +01:00
)
2022-03-03 07:01:44 +01:00
$ { backend } . wait_for_file ( " / t m p / d o n e " )
assert " 0 " == $ { backend } . succeed ( " c a t / t m p / e x i t c o d e " ) . strip ( ) , " n o n - z e r o e x i t c o d e "
assert " H e l l o " in $ { backend } . succeed ( " c a t / t m p / r e s u l t " ) , " n o g r e e t i n g i n o u t p u t "
2021-02-24 14:10:22 +01:00
'' ;
} ;
mkContainersTest = makeTest {
name = " c n t r - c o n t a i n e r s " ;
meta = with pkgs . lib . maintainers ; { maintainers = [ sorki mic92 ] ; } ;
2022-03-21 00:15:30 +01:00
nodes . machine = { lib , . . . }: {
2021-02-24 14:10:22 +01:00
environment . systemPackages = [ pkgs . cntr ] ;
containers . test = {
autoStart = true ;
privateNetwork = true ;
hostAddress = " 1 7 2 . 1 6 . 0 . 1 " ;
localAddress = " 1 7 2 . 1 6 . 0 . 2 " ;
config = { } ;
} ;
} ;
testScript = ''
machine . start ( )
machine . wait_for_unit ( " c o n t a i n e r @ t e s t . s e r v i c e " )
2022-03-03 07:01:44 +01:00
# I haven't observed the same hanging behaviour in this version as in the OCI version which necessetates this messy invocation, but it's probably better to be safe than sorry and use it here as well
machine . execute (
" ( c n t r a t t a c h t e s t s h - - - c ' p i n g - c 5 1 7 2 . 1 6 . 0 . 1 ' ; e c h o $ ? > / t m p / e x i t c o d e ; t o u c h / t m p / d o n e ) & "
)
machine . wait_for_file ( " / t m p / d o n e " )
assert " 0 " == machine . succeed ( " c a t / t m p / e x i t c o d e " ) . strip ( ) , " n o n - z e r o e x i t c o d e "
2021-02-24 14:10:22 +01:00
'' ;
} ;
in {
nixos-container = mkContainersTest ;
} // ( lib . foldl' ( attrs : backend : attrs // { ${ backend } = mkOCITest backend ; } )
{ } [ " d o c k e r " " p o d m a n " ] )