Skip to content

Commit

Permalink
Implement TmpAbsolutePath
Browse files Browse the repository at this point in the history
Remove tempRelPath, tempBaseAbsPath, logDir and socketDir fields from
Conf as we now construct these from the TmpAbsolutePath argument
Co-authored-by: Marc Fontaine <marc.fontaine@iohk.io>
  • Loading branch information
Jimbo4350 committed Jun 2, 2023
1 parent c6c3b9a commit 2145f54
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 24 deletions.
17 changes: 2 additions & 15 deletions cardano-testnet/src/Testnet/Conf.hs
Expand Up @@ -7,11 +7,8 @@ module Testnet.Conf
, mkConf
) where

import System.FilePath ((</>))

import qualified Hedgehog.Extras.Test.Base as H
import qualified Hedgehog.Extras.Test.File as H
import qualified System.FilePath.Posix as FP
import qualified System.Random as IO

newtype ProjectBase = ProjectBase
Expand All @@ -24,29 +21,19 @@ newtype YamlFilePath = YamlFilePath

data Conf = Conf
{ tempAbsPath :: FilePath
, tempRelPath :: FilePath
, tempBaseAbsPath :: FilePath
, logDir :: FilePath
, socketDir :: FilePath
, configurationTemplate :: Maybe FilePath
, testnetMagic :: Int
} deriving (Eq, Show)

mkConf :: Maybe YamlFilePath -> FilePath -> Maybe Int -> H.Integration Conf
mkConf mConfigTemplate tempAbsPath' maybeMagic = do
testnetMagic' <- H.noteShowIO $ maybe (IO.randomRIO (1000, 2000)) return maybeMagic
tempBaseAbsPath' <- H.noteShow $ FP.takeDirectory tempAbsPath'
tempRelPath' <- H.noteShow $ FP.makeRelative tempBaseAbsPath' tempAbsPath'
socketDir' <- H.createSubdirectoryIfMissing tempBaseAbsPath' $ tempRelPath' </> "socket"
logDir' <- H.createDirectoryIfMissing $ tempAbsPath' </> "logs"
let configTemplate = unYamlFilePath <$> mConfigTemplate

return $ Conf
{ tempAbsPath = tempAbsPath'
, tempRelPath = tempRelPath'
, tempBaseAbsPath = tempBaseAbsPath'
, logDir = logDir'
, socketDir = socketDir'
, configurationTemplate = configTemplate
, testnetMagic = testnetMagic'
}


34 changes: 25 additions & 9 deletions cardano-testnet/src/Testnet/Util/Runtime.hs
Expand Up @@ -10,6 +10,7 @@ module Testnet.Util.Runtime
, NodeLoggingFormat(..)
, PaymentKeyPair(..)
, StakingKeyPair(..)
, TmpAbsolutePath(..)
, TestnetRuntime(..)
, NodeRuntime(..)
, PoolNode(..)
Expand Down Expand Up @@ -116,6 +117,23 @@ data LeadershipSlot = LeadershipSlot
, slotTime :: Text
} deriving (Eq, Show, Generic, FromJSON)

-- Temporary path used at runtime
newtype TmpAbsolutePath = TmpAbsolutePath
{ unTmpPath :: FilePath
} deriving (Eq, Show)

makeTmpRelPath :: TmpAbsolutePath -> FilePath
makeTmpRelPath (TmpAbsolutePath fp) = makeRelative (makeTmpBaseAbsPath (TmpAbsolutePath fp)) fp

makeSocketDir :: TmpAbsolutePath -> FilePath
makeSocketDir fp = makeTmpRelPath fp </> "socket"

makeTmpBaseAbsPath :: TmpAbsolutePath -> FilePath
makeTmpBaseAbsPath (TmpAbsolutePath fp) = takeDirectory fp

makeLogDir :: TmpAbsolutePath -> FilePath
makeLogDir (TmpAbsolutePath fp) = fp </> "logs"

poolNodeStdout :: PoolNode -> FilePath
poolNodeStdout = nodeStdout . poolRuntime

Expand Down Expand Up @@ -167,20 +185,18 @@ allNodes tr = bftNodes tr <> fmap poolRuntime (poolNodes tr)

-- | Start a node, creating file handles, sockets and temp-dirs.
startNode
:: String
-- ^ The tempBaseAbsPath
-> FilePath
-- ^ The tempAbsPath
-> FilePath
-- ^ The log directory
-> FilePath
-- ^ The directory where the sockets are created
:: TmpAbsolutePath
-- ^ The temporary absolute path
-> String
-- ^ The name of the node
-> [String]
-- ^ The command --socket-path and --port will be added automatically.
-> H.Integration NodeRuntime
startNode tempBaseAbsPath tempAbsPath logDir socketDir node nodeCmd = do
startNode tp@(TmpAbsolutePath tempAbsPath) node nodeCmd = do
let tempBaseAbsPath = makeTmpBaseAbsPath tp
socketDir = makeSocketDir tp
logDir = makeLogDir tp

nodeStdoutFile <- H.noteTempFile logDir $ node <> ".stdout.log"
nodeStderrFile <- H.noteTempFile logDir $ node <> ".stderr.log"
sprocket <- H.noteShow $ Sprocket tempBaseAbsPath (socketDir </> node)
Expand Down

0 comments on commit 2145f54

Please sign in to comment.