Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move common testnet functions to test/Helpers.hs for reuse
- Loading branch information
Showing
3 changed files
with
92 additions
and
74 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
{-# LANGUAGE LambdaCase #-} | ||
{-# LANGUAGE NamedFieldPuns #-} | ||
|
||
module Helpers where | ||
|
||
import Control.Monad (when) | ||
import Control.Monad.IO.Class (MonadIO, liftIO) | ||
import Hedgehog (MonadTest, assert) | ||
import System.FilePath ((</>)) | ||
|
||
import Cardano.Api qualified as C | ||
import Data.Set qualified as Set | ||
import GHC.Stack qualified as GHC | ||
import Hedgehog qualified as H | ||
import Hedgehog.Extras.Stock.IO.Network.Sprocket qualified as IO | ||
import Hedgehog.Extras.Test qualified as HE | ||
import Hedgehog.Extras.Test.Base qualified as H | ||
import Ouroboros.Network.Protocol.LocalTxSubmission.Type (SubmitResult (SubmitFail, SubmitSuccess)) | ||
import System.Directory qualified as IO | ||
import System.Environment qualified as IO | ||
import System.IO.Temp qualified as IO | ||
import System.Info qualified as IO | ||
import Testnet.Cardano qualified as TN | ||
import Testnet.Conf qualified as TC (Conf (..), ProjectBase (ProjectBase), YamlFilePath (YamlFilePath), mkConf) | ||
|
||
-- * Helpers | ||
|
||
startTestnet :: FilePath -> FilePath -> H.Integration (String, C.NetworkId, FilePath) | ||
startTestnet base tempAbsBasePath' = do | ||
configurationTemplate <- H.noteShow $ base </> "configuration/defaults/byron-mainnet/configuration.yaml" | ||
conf@TC.Conf { TC.tempBaseAbsPath, TC.tempAbsPath } <- HE.noteShowM $ | ||
TC.mkConf (TC.ProjectBase base) (TC.YamlFilePath configurationTemplate) | ||
(tempAbsBasePath' <> "/") | ||
Nothing | ||
assert $ tempAbsPath == (tempAbsBasePath' <> "/") | ||
&& tempAbsPath == (tempBaseAbsPath <> "/") | ||
tn <- TN.testnet TN.defaultTestnetOptions conf | ||
let networkId = C.Testnet $ C.NetworkMagic $ fromIntegral (TN.testnetMagic tn) | ||
socketPath <- IO.sprocketArgumentName <$> headM (TN.nodeSprocket <$> TN.bftNodes tn) | ||
socketPathAbs <- H.note =<< (liftIO $ IO.canonicalizePath $ tempAbsPath </> socketPath) | ||
pure (socketPathAbs, networkId, tempAbsPath) | ||
|
||
readAs :: (C.HasTextEnvelope a, MonadIO m, MonadTest m) => C.AsType a -> FilePath -> m a | ||
readAs as path = H.leftFailM . liftIO $ C.readFileTextEnvelope as path | ||
|
||
findUTxOByAddress | ||
:: (MonadIO m, MonadTest m) | ||
=> C.LocalNodeConnectInfo C.CardanoMode -> C.AddressAny -> m (C.UTxO C.AlonzoEra) | ||
findUTxOByAddress localNodeConnectInfo address = let | ||
query = C.QueryInShelleyBasedEra C.ShelleyBasedEraAlonzo $ C.QueryUTxO $ | ||
C.QueryUTxOByAddress $ Set.singleton address | ||
in | ||
H.leftFailM . H.leftFailM . liftIO $ C.queryNodeLocalState localNodeConnectInfo Nothing $ | ||
C.QueryInEra C.AlonzoEraInCardanoMode query | ||
|
||
submitTx :: (MonadIO m, MonadTest m) => C.LocalNodeConnectInfo C.CardanoMode -> C.Tx C.AlonzoEra -> m () | ||
submitTx localNodeConnectInfo tx = do | ||
submitResult :: SubmitResult (C.TxValidationErrorInMode C.CardanoMode) <- | ||
liftIO $ C.submitTxToNodeLocal localNodeConnectInfo $ C.TxInMode tx C.AlonzoEraInCardanoMode | ||
failOnTxSubmitFail submitResult | ||
where | ||
failOnTxSubmitFail :: (Show a, MonadTest m) => SubmitResult a -> m () | ||
failOnTxSubmitFail = \case | ||
SubmitFail reason -> H.failMessage GHC.callStack $ "Transaction failed: " <> show reason | ||
SubmitSuccess -> pure () | ||
|
||
-- TODO: remove when this is exported from hedgehog-extras/src/Hedgehog/Extras/Test/Base.hs | ||
headM :: (MonadTest m, GHC.HasCallStack) => [a] -> m a | ||
headM (a:_) = return a | ||
headM [] = GHC.withFrozenCallStack $ H.failMessage GHC.callStack "Cannot take head of empty list" | ||
|
||
workspace :: (MonadTest m, MonadIO m, GHC.HasCallStack) => FilePath -> (FilePath -> m ()) -> m () | ||
workspace prefixPath f = GHC.withFrozenCallStack $ do | ||
systemTemp <- case IO.os of | ||
"darwin" -> pure "/tmp" | ||
_ -> H.evalIO IO.getCanonicalTemporaryDirectory | ||
maybeKeepWorkspace <- H.evalIO $ IO.lookupEnv "KEEP_WORKSPACE" | ||
let systemPrefixPath = systemTemp <> "/" <> prefixPath | ||
H.evalIO $ IO.createDirectoryIfMissing True systemPrefixPath | ||
ws <- H.evalIO $ IO.createTempDirectory systemPrefixPath "test" | ||
H.annotate $ "Workspace: " <> ws | ||
-- liftIO $ IO.writeFile (ws <> "/module") callerModuleName | ||
f ws | ||
when (IO.os /= "mingw32" && maybeKeepWorkspace /= Just "1") $ do | ||
H.evalIO $ IO.removeDirectoryRecursive ws |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters