Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Denis Shevchenko
committed
Jun 7, 2021
1 parent
5115a76
commit c8184e1
Showing
2 changed files
with
82 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,86 @@ | ||
{-# LANGUAGE CPP #-} | ||
{-# LANGUAGE LambdaCase #-} | ||
{-# LANGUAGE FlexibleInstances #-} | ||
{-# LANGUAGE ScopedTypeVariables #-} | ||
|
||
{-# OPTIONS_GHC -Wno-orphans #-} | ||
|
||
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 |