diff --git a/cardano-cli/cardano-cli.cabal b/cardano-cli/cardano-cli.cabal index 8c5739ba1f..83aecb26b0 100644 --- a/cardano-cli/cardano-cli.cabal +++ b/cardano-cli/cardano-cli.cabal @@ -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 @@ -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 diff --git a/cardano-cli/test/cardano-cli-golden/Test/Golden/CreateStaked.hs b/cardano-cli/test/cardano-cli-golden/Test/Golden/CreateStaked.hs new file mode 100644 index 0000000000..462fe093c3 --- /dev/null +++ b/cardano-cli/test/cardano-cli-golden/Test/Golden/CreateStaked.hs @@ -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 + 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) diff --git a/cardano-cli/test/cardano-cli-golden/files/golden/conway/create-staked.out b/cardano-cli/test/cardano-cli-golden/files/golden/conway/create-staked.out new file mode 100644 index 0000000000..02d7d2bfb4 --- /dev/null +++ b/cardano-cli/test/cardano-cli-golden/files/golden/conway/create-staked.out @@ -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 \ No newline at end of file