Skip to content

Commit 46484ea

Browse files
authored
Merge pull request #6095 from IntersectMBO/smelc/cardano-testnet-custom-output-dir
cardano-tesnet: allow to specify output directory
2 parents 7c2c630 + 4405b07 commit 46484ea

File tree

7 files changed

+46
-16
lines changed

7 files changed

+46
-16
lines changed

cardano-testnet/src/Parsers/Cardano.hs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,11 @@ pCardanoTestnetCliOptions envCli = CardanoTestnetOptions
5757
<> OA.help "Enable new epoch state logging to logs/ledger-epoch-state.log"
5858
<> OA.showDefault
5959
)
60+
<*> optional (OA.strOption
61+
( OA.long "output-dir"
62+
<> OA.help "Directory where to store files, sockets, and so on. It is created if it doesn't exist. If unset, a temporary directory is used."
63+
<> OA.metavar "DIRECTORY"
64+
))
6065
where
6166
pAnyShelleyBasedEra' :: Parser AnyShelleyBasedEra
6267
pAnyShelleyBasedEra' =

cardano-testnet/src/Parsers/Run.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,4 +53,4 @@ runTestnetCmd = \case
5353

5454
runCardanoOptions :: CardanoTestnetCliOptions -> IO ()
5555
runCardanoOptions (CardanoTestnetCliOptions testnetOptions shelleyOptions) =
56-
runTestnet $ cardanoTestnetDefault testnetOptions shelleyOptions
56+
runTestnet testnetOptions $ cardanoTestnetDefault testnetOptions shelleyOptions

cardano-testnet/src/Testnet/Filepath.hs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ makeSprocket
2525
makeSprocket tmpAbsPath node
2626
= Sprocket (makeTmpBaseAbsPath tmpAbsPath) (makeSocketDir tmpAbsPath </> node)
2727

28+
-- TODO rename me: since the introduction of --output-dir in the cardano-testnet
29+
-- executable, this is a directory that can persist after the test ends.
2830
-- Temporary path used at runtime
2931
newtype TmpAbsolutePath = TmpAbsolutePath
3032
{ unTmpAbsPath :: FilePath

cardano-testnet/src/Testnet/Property/Run.hs

Lines changed: 31 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
{-# LANGUAGE NamedFieldPuns #-}
2+
13
module Testnet.Property.Run
24
( runTestnet
35
-- Ignore tests on various OSs
@@ -19,11 +21,12 @@ import Data.Bool (bool)
1921
import Data.String (IsString (..))
2022
import qualified System.Console.ANSI as ANSI
2123
import System.Console.ANSI (Color (..), ColorIntensity (..), ConsoleLayer (..), SGR (..))
24+
import System.Directory
2225
import qualified System.Exit as IO
2326
import qualified System.Info as SYS
2427
import qualified System.IO as IO
2528

26-
import Testnet.Property.Util (integrationWorkspace)
29+
import Testnet.Property.Util (integration, integrationWorkspace)
2730
import Testnet.Start.Types
2831

2932
import Hedgehog (Property)
@@ -35,11 +38,11 @@ import qualified Test.Tasty.Hedgehog as H
3538
import Test.Tasty.Providers (testPassed)
3639
import Test.Tasty.Runners (Result (resultShortDescription), TestTree)
3740

38-
runTestnet :: (Conf -> H.Integration a) -> IO ()
39-
runTestnet tn = do
41+
runTestnet :: CardanoTestnetOptions -> (Conf -> H.Integration a) -> IO ()
42+
runTestnet tnOpts tn = do
4043
tvRunning <- STM.newTVarIO False
4144

42-
void . H.check $ testnetProperty $ \c -> do
45+
void . H.check $ testnetProperty tnOpts $ \c -> do
4346
void $ tn c
4447
H.evalIO . STM.atomically $ STM.writeTVar tvRunning True
4548

@@ -60,17 +63,30 @@ runTestnet tn = do
6063
IO.exitFailure
6164

6265

63-
testnetProperty :: (Conf -> H.Integration ()) -> H.Property
64-
testnetProperty tn = integrationWorkspace "testnet" $ \workspaceDir -> do
65-
conf <- mkConf workspaceDir
66-
67-
-- Fork a thread to keep alive indefinitely any resources allocated by testnet.
68-
void . H.evalM . liftResourceT . resourceForkIO . forever . liftIO $ IO.threadDelay 10000000
69-
70-
void $ tn conf
71-
72-
H.failure -- Intentional failure to force failure report
73-
66+
testnetProperty :: CardanoTestnetOptions -> (Conf -> H.Integration ()) -> H.Property
67+
testnetProperty CardanoTestnetOptions{cardanoOutputDir} runTn =
68+
case cardanoOutputDir of
69+
Nothing -> do
70+
integrationWorkspace "testnet" $ \workspaceDir -> do
71+
mkConf workspaceDir >>= forkAndRunTestnet
72+
Just userOutputDir ->
73+
integration $ do
74+
absUserOutputDir <- H.evalIO $ makeAbsolute userOutputDir
75+
dirExists <- H.evalIO $ doesDirectoryExist absUserOutputDir
76+
(if dirExists then
77+
-- Likely dangerous, but who are we to judge the user?
78+
H.note_ $ "Reusing " <> absUserOutputDir
79+
else do
80+
liftIO $ createDirectory absUserOutputDir
81+
H.note_ $ "Created " <> absUserOutputDir)
82+
conf <- mkConf absUserOutputDir
83+
forkAndRunTestnet conf
84+
where
85+
forkAndRunTestnet conf = do
86+
-- Fork a thread to keep alive indefinitely any resources allocated by testnet.
87+
void $ H.evalM . liftResourceT . resourceForkIO . forever . liftIO $ IO.threadDelay 10000000
88+
void $ runTn conf
89+
H.failure -- Intentional failure to force failure report
7490

7591
-- Ignore properties on various OSs
7692

cardano-testnet/src/Testnet/Start/Types.hs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ data CardanoTestnetOptions = CardanoTestnetOptions
7474
, cardanoNodeLoggingFormat :: NodeLoggingFormat
7575
, cardanoNumDReps :: NumDReps -- ^ The number of DReps to generate at creation
7676
, cardanoEnableNewEpochStateLogging :: Bool -- ^ if epoch state logging is enabled
77+
, cardanoOutputDir :: Maybe FilePath -- ^ The output directory where to store files, sockets, and so on. If unset, a temporary directory is used.
7778
} deriving (Eq, Show)
7879

7980
cardanoNumPools :: CardanoTestnetOptions -> NumPools
@@ -105,6 +106,7 @@ instance Default CardanoTestnetOptions where
105106
, cardanoNodeLoggingFormat = NodeLoggingFormatAsJson
106107
, cardanoNumDReps = 3
107108
, cardanoEnableNewEpochStateLogging = True
109+
, cardanoOutputDir = Nothing
108110
}
109111

110112
-- | Options that are implemented by writing fields in the Shelley genesis file.

cardano-testnet/test/cardano-testnet-golden/files/golden/help.cli

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ Usage: cardano-testnet cardano [--num-pool-nodes COUNT]
1313
[--nodeLoggingFormat LOGGING_FORMAT]
1414
[--num-dreps NUMBER]
1515
[--enable-new-epoch-state-logging]
16+
[--output-dir DIRECTORY]
1617
--testnet-magic INT
1718
[--epoch-length SLOTS]
1819
[--slot-length SECONDS]

cardano-testnet/test/cardano-testnet-golden/files/golden/help/cardano.cli

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ Usage: cardano-testnet cardano [--num-pool-nodes COUNT]
1111
[--nodeLoggingFormat LOGGING_FORMAT]
1212
[--num-dreps NUMBER]
1313
[--enable-new-epoch-state-logging]
14+
[--output-dir DIRECTORY]
1415
--testnet-magic INT
1516
[--epoch-length SLOTS]
1617
[--slot-length SECONDS]
@@ -44,6 +45,9 @@ Available options:
4445
--enable-new-epoch-state-logging
4546
Enable new epoch state logging to
4647
logs/ledger-epoch-state.log
48+
--output-dir DIRECTORY Directory where to store files, sockets, and so on.
49+
It is created if it doesn't exist. If unset, a
50+
temporary directory is used.
4751
--testnet-magic INT Specify a testnet magic id.
4852
--epoch-length SLOTS Epoch length, in number of slots (default: 500)
4953
--slot-length SECONDS Slot length (default: 0.1)

0 commit comments

Comments
 (0)