-
Notifications
You must be signed in to change notification settings - Fork 721
/
Rotator.hs
86 lines (78 loc) · 2.8 KB
/
Rotator.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
{-# LANGUAGE CPP #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE ScopedTypeVariables #-}
module Cardano.Tracer.Test.Logs.Rotator
( tests
) where
import Control.Concurrent (forkIO, killThread, threadDelay)
import Data.Word (Word16)
import Test.Tasty
import Test.Tasty.QuickCheck
import System.Directory
import System.FilePath
import Cardano.Tracer.Configuration
import Cardano.Tracer.Handlers.Logs.Log (isItLog)
import Cardano.Tracer.Run (runCardanoTracerWithConfig)
import Cardano.Tracer.Test.Forwarder (launchForwardersSimple)
tests :: TestTree
tests = localOption (QuickCheckTests 1) $ testGroup "Test.Logs.Rotator"
[ testProperty "basic" $ propRotator "127.0.0.1" 3020
]
propRotator
:: String
-> Word16
-> Property
propRotator host port = ioProperty $ do
tmpDir <- getTemporaryDirectory
let rootDir = tmpDir </> "test-logs-rotator"
-- Remove rootDir if needed.
removePathForcibly rootDir
-- Run cardano-tracer and demo-forwarder-mux.
tracerThr <- forkIO $ runCardanoTracerWithConfig (config rootDir)
threadDelay 500000
forwarderThr <- forkIO $ launchForwardersSimple (host, port)
-- Wait while rotation will occure...
threadDelay 25000000
-- Stop both sides.
killThread forwarderThr
killThread tracerThr
threadDelay 100000
-- Check that rootDir exists...
doesDirectoryExist rootDir >>= \case
True ->
-- ... and contains one node's subdir...
listDirectory rootDir >>= \case
[] -> false "root dir is empty"
[subDir] ->
withCurrentDirectory rootDir $
-- ... with *.log-files inside...
listDirectory subDir >>= \case
[] -> false "subdir is empty"
logsAndSymLink ->
withCurrentDirectory subDir $
case filter (isItLog format) logsAndSymLink of
[] -> false "subdir doesn't contain expected logs"
logsWeNeed -> do
let thereAreMoreThanOneLog = length logsWeNeed > 1
return $ thereAreMoreThanOneLog === True
_ -> false "root dir contains more than one subdir"
False -> false "root dir doesn't exist"
where
config rootDir' = TracerConfig
{ acceptAt = RemoteSocket host (fromIntegral port)
, loRequestNum = 1
, ekgRequestFreq = 1.0
, hasEKG = Nothing
, hasPrometheus = Nothing
, logging = [LoggingParams rootDir' FileMode format]
, rotation = Just $
RotationParams
{ rpLogLimitBytes = 100
, rpMaxAgeHours = 1
, rpKeepFilesNum = 10
}
}
format = AsText
false :: String -> IO Property
false msg = return . counterexample msg $ property False