Skip to content

Commit

Permalink
Use absolute/relative directory/file filepaths in local-cluster (#4560)
Browse files Browse the repository at this point in the history
- [x] Use `pathtype` library  as a backend for `FileOf` type
- [x] Fix effects of separating between `FileOf` and `DirOf` as values
of absolute paths
- [x] Add `FileOf` / `DirOf` / `RelDirOf` where `Filepath` (`String`)
was still used
- [x] opt-out `path` and `path-io` wherever used in favor of `pathtype`

ADP-3305
  • Loading branch information
paolino authored Apr 23, 2024
2 parents 2bd29c4 + 37e5d4e commit bb06d1e
Show file tree
Hide file tree
Showing 42 changed files with 881 additions and 675 deletions.
2 changes: 2 additions & 0 deletions lib/benchmarks/cardano-wallet-benchmarks.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ benchmark latency
, cardano-wallet-application-extras
, cardano-wallet-benchmarks
, cardano-wallet-integration:framework
, cardano-wallet-launcher
, cardano-wallet-network-layer
, cardano-wallet-primitive
, directory
Expand All @@ -144,6 +145,7 @@ benchmark latency
, local-cluster
, mtl
, optparse-applicative
, pathtype
, resourcet
, servant-client
, temporary-extra
Expand Down
38 changes: 25 additions & 13 deletions lib/benchmarks/exe/latency-bench.hs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
{-# LANGUAGE NumericUnderscores #-}
{-# LANGUAGE OverloadedLabels #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE TypeFamilies #-}

module Main where
Expand Down Expand Up @@ -84,13 +83,18 @@ import Cardano.Wallet.Faucet
import Cardano.Wallet.Launch.Cluster
( Config (..)
, FaucetFunds (..)
, FileOf (..)
, RunningNode (..)
, defaultPoolConfigs
, testnetMagicToNatural
, withCluster
, withFaucet
)
import Cardano.Wallet.Launch.Cluster.FileOf
( DirOf (..)
, mkRelDirOf
, newAbsolutizer
, toFilePath
)
import Cardano.Wallet.LocalCluster
( clusterConfigsDirParser
)
Expand Down Expand Up @@ -199,13 +203,15 @@ import System.Directory
import System.Environment.Extended
( isEnvSet
)
import System.FilePath
( (</>)
)
import System.IO.Temp.Extra
( SkipCleanup (..)
, withSystemTempDir
)
import System.Path
( absDir
, relDir
, (</>)
)
import Test.Integration.Framework.DSL
( Context (..)
, eventually
Expand Down Expand Up @@ -638,19 +644,22 @@ withShelleyServer tracers action = withFaucet $ \faucetClientEnv -> do
withServer cfgTestnetMagic faucetFunds setupAction = do
skipCleanup <- SkipCleanup <$> isEnvSet "NO_CLEANUP"
withSystemTempDir stdoutTextTracer "latency" skipCleanup $ \dir -> do
let db = dir </> "wallets"
createDirectory db
let testDir = absDir dir
db = testDir </> relDir "wallets"
createDirectory $ toFilePath db
CommandLineOptions{clusterConfigsDir} <- parseCommandLineOptions
clusterEra <- Cluster.clusterEraFromEnv
cfgNodeLogging <-
Cluster.logFileConfigFromEnv
(Just (Cluster.clusterEraToString clusterEra))
$ Just
$ mkRelDirOf
$ Cluster.clusterEraToString clusterEra
let clusterConfig =
Cluster.Config
{ cfgStakePools = pure (NE.head defaultPoolConfigs)
, cfgLastHardFork = clusterEra
, cfgNodeLogging
, cfgClusterDir = FileOf @"cluster" dir
, cfgClusterDir = DirOf testDir
, cfgClusterConfigs = clusterConfigsDir
, cfgTestnetMagic
, cfgShelleyGenesisMods =
Expand Down Expand Up @@ -678,7 +687,7 @@ withShelleyServer tracers action = withFaucet $ \faucetClientEnv -> do
(NTestnet . fromIntegral $ testnetMagicToNatural testnetMagic)
[] -- pool certificates
tracers
(Just db)
(Just $ toFilePath db)
Nothing -- db decorator
"127.0.0.1"
(ListenOnPort 8_090)
Expand All @@ -703,12 +712,15 @@ era = maxBound
-- Command line options --------------------------------------------------------

newtype CommandLineOptions = CommandLineOptions
{clusterConfigsDir :: FileOf "cluster-configs"}
{clusterConfigsDir :: DirOf "cluster-configs"}
deriving stock (Show)

parseCommandLineOptions :: IO CommandLineOptions
parseCommandLineOptions =
parseCommandLineOptions = do
absolutizer <- newAbsolutizer
O.execParser
$ O.info
(fmap CommandLineOptions clusterConfigsDirParser <**> O.helper)
(fmap CommandLineOptions (clusterConfigsDirParser absolutizer)
<**> O.helper
)
(O.progDesc "Cardano Wallet's Latency Benchmark")
2 changes: 2 additions & 0 deletions lib/integration/cardano-wallet-integration.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ library framework
, network-uri
, resourcet
, retry
, pathtype
, serialise
, servant-client
, string-interpolate
Expand Down Expand Up @@ -155,6 +156,7 @@ library scenarios
, lens-aeson
, local-cluster
, memory
, pathtype
, pretty-simple
, resourcet
, servant-client
Expand Down
28 changes: 18 additions & 10 deletions lib/integration/framework/Test/Integration/Framework/Logging.hs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE LambdaCase #-}

module Test.Integration.Framework.Logging
Expand Down Expand Up @@ -40,6 +41,11 @@ import Cardano.Wallet.Launch.Cluster
, testMinSeverityFromEnv
, walletMinSeverityFromEnv
)
import Cardano.Wallet.Launch.Cluster.FileOf
( DirOf (..)
, mkRelDirOf
, toFilePath
)
import Cardano.Wallet.Shelley
( Tracers
, setupTracers
Expand All @@ -52,9 +58,6 @@ import Control.Tracer
( Tracer (..)
, contramap
)
import Data.Maybe
( fromMaybe
)
import Data.Text
( Text
)
Expand All @@ -64,8 +67,9 @@ import Data.Text.Class
import Network.URI
( URI
)
import System.FilePath
( (</>)
import System.Path
( relFile
, (</>)
)
import Test.Integration.Framework.Context
( PoolGarbageCollectionEvent (..)
Expand Down Expand Up @@ -130,21 +134,25 @@ instance HasSeverityAnnotation TestsLog where
| otherwise -> Warning

withTracers
:: FilePath
:: DirOf "cluster"
-> ((Tracer IO TestsLog, Tracers IO) -> IO a)
-> IO a
withTracers testDir action = do
let getLogOutputs getMinSev name = do
minSev <- getMinSev
eraStr <- clusterEraToString <$> clusterEraFromEnv
logDir <- fromMaybe testDir <$> testLogDirFromEnv (Just eraStr)
mLogDir <- testLogDirFromEnv $ Just $ mkRelDirOf eraStr
let logDir = case mLogDir of
Just d -> absDirOf d
Nothing -> absDirOf testDir -- re-purpose the "cluster" dir
pure
[ LogToFile (logDir </> name) (min minSev Info)
[ LogToFile (toFilePath $ logDir </> name) (min minSev Info)
, LogToStdStreams minSev
]

walletLogOutputs <- getLogOutputs walletMinSeverityFromEnv "wallet.log"
testLogOutputs <- getLogOutputs testMinSeverityFromEnv "test.log"
walletLogOutputs <- getLogOutputs walletMinSeverityFromEnv
$ relFile "wallet.log"
testLogOutputs <- getLogOutputs testMinSeverityFromEnv $ relFile "test.log"

withLogging walletLogOutputs $ \(sb, (cfg, walTr)) -> do
ekgEnabled >>= flip when (EKG.plugin cfg walTr sb >>= loadPlugin sb)
Expand Down
75 changes: 42 additions & 33 deletions lib/integration/framework/Test/Integration/Framework/Setup.hs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,10 @@ import Cardano.Wallet.Launch.Cluster
import Cardano.Wallet.Launch.Cluster.ClusterEra
( nodeOutputFileFromEnv
)
import Cardano.Wallet.Launch.Cluster.FileOf
( DirOf (..)
, toFilePath
)
import Cardano.Wallet.Network.Implementation.Ouroboros
( tunedForMainnetPipeliningStrategy
)
Expand Down Expand Up @@ -167,13 +171,16 @@ import System.Environment.Extended
import System.Exit
( ExitCode
)
import System.FilePath
( (</>)
)
import System.IO.Temp.Extra
( SkipCleanup (..)
, withSystemTempDir
)
import System.Path
( absDir
, relDir
, relFile
, (</>)
)
import Test.Integration.Framework.Context
( Context (..)
, PoolGarbageCollectionEvent (..)
Expand Down Expand Up @@ -217,7 +224,7 @@ import qualified Data.Text as T

-- | Do all the program setup required for integration tests, create a temporary
-- directory, and pass this info to the main hspec action.
withTestsSetup :: (FilePath -> (Tracer IO TestsLog, Tracers IO) -> IO a) -> IO a
withTestsSetup :: (DirOf "cluster" -> (Tracer IO TestsLog, Tracers IO) -> IO a) -> IO a
withTestsSetup action = do
-- Handle SIGTERM properly
installSignalHandlersNoLogging
Expand All @@ -235,8 +242,9 @@ withTestsSetup action = do
-- This temporary directory will contain logs, and all other data
-- produced by the integration tests.
withSystemTempDir stdoutTextTracer "test" skipCleanup
$ \testDir ->
withTracers testDir $ action testDir
$ \testDir -> do
let clusterDir = DirOf $ absDir testDir
withTracers clusterDir $ action clusterDir

mkFaucetFunds :: Cluster.TestnetMagic -> FaucetM FaucetFunds
mkFaucetFunds testnetMagic = do
Expand Down Expand Up @@ -275,11 +283,11 @@ mkFaucetFunds testnetMagic = do

data TestingCtx = TestingCtx
{ testnetMagic :: Cluster.TestnetMagic
, testDir :: FilePath
, testDir :: DirOf "cluster"
, tr :: Tracer IO TestsLog
, tracers :: Tracers IO
, localClusterEra :: ClusterEra
, testDataDir :: FileOf "test-data"
, testDataDir :: DirOf "test-data"
}

-- A decorator for the pool database that records all calls to the
Expand Down Expand Up @@ -309,7 +317,7 @@ recordPoolGarbageCollectionEvents TestingCtx{..} eventsRef =

withServer
:: TestingCtx
-> FileOf "cluster-configs"
-> DirOf "cluster-configs"
-> FaucetFunds
-> Pool.DBDecorator IO
-> Maybe (FileOf "node-output")
Expand All @@ -330,13 +338,13 @@ withServer
bracketTracer' tr "withServer" $ do
let tr' = contramap MsgCluster tr
era <- clusterEraFromEnv
withSMASH tr' testDir $ \smashUrl -> do
withSMASH tr' (toFilePath . absDirOf $ testDir) $ \smashUrl -> do
let clusterConfig =
Cluster.Config
{ cfgStakePools = Cluster.defaultPoolConfigs
, cfgLastHardFork = era
, cfgNodeLogging = LogFileConfig Info Nothing Info
, cfgClusterDir = FileOf @"cluster" testDir
, cfgClusterDir = testDir
, cfgClusterConfigs = clusterConfigs
, cfgTestnetMagic = testnetMagic
, cfgShelleyGenesisMods = []
Expand All @@ -362,28 +370,29 @@ onClusterStart
(RunningNode nodeConnection genesisData vData) = do
let (networkParameters, block0, genesisPools) =
fromGenesisData genesisData
let db = testDir </> "wallets"
createDirectory db
let db = absDirOf testDir </> relDir "wallets"
createDirectory $ toFilePath db
listen <- walletListenFromEnv envFromText
let testMetadata = pathOf testDataDir </> "token-metadata.json"
withMetadataServer (queryServerStatic testMetadata) $ \tokenMetaUrl -> do
serveWallet
(NodeSource nodeConnection vData (SyncTolerance 10))
networkParameters
tunedForMainnetPipeliningStrategy
(NTestnet (fromIntegral (sgNetworkMagic genesisData)))
genesisPools
tracers
(Just db)
(Just dbDecorator)
"127.0.0.1"
listen
Nothing
Nothing
(Just tokenMetaUrl)
block0
(callback nodeConnection networkParameters)
`withException` (traceWith tr . MsgServerError)
let testMetadata = absDirOf testDataDir </> relFile "token-metadata.json"
withMetadataServer (queryServerStatic $ toFilePath testMetadata)
$ \tokenMetaUrl -> do
serveWallet
(NodeSource nodeConnection vData (SyncTolerance 10))
networkParameters
tunedForMainnetPipeliningStrategy
(NTestnet (fromIntegral (sgNetworkMagic genesisData)))
genesisPools
tracers
(Just $ toFilePath db)
(Just dbDecorator)
"127.0.0.1"
listen
Nothing
Nothing
(Just tokenMetaUrl)
block0
(callback nodeConnection networkParameters)
`withException` (traceWith tr . MsgServerError)

-- threadDelay $ 3 * 60 * 1_000_000 -- Wait 3 minutes for the node to start
-- exitSuccess
Expand Down Expand Up @@ -446,7 +455,7 @@ setupContext
{ cfgStakePools = error "cfgStakePools: unused"
, cfgLastHardFork = localClusterEra
, cfgNodeLogging = error "cfgNodeLogging: unused"
, cfgClusterDir = FileOf @"cluster" testDir
, cfgClusterDir = testDir
, cfgClusterConfigs = clusterConfigs
, cfgTestnetMagic = testnetMagic
, cfgShelleyGenesisMods = []
Expand Down
13 changes: 11 additions & 2 deletions lib/integration/scenarios/Test/Integration/Run.hs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ import Cardano.Wallet.Launch.Cluster.ClusterEra
( ignoreInBabbage
, ignoreInConway
)
import Cardano.Wallet.Launch.Cluster.FileOf
( DirOf (..)
, absolutize
)
import Cardano.Wallet.Primitive.NetworkId
( NetworkDiscriminant (..)
)
Expand All @@ -36,6 +40,10 @@ import GHC.TypeNats
import System.Environment
( lookupEnv
)
import System.Path
( absDir
, absRel
)
import Test.Hspec
( mapSubject
)
Expand Down Expand Up @@ -97,9 +105,10 @@ main = withTestsSetup $ \testDir (tr, tracers) -> do
_noConway = ignoreInConway localClusterEra
_noBabbage = ignoreInBabbage localClusterEra
testnetMagic = Cluster.TestnetMagic (natVal (Proxy @netId))
testDataDir <-
FileOf . fromMaybe "."
testDataDir <- do
dir <- fromMaybe "."
<$> lookupEnv "CARDANO_WALLET_TEST_DATA"
DirOf <$> absolutize (absRel dir)
let testingCtx = TestingCtx{..}
hspecMain $ do
describe "No backend required"
Expand Down
Loading

0 comments on commit bb06d1e

Please sign in to comment.