From 9d245e2baaa5dd7b8fe6c87c89493dd5098b9bd2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=BCrgen=20Nicklisch-Franken?= Date: Mon, 3 Nov 2025 13:20:42 +0100 Subject: [PATCH 1/2] cardano-tracer: added restart test case --- cardano-tracer/cardano-tracer.cabal | 24 +++ .../test/restart-test/restart-config.yaml | 8 + .../test/restart-test/restart-test.hs | 137 ++++++++++++++++++ 3 files changed, 169 insertions(+) create mode 100644 cardano-tracer/test/restart-test/restart-config.yaml create mode 100644 cardano-tracer/test/restart-test/restart-test.hs diff --git a/cardano-tracer/cardano-tracer.cabal b/cardano-tracer/cardano-tracer.cabal index f1b91539020..935b301231e 100644 --- a/cardano-tracer/cardano-tracer.cabal +++ b/cardano-tracer/cardano-tracer.cabal @@ -440,6 +440,30 @@ test-suite cardano-tracer-test-ext -rtsopts -with-rtsopts=-N +test-suite cardano-tracer-restart + import: project-config + type: exitcode-stdio-1.0 + + -- we expect unixy paths + if os(windows) + buildable: False + + hs-source-dirs: test/restart-test + main-is: restart-test.hs + + build-tool-depends: cardano-tracer:cardano-tracer + , cardano-tracer:demo-forwarder + + -- keep this minimal: what the script actually uses + build-depends: base + , directory + , filepath + , process + + ghc-options: -threaded + -rtsopts + -with-rtsopts=-N + benchmark cardano-tracer-bench import: project-config type: exitcode-stdio-1.0 diff --git a/cardano-tracer/test/restart-test/restart-config.yaml b/cardano-tracer/test/restart-test/restart-config.yaml new file mode 100644 index 00000000000..d64e32456a7 --- /dev/null +++ b/cardano-tracer/test/restart-test/restart-config.yaml @@ -0,0 +1,8 @@ +networkMagic: 42 +network: + tag: AcceptAt + contents: "sock" +logging: +- logRoot: "/tmp/cardano-tracer-restart-test-logs" + logMode: FileMode + logFormat: ForMachine diff --git a/cardano-tracer/test/restart-test/restart-test.hs b/cardano-tracer/test/restart-test/restart-test.hs new file mode 100644 index 00000000000..d3c7e7a7a63 --- /dev/null +++ b/cardano-tracer/test/restart-test/restart-test.hs @@ -0,0 +1,137 @@ +{-# LANGUAGE ScopedTypeVariables #-} + +module Main where + +import Control.Concurrent (threadDelay) +import Control.Exception (IOException, catch, finally) +import Control.Monad (unless, void, when) +import System.Directory (doesFileExist, getCurrentDirectory, listDirectory, + removePathForcibly) +import System.Exit (exitFailure) +import System.FilePath (takeDirectory, ()) +import System.IO (hPutStrLn, stderr) +import System.Process (CreateProcess (..), ProcessHandle, StdStream (..), createProcess, + proc, terminateProcess, waitForProcess) + +main :: IO () +main = do + let dir = "/tmp/cardano-tracer-restart-test-logs" + sock = "sock" + logLink = dir (sock ++ "@0") "node.json" + waitSecs = 5 + + cfg <- resolveConfigPath + + -- Fresh start. + catchIgnore (removePathForcibly dir) + + -- Run demo-forwarder + forwarderH <- startProc "demo-forwarder" [sock, "Initiator"] + + let cleanup = catchIgnore (removePathForcibly dir) + + -- Ensure the forwarder is killed and logs cleaned at the very end + finally + (do + -- Start first cardano-tracer + tracer1H <- startProc "cardano-tracer" ["--config", cfg] + + finally + (do + sleepSeconds waitSecs + ensureNonEmpty logLink + "Nothing has been written to the file with trace messages" + + -- Kill first tracer + terminateProcess tracer1H + void (waitForProcess tracer1H) + + -- Start second tracer + tracer2H <- startProc "cardano-tracer" ["--config", cfg] + + finally + (do + sleepSeconds waitSecs + ensureNonEmpty logLink + "Nothing has been written to the file since the restart of cardano-tracer" + + let logDir = dir (sock ++ "@0") + entries <- listDirectory logDir + let candidates = filter (not . isSpecialEntry) entries + let count = length candidates + when (count /= 2) $ do + hPutStrLn stderr $ + "Two log files are expected to be present, found: " ++ show count + exitFailure + pure ()) + (safeTerminate tracer2H)) + (safeTerminate tracer1H)) + (do + safeTerminate forwarderH + cleanup) + +-------------------------------------------------------------------------------- +-- Helpers + +startProc :: FilePath -> [String] -> IO ProcessHandle +startProc cmd args = do + (_, _, _, ph) <- createProcess (proc cmd args) + { std_in = Inherit + , std_out = Inherit + , std_err = Inherit + } + pure ph + +sleepSeconds :: Int -> IO () +sleepSeconds s = threadDelay (s * 1000000) + +ensureNonEmpty :: FilePath -> String -> IO () +ensureNonEmpty fp errMsg = do + exists <- doesFileExist fp + unless exists $ failWith errMsg + sz <- getFileSize fp + when (sz == 0) $ failWith errMsg + where + failWith msg = hPutStrLn stderr msg >> exitFailure + +getFileSize :: FilePath -> IO Integer +getFileSize fp = do + contents <- readFile fp + pure (toInteger (length contents)) + +safeTerminate :: ProcessHandle -> IO () +safeTerminate ph = terminateProcess ph >> void (waitForProcess ph) + +catchIgnore :: IO () -> IO () +catchIgnore act = act `catch` \(_ :: IOException) -> pure () + +isDot :: FilePath -> Bool +isDot "." = True +isDot ".." = True +isDot _ = False + +isCurrentLog :: FilePath -> Bool +isCurrentLog "node.json" = True +isCurrentLog _ = False + +isSpecialEntry :: FilePath -> Bool +isSpecialEntry fp = isDot fp || isCurrentLog fp + +resolveConfigPath :: IO FilePath +resolveConfigPath = go =<< getCurrentDirectory + where + configRelative root = + root "cardano-tracer" "test" "restart-test" "restart-config.yaml" + + go dir = do + let cfgPath = configRelative dir + exists <- doesFileExist cfgPath + if exists + then pure cfgPath + else do + let parent = takeDirectory dir + if parent == dir + then do + hPutStrLn stderr "Unable to locate restart-config.yaml" + exitFailure + else go parent From 2e3b91dc47dcab57e4a32d7330c562b3bad364d3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=BCrgen=20Nicklisch-Franken?= Date: Fri, 7 Nov 2025 11:16:02 +0100 Subject: [PATCH 2/2] hlint fix --- cardano-tracer/test/restart-test/restart-test.hs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/cardano-tracer/test/restart-test/restart-test.hs b/cardano-tracer/test/restart-test/restart-test.hs index d3c7e7a7a63..9d25da79eb9 100644 --- a/cardano-tracer/test/restart-test/restart-test.hs +++ b/cardano-tracer/test/restart-test/restart-test.hs @@ -46,6 +46,8 @@ main = do terminateProcess tracer1H void (waitForProcess tracer1H) + sleepSeconds 2 + -- Start second tracer tracer2H <- startProc "cardano-tracer" ["--config", cfg] @@ -62,8 +64,7 @@ main = do when (count /= 2) $ do hPutStrLn stderr $ "Two log files are expected to be present, found: " ++ show count - exitFailure - pure ()) + exitFailure) (safeTerminate tracer2H)) (safeTerminate tracer1H)) (do