Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

create-staked: add test #498

Merged
merged 1 commit into from
Dec 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions cardano-cli/cardano-cli.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,7 @@ test-suite cardano-cli-golden
, cardano-cli:cardano-cli-test-lib
, cardano-crypto-wrapper
, cardano-ledger-byron
, cardano-ledger-shelley >=1.7.0.0
, cborg
, containers
, directory
Expand All @@ -362,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.CreateTestnetData
Test.Golden.Conway.Transaction.Assemble
Test.Golden.EraBased.Governance.AnswerPoll
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.Api.Ledger (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
Copy link
Contributor

@carbolymer carbolymer Dec 4, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the order of the files? If listDirectory does not give us any order
guarantees, it would be nice to sort the list to have consistency between filesystems.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@carbolymer> actually it's the caller that postprocesses the result of tree to make it stable, along with normalization and making it relative (to avoid depending on a temporary directory name) 👍

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