Skip to content

Commit

Permalink
Merge pull request #223 from input-output-hk/KtorZ/220/debt-coverage-…
Browse files Browse the repository at this point in the history
…integration-tests

Code Coverage for integration tests scenarios
  • Loading branch information
KtorZ committed May 7, 2019
2 parents fb20bfc + b053b11 commit 9b2dfbc
Show file tree
Hide file tree
Showing 10 changed files with 101 additions and 39 deletions.
3 changes: 3 additions & 0 deletions lib/http-bridge/cardano-wallet-http-bridge.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -147,18 +147,21 @@ test-suite integration
, http-api-data
, http-types
, process
, servant-server
, template-haskell
, text
, text-class
, time
, transformers
, warp
type:
exitcode-stdio-1.0
hs-source-dirs:
test/integration
main-is:
Main.hs
other-modules:
Cardano.LauncherSpec
Cardano.WalletSpec
Cardano.Wallet.Network.HttpBridgeSpec
Test.Integration.Framework.DSL
Expand Down
14 changes: 4 additions & 10 deletions lib/http-bridge/src/Cardano/Environment.hs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ instance Show ErrMissingOrInvalidEnvVar where
-- ENV[NETWORK] = patate
-- |
-- |
-- *--> patate is neither "mainnet", "testnet", "staging" nor "local".
-- *--> patate is neither "mainnet", "testnet" nor "staging"
--
-- @
--
Expand Down Expand Up @@ -129,26 +129,23 @@ unsafeLookupEnv k = unsafePerformIO $ do
Environment
-------------------------------------------------------------------------------}

-- | Available network options. 'Local' means a local cluster running on the
-- host machine.
data Network = Mainnet | Testnet | Staging | Local
-- | Available network options.
data Network = Mainnet | Testnet | Staging
deriving (Generic, Show, Eq, Enum)

instance FromText Network where
fromText = \case
"mainnet" -> Right Mainnet
"testnet" -> Right Testnet
"staging" -> Right Staging
"local" -> Right Local
s -> Left $ TextDecodingError $ T.unpack s
<> " is neither \"mainnet\", \"testnet\", \"staging\" nor \"local\"."
<> " is neither \"mainnet\", \"testnet\" nor \"staging\"."

instance ToText Network where
toText = \case
Mainnet -> "mainnet"
Testnet -> "testnet"
Staging -> "staging"
Local -> "local"

-- | Get the current target 'Network' from the Environment.
--
Expand All @@ -162,11 +159,8 @@ newtype ProtocolMagic = ProtocolMagic Int32
deriving (Generic, Show)

-- | Get the 'ProtocolMagic' corresponding to a given 'Network'.
--
-- Note that the 'ProtocolMagic' for 'Local' and 'Testnet' are the same.
protocolMagic :: Network -> ProtocolMagic
protocolMagic = \case
Mainnet -> ProtocolMagic 764824073
Staging -> ProtocolMagic 633343913
Testnet -> ProtocolMagic 1097911063
Local -> ProtocolMagic 1097911063
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ module Cardano.Wallet.Compatibility.HttpBridge
import Prelude

import Cardano.Environment
( Network (Local, Mainnet, Staging, Testnet), network, protocolMagic )
( Network (Mainnet, Staging, Testnet), network, protocolMagic )
import Cardano.Wallet.Binary.HttpBridge
( encodeAddress, encodeProtocolMagic, encodeTx )
import Cardano.Wallet.Primitive.AddressDerivation
Expand Down Expand Up @@ -66,7 +66,6 @@ instance KeyToAddress HttpBridge where
Mainnet -> emptyAttributes
Staging -> emptyAttributes
Testnet -> attributesWithProtocolMagic (protocolMagic network)
Local -> attributesWithProtocolMagic (protocolMagic network)

attributesWithProtocolMagic pm = mempty
<> CBOR.encodeMapLen 1
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,6 @@ newTransactionLayer = TransactionLayer
Mainnet -> 1 + 43 + sizeOfCoin c
Staging -> 1 + 43 + sizeOfCoin c
Testnet -> 1 + 50 + sizeOfCoin c
Local -> 1 + 50 + sizeOfCoin c

-- tx ------------------------------------- 6 + Σs(i) + ls(o) + Σs(c)
-- | list len 3 -- 1
Expand Down
36 changes: 36 additions & 0 deletions lib/http-bridge/test/integration/Cardano/LauncherSpec.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
module Cardano.LauncherSpec
( spec
) where

import Prelude

import Cardano.Launcher
( Command (..), StdStream (..), launch )
import Control.Concurrent
( threadDelay )
import Control.Concurrent.Async
( async, cancel, race, wait )
import Control.Monad
( void )
import Test.Hspec
( Spec, describe, expectationFailure, it )

spec :: Spec
spec = describe "cardano-wallet-launcher" $ do
it "Can start launcher against testnet" $ do
let cardanoWalletLauncher = Command
"cardano-wallet-launcher"
[ "--wallet-server-port", "1337"
, "--http-bridge-port", "8080"
] (return ())
Inherit
handle <- async $ void $ launch [cardanoWalletLauncher]
let fiveSeconds = 5000000
winner <- race (threadDelay fiveSeconds) (wait handle)
case winner of
Left _ ->
cancel handle
Right _ ->
expectationFailure
"cardano-wallet-launcher isn't supposed to terminate. \
\Something went wrong."
Original file line number Diff line number Diff line change
Expand Up @@ -196,15 +196,11 @@ spec = do

requireTestnet :: Spec -> Spec
requireTestnet prop = case network of
Testnet -> prop
Mainnet -> notDefinedFor network
Staging -> notDefinedFor network
Local -> notDefinedFor network

Testnet -> prop
Mainnet -> notDefinedFor network
Staging -> notDefinedFor network
where
notDefinedFor n =
it ("defined for NETWORK=testnet, not NETWORK=" ++ toStr n) False

toStr
= T.unpack . toText

63 changes: 53 additions & 10 deletions lib/http-bridge/test/integration/Main.hs
Original file line number Diff line number Diff line change
@@ -1,37 +1,66 @@
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE TypeOperators #-}

module Main where

import Prelude

import Cardano.Environment
( Network (..), network )
import Cardano.Launcher
( Command (..), StdStream (..), launch )
import Cardano.Wallet
( mkWalletLayer )
import Cardano.Wallet.Api
( Api )
import Cardano.Wallet.Api.Server
( server )
import Cardano.Wallet.Compatibility.HttpBridge
( HttpBridge )
import Control.Concurrent
( threadDelay )
( forkIO, threadDelay )
import Control.Concurrent.Async
( async, cancel, link )
import Control.Monad
( void )
import Data.Function
( (&) )
import Data.Proxy
( Proxy (..) )
import Data.Time
( addUTCTime, defaultTimeLocale, formatTime, getCurrentTime )
import Network.HTTP.Client
( defaultManagerSettings, newManager )
import System.Environment
( setEnv )
import Servant
( (:>), serve )
import System.IO
( IOMode (..), hClose, openFile )
import Test.Hspec
( after, afterAll, beforeAll, describe, hspec )
import Test.Integration.Framework.DSL
( Context (..), tearDown )

import qualified Cardano.LauncherSpec as Launcher
import qualified Cardano.Wallet.DB.MVar as MVar
import qualified Cardano.Wallet.Network.HttpBridge as HttpBridge
import qualified Cardano.Wallet.Network.HttpBridgeSpec as HttpBridge
import qualified Cardano.Wallet.Transaction.HttpBridge as HttpBridge
import qualified Cardano.WalletSpec as Wallet
import qualified Network.Wai.Handler.Warp as Warp
import qualified Test.Integration.Scenario.Wallets as Wallets

main :: IO ()
main = do
case network of
Testnet ->
return ()
_ ->
fail $ "unsupported integration environment: " <> show network
hspec $ do
describe "Cardano.LauncherSpec" Launcher.spec
describe "Cardano.WalletSpec" Wallet.spec
describe "Cardano.Wallet.Network.HttpBridge" HttpBridge.spec
describe "Cardano.Wallet.Network.HttpBridgeSpec" HttpBridge.spec
beforeAll startCluster $ afterAll killCluster $ after tearDown $ do
describe "Wallets API endpoint tests" Wallets.spec
where
Expand All @@ -52,9 +81,10 @@ 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")
, cardanoWalletLauncher "1337" "8080" "local" handle
, cardanoHttpBridge "8080" "local" handle
]
link cluster
cardanoWalletServer 1337 8080
let baseURL = "http://localhost:1337/"
manager <- newManager defaultManagerSettings
threadDelay (2 * startUpDelay)
Expand All @@ -80,9 +110,22 @@ main = do
] (pure ())
NoStream

cardanoWalletLauncher serverPort bridgePort network handle = Command
"cardano-wallet-launcher"
[ "--wallet-server-port", serverPort
, "--http-bridge-port", bridgePort
] (setEnv "NETWORK" network *> threadDelay startUpDelay)
cardanoHttpBridge port template handle = Command
"cardano-http-bridge"
[ "start"
, "--template", template
, "--port", port
] (threadDelay startUpDelay)
(UseHandle handle)

-- NOTE
-- We start the wallet server in the same process such that we get
-- code coverage measures from running the scenarios on top of it!
cardanoWalletServer serverPort bridgePort = void $ forkIO $ do
threadDelay startUpDelay
db <- MVar.newDBLayer
nl <- HttpBridge.newNetworkLayer bridgePort
let tl = HttpBridge.newTransactionLayer
let wallet = mkWalletLayer @_ @HttpBridge db nl tl
let settings = Warp.defaultSettings & Warp.setPort serverPort
Warp.runSettings settings (serve (Proxy @("v2" :> Api)) (server wallet))
2 changes: 1 addition & 1 deletion lib/http-bridge/test/unit/Cardano/EnvironmentSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ spec = do
let ctx =
( "not-a-network"
, TextDecodingError "not-a-network is neither \"mainnet\",\
\ \"testnet\", \"staging\" nor \"local\"."
\ \"testnet\" nor \"staging\"."
)
let selector (ErrMissingOrInvalidEnvVar n _ c) =
n == "PATATE" && c == Just ctx
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,6 @@ spec = do
Staging ->
xit "No golden tests for 'Staging' network" False

Local ->
xit "No golden tests for 'Local' network" False

{-------------------------------------------------------------------------------
Golden Tests
-------------------------------------------------------------------------------}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -640,9 +640,6 @@ spec = do
\61cc5fbfc0e4503b9d3f54ab80c328c0fc8c00b1488bb90fdc9eded69567f2\
\ee5af248a6060c758176885604c4c36b076bdd0061ce638b0d01"

Local ->
xit "No golden tests for 'Local' network" False

Staging ->
xit "No golden tests for 'Staging' network" False

Expand Down Expand Up @@ -706,7 +703,6 @@ isValidSelection (CoinSelection i o c) =
-- | Testnet | Sequential | 46,47,48 or 50 |
--
-- The address format on 'Staging' is the same as 'Mainnet'.
-- The address format on 'Local' is the same as 'Testnet'.
genAddress :: (Int, Int) -> Gen Address
genAddress range = do
n <- choose range
Expand Down Expand Up @@ -770,7 +766,6 @@ instance {-# OVERLAPS #-} Arbitrary (Network -> Address) where
Mainnet -> mainnetA
Staging -> mainnetA
Testnet -> testnetA
Local -> testnetA

instance Arbitrary CoinSelection where
shrink sel@(CoinSelection inps outs chgs) = case (inps, outs, chgs) of
Expand Down

0 comments on commit 9b2dfbc

Please sign in to comment.