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 "jail-to-host-channel" $ do it "calls a script on the outside of the jail with an argument and returns its stdout" $ do tmpDir <- getTestDir liftIO $ writeFile (tmpDir > "secret") "hunter2" [i| jail "test" (sh ''getsha1 "#{tmpDir > "secret"}"'') (c: [ (c.jail-to-host-channel "getsha1" '' echo "this runs outside of the jail" sha1sum < "$1" '') ]) |] `shouldOutput` "this runs outside of the jail\nf3bbbd66a63d4bf1747940578ec3d0103530e21d -\n" describe "network" $ do it "grants network access to the jailed program" $ do out <- runNixDrv [i| jail "test" (sh ''curl https://example.org'') (c: [ (c.add-pkg-deps [ pkgs.curl ]) c.network ]) |] liftIO $ out `shouldContain` "