Skip to content

Commit 33bc1b1

Browse files
sgillespiekderme
authored andcommitted
test(cardano-chain-gen): Allow tests to override config file name
1 parent 0351d3e commit 33bc1b1

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+1126
-220
lines changed

cardano-chain-gen/cardano-chain-gen.cabal

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -141,8 +141,8 @@ test-suite cardano-chain-gen
141141
Test.Cardano.Db.Mock.Unit.Babbage.CommandLineArg.ConfigFile
142142
Test.Cardano.Db.Mock.Unit.Babbage.CommandLineArg.EpochDisabled
143143
Test.Cardano.Db.Mock.Unit.Babbage.CommandLineArg.ForceIndex
144-
Test.Cardano.Db.Mock.Unit.Babbage.CommandLineArg.MigrateConsumedPruneTxOut
145-
Test.Cardano.Db.Mock.Unit.Babbage.Config
144+
Test.Cardano.Db.Mock.Unit.Babbage.Config.MigrateConsumedPruneTxOut
145+
Test.Cardano.Db.Mock.Unit.Babbage.Config.Parse
146146
Test.Cardano.Db.Mock.Unit.Babbage.InlineAndReference
147147
Test.Cardano.Db.Mock.Unit.Babbage.Other
148148
Test.Cardano.Db.Mock.Unit.Babbage.Plutus
@@ -155,8 +155,8 @@ test-suite cardano-chain-gen
155155
Test.Cardano.Db.Mock.Unit.Conway.CommandLineArg.ConfigFile
156156
Test.Cardano.Db.Mock.Unit.Conway.CommandLineArg.EpochDisabled
157157
Test.Cardano.Db.Mock.Unit.Conway.CommandLineArg.ForceIndex
158-
Test.Cardano.Db.Mock.Unit.Conway.CommandLineArg.MigrateConsumedPruneTxOut
159-
Test.Cardano.Db.Mock.Unit.Conway.Config
158+
Test.Cardano.Db.Mock.Unit.Conway.Config.Parse
159+
Test.Cardano.Db.Mock.Unit.Conway.Config.MigrateConsumedPruneTxOut
160160
Test.Cardano.Db.Mock.Unit.Conway.InlineAndReference
161161
Test.Cardano.Db.Mock.Unit.Conway.Other
162162
Test.Cardano.Db.Mock.Unit.Conway.Plutus

cardano-chain-gen/test/Test/Cardano/Db/Mock/Config.hs

Lines changed: 42 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ module Test.Cardano.Db.Mock.Config (
4141
withCustomConfigAndDropDB,
4242
withCustomConfigAndLogs,
4343
withFullConfig',
44+
replaceConfigFile,
4445
) where
4546

4647
import Cardano.Api (NetworkMagic (..))
@@ -81,7 +82,7 @@ import qualified Ouroboros.Consensus.Node.ProtocolInfo as Consensus
8182
import Ouroboros.Consensus.Shelley.Eras (StandardCrypto)
8283
import Ouroboros.Consensus.Shelley.Node (ShelleyLeaderCredentials)
8384
import System.Directory (createDirectoryIfMissing, removePathForcibly)
84-
import System.FilePath.Posix ((</>))
85+
import System.FilePath.Posix (takeDirectory, (</>))
8586
import System.IO.Silently (hSilence)
8687

8788
data Config = Config
@@ -94,12 +95,13 @@ data Config = Config
9495

9596
data DBSyncEnv = DBSyncEnv
9697
{ dbSyncParams :: SyncNodeParams
97-
, partialRunDbSync :: SyncNodeParams -> IO ()
98+
, dbSyncConfig :: SyncNodeConfig
99+
, partialRunDbSync :: SyncNodeParams -> SyncNodeConfig -> IO ()
98100
, dbSyncThreadVar :: TMVar (Async ())
99101
}
100102

101103
data CommandLineArgs = CommandLineArgs
102-
{ claHasConfigFile :: Bool
104+
{ claConfigFilename :: FilePath
103105
, claEpochDisabled :: Bool
104106
, claHasCache :: Bool
105107
, claHasLedger :: Bool
@@ -151,12 +153,17 @@ fingerprintRoot = rootTestDir </> "fingerprint"
151153
mkFingerPrint :: FilePath -> FilePath
152154
mkFingerPrint testLabel = fingerprintRoot </> testLabel
153155

154-
mkDBSyncEnv :: SyncNodeParams -> (SyncNodeParams -> IO ()) -> IO DBSyncEnv
155-
mkDBSyncEnv params pRunDbSync = do
156+
mkDBSyncEnv ::
157+
SyncNodeParams ->
158+
SyncNodeConfig ->
159+
(SyncNodeParams -> SyncNodeConfig -> IO ()) ->
160+
IO DBSyncEnv
161+
mkDBSyncEnv params cfg pRunDbSync = do
156162
runningVar <- newEmptyTMVarIO
157163
pure $
158164
DBSyncEnv
159165
{ dbSyncParams = params
166+
, dbSyncConfig = cfg
160167
, partialRunDbSync = pRunDbSync
161168
, dbSyncThreadVar = runningVar
162169
}
@@ -188,7 +195,7 @@ startDBSync env = do
188195
case thr of
189196
Just _a -> error "db-sync already running"
190197
Nothing -> do
191-
let appliedRunDbSync = partialRunDbSync env $ dbSyncParams env
198+
let appliedRunDbSync = partialRunDbSync env (dbSyncParams env) (dbSyncConfig env)
192199
-- we async the fully applied runDbSync here ad put it into the thread
193200
asyncApplied <- async appliedRunDbSync
194201
void . atomically $ tryPutTMVar (dbSyncThreadVar env) asyncApplied
@@ -220,17 +227,21 @@ getPoolLayer env = do
220227

221228
mkConfig :: FilePath -> FilePath -> CommandLineArgs -> SyncNodeConfig -> IO Config
222229
mkConfig staticDir mutableDir cmdLineArgs config = do
230+
let cfgDir = mkConfigDir staticDir
223231
genCfg <- runOrThrowIO $ runExceptT (readCardanoGenesisConfig config)
224232
let (pInfoDbSync, _) = mkProtocolInfoCardano genCfg []
225-
creds <- mkShelleyCredentials $ staticDir </> "pools" </> "bulk1.creds"
233+
creds <- mkShelleyCredentials $ cfgDir </> "pools" </> "bulk1.creds"
226234
let (pInfoForger, forging) = mkProtocolInfoCardano genCfg creds
227235
forging' <- forging
228236
syncPars <- mkSyncNodeParams staticDir mutableDir cmdLineArgs
229237
pure $ Config (Consensus.pInfoConfig pInfoDbSync) pInfoDbSync pInfoForger forging' syncPars
230238

231-
mkSyncNodeConfig :: FilePath -> IO SyncNodeConfig
232-
mkSyncNodeConfig configFilePath =
233-
readSyncNodeConfig $ ConfigFile (mkConfigDir configFilePath </> "test-db-sync-config.json")
239+
mkSyncNodeConfig :: FilePath -> CommandLineArgs -> IO SyncNodeConfig
240+
mkSyncNodeConfig configFilePath cmdLineArgs =
241+
readSyncNodeConfig $ mkConfigFile configDir configFilename
242+
where
243+
configFilename = claConfigFilename cmdLineArgs
244+
configDir = mkConfigDir configFilePath
234245

235246
mkShelleyCredentials :: FilePath -> IO [ShelleyLeaderCredentials StandardCrypto]
236247
mkShelleyCredentials bulkFile = do
@@ -250,9 +261,10 @@ mkShelleyCredentials bulkFile = do
250261
mkSyncNodeParams :: FilePath -> FilePath -> CommandLineArgs -> IO SyncNodeParams
251262
mkSyncNodeParams staticDir mutableDir CommandLineArgs {..} = do
252263
pgconfig <- runOrThrowIO Db.readPGPassDefault
264+
253265
pure $
254266
SyncNodeParams
255-
{ enpConfigFile = ConfigFile $ staticDir </> (if claHasConfigFile then "test-db-sync-config.json" else "")
267+
{ enpConfigFile = mkConfigFile staticDir claConfigFilename
256268
, enpSocketPath = SocketPath $ mutableDir </> ".socket"
257269
, enpMaybeLedgerStateDir = Just $ LedgerStateDir $ mutableDir </> "ledger-states"
258270
, enpMigrationDir = MigrationDir "../schema"
@@ -285,10 +297,14 @@ mkSyncNodeParams staticDir mutableDir CommandLineArgs {..} = do
285297
, enpMaybeRollback = Nothing
286298
}
287299

300+
mkConfigFile :: FilePath -> FilePath -> ConfigFile
301+
mkConfigFile staticDir cliConfigFilename =
302+
ConfigFile $ staticDir </> cliConfigFilename
303+
288304
initCommandLineArgs :: CommandLineArgs
289305
initCommandLineArgs =
290306
CommandLineArgs
291-
{ claHasConfigFile = True
307+
{ claConfigFilename = "test-db-sync-config.json"
292308
, claEpochDisabled = True
293309
, claHasCache = True
294310
, claHasLedger = True
@@ -460,17 +476,17 @@ withFullConfig' WithConfigArgs {..} cmdLineArgs mSyncNodeConfig configFilePath t
460476
syncNodeConfig <-
461477
case mSyncNodeConfig of
462478
Just snc -> pure snc
463-
Nothing -> mkSyncNodeConfig configFilePath
479+
Nothing -> mkSyncNodeConfig configFilePath cmdLineArgs
464480

465-
cfg <- mkConfig (mkConfigDir configFilePath) mutableDir cmdLineArgs syncNodeConfig
481+
cfg <- mkConfig configFilePath mutableDir cmdLineArgs syncNodeConfig
466482
fingerFile <- if hasFingerprint then Just <$> prepareFingerprintFile testLabelFilePath else pure Nothing
467483
let dbsyncParams = syncNodeParams cfg
468484
trce <-
469485
if shouldLog
470486
then configureLogging syncNodeConfig "db-sync-node"
471487
else pure nullTracer
472488
-- runDbSync is partially applied so we can pass in syncNodeParams at call site / within tests
473-
let partialDbSyncRun params = runDbSync emptyMetricsSetters migr iom trce params syncNodeConfig True
489+
let partialDbSyncRun params cfg' = runDbSync emptyMetricsSetters migr iom trce params cfg' True
474490
initSt = Consensus.pInfoInitLedger $ protocolInfo cfg
475491

476492
withInterpreter (protocolInfoForging cfg) (protocolInfoForger cfg) nullTracer fingerFile $ \interpreter -> do
@@ -483,7 +499,7 @@ withFullConfig' WithConfigArgs {..} cmdLineArgs mSyncNodeConfig configFilePath t
483499
(unSocketPath (enpSocketPath $ syncNodeParams cfg))
484500
$ \mockServer ->
485501
-- we dont fork dbsync here. Just prepare it as an action
486-
withDBSyncEnv (mkDBSyncEnv dbsyncParams partialDbSyncRun) $ \dbSyncEnv -> do
502+
withDBSyncEnv (mkDBSyncEnv dbsyncParams syncNodeConfig partialDbSyncRun) $ \dbSyncEnv -> do
487503
let pgPass = getDBSyncPGPass dbSyncEnv
488504
tableNames <- Db.getAllTablleNames pgPass
489505
-- We only want to create the table schema once for the tests so here we check
@@ -509,3 +525,13 @@ recreateDir path = do
509525

510526
textShow :: (Show a) => a -> Text
511527
textShow = Text.pack . show
528+
529+
replaceConfigFile :: FilePath -> DBSyncEnv -> IO DBSyncEnv
530+
replaceConfigFile newFilename dbSync@DBSyncEnv {..} = do
531+
cfg <- readSyncNodeConfig $ mkConfigFile configDir newFilename
532+
533+
pure $ dbSync {dbSyncParams = newParams, dbSyncConfig = cfg}
534+
where
535+
configDir = mkConfigDir . takeDirectory . unConfigFile . enpConfigFile $ dbSyncParams
536+
newParams =
537+
dbSyncParams {enpConfigFile = ConfigFile $ configDir </> newFilename}

cardano-chain-gen/test/Test/Cardano/Db/Mock/Unit/Alonzo/Config.hs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,12 @@ import Prelude ()
1313

1414
defaultInsertConfig :: Assertion
1515
defaultInsertConfig = do
16-
cfg <- mkSyncNodeConfig alonzoConfigDir
16+
cfg <- mkSyncNodeConfig alonzoConfigDir initCommandLineArgs
1717
dncInsertConfig cfg @?= def
1818

1919
insertConfig :: Assertion
2020
insertConfig = do
21-
cfg <- mkSyncNodeConfig configDir
21+
cfg <- mkSyncNodeConfig configDir initCommandLineArgs
2222
let expected =
2323
SyncInsertConfig
2424
{ spcTxOut = TxOutDisable

cardano-chain-gen/test/Test/Cardano/Db/Mock/Unit/Babbage.hs

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ import Test.Tasty.HUnit (Assertion, testCase)
1515
import qualified Test.Cardano.Db.Mock.Unit.Babbage.CommandLineArg.ConfigFile as ConfigFile
1616
import qualified Test.Cardano.Db.Mock.Unit.Babbage.CommandLineArg.EpochDisabled as EpochDisabled
1717
import qualified Test.Cardano.Db.Mock.Unit.Babbage.CommandLineArg.ForceIndex as ForceIndex
18-
import qualified Test.Cardano.Db.Mock.Unit.Babbage.CommandLineArg.MigrateConsumedPruneTxOut as MigrateConsumedPruneTxOut
19-
import qualified Test.Cardano.Db.Mock.Unit.Babbage.Config as BabConfig
18+
import qualified Test.Cardano.Db.Mock.Unit.Babbage.Config.MigrateConsumedPruneTxOut as MigrateConsumedPruneTxOut
19+
import qualified Test.Cardano.Db.Mock.Unit.Babbage.Config.Parse as Config
2020
import qualified Test.Cardano.Db.Mock.Unit.Babbage.InlineAndReference as BabInlineRef
2121
import qualified Test.Cardano.Db.Mock.Unit.Babbage.Other as BabOther
2222
import qualified Test.Cardano.Db.Mock.Unit.Babbage.Plutus as BabPlutus
@@ -32,23 +32,11 @@ unitTests iom knownMigrations =
3232
"Babbage unit tests"
3333
[ testGroup
3434
"config"
35-
[ testCase "default insert config" BabConfig.defaultInsertConfig
36-
, testCase "insert config" BabConfig.insertConfig
37-
]
38-
, testGroup
39-
"simple"
40-
[ test "simple forge blocks" BabSimple.forgeBlocks
41-
, test "sync one block" BabSimple.addSimple
42-
, test "sync small chain" BabSimple.addSimpleChain
43-
, test "restart db-sync" BabSimple.restartDBSync
44-
, test "node restart" BabSimple.nodeRestart
45-
, test "node restart boundary" BabSimple.nodeRestartBoundary
46-
]
47-
, testGroup
48-
"Command Line Arguments"
49-
[ testGroup
35+
[ testCase "default insert config" Config.defaultInsertConfig
36+
, testCase "insert config" Config.insertConfig
37+
, testGroup
5038
"consumed-tx-out and prune-tx-out"
51-
[ test "flag check" MigrateConsumedPruneTxOut.commandLineArgCheck
39+
[ test "flag check" MigrateConsumedPruneTxOut.txConsumedColumnCheck
5240
, test "basic prune" MigrateConsumedPruneTxOut.basicPrune
5341
, test "prune with simple rollback" MigrateConsumedPruneTxOut.pruneWithSimpleRollback
5442
, test "prune with full tx rollback" MigrateConsumedPruneTxOut.pruneWithFullTxRollback
@@ -61,9 +49,21 @@ unitTests iom knownMigrations =
6149
, expectFail $ test "set prune flag, restart missing prune flag" MigrateConsumedPruneTxOut.pruneRestartMissingFlag
6250
, expectFail $ test "set bootstrap flag, restart missing bootstrap flag" MigrateConsumedPruneTxOut.bootstrapRestartMissingFlag
6351
]
64-
, testGroup
52+
]
53+
, testGroup
54+
"simple"
55+
[ test "simple forge blocks" BabSimple.forgeBlocks
56+
, test "sync one block" BabSimple.addSimple
57+
, test "sync small chain" BabSimple.addSimpleChain
58+
, test "restart db-sync" BabSimple.restartDBSync
59+
, test "node restart" BabSimple.nodeRestart
60+
, test "node restart boundary" BabSimple.nodeRestartBoundary
61+
]
62+
, testGroup
63+
"Command Line Arguments"
64+
[ testGroup
6565
"config"
66-
[ expectFail $ test "fails if incorrect or no config file given" ConfigFile.checkConfigFileArg
66+
[ expectFail $ test "fails if incorrect config file given" ConfigFile.checkConfigFileArg
6767
]
6868
, testGroup
6969
"disable-epoch"

cardano-chain-gen/test/Test/Cardano/Db/Mock/Unit/Babbage/CommandLineArg/ConfigFile.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,4 @@ checkConfigFileArg =
1717
checkStillRuns dbSyncEnv
1818
where
1919
testLabel = "CLAcheckConfigFileArg"
20-
commandLineConfigArgs = initCommandLineArgs {claHasConfigFile = False}
20+
commandLineConfigArgs = initCommandLineArgs {claConfigFilename = "does-not-exist"}

0 commit comments

Comments
 (0)