Skip to content

Commit 0cadbcd

Browse files
authored
Merge pull request #1460 from input-output-hk/tests-command-line-args
Tests more command line args
2 parents 542198e + 6b2ceba commit 0cadbcd

28 files changed

+298
-85
lines changed

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,10 @@ test-suite cardano-chain-gen
165165
Test.Cardano.Db.Mock.Unit.Alonzo.Stake
166166
Test.Cardano.Db.Mock.Unit.Alonzo.Tx
167167
Test.Cardano.Db.Mock.Unit.Babbage
168-
Test.Cardano.Db.Mock.Unit.Babbage.Flag.ConsumedTxOut
168+
Test.Cardano.Db.Mock.Unit.Babbage.CommandLineArg.ConfigFile
169+
Test.Cardano.Db.Mock.Unit.Babbage.CommandLineArg.EpochDisabled
170+
Test.Cardano.Db.Mock.Unit.Babbage.CommandLineArg.ForceIndex
171+
Test.Cardano.Db.Mock.Unit.Babbage.CommandLineArg.MigrateConsumedPruneTxOut
169172
Test.Cardano.Db.Mock.Unit.Babbage.InlineAndReference
170173
Test.Cardano.Db.Mock.Unit.Babbage.Other
171174
Test.Cardano.Db.Mock.Unit.Babbage.Plutus
@@ -209,6 +212,7 @@ test-suite cardano-chain-gen
209212
, stm
210213
, strict-stm
211214
, tasty
215+
, tasty-expected-failure
212216
, tasty-quickcheck
213217
, text
214218
, transformers

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

Lines changed: 84 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
{-# LANGUAGE OverloadedStrings #-}
22
{-# LANGUAGE ScopedTypeVariables #-}
33
{-# LANGUAGE TypeApplications #-}
4+
{-# LANGUAGE RecordWildCards #-}
45

56
module Test.Cardano.Db.Mock.Config (
67
Config (..),
78
DBSyncEnv (..),
8-
TxOutParam(..),
9+
CommandLineArgs(..),
10+
TxOutParam (..),
11+
initCommandLineArgs,
912
babbageConfigDir,
1013
alonzoConfigDir,
1114
emptyMetricsSetters,
@@ -30,7 +33,8 @@ module Test.Cardano.Db.Mock.Config (
3033
startDBSync,
3134
withDBSyncEnv,
3235
withFullConfig,
33-
withTxOutParamConfig,
36+
withCustomConfig,
37+
withCustomConfigAndLogs,
3438
withFullConfig',
3539
) where
3640

@@ -93,8 +97,26 @@ data DBSyncEnv = DBSyncEnv
9397

9498
-- used for testing of tx out pruning feature
9599
data TxOutParam = TxOutParam
96-
{ paramMigrateConsumed :: Bool,
97-
paramPruneTxOut :: Bool
100+
{ paramMigrateConsumed :: Bool
101+
, paramPruneTxOut :: Bool
102+
}
103+
104+
data CommandLineArgs = CommandLineArgs
105+
{ claHasConfigFile :: Bool
106+
, claEpochDisabled :: Bool
107+
, claHasCache :: Bool
108+
, claShouldUseLedger :: Bool
109+
, claSkipFix :: Bool
110+
, claOnlyFix :: Bool
111+
, claForceIndexes :: Bool
112+
, claHasMultiAssets :: Bool
113+
, claHasMetadata :: Bool
114+
, claHasPlutusExtra :: Bool
115+
, claHasOfflineData :: Bool
116+
, claTurboMode :: Bool
117+
, claFullMode :: Bool
118+
, claMigrateConsumed :: Bool
119+
, claPruneTxOut :: Bool
98120
}
99121

100122
babbageConfigDir :: FilePath
@@ -203,14 +225,14 @@ setupTestsDir dir = do
203225
0
204226
Nothing
205227

206-
mkConfig :: FilePath -> FilePath -> Maybe TxOutParam -> IO Config
207-
mkConfig staticDir mutableDir mTxOutParam = do
228+
mkConfig :: FilePath -> FilePath -> CommandLineArgs -> IO Config
229+
mkConfig staticDir mutableDir cmdLineArgs = do
208230
config <- readSyncNodeConfig $ ConfigFile (staticDir </> "test-db-sync-config.json")
209231
genCfg <- either (error . Text.unpack . renderSyncNodeError) id <$> runExceptT (readCardanoGenesisConfig config)
210232
let pInfoDbSync = mkProtocolInfoCardano genCfg []
211233
creds <- mkShelleyCredentials $ staticDir </> "pools" </> "bulk1.creds"
212234
let pInfoForger = mkProtocolInfoCardano genCfg creds
213-
syncPars <- mkSyncNodeParams staticDir mutableDir mTxOutParam
235+
syncPars <- mkSyncNodeParams staticDir mutableDir cmdLineArgs
214236
pure $ Config (Consensus.pInfoConfig pInfoDbSync) pInfoDbSync pInfoForger syncPars
215237

216238
mkShelleyCredentials :: FilePath -> IO [ShelleyLeaderCredentials StandardCrypto]
@@ -228,35 +250,55 @@ mkShelleyCredentials bulkFile = do
228250
}
229251

230252
-- | staticDir can be shared by tests running in parallel. mutableDir not.
231-
mkSyncNodeParams :: FilePath -> FilePath -> Maybe TxOutParam -> IO SyncNodeParams
232-
mkSyncNodeParams staticDir mutableDir mTxOutParam = do
253+
mkSyncNodeParams :: FilePath -> FilePath -> CommandLineArgs -> IO SyncNodeParams
254+
mkSyncNodeParams staticDir mutableDir CommandLineArgs{..} = do
233255
pgconfig <- orDie Db.renderPGPassError $ newExceptT Db.readPGPassDefault
234256
pure $
235257
SyncNodeParams
236-
{ enpConfigFile = ConfigFile $ staticDir </> "test-db-sync-config.json"
258+
{ enpConfigFile = ConfigFile $ staticDir </> (if claHasConfigFile then "test-db-sync-config.json" else "")
237259
, enpSocketPath = SocketPath $ mutableDir </> ".socket"
238260
, enpMaybeLedgerStateDir = Just $ LedgerStateDir $ mutableDir </> "ledger-states"
239261
, enpMigrationDir = MigrationDir "../schema"
240262
, enpPGPassSource = Db.PGPassCached pgconfig
241-
, enpExtended = True
242-
, enpHasCache = True
243-
, enpShouldUseLedger = True
244-
, enpSkipFix = True
245-
, enpOnlyFix = False
246-
, enpForceIndexes = False
247-
, enpHasMultiAssets = True
248-
, enpHasMetadata = True
263+
, enpEpochDisabled = claEpochDisabled
264+
, enpHasCache = claHasCache
265+
, enpShouldUseLedger = claShouldUseLedger
266+
, enpSkipFix = claSkipFix
267+
, enpOnlyFix = claOnlyFix
268+
, enpForceIndexes = claForceIndexes
269+
, enpHasMultiAssets = claHasMultiAssets
270+
, enpHasMetadata = claHasMetadata
249271
, enpHasPlutusExtra = True
250272
, enpHasOfflineData = True
251273
, enpTurboMode = False
252274
, enpFullMode = True
253-
, enpMigrateConsumed = maybe False paramMigrateConsumed mTxOutParam
254-
, enpPruneTxOut = maybe False paramPruneTxOut mTxOutParam
275+
, enpMigrateConsumed = claMigrateConsumed
276+
, enpPruneTxOut = claPruneTxOut
255277
, enpSnEveryFollowing = 35
256278
, enpSnEveryLagging = 35
257279
, enpMaybeRollback = Nothing
258280
}
259281

282+
initCommandLineArgs :: CommandLineArgs
283+
initCommandLineArgs =
284+
CommandLineArgs
285+
{ claHasConfigFile = True
286+
, claEpochDisabled = True
287+
, claHasCache = True
288+
, claShouldUseLedger = True
289+
, claSkipFix = True
290+
, claOnlyFix = False
291+
, claForceIndexes = False
292+
, claHasMultiAssets = True
293+
, claHasMetadata = True
294+
, claHasPlutusExtra = True
295+
, claHasOfflineData = True
296+
, claTurboMode = False
297+
, claFullMode = True
298+
, claMigrateConsumed = True
299+
, claPruneTxOut = True
300+
}
301+
260302
emptyMetricsSetters :: MetricSetters
261303
emptyMetricsSetters =
262304
MetricSetters
@@ -273,37 +315,49 @@ withFullConfig ::
273315
IOManager ->
274316
[(Text, Text)] ->
275317
IO a
276-
withFullConfig = withFullConfig' True Nothing
318+
withFullConfig = withFullConfig' True False initCommandLineArgs
319+
320+
withCustomConfig ::
321+
CommandLineArgs ->
322+
FilePath ->
323+
FilePath ->
324+
(Interpreter -> ServerHandle IO CardanoBlock -> DBSyncEnv -> IO a) ->
325+
IOManager ->
326+
[(Text, Text)] ->
327+
IO a
328+
withCustomConfig = withFullConfig' True False
277329

278-
withTxOutParamConfig ::
279-
TxOutParam ->
330+
-- when wanting to check for a failure in a test for some reason logging has to be enabled
331+
withCustomConfigAndLogs ::
332+
CommandLineArgs ->
280333
FilePath ->
281334
FilePath ->
282335
(Interpreter -> ServerHandle IO CardanoBlock -> DBSyncEnv -> IO a) ->
283336
IOManager ->
284337
[(Text, Text)] ->
285338
IO a
286-
withTxOutParamConfig txOutParam = withFullConfig' True (Just txOutParam)
339+
withCustomConfigAndLogs = withFullConfig' True True
287340

288341
withFullConfig' ::
289342
Bool ->
290-
Maybe TxOutParam ->
343+
Bool ->
344+
CommandLineArgs ->
291345
FilePath ->
292346
FilePath ->
293347
(Interpreter -> ServerHandle IO CardanoBlock -> DBSyncEnv -> IO a) ->
294348
IOManager ->
295349
[(Text, Text)] ->
296350
IO a
297-
withFullConfig' hasFingerprint mTxOutParam config testLabel action iom migr = do
351+
withFullConfig' hasFingerprint shouldLog cmdLineArgs config testLabel action iom migr = do
298352
recreateDir mutableDir
299-
cfg <- mkConfig configDir mutableDir mTxOutParam
353+
cfg <- mkConfig configDir mutableDir cmdLineArgs
300354
fingerFile <- if hasFingerprint then Just <$> prepareFingerprintFile testLabel else pure Nothing
301355
let dbsyncParams = syncNodeParams cfg
302356
-- Set to True to disable logging, False to enable it.
303357
trce <-
304-
if True
305-
then pure nullTracer
306-
else configureLogging dbsyncParams "db-sync-node"
358+
if shouldLog
359+
then configureLogging dbsyncParams "db-sync-node"
360+
else pure nullTracer
307361
let dbsyncRun = runDbSync emptyMetricsSetters migr iom trce dbsyncParams True
308362
let initSt = Consensus.pInfoInitLedger $ protocolInfo cfg
309363
withInterpreter (protocolInfoForging cfg) nullTracer fingerFile $ \interpreter -> do

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -273,4 +273,4 @@ prop_empty_blocks iom knownMigrations = withMaxSuccess 20 $ noShrinking $ forAll
273273
prettyCommands smSymbolic hist (checkCommandNames cmds (res === Ok))
274274
where
275275
smSymbolic = sm (error "inter") (error "mockServer") (error "dbSync")
276-
runAction action = withFullConfig' False Nothing "config" "qsm" action iom knownMigrations
276+
runAction action = withFullConfig' False False initCommandLineArgs "config" "qsm" action iom knownMigrations

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

Lines changed: 31 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,13 @@ module Test.Cardano.Db.Mock.Unit.Babbage (
99
import Cardano.Mock.ChainSync.Server (IOManager)
1010
import Data.Text (Text)
1111
import Test.Tasty (TestTree, testGroup)
12+
import Test.Tasty.ExpectedFailure (expectFail)
1213
import Test.Tasty.HUnit (Assertion, testCase)
1314

14-
import qualified Test.Cardano.Db.Mock.Unit.Babbage.Flag.ConsumedTxOut as FlagConsumedTxOut
15+
import qualified Test.Cardano.Db.Mock.Unit.Babbage.CommandLineArg.ConfigFile as ConfigFile
16+
import qualified Test.Cardano.Db.Mock.Unit.Babbage.CommandLineArg.EpochDisabled as EpochDisabled
17+
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
1519
import qualified Test.Cardano.Db.Mock.Unit.Babbage.InlineAndReference as BabInlineRef
1620
import qualified Test.Cardano.Db.Mock.Unit.Babbage.Other as BabOther
1721
import qualified Test.Cardano.Db.Mock.Unit.Babbage.Plutus as BabPlutus
@@ -35,19 +39,33 @@ unitTests iom knownMigrations =
3539
, test "node restart boundary" BabSimple.nodeRestartBoundary
3640
]
3741
, testGroup
38-
"flags"
42+
"Command Line Arguments"
3943
[ testGroup
40-
"consumed-tx-out"
41-
[ test "flag check" FlagConsumedTxOut.flagCheck
42-
, test "basic prune" FlagConsumedTxOut.basicPrune
43-
, test "prune with simple rollback" FlagConsumedTxOut.pruneWithSimpleRollback
44-
, test "prune with full tx rollback" FlagConsumedTxOut.pruneWithFullTxRollback
45-
, test "pruning should keep some tx" FlagConsumedTxOut.pruningShouldKeepSomeTx
46-
, test "prune and rollback one block" FlagConsumedTxOut.pruneAndRollBackOneBlock
47-
, test "no pruning and rollback" FlagConsumedTxOut.noPruneAndRollBack
48-
, test "prune same block" FlagConsumedTxOut.pruneSameBlock
49-
, test "no pruning same block" FlagConsumedTxOut.noPruneSameBlock
50-
]
44+
"consumed-tx-out + prune-tx-out"
45+
[ test "flag check" MigrateConsumedPruneTxOut.commandLineArgCheck
46+
, test "basic prune" MigrateConsumedPruneTxOut.basicPrune
47+
, test "prune with simple rollback" MigrateConsumedPruneTxOut.pruneWithSimpleRollback
48+
, test "prune with full tx rollback" MigrateConsumedPruneTxOut.pruneWithFullTxRollback
49+
, test "pruning should keep some tx" MigrateConsumedPruneTxOut.pruningShouldKeepSomeTx
50+
, test "prune and rollback one block" MigrateConsumedPruneTxOut.pruneAndRollBackOneBlock
51+
, test "no pruning and rollback" MigrateConsumedPruneTxOut.noPruneAndRollBack
52+
, test "prune same block" MigrateConsumedPruneTxOut.pruneSameBlock
53+
, test "no pruning same block" MigrateConsumedPruneTxOut.noPruneSameBlock
54+
]
55+
, testGroup
56+
"config"
57+
[ expectFail $ test "fails if incorrect or no config file given" ConfigFile.checkConfigFileArg
58+
]
59+
, testGroup
60+
"disable-epoch"
61+
[ test "Epoch doesn't update when disabled" EpochDisabled.checkEpochDisabledArg
62+
, test "Epoch updates when enabled" EpochDisabled.checkEpochEnabled
63+
]
64+
, testGroup
65+
"force-indexes"
66+
[ test "check force-index adds indexes" ForceIndex.checkForceIndexesArg
67+
, test "check no force-index doesn't add indexes" ForceIndex.checkNoForceIndexesArg
68+
]
5169
]
5270
, testGroup
5371
"rollbacks"
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
module Test.Cardano.Db.Mock.Unit.Babbage.CommandLineArg.ConfigFile (
2+
checkConfigFileArg,
3+
)
4+
where
5+
6+
import Cardano.Mock.ChainSync.Server (IOManager)
7+
import Data.Text (Text)
8+
import Test.Cardano.Db.Mock.Config (CommandLineArgs (..), babbageConfigDir, initCommandLineArgs, withCustomConfigAndLogs)
9+
import Test.Tasty.HUnit (Assertion)
10+
11+
-- this test fails as incorrect configuration file given
12+
checkConfigFileArg :: IOManager -> [(Text, Text)] -> Assertion
13+
checkConfigFileArg =
14+
withCustomConfigAndLogs commandLineConfigArgs babbageConfigDir testLabel $ \_ _ _ -> do
15+
pure ()
16+
where
17+
testLabel = "CLAcheckConfigFileArg"
18+
commandLineConfigArgs = initCommandLineArgs {claHasConfigFile = False}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
module Test.Cardano.Db.Mock.Unit.Babbage.CommandLineArg.EpochDisabled (
2+
checkEpochDisabledArg,
3+
checkEpochEnabled,
4+
)
5+
where
6+
7+
import qualified Cardano.Db as DB
8+
import Cardano.Mock.ChainSync.Server (IOManager)
9+
import qualified Cardano.Mock.Forging.Tx.Babbage as Babbage
10+
import Cardano.Mock.Forging.Types (UTxOIndex (..))
11+
import Control.Monad (void)
12+
import Data.Text (Text)
13+
import Test.Cardano.Db.Mock.Config (CommandLineArgs (..), babbageConfigDir, startDBSync, withCustomConfig, initCommandLineArgs)
14+
import Test.Cardano.Db.Mock.UnifiedApi (forgeAndSubmitBlocks, withBabbageFindLeaderAndSubmitTx)
15+
import Test.Cardano.Db.Mock.Validate (assertBlockNoBackoff, assertEqQuery)
16+
import Test.Tasty.HUnit (Assertion)
17+
18+
mkCommandLineArgs :: Bool -> CommandLineArgs
19+
mkCommandLineArgs epochDisabled = initCommandLineArgs {claEpochDisabled = epochDisabled}
20+
21+
-- this test fails as incorrect configuration file given
22+
checkEpochDisabledArg :: IOManager -> [(Text, Text)] -> Assertion
23+
checkEpochDisabledArg =
24+
withCustomConfig (mkCommandLineArgs True) babbageConfigDir testLabel $ \interpreter mockServer dbSyncEnv -> do
25+
startDBSync dbSyncEnv
26+
b1 <- forgeAndSubmitBlocks interpreter mockServer 50
27+
-- add 2 blocks with tx
28+
void $ withBabbageFindLeaderAndSubmitTx interpreter mockServer $ Babbage.mkPaymentTx (UTxOIndex 0) (UTxOIndex 1) 10000 10000
29+
void $ withBabbageFindLeaderAndSubmitTx interpreter mockServer $ Babbage.mkPaymentTx (UTxOIndex 1) (UTxOIndex 0) 10000 10000
30+
b2 <- forgeAndSubmitBlocks interpreter mockServer 60
31+
32+
assertBlockNoBackoff dbSyncEnv (fromIntegral $ length (b1 <> b2) + 2)
33+
assertEqQuery dbSyncEnv DB.queryEpochCount 0 "new epoch didn't prune tx_out column that are null"
34+
where
35+
testLabel = "CLAcheckEpochDisabledArg "
36+
37+
checkEpochEnabled :: IOManager -> [(Text, Text)] -> Assertion
38+
checkEpochEnabled =
39+
withCustomConfig (mkCommandLineArgs False) babbageConfigDir testLabel $ \interpreter mockServer dbSyncEnv -> do
40+
startDBSync dbSyncEnv
41+
b1 <- forgeAndSubmitBlocks interpreter mockServer 50
42+
-- add 2 blocks with tx
43+
void $ withBabbageFindLeaderAndSubmitTx interpreter mockServer $ Babbage.mkPaymentTx (UTxOIndex 0) (UTxOIndex 1) 10000 10000
44+
void $ withBabbageFindLeaderAndSubmitTx interpreter mockServer $ Babbage.mkPaymentTx (UTxOIndex 1) (UTxOIndex 0) 10000 10000
45+
b2 <- forgeAndSubmitBlocks interpreter mockServer 60
46+
47+
assertBlockNoBackoff dbSyncEnv (fromIntegral $ length (b1 <> b2) + 2)
48+
assertEqQuery dbSyncEnv DB.queryEpochCount 1 "new epoch didn't prune tx_out column that are null"
49+
where
50+
testLabel = "CLAcheckEpochDisabledArg "
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
{-# LANGUAGE NumericUnderscores #-}
2+
3+
module Test.Cardano.Db.Mock.Unit.Babbage.CommandLineArg.ForceIndex (
4+
checkForceIndexesArg,
5+
checkNoForceIndexesArg
6+
)
7+
where
8+
9+
import Cardano.Mock.ChainSync.Server (IOManager)
10+
import Data.Text (Text)
11+
import Test.Cardano.Db.Mock.Config (CommandLineArgs (..), babbageConfigDir, initCommandLineArgs, withCustomConfig, startDBSync)
12+
import Test.Tasty.HUnit (Assertion)
13+
import qualified Cardano.Db as DB
14+
import Test.Cardano.Db.Mock.Validate (assertEqQuery)
15+
import GHC.Conc.IO (threadDelay)
16+
17+
checkForceIndexesArg :: IOManager -> [(Text, Text)] -> Assertion
18+
checkForceIndexesArg =
19+
withCustomConfig commandLineForceIndexArgs babbageConfigDir testLabel $ \_ _ dbSyncEnv -> do
20+
startDBSync dbSyncEnv
21+
-- assertBlockNoBackoff dbSyncEnv 0
22+
threadDelay 3_000_000
23+
assertEqQuery dbSyncEnv DB.queryPgIndexesCount 143 "there wasn't the correct number of indexes"
24+
where
25+
testLabel = "CLAcheckForceIndexesArg"
26+
commandLineForceIndexArgs = initCommandLineArgs {claForceIndexes = True}
27+
28+
checkNoForceIndexesArg :: IOManager -> [(Text, Text)] -> Assertion
29+
checkNoForceIndexesArg =
30+
withCustomConfig commandLineNoForceIndexArgs babbageConfigDir testLabel $ \_ _ dbSyncEnv -> do
31+
startDBSync dbSyncEnv
32+
-- assertBlockNoBackoff dbSyncEnv 0
33+
threadDelay 3_000_000
34+
assertEqQuery dbSyncEnv DB.queryPgIndexesCount 78 "there wasn't the correct number of indexes"
35+
where
36+
testLabel = "CLAcheckNoForceIndexesArg"
37+
commandLineNoForceIndexArgs = initCommandLineArgs {claForceIndexes = False}

0 commit comments

Comments
 (0)