Rewrite tests in haskell
This commit is contained in:
parent
b28610bb1e
commit
d8b14ca5ee
9 changed files with 530 additions and 255 deletions
153
tests/spec/JailSpec.hs
Normal file
153
tests/spec/JailSpec.hs
Normal file
|
|
@ -0,0 +1,153 @@
|
|||
module JailSpec (spec) where
|
||||
|
||||
import Test.Hspec
|
||||
import TestPrelude
|
||||
|
||||
spec :: Spec
|
||||
spec = parallel $ inTestM $ do
|
||||
it "runs the wrapped program" $ do
|
||||
"jail \"hello\" pkgs.hello []"
|
||||
`shouldOutput` "Hello, world!\n"
|
||||
|
||||
it "can be imported using callPackage" $ do
|
||||
[i|
|
||||
let
|
||||
jail-nix-without-flake = pkgs.callPackage <jail-nix> {};
|
||||
jail-without-flake = jail-nix-without-flake.init pkgs;
|
||||
in
|
||||
jail-without-flake "hello" pkgs.hello []
|
||||
|]
|
||||
`shouldOutput` "Hello, world!\n"
|
||||
|
||||
it "sets a sane default path" $ do
|
||||
defaultPath <- evalNixExpr [i| "${pkgs.coreutils}/bin" |]
|
||||
[i| jail "test" (sh ''echo -n "$PATH"'') [] |]
|
||||
`shouldOutput` defaultPath
|
||||
|
||||
it "it does not forward environment variables by default" $ do
|
||||
withEnv "MY_ENV_VAR" "secret" $ do
|
||||
[i| jail "test" (sh ''echo -n "''${MY_ENV_VAR-unset}"'') [] |]
|
||||
`shouldOutput` "unset"
|
||||
|
||||
describe "advanced configuration" $ do
|
||||
it "allows overriding base permissions shared across all jails" $ do
|
||||
tmpDir <- getTestDir
|
||||
let secretPath = tmpDir </> "secret"
|
||||
liftIO $ writeFile secretPath "hunter2"
|
||||
[i|
|
||||
let
|
||||
jail' = jail-nix.extend {
|
||||
inherit pkgs;
|
||||
basePermissions = c: [
|
||||
(c.readonly #{toNixString secretPath})
|
||||
(c.add-pkg-deps [ pkgs.coreutils ])
|
||||
(c.readonly "/nix/store")
|
||||
];
|
||||
};
|
||||
in
|
||||
jail' "hello" (sh #{toNixString ("cat " <> secretPath)}) []
|
||||
|]
|
||||
`shouldOutput` "hunter2"
|
||||
|
||||
it "allows overriding the bubblewrap package" $ do
|
||||
[i|
|
||||
let
|
||||
jail' = jail-nix.extend {
|
||||
inherit pkgs;
|
||||
bubblewrapPackage = pkgs.writeShellApplication {
|
||||
name = "my-bubblewrap";
|
||||
text = ''
|
||||
# A version of bwrap that just prints its arguments instead of
|
||||
# actually running a jail.
|
||||
echo -n "$@"
|
||||
'';
|
||||
};
|
||||
basePermissions = null;
|
||||
};
|
||||
in
|
||||
jail' "test" "some-entrypoint" []
|
||||
|]
|
||||
`shouldOutput` "--unshare-user --unshare-ipc --unshare-pid --unshare-net --unshare-uts --unshare-cgroup --new-session -- some-entrypoint"
|
||||
|
||||
describe "sandboxed nix store" $ do
|
||||
let checkRuntimeDepIsExposed :: Bool -> String -> TestM ()
|
||||
checkRuntimeDepIsExposed shouldBeExposed jailExpr = do
|
||||
output <-
|
||||
runNixDrv
|
||||
[i|
|
||||
let
|
||||
buildtime = pkgs.runCommand "buildtime" {} "touch $out";
|
||||
runtime = pkgs.runCommand "runtime" {} "touch $out";
|
||||
dep = pkgs.runCommand "dep" {} ''
|
||||
echo ${buildtime}
|
||||
echo ${runtime} > $out
|
||||
'';
|
||||
entryScript = sh ''
|
||||
if [ -e "$1" ]; then
|
||||
echo "file exists"
|
||||
else
|
||||
echo "file doesn't exist"
|
||||
fi
|
||||
'';
|
||||
jailed = (#{jailExpr}) entryScript dep;
|
||||
in
|
||||
sh ''
|
||||
echo "buildtime: $(${lib.getExe jailed} ${buildtime})"
|
||||
echo "runtime: $(${lib.getExe jailed} ${runtime})"
|
||||
''
|
||||
|]
|
||||
liftIO $
|
||||
output
|
||||
`shouldBe` unlines
|
||||
[ "buildtime: file doesn't exist",
|
||||
if shouldBeExposed
|
||||
then "runtime: file exists"
|
||||
else "runtime: file doesn't exist"
|
||||
]
|
||||
|
||||
it "does not expose unrelated paths in the nix store" $ do
|
||||
checkRuntimeDepIsExposed
|
||||
False
|
||||
[i| entryScript: _dep:
|
||||
jail "test" entryScript []
|
||||
|]
|
||||
|
||||
it "exposes runtime closures of deps in the entry" $ do
|
||||
checkRuntimeDepIsExposed
|
||||
True
|
||||
[i| entryScript: dep:
|
||||
jail
|
||||
"test"
|
||||
(sh ''
|
||||
# ${dep}
|
||||
${lib.getExe entryScript} "$@"
|
||||
'')
|
||||
[]
|
||||
|]
|
||||
|
||||
it "exposes runtime closures of deps in add-pkg-deps" $ do
|
||||
checkRuntimeDepIsExposed
|
||||
True
|
||||
[i| entryScript: dep:
|
||||
jail "test" entryScript (c: [
|
||||
(c.add-pkg-deps [ dep ])
|
||||
])
|
||||
|]
|
||||
|
||||
it "exposes runtime closures of deps in argv" $ do
|
||||
checkRuntimeDepIsExposed
|
||||
True
|
||||
[i| entryScript: dep:
|
||||
jail "test" entryScript (c: [
|
||||
(c.set-argv [ (c.noescape ''"$1"'') dep ])
|
||||
])
|
||||
|]
|
||||
|
||||
it "exposes runtime closures of deps in write-text string contexts" $ do
|
||||
checkRuntimeDepIsExposed
|
||||
True
|
||||
[i| entryScript: dep:
|
||||
jail "test" entryScript (c: [
|
||||
(c.write-text "/write/text/with/runtime/dep" "${dep}")
|
||||
])
|
||||
|]
|
||||
Loading…
Add table
Add a link
Reference in a new issue