Skip to content

Commit

Permalink
Add test for hash anchor-data with IPFS url
Browse files Browse the repository at this point in the history
  • Loading branch information
palas committed Sep 13, 2024
1 parent a9a0152 commit 007a2b5
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 2 deletions.
16 changes: 15 additions & 1 deletion cardano-cli/test/cardano-cli-test-lib/Test/Cardano/CLI/Util.hs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ module Test.Cardano.CLI.Util
, equivalence
, execCardanoCLI
, execDetailCardanoCLI
, execDetailCfgCardanoCLI
, tryExecCardanoCLI
, propertyOnce
, withSnd
Expand Down Expand Up @@ -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)
Expand Down
44 changes: 43 additions & 1 deletion cardano-cli/test/cardano-cli-test/Test/Cli/Hash.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 007a2b5

Please sign in to comment.