Skip to content

Commit

Permalink
Add tests for the application http API
Browse files Browse the repository at this point in the history
  • Loading branch information
paolino committed May 8, 2024
1 parent 60e08dd commit eb51c75
Show file tree
Hide file tree
Showing 4 changed files with 135 additions and 6 deletions.
9 changes: 9 additions & 0 deletions .buildkite/pipeline.yml
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,15 @@ steps:
env:
TMPDIR: "/cache"

- label: Run local-cluster tests
key: local-cluster-tests
depends_on: linux-nix
command: nix shell .#local-cluster -c cabal test -O0 local-cluster
agents:
system: ${linux}
env:
TMPDIR: "/cache"

- label: "Babbage integration tests (linux)"
key: linux-tests-integration-babbage
depends_on: linux-nix
Expand Down
3 changes: 3 additions & 0 deletions justfile
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ unit-tests-cabal-match match:
-O0 -v0 \
--test-options '--match="{{match}}"'

unit-tests-local-cluster-match match:
nix shell '.#local-cluster' 'nixpkgs#just' \
-c just unit-tests-cabal-match {{match}}
# run unit tests
unit-tests-cabal:
just unit-tests-cabal-match ""
Expand Down
9 changes: 8 additions & 1 deletion lib/local-cluster/local-cluster.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -191,21 +191,28 @@ test-suite test
, aeson
, base
, bytestring
, cardano-wallet-application-extras
, cardano-wallet-launcher
, cardano-wallet-primitive
, cardano-wallet-test-utils
, contra-tracer
, filepath
, foldl
, hspec
, hspec-golden
, local-cluster
, mtl
, openapi3
, process
, QuickCheck
, time
, unliftio
, with-utf8

build-tool-depends: hspec-discover:hspec-discover
build-tool-depends:
, hspec-discover:hspec-discover
, local-cluster:local-cluster

other-modules:
Cardano.Wallet.Launch.Cluster.Monitoring.Http.APISpec
Cardano.Wallet.Launch.Cluster.Monitoring.Http.OpenAPISpec
Expand Down
Original file line number Diff line number Diff line change
@@ -1,30 +1,52 @@
{-# LANGUAGE LambdaCase #-}

module Cardano.Wallet.Launch.Cluster.Monitoring.MonitorSpec
( spec
)
where

import Prelude

import Cardano.Launcher
( Command (..)
, ProcessHandles (..)
, withBackendProcess
)
import Cardano.Wallet.Launch.Cluster.Monitoring.Http.Application.Client
( RunApplicationQ
)
import Cardano.Wallet.Launch.Cluster.Monitoring.Http.Control.Client
( Query (..)
, RunQuery (..)
)
import Cardano.Wallet.Launch.Cluster.Monitoring.Monitor
( MonitorConfiguration (..)
, withMonitoring
, withMonitoringClient
)
import Cardano.Wallet.Launch.Cluster.Monitoring.Phase
( History (..)
, Phase (..)
)
import Cardano.Wallet.Network.Ports
( PortNumber
, getRandomPort
)
import Cardano.Wallet.Primitive.NetworkId
( SNetworkId (SMainnet)
( NetworkId (..)
, SNetworkId (SMainnet)
, withSNetworkId
)
import Control.Exception
( finally
)
import Control.Monad
( unless
( forM_
, unless
)
import Control.Monad.Cont
( evalContT
( ContT (..)
, evalContT
)
import Control.Monad.Fix
( fix
Expand All @@ -40,6 +62,16 @@ import Control.Tracer
, nullTracer
, traceWith
)
import System.Environment
( lookupEnv
)
import System.FilePath
( (</>)
)
import System.Process
( StdStream (..)
, cleanupProcess
)
import Test.Hspec
( Spec
, describe
Expand All @@ -53,6 +85,9 @@ import UnliftIO.Async
import UnliftIO.Concurrent
( threadDelay
)
import UnliftIO.Directory
( createDirectoryIfMissing
)

testMonitoring
:: MonitorState
Expand All @@ -61,11 +96,72 @@ testMonitoring
testMonitoring w f =
evalContT $ do
(tracer, (query, _)) <-
withMonitoring SMainnet (error "No connection")
(error "No cluster") nullTracer
withMonitoring
SMainnet
(error "No connection")
(error "No cluster")
nullTracer
$ MonitorConfiguration Nothing w
liftIO $ f tracer query

localClusterCommand
:: FilePath -- ^ filename to append to the logs dir
-> PortNumber -- ^ monitoring port
-> IO Command
localClusterCommand name port = do
configsPath <- getClusterConfigsPathFromEnv
mLogsPath <- getClusterLogsFilePathFromEnv
mMinSeverity <- getClusterLogsMinSeverity
pure
$ Command
{ cmdName = "local-cluster"
, cmdArgs =
[ "--monitoring-port"
, show port
, "--cluster-configs"
, configsPath
]
<> case mLogsPath of
Nothing -> []
Just logsPath -> ["--cluster-logs", logsPath </> name]
<> case mMinSeverity of
Nothing -> []
Just minSeverity -> ["--min-severity", show minSeverity]
, cmdSetup = pure ()
, cmdInput = NoStream
, cmdOutput = NoStream
}

getClusterConfigsPathFromEnv :: IO FilePath
getClusterConfigsPathFromEnv = do
lookupEnv "LOCAL_CLUSTER_CONFIGS" >>= \case
Just path -> pure path
Nothing -> error "LOCAL_CLUSTER_CONFIGS not set"

getClusterLogsFilePathFromEnv :: IO (Maybe FilePath)
getClusterLogsFilePathFromEnv = do
mp <- lookupEnv "CLUSTER_LOGS_DIR_PATH"
forM_ mp $ \dir ->
createDirectoryIfMissing True dir
pure mp

getClusterLogsMinSeverity :: IO (Maybe String)
getClusterLogsMinSeverity = lookupEnv "CLUSTER_LOGS_MIN_SEVERITY"

testMonitoringWithCluster
:: FilePath
-> ((RunQuery IO, RunApplicationQ IO) -> IO ())
-> IO ()
testMonitoringWithCluster name = runContT $ do
port <- liftIO getRandomPort
command <- liftIO $ localClusterCommand name port
ProcessHandles in' out err kill <- do
ContT $ withBackendProcess nullTracer command
queries <- withSNetworkId (NTestnet 42)
$ \network -> withMonitoringClient network port nullTracer
ContT $ \k -> do
k queries `finally` cleanupProcess (in', out, err, kill)

spec :: Spec
spec = do
describe "withMonitoring control" $ do
Expand Down Expand Up @@ -123,3 +219,17 @@ spec = do
wait tracer'
(History phases, _state) <- query ObserveQ
snd <$> phases `shouldBe` [RetrievingFunds]
describe "withMonitoring application" $ do
it "can start and stop" $ do
testMonitoringWithCluster
"can-start-and-stop.log"
$ \(RunQuery query, _) -> do
result <- query ReadyQ
result `shouldBe` False
it "can wait for cluster ready before ending" $ do
testMonitoringWithCluster
"can-wait-for-cluster-ready-before-ending.log"
$ \(RunQuery query, _) -> do
fix $ \loop -> do
result <- query ReadyQ
unless result $ threadDelay 10000 >> loop

0 comments on commit eb51c75

Please sign in to comment.