Skip to content

Commit

Permalink
Merge pull request #135 from input-output-hk/KtorZ/67/local-cluster-p…
Browse files Browse the repository at this point in the history
…art2

Get 'cardano-node-simple' available in CI & allow manipulating stream output when launching commands
  • Loading branch information
KtorZ committed Mar 29, 2019
2 parents ed9d692 + eb79a14 commit 8164411
Show file tree
Hide file tree
Showing 9 changed files with 44 additions and 31 deletions.
8 changes: 6 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ addons:
apt:
packages:
- libgmp-dev
- librocksdb-dev

# Define custom set of stages
stages:
Expand All @@ -63,7 +64,11 @@ jobs:
name: "Compiling"
script:
- mkdir -p ~/.local/bin
- travis_retry curl -L -o cardano-node-simple.tar.gz https://raw.githubusercontent.com/input-output-hk/cardano-wallet/master/test/data/cardano-node-simple/cardano-node-simple-3.0.1.tar.gz
- tar xzf cardano-node-simple.tar.gz -C $HOME/.local/bin
- cardano-node-simple --version
- test "$(cardano-http-bridge --version)" = "cardano-http-bridge 0.0.1" || travis_retry cargo install --force --branch cardano-wallet-integration --git https://github.com/KtorZ/cardano-http-bridge.git
- cardano-http-bridge --version
- travis_retry curl -L https://get.haskellstack.org/stable/linux-x86_64.tar.gz | tar xz --wildcards --strip-components=1 -C ~/.local/bin '*/stack'
- stack --no-terminal setup
- stack --no-terminal build --only-snapshot
Expand All @@ -87,9 +92,8 @@ jobs:
name: "Tests"
script:
- tar xzf $STACK_WORK_CACHE
- travis_retry curl -L -o hermes-testnet.tar.gz https://github.com/input-output-hk/cardano-wallet/raw/data-integration-testing/hermes-testnet.tar.gz
- travis_retry curl -L -o hermes-testnet.tar.gz https://raw.githubusercontent.com/input-output-hk/cardano-wallet/master/test/data/cardano-http-bridge/hermes-testnet.tar.gz
- tar xzf hermes-testnet.tar.gz -C $HOME
- cardano-http-bridge --help
- stack --no-terminal test --coverage
- tar czf $STACK_WORK_CACHE .stack-work

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ $ tar xzf cardano-node-simple-3.0.1.tar.gz -C /usr/local/bin && rm cardano-node-
3. Import the initial testnet chain bootstrap for the `cardano-http-bridge`

```
$ curl -L -o hermes-testnet.tar.gz https://raw.githubusercontent.com/input-output-hk/cardano-wallet/data-integration-testing/hermes-testnet.tar.gz
$ curl -L -o hermes-testnet.tar.gz https://raw.githubusercontent.com/input-output-hk/cardano-wallet/master/test/data/cardano-http-bridge/hermes-testnet.tar.gz
$ tar xzf hermes-testnet.tar.gz -C $HOME && rm hermes-testnet.tar.gz
```

Expand Down
17 changes: 13 additions & 4 deletions app/Cardano/Launcher.hs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

module Cardano.Launcher
( Command (..)
, StdStream(..)
, ProcessHasExited(..)
, launch
, installSignalHandlers
Expand All @@ -28,7 +29,12 @@ import Fmt
import System.Exit
( ExitCode )
import System.Process
( proc, waitForProcess, withCreateProcess )
( CreateProcess (..)
, StdStream (..)
, proc
, waitForProcess
, withCreateProcess
)

#ifdef mingw32_HOST_OS
import Cardano.Launcher.Windows
Expand All @@ -43,6 +49,8 @@ data Command = Command
, cmdArgs :: [String]
, cmdSetup :: IO ()
-- ^ An extra action to run _before_ the command
, cmdOutput :: StdStream
-- ^ What to do with stdout & stderr
}

-- Format a command nicely with one argument / option per line.
Expand All @@ -54,7 +62,7 @@ data Command = Command
-- --port 8080
-- --network mainnet
instance Buildable Command where
build (Command name args _) = build name
build (Command name args _ _) = build name
<> "\n"
<> indentF 4
(blockListF' "" build $ snd $ foldl buildOptions ("", []) args)
Expand All @@ -77,9 +85,10 @@ instance Exception ProcessHasExited

launch :: [Command] -> IO ProcessHasExited
launch cmds = do
res <- try $ forConcurrently cmds $ \(Command name args before) -> do
res <- try $ forConcurrently cmds $ \(Command name args before output) -> do
before
withCreateProcess (proc name args) $ \_ _ _ h -> do
let process = (proc name args) { std_out = output , std_err = output }
withCreateProcess process $ \_ _ _ h -> do
code <- waitForProcess h
throwIO $ ProcessHasExited name code
case res of
Expand Down
3 changes: 3 additions & 0 deletions app/launcher/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import Cardano.CLI
import Cardano.Launcher
( Command (Command)
, ProcessHasExited (ProcessHasExited)
, StdStream (..)
, installSignalHandlers
, launch
)
Expand Down Expand Up @@ -85,6 +86,7 @@ nodeHttpBridgeOn port net = Command
, "--template", encode net
]
(return ())
Inherit

walletOn :: Port "Wallet" -> Port "Node" -> Network -> Command
walletOn wp np net = Command
Expand All @@ -94,5 +96,6 @@ walletOn wp np net = Command
, "--network", encode net
]
(threadDelay oneSecond)
Inherit
where
oneSecond = 1000000
3 changes: 2 additions & 1 deletion test/integration/Cardano/Wallet/Network/HttpBridgeSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ module Cardano.Wallet.Network.HttpBridgeSpec
import Prelude

import Cardano.Launcher
( Command (..), launch )
( Command (..), StdStream (..), launch )
import Cardano.Wallet.Network
( NetworkLayer (..) )
import Cardano.Wallet.Network.HttpBridge
Expand Down Expand Up @@ -93,6 +93,7 @@ spec = do
, "--template", "testnet"
]
(return ())
Inherit
]
network <- newNetworkLayer
threadDelay 1000000
Expand Down
3 changes: 2 additions & 1 deletion test/integration/Cardano/WalletSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ module Cardano.WalletSpec
import Prelude

import Cardano.Launcher
( Command (..), launch )
( Command (..), StdStream (..), launch )
import Cardano.Wallet
( NewWallet (..), WalletLayer (..), mkWalletLayer )
import Cardano.Wallet.Primitive.AddressDerivation
Expand Down Expand Up @@ -67,6 +67,7 @@ spec = do
, "--template", "testnet"
]
(return ())
Inherit
]
threadDelay 1000000
(handle,) <$> (mkWalletLayer
Expand Down
18 changes: 6 additions & 12 deletions test/integration/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ module Main where
import Prelude

import Cardano.Launcher
( Command (..), launch )
( Command (..), StdStream (..), launch )
import Control.Concurrent
( threadDelay )
import Control.Concurrent.Async
Expand Down Expand Up @@ -61,8 +61,7 @@ main = do
, cardanoNodeSimple stateDir systemStart ("core1", "127.0.0.1:3001")
, cardanoNodeSimple stateDir systemStart ("core2", "127.0.0.1:3002")
, cardanoNodeSimple stateDir systemStart ("relay", "127.0.0.1:3100")
, cardanoHttpBridge "8080" "local"
, cardanoWalletServer "1337" "8080" "local"
, cardanoWalletLauncher "1337" "8080" "local"
]
link cluster
let baseURL = "http://localhost:1337/"
Expand All @@ -85,20 +84,15 @@ main = do
, "--log-config", stateDir <> "/logs/" <> nodeId <> "/config.json"
, "--rebuild-db"
] (pure ())
NoStream

cardanoHttpBridge port network = Command
"cardano-http-bridge"
[ "start"
, "--port", port
, "--template", network
] (threadDelay 5000000)

cardanoWalletServer serverPort bridgePort network = Command
"cardano-wallet-server"
cardanoWalletLauncher serverPort bridgePort network = Command
"cardano-wallet-launcher"
[ "--wallet-server-port", serverPort
, "--http-bridge-port", bridgePort
, "--network", network
] (threadDelay 6000000)
Inherit


-- Exercise the request functions, which just fail at the moment.
Expand Down
20 changes: 10 additions & 10 deletions test/unit/Cardano/LauncherSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ module Cardano.LauncherSpec
import Prelude

import Cardano.Launcher
( Command (..), ProcessHasExited (..), launch )
( Command (..), ProcessHasExited (..), StdStream (..), launch )
import Control.Concurrent.MVar
( newEmptyMVar, putMVar, tryReadMVar )
import System.Exit
Expand All @@ -21,35 +21,35 @@ spec :: Spec
spec = do
it "1st process exits with 0, others are cancelled" $ do
let commands =
[ Command "./test/data/Launcher/once.sh" ["0"] (pure ())
, Command "./test/data/Launcher/forever.sh" [] (pure ())
[ Command "./test/data/Launcher/once.sh" ["0"] (pure ()) Inherit
, Command "./test/data/Launcher/forever.sh" [] (pure ()) Inherit
]
(ProcessHasExited name code) <- launch commands
name `shouldBe` cmdName (commands !! 0)
code `shouldBe` ExitSuccess

it "2nd process exits with 0, others are cancelled" $ do
let commands =
[ Command "./test/data/Launcher/forever.sh" [] (pure ())
, Command "./test/data/Launcher/once.sh" ["0"] (pure ())
[ Command "./test/data/Launcher/forever.sh" [] (pure ()) Inherit
, Command "./test/data/Launcher/once.sh" ["0"] (pure ()) Inherit
]
(ProcessHasExited name code) <- launch commands
name `shouldBe` cmdName (commands !! 1)
code `shouldBe` ExitSuccess

it "1st process exits with 14, others are cancelled" $ do
let commands =
[ Command "./test/data/Launcher/once.sh" ["14"] (pure ())
, Command "./test/data/Launcher/forever.sh" [] (pure ())
[ Command "./test/data/Launcher/once.sh" ["14"] (pure ()) Inherit
, Command "./test/data/Launcher/forever.sh" [] (pure ()) Inherit
]
(ProcessHasExited name code) <- launch commands
name `shouldBe` cmdName (commands !! 0)
code `shouldBe` (ExitFailure 14)

it "2nd process exits with 14, others are cancelled" $ do
let commands =
[ Command "./test/data/Launcher/forever.sh" [] (pure ())
, Command "./test/data/Launcher/once.sh" ["14"] (pure ())
[ Command "./test/data/Launcher/forever.sh" [] (pure ()) Inherit
, Command "./test/data/Launcher/once.sh" ["14"] (pure ()) Inherit
]
(ProcessHasExited name code) <- launch commands
name `shouldBe` cmdName (commands !! 1)
Expand All @@ -59,7 +59,7 @@ spec = do
mvar <- newEmptyMVar
let before = putMVar mvar "executed"
let commands =
[ Command "./test/data/Launcher/once.sh" ["0"] before
[ Command "./test/data/Launcher/once.sh" ["0"] before Inherit
]
(ProcessHasExited _ code) <- launch commands
code `shouldBe` ExitSuccess
Expand Down

0 comments on commit 8164411

Please sign in to comment.