From 007a2b582afd9e95e5bae0129b8f9ad75787c39b Mon Sep 17 00:00:00 2001 From: Pablo Lamela Date: Thu, 12 Sep 2024 22:08:25 +0200 Subject: [PATCH] Add test for `hash anchor-data` with IPFS url --- .../Test/Cardano/CLI/Util.hs | 16 ++++++- .../test/cardano-cli-test/Test/Cli/Hash.hs | 44 ++++++++++++++++++- 2 files changed, 58 insertions(+), 2 deletions(-) diff --git a/cardano-cli/test/cardano-cli-test-lib/Test/Cardano/CLI/Util.hs b/cardano-cli/test/cardano-cli-test-lib/Test/Cardano/CLI/Util.hs index d73125d966..d1d4182f50 100644 --- a/cardano-cli/test/cardano-cli-test-lib/Test/Cardano/CLI/Util.hs +++ b/cardano-cli/test/cardano-cli-test-lib/Test/Cardano/CLI/Util.hs @@ -11,6 +11,7 @@ module Test.Cardano.CLI.Util , equivalence , execCardanoCLI , execDetailCardanoCLI + , execDetailCfgCardanoCLI , tryExecCardanoCLI , propertyOnce , withSnd @@ -82,7 +83,20 @@ execDetailCardanoCLI -- ^ Arguments to the CLI command -> m (IO.ExitCode, String, String) -- ^ Captured stdout -execDetailCardanoCLI = GHC.withFrozenCallStack $ execDetailFlex H.defaultExecConfig "cardano-cli" "CARDANO_CLI" +execDetailCardanoCLI params = GHC.withFrozenCallStack $ execDetailCfgCardanoCLI H.defaultExecConfig params + +-- | Execute cardano-cli via the command line, expecting it to fail, and accepting custom config. +-- +-- Waits for the process to finish and returns the exit code, stdout and stderr. +execDetailCfgCardanoCLI + :: (MonadTest m, MonadCatch m, MonadIO m, HasCallStack) + => ExecConfig + -- ^ Configuration for the execution + -> [String] + -- ^ Arguments to the CLI command + -> m (IO.ExitCode, String, String) + -- ^ Captured stdout +execDetailCfgCardanoCLI cfg = GHC.withFrozenCallStack $ execDetailFlex cfg "cardano-cli" "CARDANO_CLI" procFlex' :: (MonadTest m, MonadCatch m, MonadIO m, HasCallStack) diff --git a/cardano-cli/test/cardano-cli-test/Test/Cli/Hash.hs b/cardano-cli/test/cardano-cli-test/Test/Cli/Hash.hs index b769f2550a..7f80feb3c0 100644 --- a/cardano-cli/test/cardano-cli-test/Test/Cli/Hash.hs +++ b/cardano-cli/test/cardano-cli-test/Test/Cli/Hash.hs @@ -9,14 +9,16 @@ import Control.Exception.Lifted (bracket) import Control.Monad (void) import Control.Monad.Trans.Control (MonadBaseControl) import Data.List (intercalate) +import Data.Monoid (Last (..)) import qualified Data.Text as T -import GHC.IO.Exception (ExitCode (ExitFailure)) +import GHC.IO.Exception (ExitCode (..)) import Network.HTTP.Types.Status (status200, status404) import Network.Socket (close) import Network.Wai (Request, Response, ResponseReceived, pathInfo, responseFile, responseLBS) import Network.Wai.Handler.Warp (defaultSettings, openFreePort, runSettingsSocket) import System.Directory (getCurrentDirectory) +import System.Environment (getEnvironment) import System.FilePath (dropTrailingPathSeparator) import System.FilePath.Posix (splitDirectories) @@ -32,6 +34,9 @@ exampleAnchorDataHash = "de38a4f5b8b9d8372386cc923bad19d1a0662298cf355bbe947e5ee exampleAnchorDataPath :: String exampleAnchorDataPath = "test/cardano-cli-test/files/input/example_anchor_data.txt" +exampleAchorDataIpfsHash :: String +exampleAchorDataIpfsHash = "QmbL5EBFJLf8DdPkWAskG3Euin9tHY8naqQ2JDoHnWHHXJ" + -- | Execute me with: -- @cabal test cardano-cli-test --test-options '-p "/generate anchor data hash from file/"'@ hprop_generate_anchor_data_hash_from_file :: Property @@ -129,6 +134,43 @@ hprop_check_anchor_data_hash_from_http_uri = ] ) +-- | Execute me with: +-- @cabal test cardano-cli-test --test-options '-p "/check anchor data hash from ipfs uri/"'@ +hprop_check_anchor_data_hash_from_ipfs_uri :: Property +hprop_check_anchor_data_hash_from_ipfs_uri = + propertyOnce $ do + let relativeUrl = ["ipfs", exampleAchorDataIpfsHash] + serveFileWhile + relativeUrl + exampleAnchorDataPath + ( \port -> do + env <- H.evalIO getEnvironment + result <- + execDetailCfgCardanoCLI + H.defaultExecConfig + { H.execConfigEnv = + Last $ + Just + ( ( "IPFS_GATEWAY_URI" + , "http://localhost:" ++ show port ++ "/" + ) + : env + ) + } + [ "hash" + , "anchor-data" + , "--url" + , "ipfs://" ++ exampleAchorDataIpfsHash + , "--expected-hash" + , exampleAnchorDataHash + ] + case result of + (ExitFailure _, _, stderr) -> do + H.note_ stderr + failure + (ExitSuccess, _, _) -> success + ) + -- | Takes a relative url (as a list of segments), a file path, and an action, and it serves -- the file in the url provided in a random free port that is passed as a parameter to the -- action. After the action returns, it shuts down the server. It returns the result of the