Skip to content
Draft
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
2 changes: 1 addition & 1 deletion .github/workflows/haskell.yml
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ jobs:
chmod 600 $PGPASSFILE

# Run tests
cabal test all -j1
cabal test all -j4
- name: postgres shutdown
if: ${{ always() }}
run: |
Expand Down
35 changes: 26 additions & 9 deletions cardano-chain-gen/test/Main.hs
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE RecordWildCards #-}

import Cardano.Db (PGConfig (..), PGPassSource (..), readPGPassDefault)
import Cardano.Mock.ChainSync.Server
import Cardano.Prelude (Text)
import Control.Exception (throwIO)
import Control.Monad (when, (>=>))
import Data.Maybe (isNothing)
import MigrationValidations (KnownMigration (..), knownMigrations)
Expand All @@ -12,7 +17,6 @@ import qualified Test.Cardano.Db.Mock.Unit.Babbage as Babbage
import qualified Test.Cardano.Db.Mock.Unit.Conway as Conway
import Test.Tasty
import Test.Tasty.QuickCheck (testProperty)
import Test.Tasty.Runners (NumThreads (..))
import Prelude

main :: IO ()
Expand All @@ -30,15 +34,28 @@ tests :: IOManager -> IO TestTree
tests iom = do
-- Tests share a single Postgres instance and DB, so they must run serially.
-- tasty 1.5+ defaults to parallel execution; override that here.
pgCfg@PGConfig {..} <- either throwIO pure =<< readPGPassDefault

-- TODO[sgillespie]: create a PG config for each era
let
pgCfg1 = pgCfg {pgcDbname = pgcDbname <> "_1"}
pgCfg2 = pgCfg {pgcDbname = pgcDbname <> "_2"}
pgCfg3 = pgCfg {pgcDbname = pgcDbname <> "_3"}
pgCfg4 = pgCfg {pgcDbname = pgcDbname <> "_4"}

pure $
localOption (NumThreads 1) $
testGroup
"cardano-chain-gen"
[ Conway.unitTests iom knownMigrationsPlain
, Babbage.unitTests iom knownMigrationsPlain
, Alonzo.unitTests iom knownMigrationsPlain
, testProperty "QSM" $ Property.prop_empty_blocks iom knownMigrationsPlain
]
testGroup
"all"
[ -- Tests share a single Postgres instance and DB, so they must run serially.
-- tasty 1.5+ defaults to parallel execution; override that here.
testGroup
"cardano-chain-gen"
[ Conway.unitTests iom knownMigrationsPlain (PGPassCached pgCfg1)
, Babbage.unitTests iom knownMigrationsPlain (PGPassCached pgCfg2)
, Alonzo.unitTests iom knownMigrationsPlain (PGPassCached pgCfg3)
, testProperty "QSM" $ Property.prop_empty_blocks (PGPassCached pgCfg4) iom knownMigrationsPlain
]
]
where
knownMigrationsPlain :: [(Text, Text)]
knownMigrationsPlain = (\x -> (hash x, filepath x)) <$> knownMigrations
26 changes: 18 additions & 8 deletions cardano-chain-gen/test/Test/Cardano/Db/Mock/Config.hs
Original file line number Diff line number Diff line change
Expand Up @@ -259,8 +259,8 @@ getPoolLayer env = do
nullTracer
pool

withConfig :: FilePath -> FilePath -> CommandLineArgs -> SyncNodeConfig -> (Config -> IO a) -> IO a
withConfig staticDir mutableDir cmdLineArgs config action = do
withConfig :: DB.PGPassSource -> FilePath -> FilePath -> CommandLineArgs -> SyncNodeConfig -> (Config -> IO a) -> IO a
withConfig source staticDir mutableDir cmdLineArgs config action = do
let cfgDir = mkConfigDir staticDir
genCfg <- runOrThrowIO $ runExceptT (readCardanoGenesisConfig config)
let (pInfoDbSync, _) = mkProtocolInfoCardano genCfg []
Expand All @@ -271,7 +271,7 @@ withConfig staticDir mutableDir cmdLineArgs config action = do
(allocateRes mkForgings)
(mapM finalize)
( \forgings -> do
syncPars <- mkSyncNodeParams staticDir mutableDir cmdLineArgs
syncPars <- mkSyncNodeParams source staticDir mutableDir cmdLineArgs
let cfg = Config (Consensus.pInfoConfig pInfoDbSync) pInfoDbSync pInfoForger forgings syncPars
action cfg
)
Expand Down Expand Up @@ -310,9 +310,9 @@ mkShelleyCredentials bulkFile = do
}

-- | staticDir can be shared by tests running in parallel. mutableDir not.
mkSyncNodeParams :: FilePath -> FilePath -> CommandLineArgs -> IO SyncNodeParams
mkSyncNodeParams staticDir mutableDir CommandLineArgs {..} = do
pgconfig <- runOrThrowIO DB.readPGPassDefault
mkSyncNodeParams :: DB.PGPassSource -> FilePath -> FilePath -> CommandLineArgs -> IO SyncNodeParams
mkSyncNodeParams source staticDir mutableDir CommandLineArgs {..} = do
pgconfig <- runOrThrowIO (DB.readPGPass source)

pure $
SyncNodeParams
Expand Down Expand Up @@ -428,6 +428,7 @@ emptyMetricsSetters =
}

withFullConfig ::
DB.PGPassSource ->
-- | config filepath
FilePath ->
-- | test label
Expand All @@ -449,6 +450,7 @@ withFullConfig =

-- this function needs to be used where the schema needs to be rebuilt
withFullConfigDropDB ::
DB.PGPassSource ->
-- | config filepath
FilePath ->
-- | test label
Expand All @@ -469,6 +471,7 @@ withFullConfigDropDB =
Nothing

withFullConfigDropDBLog ::
DB.PGPassSource ->
-- | config filepath
FilePath ->
-- | test label
Expand All @@ -490,6 +493,7 @@ withFullConfigDropDBLog =

-- For tests that rollback and restart, fingerprints create divergent chains
withFullConfigDropDBNoFingerprint ::
DB.PGPassSource ->
-- | config filepath
FilePath ->
-- | test label
Expand All @@ -510,6 +514,7 @@ withFullConfigDropDBNoFingerprint =
Nothing

withFullConfigLog ::
DB.PGPassSource ->
-- | config filepath
FilePath ->
-- | test label
Expand All @@ -533,6 +538,7 @@ withCustomConfig ::
CommandLineArgs ->
-- | custom SyncNodeConfig
Maybe (SyncNodeConfig -> SyncNodeConfig) ->
DB.PGPassSource ->
-- | config filepath
FilePath ->
-- | test label
Expand All @@ -554,6 +560,7 @@ withCustomConfigDropDB ::
CommandLineArgs ->
-- | custom SyncNodeConfig
Maybe (SyncNodeConfig -> SyncNodeConfig) ->
DB.PGPassSource ->
-- | config filepath
FilePath ->
-- | test label
Expand All @@ -576,6 +583,7 @@ withCustomConfigLog ::
CommandLineArgs ->
-- | custom SyncNodeConfig
Maybe (SyncNodeConfig -> SyncNodeConfig) ->
DB.PGPassSource ->
-- | config filepath
FilePath ->
-- | test label
Expand All @@ -597,6 +605,7 @@ withCustomConfigDropDBLog ::
CommandLineArgs ->
-- | custom SyncNodeConfig
Maybe (SyncNodeConfig -> SyncNodeConfig) ->
DB.PGPassSource ->
-- | config filepath
FilePath ->
-- | test label
Expand All @@ -619,6 +628,7 @@ withFullConfig' ::
CommandLineArgs ->
-- | custom SyncNodeConfig
Maybe (SyncNodeConfig -> SyncNodeConfig) ->
DB.PGPassSource ->
-- | config filepath
FilePath ->
-- | test label
Expand All @@ -627,7 +637,7 @@ withFullConfig' ::
IOManager ->
[(Text, Text)] ->
IO a
withFullConfig' WithConfigArgs {..} cmdLineArgs mSyncNodeConfig configFilePath testLabelFilePath action iom migr = do
withFullConfig' WithConfigArgs {..} cmdLineArgs mSyncNodeConfig source configFilePath testLabelFilePath action iom migr = do
recreateDir mutableDir
-- check if custom syncNodeConfigs have been passed or not
syncNodeConfig <-
Expand All @@ -637,7 +647,7 @@ withFullConfig' WithConfigArgs {..} cmdLineArgs mSyncNodeConfig configFilePath t
pure $ updateFn initConfigFile
Nothing -> mkSyncNodeConfig configFilePath cmdLineArgs

withConfig configFilePath mutableDir cmdLineArgs syncNodeConfig $ \cfg -> do
withConfig source configFilePath mutableDir cmdLineArgs syncNodeConfig $ \cfg -> do
fingerFile <- if hasFingerprint then Just <$> prepareFingerprintFile testLabelFilePath else pure Nothing
let dbsyncParams = syncNodeParams cfg
envLog <- lookupEnv "DBSYNC_TEST_LOG"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ module Test.Cardano.Db.Mock.Property.Property (
prop_empty_blocks,
) where

import qualified Cardano.Db as DB
import Cardano.Mock.Chain
import Cardano.Mock.ChainSync.Server
import Cardano.Mock.Forging.Interpreter
Expand Down Expand Up @@ -288,12 +289,12 @@ sm interpreter mockServer dbSync =
mock
noCleanup

prop_empty_blocks :: IOManager -> [(Text, Text)] -> Property
prop_empty_blocks iom knownMigrations = withMaxSuccess 20 $ noShrinking $ forAllCommands smSymbolic (Just 20) $ \cmds -> monadicIO $ do
prop_empty_blocks :: DB.PGPassSource -> IOManager -> [(Text, Text)] -> Property
prop_empty_blocks source iom knownMigrations = withMaxSuccess 20 $ noShrinking $ forAllCommands smSymbolic (Just 20) $ \cmds -> monadicIO $ do
(hist, res) <- run $ runAction $ \interpreter mockServer dbSync -> do
(hist, _model, res) <- runCommands' (sm interpreter mockServer dbSync) cmds
pure (hist, res)
prettyCommands smSymbolic hist (checkCommandNames cmds (res === Ok))
where
smSymbolic = sm (error "inter") (error "mockServer") (error "dbSync")
runAction action = withFullConfig' (WithConfigArgs False False False) initCommandLineArgs Nothing "config" "qsm" action iom knownMigrations
runAction action = withFullConfig' (WithConfigArgs False False False) initCommandLineArgs Nothing source "config" "qsm" action iom knownMigrations
27 changes: 16 additions & 11 deletions cardano-chain-gen/test/Test/Cardano/Db/Mock/Unit/Alonzo.hs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@ module Test.Cardano.Db.Mock.Unit.Alonzo (
unitTests,
) where

import qualified Cardano.Db as DB
import Cardano.Mock.ChainSync.Server (IOManager)
import Data.Text (Text)
import Test.Tasty (TestTree, testGroup)
import Test.Tasty (DependencyType (..), TestTree, dependentTestGroup)
import Test.Tasty.HUnit (Assertion, testCase)

import qualified Test.Cardano.Db.Mock.Unit.Alonzo.Config as AlzConfig
Expand All @@ -16,28 +17,32 @@ import qualified Test.Cardano.Db.Mock.Unit.Alonzo.Tx as AlzTx

{- HLINT ignore "Reduce duplication" -}

unitTests :: IOManager -> [(Text, Text)] -> TestTree
unitTests iom knownMigrations =
testGroup
unitTests :: IOManager -> [(Text, Text)] -> DB.PGPassSource -> TestTree
unitTests iom knownMigrations source =
dependentTestGroup
"Alonzo unit tests"
[ testGroup
AllFinish
[ dependentTestGroup
"config"
[ testCase "default insert config" AlzConfig.defaultInsertConfig
, testCase "insert config" AlzConfig.insertConfig
AllFinish
[ testCase "default insert config" (AlzConfig.defaultInsertConfig source)
, testCase "insert config" (AlzConfig.insertConfig source)
]
, testGroup
, dependentTestGroup
"simple"
AllFinish
[ test "simple forge blocks" AlzSimple.forgeBlocks
, test "sync one block" AlzSimple.addSimple
, test "restart db-sync" AlzSimple.restartDBSync
, test "sync small chain" AlzSimple.addSimpleChain
]
, testGroup
, dependentTestGroup
"blocks with txs"
AllFinish
[ test "simple tx" AlzTx.addSimpleTx
, test "consume utxo same block" AlzTx.consumeSameBlock
]
]
where
test :: String -> (IOManager -> [(Text, Text)] -> Assertion) -> TestTree
test str action = testCase str (action iom knownMigrations)
test :: String -> (DB.PGPassSource -> IOManager -> [(Text, Text)] -> Assertion) -> TestTree
test str action = testCase str (action source iom knownMigrations)
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module Test.Cardano.Db.Mock.Unit.Alonzo.Config (
insertConfig,
) where

import qualified Cardano.Db as DB
import Cardano.DbSync.Config
import Cardano.DbSync.Config.Types
import Cardano.Prelude
Expand All @@ -11,13 +12,13 @@ import Test.Cardano.Db.Mock.Config
import Test.Tasty.HUnit (Assertion (), (@?=))
import Prelude ()

defaultInsertConfig :: Assertion
defaultInsertConfig = do
defaultInsertConfig :: DB.PGPassSource -> Assertion
defaultInsertConfig _source = do
cfg <- mkSyncNodeConfig alonzoConfigDir initCommandLineArgs
dncInsertOptions cfg @?= def

insertConfig :: Assertion
insertConfig = do
insertConfig :: DB.PGPassSource -> Assertion
insertConfig _source = do
cfg <- mkSyncNodeConfig configDir initCommandLineArgs
let expected =
SyncInsertOptions
Expand Down
Loading
Loading