Skip to content

Commit

Permalink
Merge #1846
Browse files Browse the repository at this point in the history
1846: Increase test code re-use by moving test support code into separate common library r=newhoggy a=newhoggy

Increase test code re-use by moving test support code into separate common `hedgehog-extras` library.

* Move generic test code into the new `hedgehog-extras` library with a module structure consistent with its genericity.
* Both `cardano-node-chairman` and `cardano-cli` modified to use the new library
* Avoid using `System.IO` libraries directly in `cardano-cli` because exceptions cause test annotations to show making tests difficult.  Instead use equivalent functions from `hedgehog-extras`
* Re-implement `execCardanoCLI` in terms of `execFlex` which has the advantage of not depending on `cabal exec` which occasionally causes problems in terms of pollution of `stdout`

Co-authored-by: John Ky <john.ky@iohk.io>
  • Loading branch information
iohk-bors[bot] and newhoggy committed Sep 16, 2020
2 parents de8e544 + e9d2490 commit 9bce1d4
Show file tree
Hide file tree
Showing 75 changed files with 825 additions and 564 deletions.
1 change: 1 addition & 0 deletions cabal.project
Expand Up @@ -6,6 +6,7 @@ packages:
cardano-config
cardano-node
cardano-node-chairman
hedgehog-extras

package cardano-api
ghc-options: -Werror -Wall -Wredundant-constraints -Wincomplete-record-updates -Wincomplete-uni-patterns -Wpartial-fields
Expand Down
8 changes: 4 additions & 4 deletions cardano-cli/cardano-cli.cabal
Expand Up @@ -165,7 +165,9 @@ test-suite cardano-cli-test
, containers
, deepseq
, directory
, exceptions
, hedgehog
, hedgehog-extras
, lifted-base
, optparse-applicative
, process
Expand Down Expand Up @@ -193,8 +195,6 @@ test-suite cardano-cli-test
-Wcompat
-threaded -rtsopts -with-rtsopts=-N -with-rtsopts=-T

build-tool-depends: cardano-cli:cardano-cli

test-suite cardano-cli-golden
hs-source-dirs: test
main-is: cardano-cli-golden.hs
Expand All @@ -211,7 +211,9 @@ test-suite cardano-cli-golden
, containers
, deepseq
, directory
, exceptions
, hedgehog
, hedgehog-extras
, lifted-base
, optparse-applicative
, process
Expand Down Expand Up @@ -280,5 +282,3 @@ test-suite cardano-cli-golden
-Wpartial-fields
-Wcompat
-threaded -rtsopts -with-rtsopts=-N -with-rtsopts=-T

build-tool-depends: cardano-cli:cardano-cli
20 changes: 11 additions & 9 deletions cardano-cli/test/Test/Cli/ITN.hs
Expand Up @@ -12,6 +12,8 @@ import Test.OptParse
import qualified Codec.Binary.Bech32 as Bech32
import qualified Data.ByteString.Base16 as Base16
import qualified Hedgehog as H
import qualified Hedgehog.Extras.Test.Base as H
import qualified Hedgehog.Extras.Test.File as H

{- HLINT ignore "Reduce duplication" -}

Expand All @@ -26,7 +28,7 @@ itnSignKey = "ed25519_sk1yhnetcmla9pskrvp5z5ff2v8gkenhmluy736jd6nrxrlxcgn70zsy94
-- | 1. Convert a bech32 ITN key pair to a haskell stake verification key and signing key
-- 2. Derive the haskell verification key from the haskell signing key.
prop_convertITNKeys :: Property
prop_convertITNKeys = propertyOnce . moduleWorkspace "tmp" $ \tempDir -> do
prop_convertITNKeys = propertyOnce . H.moduleWorkspace "tmp" $ \tempDir -> do
-- ITN input file paths
itnVerKeyFp <- noteTempFile tempDir "itnVerKey.key"
itnSignKeyFp <- noteTempFile tempDir "itnSignKey.key"
Expand All @@ -38,7 +40,7 @@ prop_convertITNKeys = propertyOnce . moduleWorkspace "tmp" $ \tempDir -> do
-- Write ITN keys to disk
liftIO $ writeFile itnVerKeyFp itnVerKey
liftIO $ writeFile itnSignKeyFp itnSignKey
assertFilesExist [itnVerKeyFp, itnSignKeyFp]
H.assertFilesExist [itnVerKeyFp, itnSignKeyFp]

-- Generate haskell stake verification key
void $ execCardanoCLI
Expand All @@ -54,11 +56,11 @@ prop_convertITNKeys = propertyOnce . moduleWorkspace "tmp" $ \tempDir -> do
]

-- Check for existence of the converted ITN keys
assertFilesExist [outputHaskellVerKeyFp, outputHaskellSignKeyFp]
H.assertFilesExist [outputHaskellVerKeyFp, outputHaskellSignKeyFp]

-- | 1. Convert a bech32 ITN extended signing key to a haskell stake signing key
prop_convertITNExtendedSigningKey :: Property
prop_convertITNExtendedSigningKey = propertyOnce . moduleWorkspace "tmp" $ \tempDir -> do
prop_convertITNExtendedSigningKey = propertyOnce . H.moduleWorkspace "tmp" $ \tempDir -> do
let itnExtendedSignKey = "\
\ed25519e_sk1qpcplz38tg4fusw0fkqljzspe9qmj06ldu9lgcve99v4fphuk9a535kwj\
\f38hkyn0shcycyaha4k9tmjy6xgvzaz7stw5t7rqjadyjcwfyx6k"
Expand All @@ -71,7 +73,7 @@ prop_convertITNExtendedSigningKey = propertyOnce . moduleWorkspace "tmp" $ \temp

-- Write ITN keys to disk
liftIO $ writeFile itnSignKeyFp itnExtendedSignKey
assertFilesExist [itnSignKeyFp]
H.assertFilesExist [itnSignKeyFp]

-- Generate haskell signing key
void $ execCardanoCLI
Expand All @@ -81,11 +83,11 @@ prop_convertITNExtendedSigningKey = propertyOnce . moduleWorkspace "tmp" $ \temp
]

-- Check for existence of the converted ITN keys
assertFilesExist [outputHaskellSignKeyFp]
H.assertFilesExist [outputHaskellSignKeyFp]

-- | 1. Convert a bech32 ITN BIP32 signing key to a haskell stake signing key
prop_convertITNBIP32SigningKey :: Property
prop_convertITNBIP32SigningKey = propertyOnce . moduleWorkspace "tmp" $ \tempDir -> do
prop_convertITNBIP32SigningKey = propertyOnce . H.moduleWorkspace "tmp" $ \tempDir -> do
let itnExtendedSignKey = "\
\xprv1spkw5suj39723c40mr55gwh7j3vryjv2zdm4e47xs0deka\
\jcza9ud848ckdqf48md9njzc5pkujfxwu2j8wdvtxkx02n3s2qa\
Expand All @@ -100,7 +102,7 @@ prop_convertITNBIP32SigningKey = propertyOnce . moduleWorkspace "tmp" $ \tempDir
-- Write ITN keys to disk
liftIO $ writeFile itnSignKeyFp itnExtendedSignKey

assertFilesExist [itnSignKeyFp]
H.assertFilesExist [itnSignKeyFp]

-- Generate haskell signing key
void $ execCardanoCLI
Expand All @@ -110,7 +112,7 @@ prop_convertITNBIP32SigningKey = propertyOnce . moduleWorkspace "tmp" $ \tempDir
]

-- Check for existence of the converted ITN keys
assertFilesExist [outputHaskellSignKeyFp]
H.assertFilesExist [outputHaskellSignKeyFp]

-- | We check our 'decodeBech32' outputs against https://slowli.github.io/bech32-buffer/
-- using 'itnVerKey' & 'itnSignKey' as inputs.
Expand Down
10 changes: 6 additions & 4 deletions cardano-cli/test/Test/Cli/Pioneers/Exercise1.hs
Expand Up @@ -9,12 +9,14 @@ import Hedgehog (Property)
import Test.OptParse

import qualified Hedgehog as H
import qualified Hedgehog.Extras.Test.Base as H
import qualified Hedgehog.Extras.Test.File as H

-- | 1. Generate a key pair
-- 2. Check for the existence of the key pair
-- 3. We use the generated verification key to build a shelley payment address.
prop_buildShelleyPaymentAddress :: Property
prop_buildShelleyPaymentAddress = propertyOnce . moduleWorkspace "tmp" $ \tempDir -> do
prop_buildShelleyPaymentAddress = propertyOnce . H.moduleWorkspace "tmp" $ \tempDir -> do
-- Key filepaths
verKey <- noteTempFile tempDir "payment-verification-key-file"
signKey <- noteTempFile tempDir "payment-signing-key-file"
Expand All @@ -26,7 +28,7 @@ prop_buildShelleyPaymentAddress = propertyOnce . moduleWorkspace "tmp" $ \tempDi
, "--signing-key-file", signKey
]

assertFilesExist [verKey, signKey]
H.assertFilesExist [verKey, signKey]

-- Build shelley payment address
void $ execCardanoCLI
Expand All @@ -41,7 +43,7 @@ prop_buildShelleyPaymentAddress = propertyOnce . moduleWorkspace "tmp" $ \tempDi
-- 3. We use the payment verification key & staking verification key
-- to build a shelley stake address.
prop_buildShelleyStakeAddress :: Property
prop_buildShelleyStakeAddress = propertyOnce . moduleWorkspace "tmp" $ \tempDir -> do
prop_buildShelleyStakeAddress = propertyOnce . H.moduleWorkspace "tmp" $ \tempDir -> do
-- Key filepaths
stakeVerKey <- noteTempFile tempDir "stake-verification-key-file"
stakeSignKey <- noteTempFile tempDir "stake-signing-key-file"
Expand All @@ -62,7 +64,7 @@ prop_buildShelleyStakeAddress = propertyOnce . moduleWorkspace "tmp" $ \tempDir
, "--signing-key-file", stakeSignKey
]

assertFilesExist [stakeVerKey, stakeSignKey, paymentVerKey, paymentSignKey]
H.assertFilesExist [stakeVerKey, stakeSignKey, paymentVerKey, paymentSignKey]

-- Build shelley stake address
void $ execCardanoCLI
Expand Down
10 changes: 6 additions & 4 deletions cardano-cli/test/Test/Cli/Pioneers/Exercise2.hs
Expand Up @@ -9,12 +9,14 @@ import Hedgehog (Property)
import Test.OptParse

import qualified Hedgehog as H
import qualified Hedgehog.Extras.Test.Base as H
import qualified Hedgehog.Extras.Test.File as H

-- | 1. We generate a payment signing key
-- 2. We create a tx body
-- 3. We sign the tx body with the generated payment signing key
prop_createTransaction :: Property
prop_createTransaction = propertyOnce . moduleWorkspace "tmp" $ \tempDir -> do
prop_createTransaction = propertyOnce . H.moduleWorkspace "tmp" $ \tempDir -> do
-- Key filepaths
paymentVerKey <- noteTempFile tempDir "payment-verification-key-file"
paymentSignKey <- noteTempFile tempDir "payment-signing-key-file"
Expand All @@ -28,7 +30,7 @@ prop_createTransaction = propertyOnce . moduleWorkspace "tmp" $ \tempDir -> do
, "--signing-key-file", paymentSignKey
]

assertFilesExist [paymentVerKey, paymentSignKey]
H.assertFilesExist [paymentVerKey, paymentSignKey]

-- Create transaction body
void $ execCardanoCLI
Expand All @@ -40,7 +42,7 @@ prop_createTransaction = propertyOnce . moduleWorkspace "tmp" $ \tempDir -> do
, "--out-file", transactionBodyFile
]

assertFilesExist [transactionBodyFile]
H.assertFilesExist [transactionBodyFile]

-- Sign transaction
void $ execCardanoCLI
Expand All @@ -51,7 +53,7 @@ prop_createTransaction = propertyOnce . moduleWorkspace "tmp" $ \tempDir -> do
, "--out-file", transactionFile
]

assertFilesExist [paymentVerKey, paymentSignKey, transactionBodyFile, transactionFile]
H.assertFilesExist [paymentVerKey, paymentSignKey, transactionBodyFile, transactionFile]

-- -----------------------------------------------------------------------------

Expand Down
10 changes: 6 additions & 4 deletions cardano-cli/test/Test/Cli/Pioneers/Exercise3.hs
Expand Up @@ -9,13 +9,15 @@ import Hedgehog (Property)
import Test.OptParse

import qualified Hedgehog as H
import qualified Hedgehog.Extras.Test.Base as H
import qualified Hedgehog.Extras.Test.File as H

-- | 1. Create KES key pair.
-- 2. Create cold keys.
-- 3. Create operational certificate.
-- 4. Create VRF key pair.
prop_createOperationalCertificate :: Property
prop_createOperationalCertificate = propertyOnce . moduleWorkspace "tmp" $ \tempDir -> do
prop_createOperationalCertificate = propertyOnce . H.moduleWorkspace "tmp" $ \tempDir -> do
-- Key filepaths
kesVerKey <- noteTempFile tempDir "KES-verification-key-file"
kesSignKey <- noteTempFile tempDir "KES-signing-key-file"
Expand All @@ -31,7 +33,7 @@ prop_createOperationalCertificate = propertyOnce . moduleWorkspace "tmp" $ \temp
, "--signing-key-file", kesSignKey
]

assertFilesExist [kesSignKey, kesVerKey]
H.assertFilesExist [kesSignKey, kesVerKey]

-- Create cold key pair
void $ execCardanoCLI
Expand All @@ -41,7 +43,7 @@ prop_createOperationalCertificate = propertyOnce . moduleWorkspace "tmp" $ \temp
, "--operational-certificate-issue-counter", operationalCertCounter
]

assertFilesExist [coldVerKey, coldSignKey, operationalCertCounter]
H.assertFilesExist [coldVerKey, coldSignKey, operationalCertCounter]

-- Create operational certificate
void $ execCardanoCLI
Expand All @@ -53,7 +55,7 @@ prop_createOperationalCertificate = propertyOnce . moduleWorkspace "tmp" $ \temp
, "--out-file", operationalCert
]

assertFilesExist [kesVerKey, kesSignKey, coldVerKey, coldSignKey, operationalCertCounter, operationalCert]
H.assertFilesExist [kesVerKey, kesSignKey, coldVerKey, coldSignKey, operationalCertCounter, operationalCert]

-- -----------------------------------------------------------------------------

Expand Down
8 changes: 5 additions & 3 deletions cardano-cli/test/Test/Cli/Pioneers/Exercise4.hs
Expand Up @@ -9,11 +9,13 @@ import Hedgehog (Property)
import Test.OptParse

import qualified Hedgehog as H
import qualified Hedgehog.Extras.Test.Base as H
import qualified Hedgehog.Extras.Test.File as H

-- | 1. Generate a stake verification key
-- 2. Create a stake address registration certificate
prop_createStakeAddressRegistrationCertificate :: Property
prop_createStakeAddressRegistrationCertificate = propertyOnce . moduleWorkspace "tmp" $ \tempDir -> do
prop_createStakeAddressRegistrationCertificate = propertyOnce . H.moduleWorkspace "tmp" $ \tempDir -> do
-- Key filepaths
verKey <- noteTempFile tempDir "stake-verification-key-file"
signKey <- noteTempFile tempDir "stake-signing-key-file"
Expand All @@ -25,7 +27,7 @@ prop_createStakeAddressRegistrationCertificate = propertyOnce . moduleWorkspace
, "--verification-key-file", verKey
, "--signing-key-file", signKey
]
assertFilesExist [verKey, signKey]
H.assertFilesExist [verKey, signKey]

-- Create stake address registration certificate
void $ execCardanoCLI
Expand All @@ -34,7 +36,7 @@ prop_createStakeAddressRegistrationCertificate = propertyOnce . moduleWorkspace
, "--out-file", stakeRegCert
]

assertFilesExist [verKey, signKey, stakeRegCert]
H.assertFilesExist [verKey, signKey, stakeRegCert]

-- -----------------------------------------------------------------------------

Expand Down
17 changes: 9 additions & 8 deletions cardano-cli/test/Test/Golden/Shelley/Address/Build.hs
Expand Up @@ -8,20 +8,21 @@ import Cardano.Prelude
import Hedgehog (Property)
import Test.OptParse as OP

import qualified System.IO as IO
import qualified Hedgehog.Extras.Test.Base as H
import qualified Hedgehog.Extras.Test.File as H

{- HLINT ignore "Use camelCase" -}

golden_shelleyAddressBuild :: Property
golden_shelleyAddressBuild = propertyOnce . moduleWorkspace "tmp" $ \tempDir -> do
golden_shelleyAddressBuild = propertyOnce . H.moduleWorkspace "tmp" $ \tempDir -> do
addressVKeyFile <- noteInputFile "test/data/golden/shelley/keys/payment_keys/verification_key"
addressSKeyFile <- noteInputFile "test/data/golden/shelley/keys/stake_keys/verification_key"
goldenStakingAddressHexFile <- noteInputFile "test/data/golden/shelley/addresses/staking-address.hex"
goldenEnterpriseAddressHexFile <- noteInputFile "test/data/golden/shelley/addresses/enterprise-address.hex"
stakingAddressHexFile <- noteTempFile tempDir "staking-address.hex"
enterpriseAddressHexFile <- noteTempFile tempDir "enterprise-address.hex"

void $ OP.readFile addressVKeyFile
void $ H.readFile addressVKeyFile

stakingAddressText <- execCardanoCLI
[ "shelley","address","build"
Expand All @@ -30,13 +31,13 @@ golden_shelleyAddressBuild = propertyOnce . moduleWorkspace "tmp" $ \tempDir ->
, "--staking-verification-key-file", addressSKeyFile
]

goldenStakingAddressHex <- OP.readFile goldenStakingAddressHexFile
goldenStakingAddressHex <- H.readFile goldenStakingAddressHexFile

liftIO $ IO.writeFile stakingAddressHexFile stakingAddressText
H.writeFile stakingAddressHexFile stakingAddressText

equivalence stakingAddressText goldenStakingAddressHex

void $ OP.readFile addressSKeyFile
void $ H.readFile addressSKeyFile

enterpriseAddressText <- execCardanoCLI
[ "shelley","address","build"
Expand All @@ -45,8 +46,8 @@ golden_shelleyAddressBuild = propertyOnce . moduleWorkspace "tmp" $ \tempDir ->
, "--staking-verification-key-file", addressSKeyFile
]

goldenEnterpriseAddressHex <- OP.readFile goldenEnterpriseAddressHexFile
goldenEnterpriseAddressHex <- H.readFile goldenEnterpriseAddressHexFile

liftIO $ IO.writeFile enterpriseAddressHexFile enterpriseAddressText
H.writeFile enterpriseAddressHexFile enterpriseAddressText

equivalence enterpriseAddressText goldenEnterpriseAddressHex
17 changes: 9 additions & 8 deletions cardano-cli/test/Test/Golden/Shelley/Address/KeyGen.hs
Expand Up @@ -8,12 +8,13 @@ import Cardano.Prelude
import Hedgehog (Property)
import Test.OptParse

import qualified Test.OptParse as OP
import qualified Hedgehog.Extras.Test.Base as H
import qualified Hedgehog.Extras.Test.File as H

{- HLINT ignore "Use camelCase" -}

golden_shelleyAddressKeyGen :: Property
golden_shelleyAddressKeyGen = propertyOnce . moduleWorkspace "tmp" $ \tempDir -> do
golden_shelleyAddressKeyGen = propertyOnce . H.moduleWorkspace "tmp" $ \tempDir -> do
addressVKeyFile <- noteTempFile tempDir "address.vkey"
addressSKeyFile <- noteTempFile tempDir "address.skey"

Expand All @@ -23,11 +24,11 @@ golden_shelleyAddressKeyGen = propertyOnce . moduleWorkspace "tmp" $ \tempDir ->
, "--signing-key-file", addressSKeyFile
]

void $ OP.readFile addressVKeyFile
void $ OP.readFile addressSKeyFile
void $ H.readFile addressVKeyFile
void $ H.readFile addressSKeyFile

assertFileOccurences 1 "PaymentVerificationKeyShelley" addressVKeyFile
assertFileOccurences 1 "PaymentSigningKeyShelley_ed25519" addressSKeyFile
H.assertFileOccurences 1 "PaymentVerificationKeyShelley" addressVKeyFile
H.assertFileOccurences 1 "PaymentSigningKeyShelley_ed25519" addressSKeyFile

assertEndsWithSingleNewline addressVKeyFile
assertEndsWithSingleNewline addressSKeyFile
H.assertEndsWithSingleNewline addressVKeyFile
H.assertEndsWithSingleNewline addressSKeyFile

0 comments on commit 9bce1d4

Please sign in to comment.