Skip to content

Commit 8a4ae19

Browse files
committed
cardano-tracer: split the Linux-only cardano-tracer-test-ext from multi-platform cardano-tracer-test
1 parent b6553af commit 8a4ae19

File tree

8 files changed

+209
-123
lines changed

8 files changed

+209
-123
lines changed

Diff for: cardano-tracer/cardano-tracer.cabal

+46-4
Original file line numberDiff line numberDiff line change
@@ -187,10 +187,10 @@ library demo-forwarder-lib
187187

188188
hs-source-dirs: test
189189

190-
other-modules: Cardano.Tracer.Test.TestSetup
191-
Cardano.Tracer.Test.Utils
190+
other-modules: Cardano.Tracer.Test.Utils
192191

193192
exposed-modules: Cardano.Tracer.Test.Forwarder
193+
Cardano.Tracer.Test.TestSetup
194194

195195
build-depends: aeson
196196
, async
@@ -282,14 +282,57 @@ test-suite cardano-tracer-test
282282
Cardano.Tracer.Test.TestSetup
283283
Cardano.Tracer.Test.Utils
284284
Cardano.Tracer.Test.Queue.Tests
285+
286+
build-depends: aeson
287+
, async
288+
, bytestring
289+
, cardano-tracer
290+
, cborg
291+
, containers
292+
, contra-tracer
293+
, directory
294+
, ekg-core
295+
, ekg-forward
296+
, extra
297+
, filepath
298+
, generic-data
299+
, optparse-applicative-fork
300+
, ouroboros-network
301+
, ouroboros-network-framework
302+
, QuickCheck
303+
, stm
304+
, tasty
305+
, tasty-quickcheck
306+
, text
307+
, time
308+
, trace-dispatcher
309+
, trace-forward
310+
, unix-compat
311+
312+
ghc-options: -threaded
313+
-rtsopts
314+
-with-rtsopts=-N
315+
316+
test-suite cardano-tracer-test-ext
317+
import: base, project-config
318+
type: exitcode-stdio-1.0
319+
default-extensions: OverloadedStrings
320+
321+
hs-source-dirs: test
322+
323+
main-is: cardano-tracer-test-ext.hs
324+
325+
other-modules: Cardano.Tracer.Test.Forwarder
326+
Cardano.Tracer.Test.TestSetup
327+
Cardano.Tracer.Test.Utils
285328
Cardano.Tracer.Test.ForwardingStressTest.Script
286329
Cardano.Tracer.Test.ForwardingStressTest.Config
287330
Cardano.Tracer.Test.ForwardingStressTest.Messages
288331
Cardano.Tracer.Test.ForwardingStressTest.Types
289332

290333
build-tool-depends: cardano-tracer:cardano-tracer
291334

292-
-- Sadly, this no longer works on Windows, because of the last external-tracer test:
335+
-- Sadly, this does not work on Windows (Path vs. PATH?):
293336
-- *** Failed! Exception: 'cardano-tracer: spawnProcess: failed (Success)' (after 1 test):
294337
if os(windows)
295338
buildable: False
@@ -313,7 +356,6 @@ test-suite cardano-tracer-test
313356
, ouroboros-network-framework
314357
, process
315358
, QuickCheck
316-
, stm
317359
, tasty
318360
, tasty-quickcheck
319361
, text

Diff for: cardano-tracer/demo/ssh/forwarder.hs

+20-4
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,27 @@
11
{-# LANGUAGE LambdaCase #-}
22

3+
import Data.Functor.Identity
34
import System.Environment (getArgs)
45

56
import Cardano.Tracer.Test.Forwarder
7+
import Cardano.Tracer.Test.TestSetup
68

79
main :: IO ()
8-
main = getArgs >>= \case
9-
[localSock, "Initiator"] -> launchForwardersSimple Initiator localSock 1000 2000
10-
[localSock, "Responder"] -> launchForwardersSimple Responder localSock 1000 2000
11-
_ -> putStrLn "Usage: ./demo-forwarder /path/to/local/sock Initiator|Responder"
10+
main = getArgs >>=
11+
\case
12+
[localSock, mode] ->
13+
let ts = TestSetup
14+
{ tsTime = Identity 0
15+
, tsThreads = Identity 0
16+
, tsMessages = Identity $ Just 0
17+
, tsSockInternal = Identity localSock
18+
, tsSockExternal = Identity ""
19+
, tsNetworkMagic = Identity $ NetworkMagic 42
20+
, tsWorkDir = Identity "."
21+
}
22+
in case mode of
23+
"Initiator" -> launchForwardersSimple ts Initiator localSock 1000 2000
24+
"Responder" -> launchForwardersSimple ts Responder localSock 1000 2000
25+
_ -> err
26+
_ -> err
27+
where err = error "Usage: ./demo-forwarder /path/to/local/sock Initiator|Responder"

Diff for: cardano-tracer/test/Cardano/Tracer/Test/DataPoint/Tests.hs

-2
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@ import Test.Tasty
1616
import Test.Tasty.QuickCheck
1717
import System.Time.Extra
1818

19-
import Ouroboros.Network.Magic (NetworkMagic (..))
20-
2119
import Trace.Forward.Protocol.DataPoint.Type
2220
import Trace.Forward.Utils.DataPoint (askForDataPoints)
2321

Diff for: cardano-tracer/test/Cardano/Tracer/Test/ForwardingStressTest/Script.hs

+1-4
Original file line numberDiff line numberDiff line change
@@ -88,22 +88,19 @@ runScriptForwarding ts@TestSetup{..} msgCounter tracerGetter =
8888
let numMsg = sum (map (\ (Script sc) -> length sc) scripts'''')
8989
in if numMsg > 0 then do
9090
-- TODO mutiple files
91-
let logfileGlobPattern = unI tsWorkDir <> "/logs/*tracer-externalsock@0/node-*.json"
91+
let logfileGlobPattern = unI tsWorkDir <> "/logs/*sock@*/node-*.json"
9292
logs <- glob logfileGlobPattern
9393
logFile <- case logs of
9494
[] -> fail $ "No files match the logfile glob pattern: " <> logfileGlobPattern
9595
_:_:_ -> fail $ "More than one file matches the logfile glob pattern: " <> logfileGlobPattern
9696
x:_ -> pure x
9797
contents <- readFile logFile
9898
let lineLength = length (lines contents)
99-
putStrLn $ "Line length " ++ show lineLength
100-
putStrLn $ "Msg length " ++ show numMsg
10199
totalNumMsg <- atomicModifyIORef msgCounter (\ac ->
102100
let nc = ac + numMsg
103101
in (nc, nc))
104102
pure (totalNumMsg == lineLength)
105103
else do
106-
putStrLn "Empty test"
107104
pure True
108105

109106
)

Diff for: cardano-tracer/test/Cardano/Tracer/Test/Logs/Tests.hs

-2
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@ import System.Directory.Extra
1616
import System.FilePath
1717
import System.Time.Extra
1818

19-
import Ouroboros.Network.Magic (NetworkMagic (..))
20-
2119
import Cardano.Tracer.Configuration
2220
import Cardano.Tracer.Handlers.Logs.Utils (isItLog)
2321
import Cardano.Tracer.Run (doRunCardanoTracer)

Diff for: cardano-tracer/test/Cardano/Tracer/Test/TestSetup.hs

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
module Cardano.Tracer.Test.TestSetup
99
( module Cardano.Tracer.Test.TestSetup
10+
, module Ouroboros.Network.Magic
1011
)
1112
where
1213

Diff for: cardano-tracer/test/cardano-tracer-test-ext.hs

+136
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
{-# LANGUAGE RecordWildCards #-}
2+
{-# LANGUAGE ScopedTypeVariables #-}
3+
{-# OPTIONS_GHC -Wno-unused-matches #-}
4+
5+
import Control.Concurrent (threadDelay)
6+
import Control.Exception
7+
import Control.Monad.Extra
8+
import Data.Functor ((<&>))
9+
import Data.Functor.Identity
10+
import Data.IORef (IORef, newIORef, readIORef, writeIORef)
11+
import qualified Data.List as L
12+
import Data.Monoid
13+
import qualified System.Directory as Sys
14+
import System.Environment (setEnv, unsetEnv)
15+
import qualified System.IO as Sys
16+
import System.PosixCompat.Files (fileExist)
17+
import qualified System.Process as Sys
18+
import Test.Tasty
19+
import Test.Tasty.QuickCheck
20+
21+
import Cardano.Logging
22+
import Cardano.Tracer.Test.ForwardingStressTest.Messages
23+
import Cardano.Tracer.Test.ForwardingStressTest.Script
24+
import Cardano.Tracer.Test.ForwardingStressTest.Types
25+
import Cardano.Tracer.Test.Utils
26+
import Ouroboros.Network.Magic (NetworkMagic (..))
27+
import Ouroboros.Network.NodeToClient (withIOManager)
28+
29+
main :: IO ()
30+
main = do
31+
setEnv "TASTY_NUM_THREADS" "1" -- For sequential running of tests (because of Windows).
32+
33+
ts' <- getTestSetup
34+
TestSetup
35+
{ tsTime = Last $ Just 10.0
36+
, tsThreads = Last $ Just 5
37+
, tsMessages = Last Nothing
38+
, tsSockInternal = Last $ Just "tracer.sock"
39+
, tsSockExternal = Last $ Just "tracer.sock"
40+
, tsNetworkMagic = Last $ Just $ NetworkMagic 42
41+
, tsWorkDir = Last $ Just "./test"
42+
}
43+
44+
-- 1. Prepare directory hierarchy
45+
tracerRoot <- Sys.canonicalizePath $ unI (tsWorkDir ts')
46+
putStrLn . mconcat $ [ "tsWorkDir ts: ", tracerRoot ]
47+
-- Weird: using path canonicalisation leads to process shutdown failures
48+
whenM (fileExist tracerRoot) $
49+
Sys.removeDirectoryRecursive tracerRoot
50+
Sys.createDirectoryIfMissing True (tracerRoot <> "/logs")
51+
Sys.setCurrentDirectory tracerRoot
52+
53+
sockInt <- Sys.canonicalizePath $ unI (tsSockInternal ts')
54+
sockExt <- Sys.canonicalizePath $ unI (tsSockExternal ts')
55+
let ts = ts' { tsWorkDir = Identity tracerRoot
56+
, tsSockInternal = Identity sockInt
57+
, tsSockExternal = Identity sockExt
58+
}
59+
putStrLn $ "Test setup: " <> show ts
60+
61+
-- 2. Actual tests
62+
msgCounterRef <- newIORef 0
63+
tracerRef <- newIORef Nothing
64+
let tracerGetter = getExternalTracerState ts tracerRef
65+
defaultMain (allTests ts msgCounterRef (tracerGetter <&> snd))
66+
`catch` (\ (e :: SomeException) -> do
67+
unsetEnv "TASTY_NUM_THREADS"
68+
trState <- readIORef tracerRef
69+
case trState of
70+
Nothing -> pure ()
71+
Just (tracerHdl, _) ->
72+
Sys.cleanupProcess (Nothing, Nothing, Nothing, tracerHdl)
73+
throwIO e)
74+
75+
allTests ::
76+
TestSetup Identity
77+
-> IORef Int
78+
-> IO (Trace IO Message)
79+
-> TestTree
80+
allTests ts msgCounter externalTracerGetter =
81+
testGroup "Tests"
82+
[ localOption (QuickCheckTests 10) $ testGroup "trace-forwarder"
83+
[ testProperty "multi-threaded forwarder stress test" $
84+
runScriptForwarding ts msgCounter externalTracerGetter
85+
]
86+
]
87+
88+
-- Caution: non-thread-safe!
89+
getExternalTracerState ::
90+
TestSetup Identity
91+
-> IORef (Maybe (Sys.ProcessHandle, Trace IO Message))
92+
-> IO (Sys.ProcessHandle, Trace IO Message)
93+
getExternalTracerState TestSetup{..} ref = do
94+
state <- readIORef ref
95+
case state of
96+
Just st -> pure st
97+
Nothing -> do
98+
stdTr <- standardTracer
99+
(procHdl, fwdTr) <- setupFwdTracer
100+
tr <- mkCardanoTracer
101+
stdTr fwdTr Nothing
102+
["Test"]
103+
namesForMessage severityForMessage privacyForMessage
104+
let st = (procHdl, tr)
105+
writeIORef ref $ Just st
106+
pure st
107+
where
108+
setupFwdTracer :: IO (Sys.ProcessHandle, Trace IO FormattedMessage)
109+
setupFwdTracer = do
110+
Sys.writeFile "config.yaml" . L.unlines $
111+
[ "networkMagic: " <> show (unNetworkMagic $ unI tsNetworkMagic)
112+
, "network:"
113+
, " tag: AcceptAt"
114+
, " contents: \""<> unI tsSockExternal <>"\""
115+
, "logging:"
116+
, "- logRoot: \"logs\""
117+
, " logMode: FileMode"
118+
, " logFormat: ForMachine"
119+
]
120+
externalTracerHdl <- Sys.spawnProcess "cardano-tracer"
121+
[ "--config" , "config.yaml"
122+
, "--state-dir" , unI tsWorkDir <> "/tracer-statedir"
123+
]
124+
threadDelay 1000000 --wait 1 seconds
125+
res <- Sys.getProcessExitCode externalTracerHdl
126+
case res of
127+
Nothing -> putStrLn "cardano-tracer started.."
128+
Just code ->
129+
error $ "cardano-tracer failed to start with code " <> show code
130+
-- TODO: check if this is the correct way to use withIOManager
131+
(forwardSink, _dpStore) <- withIOManager $ \iomgr -> do
132+
-- For simplicity, we are always 'Initiator',
133+
-- so 'cardano-tracer' is always a 'Responder'.
134+
let tracerSocketMode = Just (unI tsSockExternal, Initiator)
135+
initForwarding iomgr simpleTestConfig (unI tsNetworkMagic) Nothing tracerSocketMode
136+
pure (externalTracerHdl, forwardTracer forwardSink)

0 commit comments

Comments
 (0)