Rewrite tests in haskell
This commit is contained in:
parent
b28610bb1e
commit
d8b14ca5ee
9 changed files with 530 additions and 255 deletions
129
tests/spec/CombinatorsSpec.hs
Normal file
129
tests/spec/CombinatorsSpec.hs
Normal file
|
|
@ -0,0 +1,129 @@
|
|||
module CombinatorsSpec (spec) where
|
||||
|
||||
import Data.List.Extra
|
||||
import Data.String.Interpolate.Util
|
||||
import System.Directory.Extra (createDirectoryIfMissing, doesFileExist)
|
||||
import Test.Hspec
|
||||
import TestPrelude
|
||||
|
||||
spec :: Spec
|
||||
spec = parallel $ inTestM $ do
|
||||
describe "add-cleanup" $ do
|
||||
it "runs arbitrary logic after the jail exits" $ do
|
||||
tmpDir <- getTestDir
|
||||
[i|
|
||||
jail "test" (sh "cat /foo-in-jail") (c: [
|
||||
(c.ro-bind "foo" "/foo-in-jail")
|
||||
(c.add-runtime "echo hello > foo")
|
||||
(c.add-cleanup "rm foo")
|
||||
])
|
||||
|]
|
||||
`shouldOutput` "hello\n"
|
||||
liftIO $ doesFileExist (tmpDir </> "foo") `shouldReturn` False
|
||||
|
||||
describe "add-path" $ do
|
||||
it "prepends the passed path to $PATH" $ do
|
||||
output <-
|
||||
runNixDrv
|
||||
[i|
|
||||
jail "test" (sh ''echo "$PATH"'') (c: [
|
||||
(c.add-path "/some/new/path")
|
||||
])
|
||||
|]
|
||||
liftIO $ output `shouldStartWith` "/some/new/path:"
|
||||
|
||||
describe "add-pkg-deps" $ do
|
||||
it "sets the path" $ do
|
||||
helloPath <- evalNixExpr "toString pkgs.hello"
|
||||
output <-
|
||||
runNixDrv
|
||||
[i|
|
||||
jail "test" (sh ''echo "$PATH"'') (c: [
|
||||
(c.add-pkg-deps [ pkgs.hello ])
|
||||
])
|
||||
|]
|
||||
liftIO $ output `shouldStartWith` helloPath <> "/bin:"
|
||||
|
||||
describe "bind-pkg" $ do
|
||||
it "binds the passed nix package at the specified location" $ do
|
||||
[i|
|
||||
jail "test" (sh "cat /foo") (c: [
|
||||
(c.bind-pkg "/foo" (pkgs.writeText "foo" "bar\n"))
|
||||
])
|
||||
|]
|
||||
`shouldOutput` "bar\n"
|
||||
|
||||
describe "fwd-env" $ do
|
||||
it "forwards the passed environment variable" $ do
|
||||
withEnv "MY_ENV_VAR" "secret" $ do
|
||||
[i|
|
||||
jail "test" (sh ''echo "''${MY_ENV_VAR-unset}"'') (c: [
|
||||
(c.fwd-env "MY_ENV_VAR")
|
||||
])
|
||||
|]
|
||||
`shouldOutput` "secret\n"
|
||||
|
||||
describe "readonly-from-path-var" $ do
|
||||
forM_ [":", " "] $ \separator ->
|
||||
describe ("with separator \"" <> separator <> "\"") $ do
|
||||
it "binds all paths in the specified environment variable as read only" $ do
|
||||
tmpDir <- getTestDir
|
||||
let separator = " "
|
||||
let mkTestPath path = liftIO $ createDirectoryIfMissing True (tmpDir </> path) >> writeFile (tmpDir </> path </> "file") ""
|
||||
mkTestPath "some/path"
|
||||
mkTestPath "some/other/path"
|
||||
mkTestPath "some/not/in/var"
|
||||
let testPaths =
|
||||
intercalate
|
||||
separator
|
||||
$ map
|
||||
(tmpDir </>)
|
||||
[ "some/path",
|
||||
"some/non/existant/path",
|
||||
"some/other/path",
|
||||
"some/other/non/existant/path"
|
||||
]
|
||||
withEnv "TEST_PATHS" testPaths $ do
|
||||
[i|
|
||||
jail "test" (sh "${lib.getExe pkgs.tree} ${#{toNixString (tmpDir </> "some")}}") (c: [
|
||||
(c.readonly-paths-from-var "TEST_PATHS" #{toNixString separator})
|
||||
])
|
||||
|]
|
||||
`shouldOutput` unindent
|
||||
[i|
|
||||
#{tmpDir </> "some"}
|
||||
|-- other
|
||||
| `-- path
|
||||
| `-- file
|
||||
`-- path
|
||||
`-- file
|
||||
|
||||
4 directories, 2 files
|
||||
|]
|
||||
|
||||
it "doesn't require the var to be set" $ do
|
||||
void $
|
||||
runNixDrv
|
||||
[i|
|
||||
jail "test" (sh "true") (c: [
|
||||
(c.readonly-paths-from-var "THIS_VAR_IS_NOT_SET" #{toNixString separator})
|
||||
])
|
||||
|]
|
||||
|
||||
describe "set-argv" $ do
|
||||
it "overrides argv passed to the jailed program" $ do
|
||||
[i|
|
||||
jail "test" (sh "printf '1=%s,2=%s,3=%s' \\"$@\\"") (c: [
|
||||
(c.set-argv [ "foo" "bar baz" "foo>'bar'" ])
|
||||
])
|
||||
|]
|
||||
`shouldOutput` "1=foo,2=bar baz,3=foo>'bar'"
|
||||
|
||||
describe "set-env" $ do
|
||||
it "allows setting arbitrary environment variables" $ do
|
||||
[i|
|
||||
jail "test" (sh ''echo -n "''${MY_ENV_VAR-unset}"'') (c: [
|
||||
(c.set-env "MY_ENV_VAR" "some-value")
|
||||
])
|
||||
|]
|
||||
`shouldOutput` "some-value"
|
||||
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}")
|
||||
])
|
||||
|]
|
||||
1
tests/spec/Spec.hs
Normal file
1
tests/spec/Spec.hs
Normal file
|
|
@ -0,0 +1 @@
|
|||
{-# OPTIONS_GHC -F -pgmF hspec-discover #-}
|
||||
141
tests/spec/TestPrelude.hs
Normal file
141
tests/spec/TestPrelude.hs
Normal file
|
|
@ -0,0 +1,141 @@
|
|||
module TestPrelude
|
||||
( (</>),
|
||||
TestM,
|
||||
buildNixDrvPath,
|
||||
evalNixExpr,
|
||||
forM,
|
||||
forM_,
|
||||
getTestDir,
|
||||
i,
|
||||
inTestM,
|
||||
liftIO,
|
||||
runNixDrv,
|
||||
shouldOutput,
|
||||
toNixString,
|
||||
void,
|
||||
withEnv,
|
||||
)
|
||||
where
|
||||
|
||||
import Control.Monad
|
||||
import Control.Monad.Extra (mapMaybeM)
|
||||
import Control.Monad.IO.Class (liftIO)
|
||||
import Control.Monad.Reader (MonadIO, MonadReader, ReaderT, asks, local, runReaderT)
|
||||
import Data.Aeson qualified as Aeson
|
||||
import Data.Function ((&))
|
||||
import Data.List.Extra
|
||||
import Data.Map qualified as Map
|
||||
import Data.Maybe (fromJust)
|
||||
import Data.String.Conversions
|
||||
import Data.String.Interpolate (i)
|
||||
import System.Environment (lookupEnv)
|
||||
import System.FilePath ((</>))
|
||||
import System.IO.Temp (withSystemTempDirectory)
|
||||
import System.Process qualified as Process
|
||||
import Test.Hspec
|
||||
import Test.Hspec.Core.Spec qualified
|
||||
|
||||
data Context = Context
|
||||
{ tmpDir :: FilePath,
|
||||
env :: Map.Map String String
|
||||
}
|
||||
|
||||
newtype TestM a = TestM {testMReaderT :: ReaderT Context IO a}
|
||||
deriving newtype
|
||||
( Functor,
|
||||
Applicative,
|
||||
Monad,
|
||||
MonadIO,
|
||||
MonadReader Context
|
||||
)
|
||||
|
||||
instance Example (TestM ()) where
|
||||
type Arg (TestM ()) = Context
|
||||
evaluateExample test _ hook _ = do
|
||||
hook $ runReaderT $ testMReaderT test
|
||||
pure $ Test.Hspec.Core.Spec.Result "" Test.Hspec.Core.Spec.Success
|
||||
|
||||
getTestDir :: TestM FilePath
|
||||
getTestDir = asks tmpDir
|
||||
|
||||
inTestM :: SpecWith Context -> SpecWith ()
|
||||
inTestM = aroundWith $ \action () -> do
|
||||
withSystemTempDirectory "jail-nix-test" $ \tmpDir -> do
|
||||
baseEnv <-
|
||||
Map.fromList
|
||||
<$> mapMaybeM
|
||||
(\(name, getVal) -> ((name,) <$>) <$> getVal)
|
||||
[ ("HOME", lookupEnv "HOME"),
|
||||
("LANG", pure $ Just "en_US.UTF-8"),
|
||||
("PATH", lookupEnv "PATH")
|
||||
]
|
||||
action $
|
||||
Context
|
||||
{ tmpDir,
|
||||
env = baseEnv
|
||||
}
|
||||
|
||||
toNixString :: String -> String
|
||||
toNixString s =
|
||||
s
|
||||
& replace "\\" "\\\\"
|
||||
& replace "\"" "\\\""
|
||||
& replace "$" "\\$"
|
||||
& ("\"" <>)
|
||||
& (<> "\"")
|
||||
|
||||
withEnv :: String -> String -> TestM () -> TestM ()
|
||||
withEnv name value = local $ \ctx -> ctx {env = Map.insert name value (env ctx)}
|
||||
|
||||
runNixDrv :: String -> TestM String
|
||||
runNixDrv nixDrvExpr = do
|
||||
(exe :: String, drvPath :: FilePath) <-
|
||||
evalNixExpr
|
||||
[i| let d = #{nixDrvExpr}; in [(lib.getExe d) d.drvPath] |]
|
||||
void $ buildNixDrvPath drvPath
|
||||
env <- asks env
|
||||
let p = (Process.proc exe []) {Process.env = Just $ Map.toList env}
|
||||
liftIO $ Process.readCreateProcess p ""
|
||||
|
||||
buildNixDrvPath :: FilePath -> TestM FilePath
|
||||
buildNixDrvPath drvPath = do
|
||||
liftIO $
|
||||
Process.readProcess
|
||||
"nix"
|
||||
[ "build",
|
||||
"--no-link",
|
||||
"--print-out-paths",
|
||||
drvPath <> "^*"
|
||||
]
|
||||
""
|
||||
|
||||
evalNixExpr :: (Aeson.FromJSON a) => String -> TestM a
|
||||
evalNixExpr nixExpr = do
|
||||
tmpDir <- getTestDir
|
||||
liftIO $
|
||||
writeFile
|
||||
(tmpDir </> "test.nix")
|
||||
[i|
|
||||
let
|
||||
pkgs = import <nixpkgs> {};
|
||||
lib = pkgs.lib;
|
||||
sh = script: (pkgs.writeShellApplication { name = "test-script"; text = script; });
|
||||
jail-nix = import <jail-nix> {};
|
||||
jail = jail-nix.extend { inherit pkgs; };
|
||||
in #{nixExpr}
|
||||
|]
|
||||
liftIO $
|
||||
fromJust . Aeson.decode' . cs
|
||||
<$> Process.readProcess
|
||||
"nix"
|
||||
[ "eval",
|
||||
"--json",
|
||||
"--file",
|
||||
tmpDir </> "test.nix"
|
||||
]
|
||||
""
|
||||
|
||||
shouldOutput :: String -> String -> TestM ()
|
||||
shouldOutput nixDrvExpr expectedOutput = do
|
||||
out <- runNixDrv nixDrvExpr
|
||||
liftIO $ out `shouldBe` expectedOutput
|
||||
Loading…
Add table
Add a link
Reference in a new issue