Skip to content

Commit

Permalink
Spawn cardano-node in a test for 10 seconds.
Browse files Browse the repository at this point in the history
  • Loading branch information
newhoggy committed Jul 31, 2020
1 parent 1936eae commit 6e746c6
Show file tree
Hide file tree
Showing 8 changed files with 116 additions and 10 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Expand Up @@ -45,3 +45,5 @@ hie.yaml

# Ignore files generated by tests
tmp
/cardano-node/logs

4 changes: 3 additions & 1 deletion cardano-cli/cardano-cli.cabal
@@ -1,3 +1,5 @@
cabal-version: 2.4

name: cardano-cli
version: 1.18.0
description: The Cardano command-line interface.
Expand All @@ -8,7 +10,6 @@ license-files:
LICENSE
NOTICE
build-type: Simple
cabal-version: >= 1.10
extra-source-files: README.md

Flag unexpected_thunks
Expand Down Expand Up @@ -224,6 +225,7 @@ test-suite cardano-cli-test
Test.Pioneers.Exercise2
Test.Pioneers.Exercise3
Test.Pioneers.Exercise4
Test.Process

default-language: Haskell2010
default-extensions: NoImplicitPrelude
Expand Down
3 changes: 2 additions & 1 deletion cardano-config/cardano-config.cabal
@@ -1,3 +1,5 @@
cabal-version: 2.4

name: cardano-config
version: 0.1.0.0
author: IOHK
Expand All @@ -7,7 +9,6 @@ license-files:
LICENSE
NOTICE
build-type: Simple
cabal-version: >= 1.10
extra-source-files: README.md

flag systemd
Expand Down
19 changes: 12 additions & 7 deletions cardano-node/cardano-node.cabal
@@ -1,3 +1,5 @@
cabal-version: 2.4

name: cardano-node
version: 1.18.0
description: The cardano full node
Expand All @@ -8,7 +10,6 @@ license-files:
LICENSE
NOTICE
build-type: Simple
cabal-version: >= 1.10
extra-source-files: ChangeLog.md

Flag unexpected_thunks
Expand All @@ -20,9 +21,7 @@ flag systemd
default: True
manual: False


library

if flag(unexpected_thunks)
cpp-options: -DUNEXPECTED_THUNKS

Expand Down Expand Up @@ -265,28 +264,32 @@ test-suite cardano-node-test
base >= 4.12 && < 5
, aeson
, bytestring
, cardano-node
, cardano-config
, cardano-crypto-class
, cardano-crypto-test
, cardano-crypto-wrapper
, cardano-node
, cardano-prelude
, cardano-prelude-test
, cardano-slotting
, containers
, cryptonite
, hedgehog
, hedgehog-corpus
, iproute
, ouroboros-consensus
, ouroboros-consensus-shelley
, ouroboros-network
, process
, shelley-spec-ledger
, shelley-spec-ledger-test
, time
, hedgehog
, hedgehog-corpus

other-modules: Test.Cardano.Node.Gen
other-modules: Test.Cardano.Node.Chairman
Test.Cardano.Node.Gen
Test.Cardano.Node.Json
Test.Common.Base
Test.Common.Process

default-language: Haskell2010
default-extensions: NoImplicitPrelude
Expand All @@ -298,3 +301,5 @@ test-suite cardano-node-test
-Wpartial-fields
-Wcompat
-threaded -rtsopts -with-rtsopts=-N -with-rtsopts=-T

build-tool-depends: cardano-node:cardano-node
40 changes: 40 additions & 0 deletions cardano-node/test/Test/Cardano/Node/Chairman.hs
@@ -0,0 +1,40 @@
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE TemplateHaskell #-}

module Test.Cardano.Node.Chairman
( tests
) where

import Cardano.Prelude
import Hedgehog (Property, discover)

import qualified Hedgehog as H
import qualified System.Process as IO
import qualified Test.Common.Base as H
import qualified Test.Common.Process as H

prop_spawnOneNode :: Property
prop_spawnOneNode = H.propertyOnce $ do
(_mIn, _mOut, _mErr, hProcess) <- H.createProcess $
( IO.proc "cardano-node"
[ "run"
, "--database-path", "../db/node-2/"
, "--socket-path", "../socket/node-2-socket"
, "--port", "3002"
, "--topology", "../configuration/defaults/simpleview/topology-node-2.json"
, "--config", "../configuration/defaults/simpleview/config-2.yaml"
, "--signing-key", "../configuration/defaults/simpleview/genesis/delegate-keys.002.key"
, "--delegation-certificate", "../configuration/defaults/simpleview/genesis/delegation-cert.002.json"
]
)
{ IO.create_group = True
}

H.threadDelay 10000000

H.interruptProcessGroupOf hProcess

void $ H.waitForProcess hProcess

tests :: IO Bool
tests = H.checkParallel $$discover
18 changes: 18 additions & 0 deletions cardano-node/test/Test/Common/Base.hs
@@ -0,0 +1,18 @@
module Test.Common.Base
( propertyOnce
, threadDelay
) where

import Control.Monad.IO.Class (liftIO)
import Data.Function (($), (.))
import Data.Int
import System.IO (IO)

import qualified Control.Concurrent as IO
import qualified Hedgehog as H

propertyOnce :: H.PropertyT IO () -> H.Property
propertyOnce = H.withTests 1 . H.property

threadDelay :: Int -> H.PropertyT IO ()
threadDelay n = liftIO $ IO.threadDelay n
36 changes: 36 additions & 0 deletions cardano-node/test/Test/Common/Process.hs
@@ -0,0 +1,36 @@
module Test.Common.Process
( createProcess
, interruptProcessGroupOf
, waitForProcess
) where

import Control.Monad.IO.Class
import Data.Function ((.))
import Data.Function (($))
import Data.Maybe (Maybe (..))
import GHC.Stack (HasCallStack)
import System.Exit (ExitCode)
import System.IO (Handle, IO)
import System.Process (CreateProcess (..), ProcessHandle)

import qualified GHC.Stack as GHC
import qualified Hedgehog as H
import qualified System.Process as IO

createProcess :: HasCallStack
=> CreateProcess
-> H.PropertyT IO (Maybe Handle, Maybe Handle, Maybe Handle, ProcessHandle)
createProcess cp = GHC.withFrozenCallStack $ do
H.evalM . liftIO $ IO.createProcess cp

interruptProcessGroupOf :: HasCallStack
=> ProcessHandle
-> H.PropertyT IO ()
interruptProcessGroupOf hProcess = GHC.withFrozenCallStack $ do
H.evalM . liftIO $ IO.interruptProcessGroupOf hProcess

waitForProcess :: HasCallStack
=> ProcessHandle
-> H.PropertyT IO ExitCode
waitForProcess hProcess = GHC.withFrozenCallStack $ do
H.evalM . liftIO $ IO.waitForProcess hProcess
4 changes: 3 additions & 1 deletion cardano-node/test/cardano-node-test.hs
Expand Up @@ -3,10 +3,12 @@ import Cardano.Prelude

import Hedgehog.Main (defaultMain)

import qualified Test.Cardano.Node.Chairman
import qualified Test.Cardano.Node.Json

main :: IO ()
main =
defaultMain
[ Test.Cardano.Node.Json.tests
[ Test.Cardano.Node.Chairman.tests
, Test.Cardano.Node.Json.tests
]

0 comments on commit 6e746c6

Please sign in to comment.