encapsule-0.6.1: test/EnterSpec.hs
-- SPDX-License-Identifier: Apache-2.0
module EnterSpec (spec) where
import Control.Exception (bracket)
import Data.List (isPrefixOf)
import Data.Maybe (fromMaybe)
import System.Directory (canonicalizePath, findExecutable, removeDirectoryRecursive)
import System.Environment (lookupEnv, setEnv, unsetEnv)
import System.FilePath ((</>))
import System.Posix.Files (setFileMode)
import System.Posix.Temp (mkdtemp)
import Test.Hspec
import EncapsuleTest (encapsule, encapsuleChecked)
spec :: Spec
spec = describe "enter container lifecycle" $ do
it "starts a stopped container before looking up its user and entering it" $
withPodman False False $ \logFile -> do
_ <- encapsuleChecked ["enter", "--debug"]
calls <- lines <$> readFile logFile
take 3 calls `shouldBe`
[ "ps --filter name=^encapsule-.*$ --format {{.Names}}"
, "ps -a --filter name=^encapsule-.*$ --format {{.Names}}"
, "start encapsule-test"
]
take 1 (drop 3 calls) `shouldSatisfy`
any ("exec encapsule-test /bin/sh -c " `isPrefixOf`)
last calls `shouldBe` enterExec
it "enters a running container without starting it again" $
withPodman True False $ \logFile -> do
_ <- encapsuleChecked ["enter", "--debug"]
calls <- lines <$> readFile logFile
calls `shouldSatisfy` notElem "start encapsule-test"
calls `shouldSatisfy`
notElem "ps -a --filter name=^encapsule-.*$ --format {{.Names}}"
last calls `shouldBe` enterExec
it "matches an explicit container name exactly and preserves its command" $
withPodman True False $ \logFile -> do
_ <- encapsuleChecked ["enter", "test", "--", "bash", "--noprofile"]
calls <- lines <$> readFile logFile
take 1 calls `shouldBe`
["ps --filter name=^encapsule-test$ --format {{.Names}}"]
last calls `shouldBe`
"exec -it --user tester -e LANG=C.UTF-8 --workdir /home/tester encapsule-test bash --noprofile"
it "does not attempt exec if starting the container fails" $
withPodman False True $ \logFile -> do
out <- encapsule ["enter", "--debug"]
out `shouldContain` "cannot start test container"
calls <- lines <$> readFile logFile
last calls `shouldBe` "start encapsule-test"
enterExec :: String
enterExec = "exec -it --user tester -e LANG=C.UTF-8 --workdir /home/tester encapsule-test bash -l"
withPodman :: Bool -> Bool -> (FilePath -> IO a) -> IO a
withPodman running failStart action = do
name <- fromMaybe "encapsule" <$> lookupEnv "ENCAPSULE"
exe <- findExecutable name >>= maybe (fail "encapsule executable not found") canonicalizePath
bracket (mkdtemp "/tmp/encapsule-enter-test-XXXXXX") removeDirectoryRecursive $ \dir -> do
let logFile = dir </> "calls"
stateFile = dir </> "running"
writeFile stateFile (if running then "true\n" else "false\n")
writeFile (dir </> "podman") $ unlines
[ "#!/bin/sh"
, "printf '%s\\n' \"$*\" >> " ++ show logFile
, "read -r running < " ++ show stateFile
, "case \"$1\" in"
, " ps) [ \"$running\" = true ] || [ \"$2\" = -a ] || exit 0"
, " echo encapsule-test ;;"
, " start) " ++ if failStart
then "echo 'cannot start test container' >&2; exit 1 ;;"
else "echo true > " ++ show stateFile ++ "; echo encapsule-test ;;"
, " exec) [ \"$running\" = true ] || exit 125"
, " if [ \"$2\" = encapsule-test ]; then"
, " printf 'tester\\n/home/tester\\n'"
, " fi ;;"
, " container) echo /home/tester ;;"
, " *) exit 1 ;;"
, "esac"
]
setFileMode (dir </> "podman") 0o700
withEnv "ENCAPSULE" exe $ withEnv "PATH" dir $ action logFile
withEnv :: String -> String -> IO a -> IO a
withEnv key value action =
bracket (lookupEnv key) restore $ \_ -> setEnv key value >> action
where
restore Nothing = unsetEnv key
restore (Just old) = setEnv key old