Skip to content

Commit 812b41d

Browse files
committed
Add test for cardano-testnet P2P topology files
1 parent 8f14fb4 commit 812b41d

File tree

5 files changed

+100
-4
lines changed

5 files changed

+100
-4
lines changed

cardano-testnet/cardano-testnet.cabal

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,7 @@ test-suite cardano-testnet-test
221221
Cardano.Testnet.Test.Gov.TreasuryWithdrawal
222222
Cardano.Testnet.Test.Misc
223223
Cardano.Testnet.Test.Node.Shutdown
224+
Cardano.Testnet.Test.P2PTopology
224225
Cardano.Testnet.Test.SanityCheck
225226
Cardano.Testnet.Test.RunTestnet
226227
Cardano.Testnet.Test.SubmitApi.Transaction

cardano-testnet/test/cardano-testnet-test/Cardano/Testnet/Test/DumpConfig.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ import qualified Hedgehog as H
4040
import qualified Hedgehog.Extras as H
4141

4242
-- | Execute me with:
43-
-- @DISABLE_RETRIES=1 cabal test cardano-testnet-test --test-options '-p "/Dumping config files/"'@
43+
-- @DISABLE_RETRIES=1 cabal test cardano-testnet-test --test-options '-p "/Supports dumping/loading config files/"'@
4444
hprop_dump_config :: H.Property
4545
hprop_dump_config = integrationRetryWorkspace 2 "dump-config-files" $ \tmpDir -> H.runWithDefaultWatchdog_ $ do
4646

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
{-# LANGUAGE DataKinds #-}
2+
{-# LANGUAGE NamedFieldPuns #-}
3+
{-# LANGUAGE OverloadedStrings #-}
4+
{-# LANGUAGE ScopedTypeVariables #-}
5+
6+
module Cardano.Testnet.Test.P2PTopology
7+
( hprop_p2p_topology
8+
) where
9+
10+
import Cardano.Api (BlockNo (..), ChainTip (..))
11+
import Cardano.CLI.Type.Output (QueryTipLocalStateOutput (..))
12+
import Cardano.Node.Configuration.Topology (NodeId)
13+
import qualified Cardano.Node.Configuration.TopologyP2P as P2P
14+
import Cardano.Testnet hiding (shelleyGenesisFile)
15+
16+
import Prelude
17+
18+
import qualified Data.Aeson as A
19+
import qualified Data.ByteString.Lazy.Char8 as B
20+
import Data.Default.Class (def)
21+
import System.Exit (ExitCode (..))
22+
import System.FilePath ((</>))
23+
import qualified System.Process as IO
24+
25+
import Testnet.Process.Run (execCli', mkExecConfig)
26+
import Testnet.Property.Util (integrationRetryWorkspace)
27+
import Testnet.Start.Types (GenesisOptions (..),
28+
UserProvidedData (..), UserProvidedEnv (..), TopologyType (..))
29+
30+
import Hedgehog ((===))
31+
import qualified Hedgehog as H
32+
import qualified Hedgehog.Extras as H
33+
34+
35+
-- | Execute me with:
36+
-- @DISABLE_RETRIES=1 cabal test cardano-testnet-test --test-options '-p "/Can be started with P2P topology file/"'@
37+
hprop_p2p_topology :: H.Property
38+
hprop_p2p_topology = integrationRetryWorkspace 2 "p2p-topology" $ \tmpDir -> H.runWithDefaultWatchdog_ $ do
39+
40+
let testnetOptions = def { cardanoOutputDir = UserProvidedEnv tmpDir }
41+
genesisOptions = def { genesisEpochLength = 200 }
42+
someTopologyFile = tmpDir </> "node-data" </> "node1" </> "topology.json"
43+
44+
-- Generate the sandbox
45+
conf <- mkConf tmpDir
46+
createTestnetEnv
47+
testnetOptions genesisOptions P2PTopology
48+
NoUserProvidedData NoUserProvidedData NoUserProvidedData
49+
conf
50+
51+
-- Check that the topology is indeed P2P
52+
eTopology <- H.readJsonFile someTopologyFile
53+
(_topology :: P2P.NetworkTopology NodeId) <- H.leftFail eTopology
54+
55+
-- Run testnet with generated config
56+
TestnetRuntime
57+
{ testnetNodes
58+
, testnetMagic
59+
} <- cardanoTestnet testnetOptions genesisOptions conf
60+
61+
-- There should only be one SPO node among three
62+
TestnetNode
63+
{ nodeProcessHandle
64+
, nodeSprocket
65+
} <- case testnetNodes of
66+
[spoNode, _relayNode1, _relayNode2] -> do
67+
(isTestnetNodeSpo <$> testnetNodes) === [True, False, False]
68+
pure spoNode
69+
_ -> H.failure
70+
71+
-- Check that blocks have been produced on the chain after 2 minutes at most
72+
H.byDurationM 5 120 "Expected blocks to be minted" $ do
73+
execConfig <- mkExecConfig tmpDir nodeSprocket testnetMagic
74+
tipStr <- H.noteM $ execCli' execConfig
75+
[ "query", "tip"
76+
, "--output-json"
77+
]
78+
QueryTipLocalStateOutput
79+
{ localStateChainTip = tip
80+
} <- H.nothingFail $ A.decode $ B.pack tipStr
81+
82+
case tip of
83+
ChainTipAtGenesis -> H.failure
84+
ChainTip _ _ (BlockNo blockNo) ->
85+
-- Blocks have been produced if the tip of the chain is > 0
86+
H.assertWith blockNo (> 0)
87+
88+
-- If everything went fine, terminate the node and exit with success
89+
exit <- H.evalIO $ do
90+
IO.terminateProcess nodeProcessHandle
91+
IO.waitForProcess nodeProcessHandle
92+
-- Nodes are expected to exit successfully when terminated
93+
exit === ExitSuccess

cardano-testnet/test/cardano-testnet-test/Cardano/Testnet/Test/RunTestnet.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ import qualified Hedgehog.Extras as H
2727
import Testnet.Process.Run (execCli',mkExecConfig)
2828

2929
-- | Execute me with:
30-
-- @DISABLE_RETRIES=1 cabal test cardano-testnet-test --test-options '-p "/Testnet produces blocks/"'@
30+
-- @DISABLE_RETRIES=1 cabal test cardano-testnet-test --test-options '-p "/Produces blocks/"'@
3131
hprop_run_testnet :: H.Property
3232
hprop_run_testnet = integrationRetryWorkspace 2 "run-testnet" $ \tmpDir -> H.runWithDefaultWatchdog_ $ do
3333

cardano-testnet/test/cardano-testnet-test/cardano-testnet-test.hs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import qualified Cardano.Testnet.Test.Gov.Transaction.HashMismatch as WrongHash
2727
import qualified Cardano.Testnet.Test.Gov.TreasuryDonation as Gov
2828
import qualified Cardano.Testnet.Test.Gov.TreasuryWithdrawal as Gov
2929
import qualified Cardano.Testnet.Test.Node.Shutdown
30+
import qualified Cardano.Testnet.Test.P2PTopology
3031
import qualified Cardano.Testnet.Test.RunTestnet
3132
import qualified Cardano.Testnet.Test.SanityCheck as LedgerEvents
3233
import qualified Cardano.Testnet.Test.SubmitApi.Transaction
@@ -110,10 +111,11 @@ tests = do
110111
, ignoreOnWindows "foldEpochState receives ledger state" Cardano.Testnet.Test.FoldEpochState.prop_foldEpochState
111112
, ignoreOnMacAndWindows "CliQueries" Cardano.Testnet.Test.Cli.Query.hprop_cli_queries
112113
]
113-
, ignoreOnMacAndWindows "Dumping config files" Cardano.Testnet.Test.DumpConfig.hprop_dump_config
114114
]
115115
, T.testGroup "Cardano-testnet"
116-
[ ignoreOnWindows "Testnet produces blocks" Cardano.Testnet.Test.RunTestnet.hprop_run_testnet
116+
[ ignoreOnWindows "Produces blocks" Cardano.Testnet.Test.RunTestnet.hprop_run_testnet
117+
, ignoreOnMacAndWindows "Supports dumping/loading config files" Cardano.Testnet.Test.DumpConfig.hprop_dump_config
118+
, ignoreOnMacAndWindows "Can be started with P2P topology file" Cardano.Testnet.Test.P2PTopology.hprop_p2p_topology
117119
]
118120
, T.testGroup "SubmitApi"
119121
[ ignoreOnMacAndWindows "transaction" Cardano.Testnet.Test.SubmitApi.Transaction.hprop_transaction

0 commit comments

Comments
 (0)