Skip to content

Commit 9abc42f

Browse files
committed
Share function that checks testnet nodes health
1 parent 7b8a6ba commit 9abc42f

File tree

6 files changed

+92
-193
lines changed

6 files changed

+92
-193
lines changed

cardano-testnet/cardano-testnet.cabal

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,7 @@ test-suite cardano-testnet-test
227227
Cardano.Testnet.Test.RunTestnet
228228
Cardano.Testnet.Test.SubmitApi.Transaction
229229
Cardano.Testnet.Test.UpdateTimeStamps
230+
Cardano.Testnet.Test.Utils
230231

231232
type: exitcode-stdio-1.0
232233

Lines changed: 5 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
{-# LANGUAGE DataKinds #-}
2-
{-# LANGUAGE NamedFieldPuns #-}
32
{-# LANGUAGE NumericUnderscores #-}
43
{-# LANGUAGE OverloadedStrings #-}
54
{-# LANGUAGE ScopedTypeVariables #-}
@@ -8,34 +7,27 @@ module Cardano.Testnet.Test.DumpConfig
87
( hprop_dump_config
98
) where
109

11-
import Cardano.Api (BlockNo (..), ChainTip (..), ShelleyGenesis, runExceptT,
12-
sgSystemStart)
10+
import Cardano.Api (ShelleyGenesis, runExceptT, sgSystemStart)
1311
import Cardano.Api.Byron (GenesisData (..))
1412
import qualified Cardano.Api.Byron as Byron
1513

16-
import Cardano.CLI.Type.Output (QueryTipLocalStateOutput (..))
1714
import Cardano.Prelude (canonicalEncodePretty)
1815
import Cardano.Testnet hiding (shelleyGenesisFile)
16+
import Cardano.Testnet.Test.Utils (nodesProduceBlocks)
1917

2018
import Prelude
2119

22-
import qualified Data.Aeson as A
2320
import Data.Aeson.Encode.Pretty (encodePretty)
24-
import qualified Data.ByteString.Lazy.Char8 as B
2521
import Data.Default.Class (def)
2622
import qualified Data.Time.Clock as Time
2723
import GHC.Float (double2Int)
28-
import System.Exit (ExitCode (..))
2924
import System.FilePath ((</>))
30-
import qualified System.Process as IO
3125

3226
import Testnet.Components.Configuration (startTimeOffsetSeconds)
33-
import Testnet.Process.Run (execCli', mkExecConfig)
3427
import Testnet.Property.Util (integrationRetryWorkspace)
3528
import Testnet.Start.Types (GenesisHashesPolicy (..), GenesisOptions (..),
3629
UserProvidedData (..), UserProvidedEnv (..))
3730

38-
import Hedgehog ((===))
3931
import qualified Hedgehog as H
4032
import qualified Hedgehog.Extras as H
4133

@@ -78,41 +70,6 @@ hprop_dump_config = integrationRetryWorkspace 2 "dump-config-files" $ \tmpDir ->
7870
H.lbsWriteFile shelleyGenesisFile $ encodePretty shelleyGenesis
7971

8072
-- Run testnet with generated config
81-
TestnetRuntime
82-
{ testnetNodes
83-
, testnetMagic
84-
} <- cardanoTestnet testnetOptions genesisOptions conf
85-
86-
-- There should only be one SPO node among three
87-
TestnetNode
88-
{ nodeProcessHandle
89-
, nodeSprocket
90-
} <- case testnetNodes of
91-
[spoNode, _relayNode1, _relayNode2] -> do
92-
(isTestnetNodeSpo <$> testnetNodes) === [True, False, False]
93-
pure spoNode
94-
_ -> H.failure
95-
96-
-- Check that blocks have been produced on the chain after 2 minutes at most
97-
H.byDurationM 5 120 "Expected blocks to be minted" $ do
98-
execConfig <- mkExecConfig tmpDir nodeSprocket testnetMagic
99-
tipStr <- H.noteM $ execCli' execConfig
100-
[ "query", "tip"
101-
, "--output-json"
102-
]
103-
QueryTipLocalStateOutput
104-
{ localStateChainTip = tip
105-
} <- H.nothingFail $ A.decode $ B.pack tipStr
106-
107-
case tip of
108-
ChainTipAtGenesis -> H.failure
109-
ChainTip _ _ (BlockNo blockNo) ->
110-
-- Blocks have been produced if the tip of the chain is > 0
111-
H.assertWith blockNo (> 0)
112-
113-
-- If everything went fine, terminate the node and exit with success
114-
exit <- H.evalIO $ do
115-
IO.terminateProcess nodeProcessHandle
116-
IO.waitForProcess nodeProcessHandle
117-
-- Nodes are expected to exit successfully when terminated
118-
exit === ExitSuccess
73+
runtime <- cardanoTestnet testnetOptions genesisOptions conf
74+
75+
nodesProduceBlocks tmpDir runtime
Lines changed: 5 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,25 @@
11
{-# LANGUAGE DataKinds #-}
2-
{-# LANGUAGE NamedFieldPuns #-}
32
{-# LANGUAGE OverloadedStrings #-}
43
{-# LANGUAGE ScopedTypeVariables #-}
54

65
module Cardano.Testnet.Test.P2PTopology
76
( hprop_p2p_topology
87
) where
98

10-
import Cardano.Api (BlockNo (..), ChainTip (..))
11-
import Cardano.CLI.Type.Output (QueryTipLocalStateOutput (..))
129
import qualified Cardano.Node.Configuration.TopologyP2P as P2P
13-
import Cardano.Testnet hiding (shelleyGenesisFile)
10+
import Cardano.Testnet (CardanoTestnetOptions (..), cardanoTestnet,
11+
createTestnetEnv, mkConf)
12+
import Cardano.Testnet.Test.Utils (nodesProduceBlocks)
1413

1514
import Prelude
1615

17-
import qualified Data.Aeson as A
18-
import qualified Data.ByteString.Lazy.Char8 as B
1916
import Data.Default.Class (def)
20-
import System.Exit (ExitCode (..))
2117
import System.FilePath ((</>))
22-
import qualified System.Process as IO
2318

24-
import Testnet.Process.Run (execCli', mkExecConfig)
2519
import Testnet.Property.Util (integrationRetryWorkspace)
2620
import Testnet.Start.Types (CreateEnvOptions (..), GenesisOptions (..), NodeId,
2721
UserProvidedData (..), UserProvidedEnv (..), TopologyType (..))
2822

29-
import Hedgehog ((===))
3023
import qualified Hedgehog as H
3124
import qualified Hedgehog.Extras as H
3225

@@ -53,41 +46,6 @@ hprop_p2p_topology = integrationRetryWorkspace 2 "p2p-topology" $ \tmpDir -> H.r
5346
(_topology :: P2P.NetworkTopology NodeId) <- H.leftFail eTopology
5447

5548
-- Run testnet with generated config
56-
TestnetRuntime
57-
{ testnetNodes
58-
, testnetMagic
59-
} <- cardanoTestnet testnetOptions genesisOptions conf
49+
runtime <- cardanoTestnet testnetOptions genesisOptions conf
6050

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
51+
nodesProduceBlocks tmpDir runtime
Lines changed: 4 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
{-# LANGUAGE DataKinds #-}
2-
{-# LANGUAGE NamedFieldPuns #-}
32
{-# LANGUAGE OverloadedStrings #-}
43
{-# LANGUAGE ScopedTypeVariables #-}
54

@@ -9,22 +8,15 @@ module Cardano.Testnet.Test.RunTestnet
98

109
import Prelude
1110

12-
import Cardano.Api (BlockNo (..), ChainTip (..))
13-
import Cardano.CLI.Type.Output (QueryTipLocalStateOutput (..))
14-
import qualified Data.Aeson as A
15-
import qualified Data.ByteString.Lazy.Char8 as B
1611
import Data.Default.Class (def)
17-
import System.Exit (ExitCode (..))
18-
import qualified System.Process as IO
1912

20-
import Cardano.Testnet
13+
import Cardano.Testnet (mkConf, createAndRunTestnet)
14+
import Cardano.Testnet.Test.Utils (nodesProduceBlocks)
2115
import Testnet.Property.Util (integrationRetryWorkspace)
2216
import Testnet.Start.Types (GenesisOptions (..))
2317

24-
import Hedgehog ((===))
2518
import qualified Hedgehog as H
2619
import qualified Hedgehog.Extras as H
27-
import Testnet.Process.Run (execCli',mkExecConfig)
2820

2921
-- | Execute me with:
3022
-- @DISABLE_RETRIES=1 cabal test cardano-testnet-test --test-options '-p "/Produces blocks/"'@
@@ -35,41 +27,6 @@ hprop_run_testnet = integrationRetryWorkspace 2 "run-testnet" $ \tmpDir -> H.run
3527
testnetOptions = def
3628

3729
conf <- mkConf tmpDir
38-
TestnetRuntime
39-
{ testnetNodes
40-
, testnetMagic
41-
} <- createAndRunTestnet testnetOptions shelleyOptions conf
30+
runtime <- createAndRunTestnet testnetOptions shelleyOptions conf
4231

43-
-- There should only be one SPO node among three
44-
TestnetNode
45-
{ nodeProcessHandle
46-
, nodeSprocket
47-
} <- case testnetNodes of
48-
[spoNode, _relayNode1, _relayNode2] -> do
49-
(isTestnetNodeSpo <$> testnetNodes) === [True, False, False]
50-
pure spoNode
51-
_ -> H.failure
52-
53-
-- Check that blocks have been produced on the chain after 2 minutes at most
54-
H.byDurationM 5 120 "Expected blocks to be minted" $ do
55-
execConfig <- mkExecConfig tmpDir nodeSprocket testnetMagic
56-
tipStr <- H.noteM $ execCli' execConfig
57-
[ "query", "tip"
58-
, "--output-json"
59-
]
60-
QueryTipLocalStateOutput
61-
{ localStateChainTip = tip
62-
} <- H.nothingFail $ A.decode $ B.pack tipStr
63-
64-
case tip of
65-
ChainTipAtGenesis -> H.failure
66-
ChainTip _ _ (BlockNo blockNo) ->
67-
-- Blocks have been produced if the tip of the chain is > 0
68-
H.assertWith blockNo (> 0)
69-
70-
-- If everything went fine, terminate the node and exit with success
71-
exit <- H.evalIO $ do
72-
IO.terminateProcess nodeProcessHandle
73-
IO.waitForProcess nodeProcessHandle
74-
-- Nodes are expected to exit successfully when terminated
75-
exit === ExitSuccess
32+
nodesProduceBlocks tmpDir runtime
Lines changed: 4 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
{-# LANGUAGE DataKinds #-}
2-
{-# LANGUAGE NamedFieldPuns #-}
32
{-# LANGUAGE NumericUnderscores #-}
43
{-# LANGUAGE OverloadedStrings #-}
54
{-# LANGUAGE ScopedTypeVariables #-}
@@ -8,29 +7,20 @@ module Cardano.Testnet.Test.UpdateTimeStamps
87
( hprop_update_time_stamps
98
) where
109

11-
import Cardano.Api (BlockNo (..), ChainTip (..))
12-
13-
import Cardano.CLI.Type.Output (QueryTipLocalStateOutput (..))
14-
import Cardano.Testnet hiding (shelleyGenesisFile)
10+
import Cardano.Testnet
11+
import Cardano.Testnet.Test.Utils (nodesProduceBlocks)
1512

1613
import Prelude
1714

18-
import qualified Data.Aeson as A
19-
import qualified Data.ByteString.Lazy.Char8 as B
2015
import Data.Default.Class (def)
2116
import GHC.Float (double2Int)
22-
import System.Exit (ExitCode (..))
23-
import System.FilePath ((</>))
24-
import qualified System.Process as IO
2517

2618
import Testnet.Components.Configuration (startTimeOffsetSeconds)
27-
import Testnet.Process.Run (execCli', mkExecConfig)
2819
import Testnet.Property.Util (integrationRetryWorkspace)
2920
import Testnet.Start.Types (CreateEnvOptions (..), CreateEnvUpdateTime (..),
3021
GenesisHashesPolicy (..), GenesisOptions (..),
3122
UserProvidedData (..), UserProvidedEnv (..))
3223

33-
import Hedgehog ((===))
3424
import qualified Hedgehog as H
3525
import qualified Hedgehog.Extras as H
3626

@@ -41,8 +31,6 @@ hprop_update_time_stamps = integrationRetryWorkspace 2 "update-time-stamps" $ \t
4131

4232
let testnetOptions = def { cardanoOutputDir = UserProvidedEnv tmpDir }
4333
genesisOptions = def { genesisEpochLength = 200 }
44-
byronGenesisFile = tmpDir </> "byron-genesis.json"
45-
shelleyGenesisFile = tmpDir </> "shelley-genesis.json"
4634

4735
-- Generate the sandbox
4836
conf <- mkConf tmpDir
@@ -65,41 +53,6 @@ hprop_update_time_stamps = integrationRetryWorkspace 2 "update-time-stamps" $ \t
6553
conf
6654

6755
-- Run testnet with generated config
68-
TestnetRuntime
69-
{ testnetNodes
70-
, testnetMagic
71-
} <- cardanoTestnet testnetOptions genesisOptions conf
72-
73-
-- There should only be one SPO node among three
74-
TestnetNode
75-
{ nodeProcessHandle
76-
, nodeSprocket
77-
} <- case testnetNodes of
78-
[spoNode, _relayNode1, _relayNode2] -> do
79-
(isTestnetNodeSpo <$> testnetNodes) === [True, False, False]
80-
pure spoNode
81-
_ -> H.failure
82-
83-
-- Check that blocks have been produced on the chain after 2 minutes at most
84-
H.byDurationM 5 120 "Expected blocks to be minted" $ do
85-
execConfig <- mkExecConfig tmpDir nodeSprocket testnetMagic
86-
tipStr <- H.noteM $ execCli' execConfig
87-
[ "query", "tip"
88-
, "--output-json"
89-
]
90-
QueryTipLocalStateOutput
91-
{ localStateChainTip = tip
92-
} <- H.nothingFail $ A.decode $ B.pack tipStr
93-
94-
case tip of
95-
ChainTipAtGenesis -> H.failure
96-
ChainTip _ _ (BlockNo blockNo) ->
97-
-- Blocks have been produced if the tip of the chain is > 0
98-
H.assertWith blockNo (> 0)
56+
runtime <- cardanoTestnet testnetOptions genesisOptions conf
9957

100-
-- If everything went fine, terminate the node and exit with success
101-
exit <- H.evalIO $ do
102-
IO.terminateProcess nodeProcessHandle
103-
IO.waitForProcess nodeProcessHandle
104-
-- Nodes are expected to exit successfully when terminated
105-
exit === ExitSuccess
58+
nodesProduceBlocks tmpDir runtime

0 commit comments

Comments
 (0)