Fix network combinator with systemd resolved

This commit is contained in:
Alex David 2025-12-02 18:58:03 -08:00
commit eaf7e343d8
3 changed files with 74 additions and 1 deletions

View file

@ -5,12 +5,12 @@ import Control.Monad
import Data.List.Extra
import Data.Maybe (fromJust)
import Data.String.Interpolate.Util
import GHC.IO.Exception (ExitCode (..))
import System.Directory.Extra (createDirectoryIfMissing, doesFileExist, doesPathExist)
import System.Posix (getProcessStatus, sigKILL, signalProcess)
import System.Process qualified as Process
import Test.Hspec
import TestPrelude
import GHC.IO.Exception (ExitCode(..))
spec :: Spec
spec = parallel $ inTestM $ do
@ -115,6 +115,15 @@ spec = parallel $ inTestM $ do
|]
liftIO $ out `shouldContain` "<h1>Example Domain</h1>"
it "allows overriding the hostname with set-hostname" $ do
[i|
jail "test" (sh ''${pkgs.nettools}/bin/hostname'') (c: [
(c.set-hostname "my-hostname")
c.network
])
|]
`shouldOutput` "my-hostname\n"
it "does not add too many bwrap args" $ do
out <-
runNixDrv
@ -137,6 +146,18 @@ spec = parallel $ inTestM $ do
|]
liftIO $ (read out :: Int) `shouldSatisfy` (< 100)
it "works on systems using systemd-resolved" $ do
out <-
runNixDrvWithinVm
[i| { services.resolved.enable = true; } |]
[i|
jail "test" (sh ''curl https://example.org'') (c: [
(c.add-pkg-deps [ pkgs.curl ])
c.network
])
|]
liftIO $ out `shouldContain` "<h1>Example Domain</h1>"
describe "no-die-with-parent" $ do
let waitForChildPid :: Process.Pid -> TestM Process.Pid
waitForChildPid pid = do

View file

@ -11,6 +11,7 @@ module TestPrelude
liftIO,
runNixDrv,
runNixDrvWithExitCode,
runNixDrvWithinVm,
spawnNixDrv,
toNixString,
void,
@ -130,6 +131,55 @@ runNixDrvWithExitCode = liftIO . flip Process.readCreateProcessWithExitCode "" <
spawnNixDrv :: String -> TestM (Maybe Handle, Maybe Handle, Maybe Handle, ProcessHandle)
spawnNixDrv = liftIO . createProcess <=< mkNixDrvProccess
runNixDrvWithinVm :: String -> String -> TestM String
runNixDrvWithinVm vmConfig nixDrvExpr = do
testDir <- getTestDir
testOutputFilePath <- (</> "test-output") <$> getTestDir
liftIO $ writeFile testOutputFilePath "---\n"
void $
runNixDrvWithExitCode
[i|
let
system = import <nixpkgs/nixos/lib/eval-config.nix> {
modules = [
({ pkgs, lib, ... }: {
system.stateVersion = "25.05";
virtualisation.vmVariant.virtualisation = {
graphics = false;
sharedDirectories.testOutput = { source = #{toNixString testDir}; target = #{toNixString testDir}; };
};
systemd.services.run-jail-nix-vm-test = {
wantedBy = [ "multi-user.target" ];
after = [ "network-online.target" ];
wants = [ "network-online.target" ];
environment = {
HOME = "/root";
};
script = let
jail-nix = import <jail-nix> {};
jail = jail-nix.extend { inherit pkgs; };
testToRun = #{nixDrvExpr};
in ''
(
${lib.getExe testToRun} || echo 'Test failed'
) >${#{toNixString testOutputFilePath}} 2>&1
shutdown now
'';
serviceConfig.type = "oneshot";
};
})
#{vmConfig}
];
};
in sh ''
TMP_DIR=$(${pkgs.coreutils}/bin/mktemp -d)
NIX_DISK_IMAGE="$TMP_DIR/img" ${system.config.system.build.vm}/bin/run-nixos-vm
${pkgs.coreutils}/bin/rm -rf "$TMP_DIR"
''
|]
liftIO $ readFile testOutputFilePath
buildNixDrvPath :: FilePath -> TestM FilePath
buildNixDrvPath drvPath = do
liftIO $