Skip to content

Commit

Permalink
create-staked: add test
Browse files Browse the repository at this point in the history
  • Loading branch information
smelc committed Dec 4, 2023
1 parent 163d80d commit c36e980
Show file tree
Hide file tree
Showing 3 changed files with 167 additions and 0 deletions.
4 changes: 4 additions & 0 deletions cardano-cli/cardano-cli.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -338,8 +338,11 @@ test-suite cardano-cli-golden
, cardano-cli:cardano-cli-test-lib
, cardano-crypto-wrapper
, cardano-ledger-byron
, cardano-ledger-core >= 1.8
, cardano-ledger-shelley >=1.7.0.0
, cborg
, containers
, directory
, filepath
, hedgehog ^>= 1.3
, hedgehog-extras ^>= 0.4.7.0
Expand All @@ -360,6 +363,7 @@ test-suite cardano-cli-golden
Test.Golden.Byron.UpdateProposal
Test.Golden.Byron.Vote
Test.Golden.Byron.Witness
Test.Golden.CreateStaked
Test.Golden.Conway.Transaction.Assemble
Test.Golden.EraBased.Governance.AnswerPoll
Test.Golden.EraBased.Governance.CreatePoll
Expand Down
88 changes: 88 additions & 0 deletions cardano-cli/test/cardano-cli-golden/Test/Golden/CreateStaked.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
{-# LANGUAGE ScopedTypeVariables #-}

module Test.Golden.CreateStaked where

import Cardano.Api.Shelley (ShelleyGenesis (sgNetworkMagic, sgStaking))

import Cardano.Ledger.Crypto (StandardCrypto)
import Cardano.Ledger.Shelley.Genesis (ShelleyGenesisStaking (sgsPools, sgsStake))

import Control.Monad (filterM, void)
import Control.Monad.IO.Class
import qualified Data.Aeson as Aeson
import qualified Data.ByteString.Lazy as LBS
import Data.List (intercalate, sort)
import System.Directory
import System.FilePath

import Test.Cardano.CLI.Util (execCardanoCLI)

import Hedgehog (Property)
import qualified Hedgehog as H
import Hedgehog.Extras (moduleWorkspace, propertyOnce)
import qualified Hedgehog.Extras as H
import qualified Hedgehog.Extras.Test.Golden as H


{- HLINT ignore "Use camelCase" -}

-- | Given a root directory, returns files within this root (recursively)
tree :: FilePath -> IO [FilePath]
tree root = do
-- listDirectory returns a path relative to 'root'. We need to prepend
-- root to it for queries below.
content <- map (root </>) <$> listDirectory root
files <- filterM doesFileExist content
subs <- filterM doesDirectoryExist content
subTrees <- mapM tree subs
return $ files ++ concat subTrees

hprop_golden_create_staked :: Property
hprop_golden_create_staked =
propertyOnce $ moduleWorkspace "tmp" $ \tempDir -> do

let alonzo = "genesis.alonzo.spec.json"
conway = "genesis.conway.spec.json"
networkMagic = 42
numPools = 4
numStake = 8

liftIO $ copyFile ("test/cardano-cli-golden/files/input/alonzo/" <> alonzo) (tempDir </> alonzo)
liftIO $ copyFile ("test/cardano-cli-golden/files/input/conway/" <> conway) (tempDir </> conway)

void $ H.note (tempDir </> alonzo)
void $ H.note (tempDir </> conway)

void $
execCardanoCLI
["conway", "genesis", "create-staked"
, "--gen-genesis-keys", "2"
, "--gen-pools", show numPools
, "--gen-utxo-keys", "3"
, "--gen-stake-delegs", show numStake
, "--supply", "1000000000000"
, "--supply-delegated", "1000000000000"
, "--testnet-magic", show networkMagic
, "--bulk-pool-cred-files", "2"
, "--bulk-pools-per-file", "2"
, "--num-stuffed-utxo", "7"
, "--genesis-dir", tempDir
]

generated <- liftIO $ tree tempDir
-- Sort output for stability, and make relative to avoid storing
-- a path that changes everytime (/tmp/nix-shell.[0-9]+/tmp-Test...)
let generated' = intercalate "\n" $ sort $ map (makeRelative tempDir) generated
-- On Windows, the path separator is backslash. Normalize it to slash, like on Unix
-- so that this test can run on all platforms.
generated'' = map (\c -> if c == '\\' then '/' else c) generated'
void $ H.note generated''

H.diffVsGoldenFile generated'' "test/cardano-cli-golden/files/golden/conway/create-staked.out"

bs <- liftIO $ LBS.readFile $ tempDir </> "genesis.json"
genesis :: ShelleyGenesis StandardCrypto <- Aeson.throwDecode bs

H.assert (sgNetworkMagic genesis == networkMagic)
H.assert ((length . sgsPools . sgStaking $ genesis) == numPools)
H.assert ((length . sgsStake . sgStaking $ genesis) == numStake)
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
delegate-keys/delegate1.counter
delegate-keys/delegate1.kes.skey
delegate-keys/delegate1.kes.vkey
delegate-keys/delegate1.skey
delegate-keys/delegate1.vkey
delegate-keys/delegate1.vrf.skey
delegate-keys/delegate1.vrf.vkey
delegate-keys/delegate2.counter
delegate-keys/delegate2.kes.skey
delegate-keys/delegate2.kes.vkey
delegate-keys/delegate2.skey
delegate-keys/delegate2.vkey
delegate-keys/delegate2.vrf.skey
delegate-keys/delegate2.vrf.vkey
delegate-keys/opcert1.cert
delegate-keys/opcert2.cert
genesis-keys/genesis1.skey
genesis-keys/genesis1.vkey
genesis-keys/genesis2.skey
genesis-keys/genesis2.vkey
genesis.alonzo.json
genesis.alonzo.spec.json
genesis.conway.json
genesis.conway.spec.json
genesis.json
genesis.spec.json
module
pools/bulk1.creds
pools/bulk2.creds
pools/cold1.skey
pools/cold1.vkey
pools/cold2.skey
pools/cold2.vkey
pools/cold3.skey
pools/cold3.vkey
pools/cold4.skey
pools/cold4.vkey
pools/kes1.skey
pools/kes1.vkey
pools/kes2.skey
pools/kes2.vkey
pools/kes3.skey
pools/kes3.vkey
pools/kes4.skey
pools/kes4.vkey
pools/opcert1.cert
pools/opcert1.counter
pools/opcert2.cert
pools/opcert2.counter
pools/opcert3.cert
pools/opcert3.counter
pools/opcert4.cert
pools/opcert4.counter
pools/staking-reward1.skey
pools/staking-reward1.vkey
pools/staking-reward2.skey
pools/staking-reward2.vkey
pools/staking-reward3.skey
pools/staking-reward3.vkey
pools/staking-reward4.skey
pools/staking-reward4.vkey
pools/vrf1.skey
pools/vrf1.vkey
pools/vrf2.skey
pools/vrf2.vkey
pools/vrf3.skey
pools/vrf3.vkey
pools/vrf4.skey
pools/vrf4.vkey
utxo-keys/utxo1.skey
utxo-keys/utxo1.vkey
utxo-keys/utxo2.skey
utxo-keys/utxo2.vkey
utxo-keys/utxo3.skey
utxo-keys/utxo3.vkey

0 comments on commit c36e980

Please sign in to comment.