From e2aea1662e2e554a971c22e4fe86c6580c140548 Mon Sep 17 00:00:00 2001 From: Julian Ospald Date: Fri, 13 Nov 2020 17:42:07 +0100 Subject: [PATCH] Add SMASH integration test --- .../Scenario/API/Shelley/StakePools.hs | 82 ++++++++++++------- lib/shelley/test/data/smash/api/v1/delisted | 1 + ...ac21162d4dabd6ab6015bd066be81af3a56fd14dfb | 1 + ...7c9bbd1719459dac0bd0a2f085e673611ebb9a5965 | 1 + ...d72d2160aa294d81a4494f99353c6bbb1207468089 | 1 + ...26ecdbbd470f04e9a0415e1e7e6b9f1d54cd353126 | 1 + lib/shelley/test/integration/Main.hs | 20 ++++- lib/test-utils/src/Test/Utils/StaticServer.hs | 4 +- 8 files changed, 79 insertions(+), 32 deletions(-) create mode 100644 lib/shelley/test/data/smash/api/v1/delisted create mode 100644 lib/shelley/test/data/smash/api/v1/metadata/1b3dc19c6ab89eaffc8501f375bb03c11bf8ed5d183736b1d80413d6/069e6a4979389b857e32b9ac21162d4dabd6ab6015bd066be81af3a56fd14dfb create mode 100644 lib/shelley/test/data/smash/api/v1/metadata/b45768c1a2da4bd13ebcaa1ea51408eda31dcc21765ccbd407cda9f2/8241de08075886a7d09c847c9bbd1719459dac0bd0a2f085e673611ebb9a5965 create mode 100644 lib/shelley/test/data/smash/api/v1/metadata/bb114cb37d75fa05260328c235a3dae295a33d0ba674a5eb1e3e568e/f1941b06d889a1a9bd8a7dd72d2160aa294d81a4494f99353c6bbb1207468089 create mode 100644 lib/shelley/test/data/smash/api/v1/metadata/ec28f33dcbe6d6400a1e5e339bd0647c0973ca6c0cf9c2bbe6838dc6/af7789093aa1fbff0f6e7026ecdbbd470f04e9a0415e1e7e6b9f1d54cd353126 diff --git a/lib/core-integration/src/Test/Integration/Scenario/API/Shelley/StakePools.hs b/lib/core-integration/src/Test/Integration/Scenario/API/Shelley/StakePools.hs index 9223ac1a991..562daedf8ee 100644 --- a/lib/core-integration/src/Test/Integration/Scenario/API/Shelley/StakePools.hs +++ b/lib/core-integration/src/Test/Integration/Scenario/API/Shelley/StakePools.hs @@ -17,7 +17,8 @@ import Prelude import Cardano.Wallet.Api.Types ( ApiCertificate (JoinPool, QuitPool, RegisterRewardAccount) - , ApiStakePool + , ApiStakePool (flags) + , ApiStakePoolFlag (..) , ApiT (..) , ApiTransaction , ApiWallet @@ -76,6 +77,8 @@ import Data.Text.Class ( showT, toText ) import Numeric.Natural ( Natural ) +import System.Environment + ( getEnv ) import Test.Hspec ( SpecWith, describe, pendingWith ) import Test.Hspec.Expectations.Lifted @@ -137,6 +140,7 @@ import Test.Integration.Framework.TestData import qualified Cardano.Wallet.Api.Link as Link import qualified Data.ByteString as BS import qualified Data.Set as Set +import qualified Data.Text as T import qualified Network.HTTP.Types.Status as HTTP spec :: forall n t. @@ -966,33 +970,6 @@ spec = describe "SHELLEY_STAKE_POOLS" $ do updateMetadataSource ctx "direct" eventually "metadata is fetched" $ do r <- listPools ctx arbitraryStake - let metadataPossible = Set.fromList - [ StakePoolMetadata - { ticker = (StakePoolTicker "GPA") - , name = "Genesis Pool A" - , description = Nothing - , homepage = "https://iohk.io" - } - , StakePoolMetadata - { ticker = (StakePoolTicker "GPB") - , name = "Genesis Pool B" - , description = Nothing - , homepage = "https://iohk.io" - } - , StakePoolMetadata - { ticker = (StakePoolTicker "GPC") - , name = "Genesis Pool C" - , description = Just "Lorem Ipsum Dolor Sit Amet." - , homepage = "https://iohk.io" - } - , StakePoolMetadata - { ticker = (StakePoolTicker "GPD") - , name = "Genesis Pool D" - , description = Just "Lorem Ipsum Dolor Sit Amet." - , homepage = "https://iohk.io" - } - ] - verify r [ expectListSize 3 , expectField Prelude.id $ \pools' -> do @@ -1120,7 +1097,56 @@ spec = describe "SHELLEY_STAKE_POOLS" $ do let epochs = poolGarbageCollectionEpochNo <$> events (reverse epochs `zip` [1 ..]) `shouldSatisfy` all (uncurry (==)) + it "STAKE_POOLS_SMASH_01 - fetching metadata from SMASH works with delisted pools" $ + \ctx -> runResourceT $ do + smashUrl <- liftIO $ getEnv "CARDANO_WALLET_SMASH_URL" + updateMetadataSource ctx (T.pack smashUrl) + eventually "metadata is fetched" $ do + r <- listPools ctx arbitraryStake + verify r + [ expectListSize 3 + , expectField Prelude.id $ \pools' -> do + let metadataActual = Set.fromList $ + mapMaybe (fmap getApiT . view #metadata) pools' + delistedPools = filter (\pool -> Delisted `elem` flags pool) + pools' + metadataActual + `shouldSatisfy` (`Set.isSubsetOf` metadataPossible) + metadataActual + `shouldSatisfy` (not . Set.null) + (fmap (getApiT . view #id) delistedPools) + `shouldBe` [PoolId . unsafeFromHex $ + "b45768c1a2da4bd13ebcaa1ea51408eda31dcc21765ccbd407cda9f2"] + ] + where + metadataPossible = Set.fromList + [ StakePoolMetadata + { ticker = (StakePoolTicker "GPA") + , name = "Genesis Pool A" + , description = Nothing + , homepage = "https://iohk.io" + } + , StakePoolMetadata + { ticker = (StakePoolTicker "GPB") + , name = "Genesis Pool B" + , description = Nothing + , homepage = "https://iohk.io" + } + , StakePoolMetadata + { ticker = (StakePoolTicker "GPC") + , name = "Genesis Pool C" + , description = Just "Lorem Ipsum Dolor Sit Amet." + , homepage = "https://iohk.io" + } + , StakePoolMetadata + { ticker = (StakePoolTicker "GPD") + , name = "Genesis Pool D" + , description = Just "Lorem Ipsum Dolor Sit Amet." + , homepage = "https://iohk.io" + } + ] + arbitraryStake :: Maybe Coin arbitraryStake = Just $ ada 10_000_000_000 where ada = Coin . (1000*1000*) diff --git a/lib/shelley/test/data/smash/api/v1/delisted b/lib/shelley/test/data/smash/api/v1/delisted new file mode 100644 index 00000000000..a46ef751975 --- /dev/null +++ b/lib/shelley/test/data/smash/api/v1/delisted @@ -0,0 +1 @@ +[{"poolId":"b45768c1a2da4bd13ebcaa1ea51408eda31dcc21765ccbd407cda9f2"}] diff --git a/lib/shelley/test/data/smash/api/v1/metadata/1b3dc19c6ab89eaffc8501f375bb03c11bf8ed5d183736b1d80413d6/069e6a4979389b857e32b9ac21162d4dabd6ab6015bd066be81af3a56fd14dfb b/lib/shelley/test/data/smash/api/v1/metadata/1b3dc19c6ab89eaffc8501f375bb03c11bf8ed5d183736b1d80413d6/069e6a4979389b857e32b9ac21162d4dabd6ab6015bd066be81af3a56fd14dfb new file mode 100644 index 00000000000..3f8e7e0e94e --- /dev/null +++ b/lib/shelley/test/data/smash/api/v1/metadata/1b3dc19c6ab89eaffc8501f375bb03c11bf8ed5d183736b1d80413d6/069e6a4979389b857e32b9ac21162d4dabd6ab6015bd066be81af3a56fd14dfb @@ -0,0 +1 @@ +{"homepage":"https://iohk.io","name":"Genesis Pool B","ticker":"GPB","description":null} \ No newline at end of file diff --git a/lib/shelley/test/data/smash/api/v1/metadata/b45768c1a2da4bd13ebcaa1ea51408eda31dcc21765ccbd407cda9f2/8241de08075886a7d09c847c9bbd1719459dac0bd0a2f085e673611ebb9a5965 b/lib/shelley/test/data/smash/api/v1/metadata/b45768c1a2da4bd13ebcaa1ea51408eda31dcc21765ccbd407cda9f2/8241de08075886a7d09c847c9bbd1719459dac0bd0a2f085e673611ebb9a5965 new file mode 100644 index 00000000000..ec115e94d02 --- /dev/null +++ b/lib/shelley/test/data/smash/api/v1/metadata/b45768c1a2da4bd13ebcaa1ea51408eda31dcc21765ccbd407cda9f2/8241de08075886a7d09c847c9bbd1719459dac0bd0a2f085e673611ebb9a5965 @@ -0,0 +1 @@ +{"homepage":"https://iohk.io","name":"Genesis Pool C","ticker":"GPC","description":"Lorem Ipsum Dolor Sit Amet."} \ No newline at end of file diff --git a/lib/shelley/test/data/smash/api/v1/metadata/bb114cb37d75fa05260328c235a3dae295a33d0ba674a5eb1e3e568e/f1941b06d889a1a9bd8a7dd72d2160aa294d81a4494f99353c6bbb1207468089 b/lib/shelley/test/data/smash/api/v1/metadata/bb114cb37d75fa05260328c235a3dae295a33d0ba674a5eb1e3e568e/f1941b06d889a1a9bd8a7dd72d2160aa294d81a4494f99353c6bbb1207468089 new file mode 100644 index 00000000000..7db9d475ba0 --- /dev/null +++ b/lib/shelley/test/data/smash/api/v1/metadata/bb114cb37d75fa05260328c235a3dae295a33d0ba674a5eb1e3e568e/f1941b06d889a1a9bd8a7dd72d2160aa294d81a4494f99353c6bbb1207468089 @@ -0,0 +1 @@ +{"homepage":"https://iohk.io","name":"Genesis Pool D","ticker":"GPD","description":"Lorem Ipsum Dolor Sit Amet."} \ No newline at end of file diff --git a/lib/shelley/test/data/smash/api/v1/metadata/ec28f33dcbe6d6400a1e5e339bd0647c0973ca6c0cf9c2bbe6838dc6/af7789093aa1fbff0f6e7026ecdbbd470f04e9a0415e1e7e6b9f1d54cd353126 b/lib/shelley/test/data/smash/api/v1/metadata/ec28f33dcbe6d6400a1e5e339bd0647c0973ca6c0cf9c2bbe6838dc6/af7789093aa1fbff0f6e7026ecdbbd470f04e9a0415e1e7e6b9f1d54cd353126 new file mode 100644 index 00000000000..34b11e016bf --- /dev/null +++ b/lib/shelley/test/data/smash/api/v1/metadata/ec28f33dcbe6d6400a1e5e339bd0647c0973ca6c0cf9c2bbe6838dc6/af7789093aa1fbff0f6e7026ecdbbd470f04e9a0415e1e7e6b9f1d54cd353126 @@ -0,0 +1 @@ +{"homepage":"https://iohk.io","name":"Genesis Pool A","ticker":"GPA","description":null} \ No newline at end of file diff --git a/lib/shelley/test/integration/Main.hs b/lib/shelley/test/integration/Main.hs index 143eef41070..49198f05efa 100644 --- a/lib/shelley/test/integration/Main.hs +++ b/lib/shelley/test/integration/Main.hs @@ -3,6 +3,7 @@ {-# LANGUAGE LambdaCase #-} {-# LANGUAGE RecordWildCards #-} {-# LANGUAGE ScopedTypeVariables #-} +{-# LANGUAGE TemplateHaskell #-} {-# LANGUAGE TupleSections #-} {-# LANGUAGE TypeApplications #-} {-# LANGUAGE TypeFamilies #-} @@ -92,6 +93,8 @@ import Network.HTTP.Client , newManager , responseTimeoutMicro ) +import System.Environment + ( setEnv ) import System.FilePath ( () ) import System.IO @@ -107,7 +110,9 @@ import Test.Integration.Framework.Context import Test.Integration.Framework.DSL ( KnownCommand (..) ) import Test.Utils.Paths - ( inNixBuild ) + ( getTestData, inNixBuild ) +import Test.Utils.StaticServer + ( withStaticServer ) import qualified Cardano.Pool.DB as Pool import qualified Cardano.Pool.DB.Sqlite as Pool @@ -243,7 +248,7 @@ specWithServer (tr, tracers) = aroundAll withContext atomicModifyIORef' eventsRef ((, ()) . (event :)) pure certificates - withServer dbDecorator action = bracketTracer' tr "withServer" $ do + withServer dbDecorator action = bracketTracer' tr "withServer" $ withSMASH $ do minSev <- nodeMinSeverityFromEnv testPoolConfigs <- poolConfigsFromEnv withSystemTempDir tr' "test" $ \dir -> do @@ -353,3 +358,14 @@ withTracers action = do bracketTracer' :: Tracer IO TestsLog -> Text -> IO a -> IO a bracketTracer' tr name = bracketTracer (contramap (MsgBracket name) tr) + +-- | Run a SMASH stub server, serving some delisted pool IDs. +withSMASH :: IO a -> IO a +withSMASH action = withStaticServer root $ \baseUrl -> do + setEnv envVar baseUrl + action + where + root = $(getTestData) "smash" + + envVar :: String + envVar = "CARDANO_WALLET_SMASH_URL" diff --git a/lib/test-utils/src/Test/Utils/StaticServer.hs b/lib/test-utils/src/Test/Utils/StaticServer.hs index 7151db30ba6..1ffc57e6507 100644 --- a/lib/test-utils/src/Test/Utils/StaticServer.hs +++ b/lib/test-utils/src/Test/Utils/StaticServer.hs @@ -12,7 +12,7 @@ module Test.Utils.StaticServer import Prelude import Network.Wai.Application.Static - ( defaultWebAppSettings, staticApp ) + ( defaultFileServerSettings, staticApp ) import Network.Wai.Handler.Warp ( withApplication ) @@ -27,5 +27,5 @@ withStaticServer withStaticServer root action = withApplication (pure app) $ \port -> action (baseUrl port) where - app = staticApp $ defaultWebAppSettings root + app = staticApp $ defaultFileServerSettings root baseUrl port = "http://localhost:" <> show port <> "/"