Backfill tests for fwd-env and try-fwd-env

This commit is contained in:
Alex David 2025-11-30 11:11:05 -08:00
commit 2f8e84bf0a
2 changed files with 53 additions and 0 deletions

View file

@ -10,6 +10,7 @@ import System.Posix (getProcessStatus, sigKILL, signalProcess)
import System.Process qualified as Process import System.Process qualified as Process
import Test.Hspec import Test.Hspec
import TestPrelude import TestPrelude
import GHC.IO.Exception (ExitCode(..))
spec :: Spec spec :: Spec
spec = parallel $ inTestM $ do spec = parallel $ inTestM $ do
@ -68,6 +69,26 @@ spec = parallel $ inTestM $ do
|] |]
`shouldOutput` "secret\n" `shouldOutput` "secret\n"
it "correctly escapes variables" $ do
withEnv "MY_ENV_VAR" "$(echo foo)" $ do
[i|
jail "test" (sh ''echo "''${MY_ENV_VAR-unset}"'') (c: [
(c.fwd-env "MY_ENV_VAR")
])
|]
`shouldOutput` "$(echo foo)\n"
it "exits non-zero if the variable isn't set at runtime" $ do
(exitCode, _stdout, stderr) <-
runNixDrvWithExitCode
[i|
jail "test" (sh ''echo ok'') (c: [
(c.fwd-env "MY_ENV_VAR")
])
|]
liftIO $ exitCode `shouldBe` ExitFailure 1
liftIO $ stderr `shouldContain` "MY_ENV_VAR: unbound variable"
describe "jail-to-host-channel" $ do describe "jail-to-host-channel" $ do
it "calls a script on the outside of the jail with an argument and returns its stdout" $ do it "calls a script on the outside of the jail with an argument and returns its stdout" $ do
tmpDir <- getTestDir tmpDir <- getTestDir
@ -379,6 +400,33 @@ spec = parallel $ inTestM $ do
|] |]
`shouldOutput` "UTC\n" `shouldOutput` "UTC\n"
describe "try-fwd-env" $ do
it "forwards the environment variable if set" $ do
withEnv "MY_ENV_VAR" "secret" $ do
[i|
jail "test" (sh ''echo "''${MY_ENV_VAR-unset}"'') (c: [
(c.try-fwd-env "MY_ENV_VAR")
])
|]
`shouldOutput` "secret\n"
it "correctly escapes variables" $ do
withEnv "MY_ENV_VAR" "$(echo foo)" $ do
[i|
jail "test" (sh ''echo "''${MY_ENV_VAR-unset}"'') (c: [
(c.try-fwd-env "MY_ENV_VAR")
])
|]
`shouldOutput` "$(echo foo)\n"
it "does not set the environment variable if it is not set" $ do
[i|
jail "test" (sh ''echo "''${MY_ENV_VAR-unset}"'') (c: [
(c.try-fwd-env "MY_ENV_VAR")
])
|]
`shouldOutput` "unset\n"
describe "try-readonly" $ do describe "try-readonly" $ do
it "binds paths as readonly in the jail" $ do it "binds paths as readonly in the jail" $ do
tmpDir <- getTestDir tmpDir <- getTestDir

View file

@ -10,6 +10,7 @@ module TestPrelude
inTestM, inTestM,
liftIO, liftIO,
runNixDrv, runNixDrv,
runNixDrvWithExitCode,
spawnNixDrv, spawnNixDrv,
toNixString, toNixString,
void, void,
@ -34,6 +35,7 @@ import Data.Map qualified as Map
import Data.Maybe (fromJust) import Data.Maybe (fromJust)
import Data.String.Conversions import Data.String.Conversions
import Data.String.Interpolate (i) import Data.String.Interpolate (i)
import GHC.IO.Exception (ExitCode)
import GHC.IO.Handle (Handle) import GHC.IO.Handle (Handle)
import System.Environment (lookupEnv) import System.Environment (lookupEnv)
import System.FilePath ((</>)) import System.FilePath ((</>))
@ -122,6 +124,9 @@ mkNixDrvProccess nixDrvExpr = do
runNixDrv :: String -> TestM String runNixDrv :: String -> TestM String
runNixDrv = liftIO . flip Process.readCreateProcess "" <=< mkNixDrvProccess runNixDrv = liftIO . flip Process.readCreateProcess "" <=< mkNixDrvProccess
runNixDrvWithExitCode :: String -> TestM (ExitCode, String, String)
runNixDrvWithExitCode = liftIO . flip Process.readCreateProcessWithExitCode "" <=< mkNixDrvProccess
spawnNixDrv :: String -> TestM (Maybe Handle, Maybe Handle, Maybe Handle, ProcessHandle) spawnNixDrv :: String -> TestM (Maybe Handle, Maybe Handle, Maybe Handle, ProcessHandle)
spawnNixDrv = liftIO . createProcess <=< mkNixDrvProccess spawnNixDrv = liftIO . createProcess <=< mkNixDrvProccess