packages feed

encapsule-0.6.1: test/VolumeSpec.hs

-- SPDX-License-Identifier: Apache-2.0

module VolumeSpec (spec) where

import Control.Exception (bracket)
import System.Directory (createDirectoryLink, removeDirectoryRecursive)
import System.Environment (lookupEnv, setEnv, unsetEnv)
import System.FilePath ((</>))
import System.Posix.Temp (mkdtemp)
import Test.Hspec

import Volume (addSelinuxLabel, disablesSelinux, expandVolumeHost)

spec :: Spec
spec = describe "volume handling" $ do
  it "keeps automatic relabeling for ordinary user-owned directories" $
    withTempDir $ \dir ->
      addSelinuxLabel False "/nonexistent-home" "/container-home" dir
        `shouldReturn` (dir ++ ":" ++ dir ++ ":z")

  it "does not automatically relabel when SELinux separation is disabled" $
    withTempDir $ \dir -> do
      addSelinuxLabel True "/nonexistent-home" "/container-home" dir
        `shouldReturn` (dir ++ ":" ++ dir)
      addSelinuxLabel True "/nonexistent-home" "/container-home" (dir ++ ":/data:ro")
        `shouldReturn` (dir ++ ":/data:ro")

  it "preserves explicitly requested labels and overlay options" $
    withTempDir $ \dir ->
      mapM_ (\opts ->
        addSelinuxLabel True "/nonexistent-home" "/container-home" (dir ++ ":/data:" ++ opts)
          `shouldReturn` (dir ++ ":/data:" ++ opts)) ["z", "Z", "O"]

  it "recognizes both forms of raw label=disable options" $ do
    disablesSelinux ["--ipc=host", "--security-opt=label=disable"] `shouldBe` True
    disablesSelinux ["--security-opt", "label=disable"] `shouldBe` True
    disablesSelinux ["--security-opt", "label=level:s0"] `shouldBe` False

  it "does not automatically relabel system mounts even when owned by the caller" $
    mapM_ (\path ->
      addSelinuxLabel False "/nonexistent-home" "/container-home" (path ++ ":rslave")
        `shouldReturn` (path ++ ":" ++ path ++ ":rslave"))
      ["/", "/dev", "/dev/", "/etc", "/usr", "/run", "/sys", "/proc", "/var", "/tmp"]

  it "recognizes system mounts through symlinks and parent traversal" $
    withTempDir $ \dir -> do
      let alias = dir </> "device-link"
      createDirectoryLink "/dev" alias
      mapM_ (\path ->
        addSelinuxLabel False "/nonexistent-home" "/container-home" path
          `shouldReturn` (path ++ ":" ++ path)) [alias, "/tmp/../dev"]

  it "rejects empty mount sources, including empty environment expansion" $
    withTempDir $ \dir -> do
      expandVolumeHost dir "" `shouldThrow` errorCall "volume host path must not be empty"
      bracket (lookupEnv "ENCAPSULE_TEST_VOLUME_SOURCE") restore $ \_ -> do
        setEnv "ENCAPSULE_TEST_VOLUME_SOURCE" ""
        expandVolumeHost dir "$ENCAPSULE_TEST_VOLUME_SOURCE" `shouldThrow` errorCall "volume host path must not be empty"
        addSelinuxLabel True dir dir "$ENCAPSULE_TEST_VOLUME_SOURCE:/data"
          `shouldThrow` errorCall "volume host path must not be empty"
  where
    restore Nothing = unsetEnv "ENCAPSULE_TEST_VOLUME_SOURCE"
    restore (Just value) = setEnv "ENCAPSULE_TEST_VOLUME_SOURCE" value

withTempDir :: (FilePath -> IO a) -> IO a
withTempDir = bracket (mkdtemp "/tmp/encapsule-volume-test-XXXXXX") removeDirectoryRecursive