Skip to content

Commit

Permalink
cardano-tracer: split the Linux-only cardano-tracer-test-ext from mul…
Browse files Browse the repository at this point in the history
…ti-platform cardano-tracer-test
  • Loading branch information
deepfire committed Nov 9, 2022
1 parent b6553af commit 8a4ae19
Show file tree
Hide file tree
Showing 8 changed files with 209 additions and 123 deletions.
50 changes: 46 additions & 4 deletions cardano-tracer/cardano-tracer.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -187,10 +187,10 @@ library demo-forwarder-lib

hs-source-dirs: test

other-modules: Cardano.Tracer.Test.TestSetup
Cardano.Tracer.Test.Utils
other-modules: Cardano.Tracer.Test.Utils

exposed-modules: Cardano.Tracer.Test.Forwarder
Cardano.Tracer.Test.TestSetup

build-depends: aeson
, async
Expand Down Expand Up @@ -282,14 +282,57 @@ test-suite cardano-tracer-test
Cardano.Tracer.Test.TestSetup
Cardano.Tracer.Test.Utils
Cardano.Tracer.Test.Queue.Tests

build-depends: aeson
, async
, bytestring
, cardano-tracer
, cborg
, containers
, contra-tracer
, directory
, ekg-core
, ekg-forward
, extra
, filepath
, generic-data
, optparse-applicative-fork
, ouroboros-network
, ouroboros-network-framework
, QuickCheck
, stm
, tasty
, tasty-quickcheck
, text
, time
, trace-dispatcher
, trace-forward
, unix-compat

ghc-options: -threaded
-rtsopts
-with-rtsopts=-N

test-suite cardano-tracer-test-ext
import: base, project-config
type: exitcode-stdio-1.0
default-extensions: OverloadedStrings

hs-source-dirs: test

main-is: cardano-tracer-test-ext.hs

other-modules: Cardano.Tracer.Test.Forwarder
Cardano.Tracer.Test.TestSetup
Cardano.Tracer.Test.Utils
Cardano.Tracer.Test.ForwardingStressTest.Script
Cardano.Tracer.Test.ForwardingStressTest.Config
Cardano.Tracer.Test.ForwardingStressTest.Messages
Cardano.Tracer.Test.ForwardingStressTest.Types

build-tool-depends: cardano-tracer:cardano-tracer

-- Sadly, this no longer works on Windows, because of the last external-tracer test:
-- Sadly, this does not work on Windows (Path vs. PATH?):
-- *** Failed! Exception: 'cardano-tracer: spawnProcess: failed (Success)' (after 1 test):
if os(windows)
buildable: False
Expand All @@ -313,7 +356,6 @@ test-suite cardano-tracer-test
, ouroboros-network-framework
, process
, QuickCheck
, stm
, tasty
, tasty-quickcheck
, text
Expand Down
24 changes: 20 additions & 4 deletions cardano-tracer/demo/ssh/forwarder.hs
Original file line number Diff line number Diff line change
@@ -1,11 +1,27 @@
{-# LANGUAGE LambdaCase #-}

import Data.Functor.Identity
import System.Environment (getArgs)

import Cardano.Tracer.Test.Forwarder
import Cardano.Tracer.Test.TestSetup

main :: IO ()
main = getArgs >>= \case
[localSock, "Initiator"] -> launchForwardersSimple Initiator localSock 1000 2000
[localSock, "Responder"] -> launchForwardersSimple Responder localSock 1000 2000
_ -> putStrLn "Usage: ./demo-forwarder /path/to/local/sock Initiator|Responder"
main = getArgs >>=
\case
[localSock, mode] ->
let ts = TestSetup
{ tsTime = Identity 0
, tsThreads = Identity 0
, tsMessages = Identity $ Just 0
, tsSockInternal = Identity localSock
, tsSockExternal = Identity ""
, tsNetworkMagic = Identity $ NetworkMagic 42
, tsWorkDir = Identity "."
}
in case mode of
"Initiator" -> launchForwardersSimple ts Initiator localSock 1000 2000
"Responder" -> launchForwardersSimple ts Responder localSock 1000 2000
_ -> err
_ -> err
where err = error "Usage: ./demo-forwarder /path/to/local/sock Initiator|Responder"
2 changes: 0 additions & 2 deletions cardano-tracer/test/Cardano/Tracer/Test/DataPoint/Tests.hs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@ import Test.Tasty
import Test.Tasty.QuickCheck
import System.Time.Extra

import Ouroboros.Network.Magic (NetworkMagic (..))

import Trace.Forward.Protocol.DataPoint.Type
import Trace.Forward.Utils.DataPoint (askForDataPoints)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,22 +88,19 @@ runScriptForwarding ts@TestSetup{..} msgCounter tracerGetter =
let numMsg = sum (map (\ (Script sc) -> length sc) scripts'''')
in if numMsg > 0 then do
-- TODO mutiple files
let logfileGlobPattern = unI tsWorkDir <> "/logs/*tracer-externalsock@0/node-*.json"
let logfileGlobPattern = unI tsWorkDir <> "/logs/*sock@*/node-*.json"
logs <- glob logfileGlobPattern
logFile <- case logs of
[] -> fail $ "No files match the logfile glob pattern: " <> logfileGlobPattern
_:_:_ -> fail $ "More than one file matches the logfile glob pattern: " <> logfileGlobPattern
x:_ -> pure x
contents <- readFile logFile
let lineLength = length (lines contents)
putStrLn $ "Line length " ++ show lineLength
putStrLn $ "Msg length " ++ show numMsg
totalNumMsg <- atomicModifyIORef msgCounter (\ac ->
let nc = ac + numMsg
in (nc, nc))
pure (totalNumMsg == lineLength)
else do
putStrLn "Empty test"
pure True

)
Expand Down
2 changes: 0 additions & 2 deletions cardano-tracer/test/Cardano/Tracer/Test/Logs/Tests.hs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@ import System.Directory.Extra
import System.FilePath
import System.Time.Extra

import Ouroboros.Network.Magic (NetworkMagic (..))

import Cardano.Tracer.Configuration
import Cardano.Tracer.Handlers.Logs.Utils (isItLog)
import Cardano.Tracer.Run (doRunCardanoTracer)
Expand Down
1 change: 1 addition & 0 deletions cardano-tracer/test/Cardano/Tracer/Test/TestSetup.hs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

module Cardano.Tracer.Test.TestSetup
( module Cardano.Tracer.Test.TestSetup
, module Ouroboros.Network.Magic
)
where

Expand Down
136 changes: 136 additions & 0 deletions cardano-tracer/test/cardano-tracer-test-ext.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
{-# LANGUAGE RecordWildCards #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# OPTIONS_GHC -Wno-unused-matches #-}

import Control.Concurrent (threadDelay)
import Control.Exception
import Control.Monad.Extra
import Data.Functor ((<&>))
import Data.Functor.Identity
import Data.IORef (IORef, newIORef, readIORef, writeIORef)
import qualified Data.List as L
import Data.Monoid
import qualified System.Directory as Sys
import System.Environment (setEnv, unsetEnv)
import qualified System.IO as Sys
import System.PosixCompat.Files (fileExist)
import qualified System.Process as Sys
import Test.Tasty
import Test.Tasty.QuickCheck

import Cardano.Logging
import Cardano.Tracer.Test.ForwardingStressTest.Messages
import Cardano.Tracer.Test.ForwardingStressTest.Script
import Cardano.Tracer.Test.ForwardingStressTest.Types
import Cardano.Tracer.Test.Utils
import Ouroboros.Network.Magic (NetworkMagic (..))
import Ouroboros.Network.NodeToClient (withIOManager)

main :: IO ()
main = do
setEnv "TASTY_NUM_THREADS" "1" -- For sequential running of tests (because of Windows).

ts' <- getTestSetup
TestSetup
{ tsTime = Last $ Just 10.0
, tsThreads = Last $ Just 5
, tsMessages = Last Nothing
, tsSockInternal = Last $ Just "tracer.sock"
, tsSockExternal = Last $ Just "tracer.sock"
, tsNetworkMagic = Last $ Just $ NetworkMagic 42
, tsWorkDir = Last $ Just "./test"
}

-- 1. Prepare directory hierarchy
tracerRoot <- Sys.canonicalizePath $ unI (tsWorkDir ts')
putStrLn . mconcat $ [ "tsWorkDir ts: ", tracerRoot ]
-- Weird: using path canonicalisation leads to process shutdown failures
whenM (fileExist tracerRoot) $
Sys.removeDirectoryRecursive tracerRoot
Sys.createDirectoryIfMissing True (tracerRoot <> "/logs")
Sys.setCurrentDirectory tracerRoot

sockInt <- Sys.canonicalizePath $ unI (tsSockInternal ts')
sockExt <- Sys.canonicalizePath $ unI (tsSockExternal ts')
let ts = ts' { tsWorkDir = Identity tracerRoot
, tsSockInternal = Identity sockInt
, tsSockExternal = Identity sockExt
}
putStrLn $ "Test setup: " <> show ts

-- 2. Actual tests
msgCounterRef <- newIORef 0
tracerRef <- newIORef Nothing
let tracerGetter = getExternalTracerState ts tracerRef
defaultMain (allTests ts msgCounterRef (tracerGetter <&> snd))
`catch` (\ (e :: SomeException) -> do
unsetEnv "TASTY_NUM_THREADS"
trState <- readIORef tracerRef
case trState of
Nothing -> pure ()
Just (tracerHdl, _) ->
Sys.cleanupProcess (Nothing, Nothing, Nothing, tracerHdl)
throwIO e)

allTests ::
TestSetup Identity
-> IORef Int
-> IO (Trace IO Message)
-> TestTree
allTests ts msgCounter externalTracerGetter =
testGroup "Tests"
[ localOption (QuickCheckTests 10) $ testGroup "trace-forwarder"
[ testProperty "multi-threaded forwarder stress test" $
runScriptForwarding ts msgCounter externalTracerGetter
]
]

-- Caution: non-thread-safe!
getExternalTracerState ::
TestSetup Identity
-> IORef (Maybe (Sys.ProcessHandle, Trace IO Message))
-> IO (Sys.ProcessHandle, Trace IO Message)
getExternalTracerState TestSetup{..} ref = do
state <- readIORef ref
case state of
Just st -> pure st
Nothing -> do
stdTr <- standardTracer
(procHdl, fwdTr) <- setupFwdTracer
tr <- mkCardanoTracer
stdTr fwdTr Nothing
["Test"]
namesForMessage severityForMessage privacyForMessage
let st = (procHdl, tr)
writeIORef ref $ Just st
pure st
where
setupFwdTracer :: IO (Sys.ProcessHandle, Trace IO FormattedMessage)
setupFwdTracer = do
Sys.writeFile "config.yaml" . L.unlines $
[ "networkMagic: " <> show (unNetworkMagic $ unI tsNetworkMagic)
, "network:"
, " tag: AcceptAt"
, " contents: \""<> unI tsSockExternal <>"\""
, "logging:"
, "- logRoot: \"logs\""
, " logMode: FileMode"
, " logFormat: ForMachine"
]
externalTracerHdl <- Sys.spawnProcess "cardano-tracer"
[ "--config" , "config.yaml"
, "--state-dir" , unI tsWorkDir <> "/tracer-statedir"
]
threadDelay 1000000 --wait 1 seconds
res <- Sys.getProcessExitCode externalTracerHdl
case res of
Nothing -> putStrLn "cardano-tracer started.."
Just code ->
error $ "cardano-tracer failed to start with code " <> show code
-- TODO: check if this is the correct way to use withIOManager
(forwardSink, _dpStore) <- withIOManager $ \iomgr -> do
-- For simplicity, we are always 'Initiator',
-- so 'cardano-tracer' is always a 'Responder'.
let tracerSocketMode = Just (unI tsSockExternal, Initiator)
initForwarding iomgr simpleTestConfig (unI tsNetworkMagic) Nothing tracerSocketMode
pure (externalTracerHdl, forwardTracer forwardSink)
Loading

0 comments on commit 8a4ae19

Please sign in to comment.