Skip to content

Commit

Permalink
Add SMASH integration test
Browse files Browse the repository at this point in the history
  • Loading branch information
Julian Ospald committed Nov 13, 2020
1 parent 0fb5139 commit e2aea16
Show file tree
Hide file tree
Showing 8 changed files with 79 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ import Prelude

import Cardano.Wallet.Api.Types
( ApiCertificate (JoinPool, QuitPool, RegisterRewardAccount)
, ApiStakePool
, ApiStakePool (flags)
, ApiStakePoolFlag (..)
, ApiT (..)
, ApiTransaction
, ApiWallet
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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*)
Expand Down
1 change: 1 addition & 0 deletions lib/shelley/test/data/smash/api/v1/delisted
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[{"poolId":"b45768c1a2da4bd13ebcaa1ea51408eda31dcc21765ccbd407cda9f2"}]
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"homepage":"https://iohk.io","name":"Genesis Pool B","ticker":"GPB","description":null}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"homepage":"https://iohk.io","name":"Genesis Pool C","ticker":"GPC","description":"Lorem Ipsum Dolor Sit Amet."}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"homepage":"https://iohk.io","name":"Genesis Pool D","ticker":"GPD","description":"Lorem Ipsum Dolor Sit Amet."}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"homepage":"https://iohk.io","name":"Genesis Pool A","ticker":"GPA","description":null}
20 changes: 18 additions & 2 deletions lib/shelley/test/integration/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE RecordWildCards #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TemplateHaskell #-}
{-# LANGUAGE TupleSections #-}
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE TypeFamilies #-}
Expand Down Expand Up @@ -92,6 +93,8 @@ import Network.HTTP.Client
, newManager
, responseTimeoutMicro
)
import System.Environment
( setEnv )
import System.FilePath
( (</>) )
import System.IO
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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"
4 changes: 2 additions & 2 deletions lib/test-utils/src/Test/Utils/StaticServer.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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 )

Expand All @@ -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 <> "/"

0 comments on commit e2aea16

Please sign in to comment.