Skip to content

Commit

Permalink
[ADP-3305] Add send faucet assets tests to local cluster (#4593)
Browse files Browse the repository at this point in the history
- [x] Add a test that proves it's possible to send assets via
local-cluster API
  • Loading branch information
paolino authored May 29, 2024
2 parents 0473f3b + 72c09a0 commit caee995
Show file tree
Hide file tree
Showing 13 changed files with 726 additions and 76 deletions.
12 changes: 10 additions & 2 deletions .buildkite/pipeline.yml
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,11 @@ steps:
- label: "Babbage integration tests (linux)"
key: linux-tests-integration-babbage
depends_on: linux-nix
command: nix shell 'nixpkgs#just' -c just babbage-integration-tests
command: |
mkdir integration-test-dir
export INTEGRATION_TEST_DIR=integration-test-dir
nix shell 'nixpkgs#just' -c just babbage-integration-tests
artifact_paths: [ "./integration-test-dir/**" ]
agents:
system: ${linux}
env:
Expand All @@ -80,7 +84,11 @@ steps:
- label: "Conway integration tests (linux)"
key: linux-tests-integration-conway
depends_on: linux-nix
command: nix shell 'nixpkgs#just' -c just conway-integration-tests
command: |
mkdir integration-test-dir
export INTEGRATION_TEST_DIR=integration-test-dir
nix shell 'nixpkgs#just' -c just conway-integration-tests
artifact_paths: [ "./integration-test-dir/**" ]
agents:
system: ${linux}
env:
Expand Down
31 changes: 20 additions & 11 deletions lib/integration/framework/Test/Integration/Framework/Setup.hs
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ import Cardano.Wallet.Launch.Cluster.Env
import Cardano.Wallet.Launch.Cluster.FileOf
( DirOf (..)
, FileOf (..)
, absolutize
, mkRelDirOf
, toFilePath
)
Expand Down Expand Up @@ -176,7 +177,8 @@ import System.Directory
( createDirectory
)
import System.Environment
( setEnv
( lookupEnv
, setEnv
)
import System.Environment.Extended
( envFromText
Expand All @@ -193,8 +195,8 @@ import System.IO.Temp.Extra
, withSystemTempDir
)
import System.Path
( absDir
, absFile
( absFile
, absRel
, relDir
, relFile
, (</>)
Expand Down Expand Up @@ -241,7 +243,9 @@ import qualified Data.Text as T

-- | Do all the program setup required for integration tests, create a temporary
-- directory, and pass this info to the main hspec action.
withTestsSetup :: (DirOf "cluster" -> (Tracer IO TestsLog, Tracers IO) -> IO a) -> IO a
withTestsSetup
:: (DirOf "cluster" -> (Tracer IO TestsLog, Tracers IO) -> IO a)
-> IO a
withTestsSetup action = do
-- Handle SIGTERM properly
installSignalHandlersNoLogging
Expand All @@ -255,13 +259,18 @@ withTestsSetup action = do
-- Flush test output as soon as a line is printed.
-- Set UTF-8, regardless of user locale.
withUtf8
$
-- This temporary directory will contain logs, and all other data
-- produced by the integration tests.
withSystemTempDir stdoutTextTracer "test" skipCleanup
$ \testDir -> do
let clusterDir = DirOf $ absDir testDir
withTracers clusterDir $ action clusterDir
$ do
mEnv <- lookupEnv "INTEGRATION_TEST_DIR"
-- This temporary directory will contain logs, and all other data
-- produced by the integration tests.
let run fp = do
fpa <- absolutize $ absRel fp
let testDir = DirOf fpa
withTracers testDir $ action testDir
case mEnv of
Just env -> run env
Nothing -> withSystemTempDir
stdoutTextTracer "test" skipCleanup run

mkFaucetFunds :: TestnetMagic -> FaucetM FaucetFunds
mkFaucetFunds testnetMagic = do
Expand Down
1 change: 1 addition & 0 deletions lib/iohk-monitoring-extra/src/Cardano/BM/ToTextTracer.hs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ newToTextTracer
newToTextTracer clusterLogsFile minSeverity = runContT $ do
ch <- newTChanIO
h <- ContT $ withFile clusterLogsFile WriteMode
hSetBuffering h NoBuffering
liftIO $ hSetBuffering h NoBuffering
liftIO $ async >=> link $ forever $ do
(x, s, t) <- atomically $ readTChan ch
Expand Down
2 changes: 1 addition & 1 deletion lib/launcher/src/Cardano/Launcher.hs
Original file line number Diff line number Diff line change
Expand Up @@ -407,7 +407,7 @@ instance HasPrivacyAnnotation WaitForProcessLog
instance HasSeverityAnnotation WaitForProcessLog where
getSeverityAnnotation = \case
MsgWaitBefore -> Debug
MsgWaitAfter _ -> Debug
MsgWaitAfter _ -> Warning
MsgWaitCancelled -> Debug

instance ToText ProcessHasExited where
Expand Down
10 changes: 7 additions & 3 deletions lib/local-cluster/exe/local-cluster.hs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import Cardano.Startup
)
import Cardano.Wallet.Launch.Cluster
( Config (..)
, runningNodeSocketPath
)
import Cardano.Wallet.Launch.Cluster.CommandLine
( CommandLineOptions (..)
Expand All @@ -32,7 +33,8 @@ import Cardano.Wallet.Launch.Cluster.FileOf
, toFilePath
)
import Cardano.Wallet.Launch.Cluster.Http.Faucet.Server
( newNodeConnVar
( NodeConnVar (setNodeConn)
, newNodeConnVar
)
import Cardano.Wallet.Launch.Cluster.Http.Service
( withServiceServer
Expand Down Expand Up @@ -283,15 +285,16 @@ main = withUtf8 $ do
}

debug "Starting the monitoring server"
(_, phaseTracer) <- withSNetworkId (NTestnet 42)
(nodeConn, phaseTracer) <- withSNetworkId (NTestnet 42)
$ \network -> do
nodeConn <- liftIO newNodeConnVar
withServiceServer
(_ , phaseTracer) <- withServiceServer
network
nodeConn
clusterCfg
tracer
httpService
pure (nodeConn, phaseTracer)

debug "Starting the faucet"

Expand All @@ -301,6 +304,7 @@ main = withUtf8 $ do
debug "Starting the cluster"
node <- ContT $ Cluster.withCluster clusterCfg faucetFunds

liftIO $ setNodeConn nodeConn $ runningNodeSocketPath node
debug "Starting the relay node"
nodeSocket <-
case parse . nodeSocketFile
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ import Cardano.Wallet.Launch.Cluster.Http.Faucet.SendFaucetAssets
( SendFaucetAssets
, WithNetwork (..)
)
import Cardano.Wallet.Launch.Cluster.Http.Monitor.Client
( recovering
)
import Cardano.Wallet.Primitive.NetworkId
( HasSNetworkId
, SNetworkId
Expand Down Expand Up @@ -54,6 +57,8 @@ import Servant.Client
)
import UnliftIO
( MonadUnliftIO
, UnliftIO (..)
, askUnliftIO
)

-- | Queries that can be run against the local cluster
Expand All @@ -79,7 +84,7 @@ mkFaucet _ =
}

newtype MsgFaucetClient = MsgFaucetRequest AnyFaucetQ
deriving stock Show
deriving stock (Show)

instance ToText MsgFaucetClient where
toText (MsgFaucetRequest q) = "Faucet request: " <> toText (show q)
Expand All @@ -95,12 +100,18 @@ newFaucetQ
-> Tracer m MsgFaucetClient
-> Faucet n
-> m (RunFaucetQ m)
newFaucetQ query tr Faucet{..} = pure
$ RunFaucetQ
$ \request -> do
traceWith tr (MsgFaucetRequest $ AnyFaucetQ request)
case request of
SendFaucetAssetsQ assets ->
liftIO
$ query
$ sendFaucetAssets (WithNetwork assets) $> ()
newFaucetQ query tr Faucet{..} = do
UnliftIO unlift <- askUnliftIO
pure
$ RunFaucetQ
$ \request -> do
let f =
unlift
. traceWith tr
. MsgFaucetRequest
$ AnyFaucetQ request
liftIO $ recovering f $ case request of
SendFaucetAssetsQ assets ->
liftIO
$ query
$ sendFaucetAssets (WithNetwork assets) $> ()
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ module Cardano.Wallet.Launch.Cluster.Http.Monitor.Client
, AnyMonitorQ (..)
, newRunQuery
, mkMonitorClient
, recovering
)
where

Expand Down Expand Up @@ -129,25 +130,24 @@ newRunQuery query tr MonitorClient{ready, observe, step, switch} =
UnliftIO unlift <- askUnliftIO
pure $ RunQuery $ \request -> do
traceWith tr $ MsgMonitorClientReq $ AnyQuery request
let recovering :: forall a. IO a -> IO a
recovering doing = recoverAll retryPolicy
$ \rt -> do
unless (firstTry rt)
$ unlift
$ traceWith tr . MsgMonitorClientRetry
$ AnyQuery request
doing
liftIO $ recovering $ case request of
let f = unlift
. traceWith tr . MsgMonitorClientRetry
$ AnyQuery request
liftIO $ recovering f $ case request of
ReadyQ -> query ready
ObserveQ -> unApiT <$> query observe
StepQ -> query step $> ()
SwitchQ -> unApiT <$> query switch

retryPolicy :: RetryPolicyM IO
retryPolicy = capDelay (60 * oneSecond) $ exponentialBackoff oneSecond
where
oneSecond = 1_000_000 :: Int

firstTry :: RetryStatus -> Bool
firstTry (RetryStatus 0 _ _) = True
firstTry _ = False
recovering :: IO () -> IO a -> IO a
recovering f doing = recoverAll retryPolicy
$ \rt -> do
unless (firstTry rt) f
doing
where
retryPolicy :: RetryPolicyM IO
retryPolicy = capDelay (60 * oneSecond) $ exponentialBackoff oneSecond
oneSecond = 1_000_000 :: Int
firstTry :: RetryStatus -> Bool
firstTry (RetryStatus 0 _ _) = True
firstTry _ = False
15 changes: 14 additions & 1 deletion lib/local-cluster/local-cluster.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -202,12 +202,21 @@ common test-common
, aeson-qq
, base
, bytestring
, cardano-addresses
, cardano-binary
, cardano-ledger-core
, cardano-ledger-alonzo
, cardano-ledger-babbage
, cardano-ledger-byron
, cardano-ledger-mary
, cardano-ledger-shelley
, cardano-wallet-application-extras
, cardano-wallet-launcher
, cardano-wallet-network-layer
, cardano-wallet-primitive
, cardano-wallet-read
, cardano-wallet-test-utils
, containers
, contra-tracer
, extra
, filepath
Expand All @@ -218,18 +227,19 @@ common test-common
, local-cluster
, mtl
, openapi3
, ouroboros-consensus-cardano
, ouroboros-network
, pathtype
, process
, QuickCheck
, streaming
, time
, unliftio
, with-utf8


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

-- until cabal has no support for multi home, hls requires to have only one home
-- for the other modules , so we cannot use the common test-common for those
Expand All @@ -249,6 +259,9 @@ test-suite test-local-cluster
Paths_local_cluster
Spec
SpecHook
build-tool-depends:
, local-cluster:local-cluster
, cardano-wallet-api:cardano-wallet

executable test-local-cluster-exe
import: test-common
Expand Down
Loading

0 comments on commit caee995

Please sign in to comment.