Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cardano-tesnet: allow to specify output directory #6095

Merged
merged 2 commits into from
Feb 4, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions cardano-testnet/src/Parsers/Cardano.hs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,11 @@ pCardanoTestnetCliOptions envCli = CardanoTestnetOptions
<> OA.help "Enable new epoch state logging to logs/ledger-epoch-state.log"
<> OA.showDefault
)
<*> optional (OA.strOption
( OA.long "output-dir"
<> 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."
<> OA.metavar "DIRECTORY"
))
where
pAnyShelleyBasedEra' :: Parser AnyShelleyBasedEra
pAnyShelleyBasedEra' =
Expand Down
2 changes: 1 addition & 1 deletion cardano-testnet/src/Parsers/Run.hs
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,4 @@ runTestnetCmd = \case

runCardanoOptions :: CardanoTestnetCliOptions -> IO ()
runCardanoOptions (CardanoTestnetCliOptions testnetOptions shelleyOptions) =
runTestnet $ cardanoTestnetDefault testnetOptions shelleyOptions
runTestnet testnetOptions $ cardanoTestnetDefault testnetOptions shelleyOptions
2 changes: 2 additions & 0 deletions cardano-testnet/src/Testnet/Filepath.hs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ makeSprocket
makeSprocket tmpAbsPath node
= Sprocket (makeTmpBaseAbsPath tmpAbsPath) (makeSocketDir tmpAbsPath </> node)

-- TODO rename me: since the introduction of --output-dir in the cardano-testnet
-- executable, this is a directory that can persist after the test ends.
-- Temporary path used at runtime
newtype TmpAbsolutePath = TmpAbsolutePath
{ unTmpAbsPath :: FilePath
Expand Down
46 changes: 31 additions & 15 deletions cardano-testnet/src/Testnet/Property/Run.hs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
{-# LANGUAGE NamedFieldPuns #-}

module Testnet.Property.Run
( runTestnet
-- Ignore tests on various OSs
Expand All @@ -19,11 +21,12 @@ import Data.Bool (bool)
import Data.String (IsString (..))
import qualified System.Console.ANSI as ANSI
import System.Console.ANSI (Color (..), ColorIntensity (..), ConsoleLayer (..), SGR (..))
import System.Directory
import qualified System.Exit as IO
import qualified System.Info as SYS
import qualified System.IO as IO

import Testnet.Property.Util (integrationWorkspace)
import Testnet.Property.Util (integration, integrationWorkspace)
import Testnet.Start.Types

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

runTestnet :: (Conf -> H.Integration a) -> IO ()
runTestnet tn = do
runTestnet :: CardanoTestnetOptions -> (Conf -> H.Integration a) -> IO ()
runTestnet tnOpts tn = do
tvRunning <- STM.newTVarIO False

void . H.check $ testnetProperty $ \c -> do
void . H.check $ testnetProperty tnOpts $ \c -> do
void $ tn c
H.evalIO . STM.atomically $ STM.writeTVar tvRunning True

Expand All @@ -60,17 +63,30 @@ runTestnet tn = do
IO.exitFailure


testnetProperty :: (Conf -> H.Integration ()) -> H.Property
testnetProperty tn = integrationWorkspace "testnet" $ \workspaceDir -> do
conf <- mkConf workspaceDir

-- Fork a thread to keep alive indefinitely any resources allocated by testnet.
void . H.evalM . liftResourceT . resourceForkIO . forever . liftIO $ IO.threadDelay 10000000

void $ tn conf

H.failure -- Intentional failure to force failure report

testnetProperty :: CardanoTestnetOptions -> (Conf -> H.Integration ()) -> H.Property
testnetProperty CardanoTestnetOptions{cardanoOutputDir} runTn =
case cardanoOutputDir of
carbolymer marked this conversation as resolved.
Show resolved Hide resolved
Nothing -> do
integrationWorkspace "testnet" $ \workspaceDir -> do
mkConf workspaceDir >>= forkAndRunTestnet
Just userOutputDir ->
integration $ do
absUserOutputDir <- H.evalIO $ makeAbsolute userOutputDir
dirExists <- H.evalIO $ doesDirectoryExist absUserOutputDir
(if dirExists then
-- Likely dangerous, but who are we to judge the user?
H.note_ $ "Reusing " <> absUserOutputDir
else do
liftIO $ createDirectory absUserOutputDir
H.note_ $ "Created " <> absUserOutputDir)
conf <- mkConf absUserOutputDir
forkAndRunTestnet conf
where
forkAndRunTestnet conf = do
-- Fork a thread to keep alive indefinitely any resources allocated by testnet.
void $ H.evalM . liftResourceT . resourceForkIO . forever . liftIO $ IO.threadDelay 10000000
void $ runTn conf
H.failure -- Intentional failure to force failure report

-- Ignore properties on various OSs

Expand Down
2 changes: 2 additions & 0 deletions cardano-testnet/src/Testnet/Start/Types.hs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ data CardanoTestnetOptions = CardanoTestnetOptions
, cardanoNodeLoggingFormat :: NodeLoggingFormat
, cardanoNumDReps :: NumDReps -- ^ The number of DReps to generate at creation
, cardanoEnableNewEpochStateLogging :: Bool -- ^ if epoch state logging is enabled
, cardanoOutputDir :: Maybe FilePath -- ^ The output directory where to store files, sockets, and so on. If unset, a temporary directory is used.
} deriving (Eq, Show)

cardanoNumPools :: CardanoTestnetOptions -> NumPools
Expand Down Expand Up @@ -105,6 +106,7 @@ instance Default CardanoTestnetOptions where
, cardanoNodeLoggingFormat = NodeLoggingFormatAsJson
, cardanoNumDReps = 3
, cardanoEnableNewEpochStateLogging = True
, cardanoOutputDir = Nothing
}

-- | Options that are implemented by writing fields in the Shelley genesis file.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ Usage: cardano-testnet cardano [--num-pool-nodes COUNT]
[--nodeLoggingFormat LOGGING_FORMAT]
[--num-dreps NUMBER]
[--enable-new-epoch-state-logging]
[--output-dir DIRECTORY]
--testnet-magic INT
[--epoch-length SLOTS]
[--slot-length SECONDS]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ Usage: cardano-testnet cardano [--num-pool-nodes COUNT]
[--nodeLoggingFormat LOGGING_FORMAT]
[--num-dreps NUMBER]
[--enable-new-epoch-state-logging]
[--output-dir DIRECTORY]
--testnet-magic INT
[--epoch-length SLOTS]
[--slot-length SECONDS]
Expand Down Expand Up @@ -44,6 +45,9 @@ Available options:
--enable-new-epoch-state-logging
Enable new epoch state logging to
logs/ledger-epoch-state.log
--output-dir DIRECTORY Directory where to store files, sockets, and so on.
It is created if it doesn't exist. If unset, a
temporary directory is used.
--testnet-magic INT Specify a testnet magic id.
--epoch-length SLOTS Epoch length, in number of slots (default: 500)
--slot-length SECONDS Slot length (default: 0.1)
Expand Down
Loading