Skip to content

Commit

Permalink
Merge #962
Browse files Browse the repository at this point in the history
962: Stake pools join quit stub tests r=piotr-iohk a=piotr-iohk

# Issue Number

#895

# Overview

<!-- Detail in a few bullet points the work accomplished in this PR -->

- [ ] Added some preliminary tests for join/quit stake pool endpoints...


# Comments

<!-- Additional comments or screenshots to attach if any -->

<!-- 
Don't forget to:

 ✓ Self-review your changes to make sure nothing unexpected slipped through
 ✓ Assign yourself to the PR
 ✓ Assign one or several reviewer(s)
 ✓ Once created, link this PR to its corresponding ticket
 ✓ Acknowledge any changes required to the Wiki
-->


Co-authored-by: Piotr Stachyra <[email protected]>
  • Loading branch information
iohk-bors[bot] and Piotr Stachyra authored Nov 7, 2019
2 parents b74a169 + a392603 commit 3797c0a
Show file tree
Hide file tree
Showing 7 changed files with 317 additions and 36 deletions.
89 changes: 78 additions & 11 deletions lib/core-integration/src/Test/Integration/Framework/DSL.hs
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,8 @@ module Test.Integration.Framework.DSL
, getFromResponseList
, getJormungandrBlock0H
, json
, joinStakePool
, quitStakePool
, listAddresses
, listTransactions
, listAllTransactions
Expand All @@ -102,6 +104,8 @@ module Test.Integration.Framework.DSL
, prepExternalTxViaJcli
, eventually
, eventuallyUsingDelay
, eventually_
, eventuallyUsingDelay_
, fixturePassphrase

-- * Endpoints
Expand All @@ -119,6 +123,9 @@ module Test.Integration.Framework.DSL
, getWalletUtxoEp
, getAddressesEp
, listStakePoolsEp
, joinStakePoolEp
, quitStakePoolEp
, stakePoolEp
, postTxEp
, postExternalTxEp
, postTxFeeEp
Expand Down Expand Up @@ -155,6 +162,7 @@ import Cardano.Wallet.Api.Types
( AddressAmount
, ApiAddress
, ApiByronWallet
, ApiStakePool
, ApiStakePoolMetrics
, ApiT (..)
, ApiTransaction
Expand Down Expand Up @@ -836,15 +844,12 @@ apparentPerformance =
-- Helpers
--


-- Retry the given action a couple of time until it doesn't throw, or until it
-- has been retried enough.
--
-- It is like @eventuallyUsingDelay@, but with the default delay of 500 ms
-- It is like 'eventuallyUsingDelay', but with the default delay of 500 ms
-- between retries.
eventually
:: IO ()
-> IO ()
eventually :: IO a -> IO a
eventually = eventuallyUsingDelay (500 * ms)
where
ms = 1000
Expand All @@ -855,21 +860,31 @@ eventually = eventuallyUsingDelay (500 * ms)
-- It sleeps for a specified delay between retries.
eventuallyUsingDelay
:: Int -- ^ Delay in microseconds
-> IO ()
-> IO ()
-> IO a
-> IO a
eventuallyUsingDelay delay io = do
winner <- race (threadDelay $ 60 * oneSecond) trial
case winner of
Left _ -> expectationFailure
Left _ -> fail
"waited more than 60s for action to eventually resolve."
Right _ ->
return ()
Right a ->
return a
where
trial :: IO ()
trial = io `catch` \(_ :: SomeException) -> do
threadDelay delay
trial

eventually_ :: IO () -> IO ()
eventually_ = eventuallyUsingDelay_ (500 * ms)
where
ms = 1000

eventuallyUsingDelay_
:: Int -- ^ Delay in microseconds
-> IO ()
-> IO ()
eventuallyUsingDelay_ = eventuallyUsingDelay

utcIso8601ToText :: UTCTime -> Text
utcIso8601ToText = utcTimeToText iso8601ExtendedUtc

Expand Down Expand Up @@ -1058,6 +1073,30 @@ getFromResponseList i getter (_, res) = case res of
json :: QuasiQuoter
json = aesonQQ

joinStakePool
:: forall t w. (HasType (ApiT WalletId) w)
=> Context t
-> ApiStakePool
-> (w, Text)
-> IO (HTTP.Status, Either RequestException (ApiTransaction 'Testnet))
joinStakePool ctx p (w, pass) = do
let payload = Json [aesonQQ| {
"passphrase": #{pass}
} |]
request @(ApiTransaction 'Testnet) ctx (joinStakePoolEp p w) Default payload

quitStakePool
:: forall t w. (HasType (ApiT WalletId) w)
=> Context t
-> ApiStakePool
-> (w, Text)
-> IO (HTTP.Status, Either RequestException (ApiTransaction 'Testnet))
quitStakePool ctx p (w, pass) = do
let payload = Json [aesonQQ| {
"passphrase": #{pass}
} |]
request @(ApiTransaction 'Testnet) ctx (quitStakePoolEp p w) Default payload

listAddresses
:: Context t
-> ApiWallet
Expand Down Expand Up @@ -1234,6 +1273,34 @@ listStakePoolsEp =
, "v2/stake-pools"
)

stakePoolEp
:: forall w. (HasType (ApiT WalletId) w)
=> Method
-> ApiStakePool
-> w
-> (Method, Text)
stakePoolEp verb p w =
( verb
, "v2/stake-pools/"
<> toText (getApiT $ p ^. #id)
<> "/wallets/"
<> w ^. walletId
)

joinStakePoolEp
:: forall w. (HasType (ApiT WalletId) w)
=> ApiStakePool
-> w
-> (Method, Text)
joinStakePoolEp = stakePoolEp "PUT"

quitStakePoolEp
:: forall w. (HasType (ApiT WalletId) w)
=> ApiStakePool
-> w
-> (Method, Text)
quitStakePoolEp = stakePoolEp "DELETE"

postWalletEp :: (Method, Text)
postWalletEp =
( "POST"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ import Test.Integration.Framework.DSL
, emptyByronWalletWith
, emptyWallet
, emptyWalletWith
, eventually
, eventually_
, expectErrorMessage
, expectEventually
, expectFieldEqual
Expand Down Expand Up @@ -252,7 +252,7 @@ spec = do

-- Check that funds become available in the target wallet:
let expectedBalance = originalBalance - expectedFee
eventually $ do
eventually_ $ do
r2 <- request @ApiWallet ctx
(getWalletEp targetWallet) Default Empty
verify r2
Expand Down Expand Up @@ -293,7 +293,7 @@ spec = do
} |]
(_, wOld) <- unsafeRequest @ApiByronWallet ctx
postByronWalletEp payloadRestore
eventually $ do
eventually_ $ do
request @ApiByronWallet ctx
(getByronWalletEp wOld)
Default
Expand Down Expand Up @@ -326,7 +326,7 @@ spec = do

-- Check that funds become available in the target wallet:
let expectedBalance = originalBalance - expectedFee
eventually $ do
eventually_ $ do
request @ApiWallet ctx
(getWalletEp wNew)
Default
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import Test.Integration.Framework.DSL
, Payload (..)
, emptyByronWallet
, emptyWallet
, eventually
, eventually_
, expectErrorMessage
, expectEventually'
, expectFieldEqual
Expand All @@ -45,14 +45,14 @@ import qualified Network.HTTP.Types.Status as HTTP
spec :: forall t. SpecWith (Context t)
spec = do
it "NETWORK - Can query network information" $ \ctx -> do
eventually $ do
eventually_ $ do
r <- request @ApiNetworkInformation ctx networkInfoEp Default Empty
verify r [ expectFieldEqual syncProgress Ready ]

it "NETWORK_SHELLEY - Wallet has the same tip as network/information" $ \ctx -> do
let getNetworkInfo = request @ApiNetworkInformation ctx networkInfoEp Default Empty
w <- emptyWallet ctx
eventually $ do
eventually_ $ do
sync <- getNetworkInfo
verify sync [ expectFieldEqual syncProgress Ready ]
r <- getNetworkInfo
Expand All @@ -68,7 +68,7 @@ spec = do
it "NETWORK_BYRON - Byron wallet has the same tip as network/information" $ \ctx -> do
let getNetworkInfo = request @ApiNetworkInformation ctx networkInfoEp Default Empty
w <- emptyByronWallet ctx
eventually $ do
eventually_ $ do
sync <- getNetworkInfo
verify sync [ expectFieldEqual syncProgress Ready ]
r <- getNetworkInfo
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ import Test.Integration.Framework.DSL
, direction
, emptyByronWallet
, emptyWallet
, eventually
, eventually_
, expectErrorMessage
, expectEventually
, expectEventually'
Expand Down Expand Up @@ -145,7 +145,7 @@ spec = do
wSrc <- fixtureWalletWith ctx [5_000_000]
wDest <- emptyWallet ctx

eventually $ do
eventually_ $ do
-- Post Tx
let amt = (1 :: Natural)
r <- postTx ctx (wSrc, postTxEp ,"Secure Passphrase") wDest amt
Expand Down Expand Up @@ -1670,15 +1670,15 @@ spec = do
]

-- transaction eventually is in source wallet
eventually $ do
eventually_ $ do
let ep = listTxEndpSrc wSrc mempty
request @[ApiTransaction n] ctx ep Default Empty >>= flip verify
[ expectListItemFieldEqual 0 direction Outgoing
, expectListItemFieldEqual 0 status InLedger
]

-- transaction eventually is in target wallet
eventually $ do
eventually_ $ do
let ep = listTxEp wDest mempty
request @[ApiTransaction n] ctx ep Default Empty >>= flip verify
[ expectListItemFieldEqual 0 direction Incoming
Expand Down Expand Up @@ -1709,7 +1709,7 @@ spec = do
let txid = getFromResponse #id rTx

-- Wait for the transaction to be accepted
eventually $ do
eventually_ $ do
let ep = listTxEndp wSrc mempty
request @([ApiTransaction n]) ctx ep Default Empty >>= flip verify
[ expectListItemFieldEqual 0 direction Outgoing
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ import Test.Integration.Framework.DSL
, direction
, emptyByronWallet
, emptyWallet
, eventually
, eventually_
, expectCliFieldBetween
, expectCliFieldEqual
, expectCliListItemFieldEqual
Expand Down Expand Up @@ -918,15 +918,15 @@ spec = do
[ expectCliFieldEqual balanceAvailable faucetAmt
]

eventually $ do
eventually_ $ do
(fromStdout <$> listTransactionsViaCLI @t ctx [wSrcId])
>>= expectValidJSON (Proxy @([ApiTransaction n]))
>>= flip verify
[ expectCliListItemFieldEqual 0 direction Outgoing
, expectCliListItemFieldEqual 0 status InLedger
]

eventually $ do
eventually_ $ do
(fromStdout <$> listTransactionsViaCLI @t ctx [wDestId])
>>= expectValidJSON (Proxy @([ApiTransaction n]))
>>= flip verify
Expand All @@ -948,7 +948,7 @@ spec = do

-- Wait for the transaction to be accepted
let wSrcId = T.unpack $ wSrc ^. walletId
eventually $ do
eventually_ $ do
(fromStdout <$> listTransactionsViaCLI @t ctx [wSrcId])
>>= expectValidJSON (Proxy @([ApiTransaction n]))
>>= flip verify
Expand Down
2 changes: 1 addition & 1 deletion lib/jormungandr/test/data/jormungandr/start_node
Original file line number Diff line number Diff line change
@@ -1 +1 @@
cardano-wallet-jormungandr launch --genesis-block block0.bin -- --secret secret.yaml
cardano-wallet-jormungandr launch --genesis-block block0.bin --node-port 8080 -- --secret secret.yaml --config config.yaml
Loading

0 comments on commit 3797c0a

Please sign in to comment.