Skip to content

Commit

Permalink
Write automated tests to cover newly introduced SPO on-chain poll com…
Browse files Browse the repository at this point in the history
…mands

  Fixture keys were generated using the command-line itself. The set of tests cover quite extensively the various commands, as well as a few 'negative' test scenarios. It is more complicated to cover the interactive part of the 'answer-poll' command through those tests; and this is therefore left as manual test. Instructions for executing the sequence will also be provided with the introduction of the commands (e.g. in the description of [PR#5050](#5050).
  • Loading branch information
KtorZ authored and Jimbo4350 committed Apr 17, 2023
1 parent 988bc65 commit e6a6e3d
Show file tree
Hide file tree
Showing 18 changed files with 498 additions and 2 deletions.
3 changes: 3 additions & 0 deletions cardano-cli/cardano-cli.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,9 @@ test-suite cardano-cli-golden
Test.Golden.Shelley.Genesis.KeyGenGenesis
Test.Golden.Shelley.Genesis.KeyGenUtxo
Test.Golden.Shelley.Genesis.KeyHash
Test.Golden.Shelley.Governance.AnswerPoll
Test.Golden.Shelley.Governance.CreatePoll
Test.Golden.Shelley.Governance.VerifyPoll
Test.Golden.Shelley.Key.ConvertCardanoAddressKey
Test.Golden.Shelley.Metadata.StakePoolMetadata
Test.Golden.Shelley.MultiSig.Address
Expand Down
35 changes: 33 additions & 2 deletions cardano-cli/test/Test/Golden/Shelley.hs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
{-# LANGUAGE OverloadedStrings #-}

module Test.Golden.Shelley
( keyTests
( keyConversionTests
, keyTests
, certificateTests
, keyConversionTests
, governancePollTests
, metadataTests
, multiSigTests
, txTests
Expand All @@ -18,6 +19,20 @@ import Test.Golden.Shelley.Genesis.KeyGenDelegate (golden_shelleyGenes
import Test.Golden.Shelley.Genesis.KeyGenGenesis (golden_shelleyGenesisKeyGenGenesis)
import Test.Golden.Shelley.Genesis.KeyGenUtxo (golden_shelleyGenesisKeyGenUtxo)
import Test.Golden.Shelley.Genesis.KeyHash (golden_shelleyGenesisKeyHash)

import Test.Golden.Shelley.Governance.AnswerPoll
(golden_shelleyGovernanceAnswerPollCold,
golden_shelleyGovernanceAnswerPollInvalidAnswer,
golden_shelleyGovernanceAnswerPollVrf)
import Test.Golden.Shelley.Governance.CreatePoll
(golden_shelleyGovernanceCreatePoll,
golden_shelleyGovernanceCreateLongPoll)
import Test.Golden.Shelley.Governance.VerifyPoll
(golden_shelleyGovernanceVerifyPollCold,
golden_shelleyGovernanceVerifyPollColdTempered,
golden_shelleyGovernanceVerifyPollVrf,
golden_shelleyGovernanceVerifyPollVrfTempered)

import Test.Golden.Shelley.Key.ConvertCardanoAddressKey
(golden_convertCardanoAddressByronSigningKey,
golden_convertCardanoAddressIcarusSigningKey,
Expand Down Expand Up @@ -168,3 +183,19 @@ multiSigTests =
, ("golden_shelleyTransactionAssembleWitness_SigningKey", golden_shelleyTransactionAssembleWitness_SigningKey)
, ("golden_shelleyTransactionSigningKeyWitness", golden_shelleyTransactionSigningKeyWitness)
]

governancePollTests :: IO Bool
governancePollTests =
H.checkSequential
$ H.Group "Governance Poll Goldens"
[ ("golden_shelleyGovernanceCreatePoll", golden_shelleyGovernanceCreatePoll)
, ("golden_shelleyGovernanceCreateLongPoll", golden_shelleyGovernanceCreateLongPoll)
, ("golden_shelleyGovernanceAnswerPoll(VRF)", golden_shelleyGovernanceAnswerPollVrf)
, ("golden_shelleyGovernanceAnswerPoll(Cold key)", golden_shelleyGovernanceAnswerPollCold)
, ("golden_shelleyGovernanceAnswerPoll(Invalid)", golden_shelleyGovernanceAnswerPollInvalidAnswer)
, ("golden_shelleyGovernanceVerifyPoll(VRF)", golden_shelleyGovernanceVerifyPollVrf)
, ("golden_shelleyGovernanceVerifyPoll(VRF, tempered)", golden_shelleyGovernanceVerifyPollVrfTempered)
, ("golden_shelleyGovernanceVerifyPoll(Cold Key)", golden_shelleyGovernanceVerifyPollCold)
, ("golden_shelleyGovernanceVerifyPoll(Cold Key, tempered)", golden_shelleyGovernanceVerifyPollColdTempered)
]

61 changes: 61 additions & 0 deletions cardano-cli/test/Test/Golden/Shelley/Governance/AnswerPoll.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
{-# LANGUAGE OverloadedStrings #-}

module Test.Golden.Shelley.Governance.AnswerPoll
( golden_shelleyGovernanceAnswerPollVrf
, golden_shelleyGovernanceAnswerPollCold
, golden_shelleyGovernanceAnswerPollInvalidAnswer
) where

import Hedgehog (Property)
import Test.OptParse

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

{- HLINT ignore "Use camelCase" -}

golden_shelleyGovernanceAnswerPollVrf :: Property
golden_shelleyGovernanceAnswerPollVrf = propertyOnce $ do
pollFile <- noteInputFile "test/data/golden/shelley/governance/poll.json"
vrfKeyFile <- noteInputFile "test/data/golden/shelley/governance/vrf.sk"

stdout <- execCardanoCLI
[ "governance", "answer-poll"
, "--poll-file", pollFile
, "--signing-key-file", vrfKeyFile
, "--answer", "0"
]

noteInputFile "test/data/golden/shelley/governance/answer-vrf.json"
>>= H.readFile
>>= (H.===) stdout

golden_shelleyGovernanceAnswerPollCold :: Property
golden_shelleyGovernanceAnswerPollCold = propertyOnce $ do
pollFile <- noteInputFile "test/data/golden/shelley/governance/poll.json"
coldKeyFile <- noteInputFile "test/data/golden/shelley/governance/cold.sk"

stdout <- execCardanoCLI
[ "governance", "answer-poll"
, "--poll-file", pollFile
, "--signing-key-file", coldKeyFile
, "--answer", "1"
]

noteInputFile "test/data/golden/shelley/governance/answer-cold.json"
>>= H.readFile
>>= (H.===) stdout

golden_shelleyGovernanceAnswerPollInvalidAnswer :: Property
golden_shelleyGovernanceAnswerPollInvalidAnswer = propertyOnce $ do
pollFile <- noteInputFile "test/data/golden/shelley/governance/poll.json"
vrfKeyFile <- noteInputFile "test/data/golden/shelley/governance/vrf.sk"

result <- tryExecCardanoCLI
[ "governance", "answer-poll"
, "--poll-file", pollFile
, "--signing-key-file", vrfKeyFile
, "--answer", "3"
]

either (const H.success) (const H.failure) result
56 changes: 56 additions & 0 deletions cardano-cli/test/Test/Golden/Shelley/Governance/CreatePoll.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
{-# LANGUAGE OverloadedStrings #-}

module Test.Golden.Shelley.Governance.CreatePoll
( golden_shelleyGovernanceCreatePoll
, golden_shelleyGovernanceCreateLongPoll
) where

import Control.Monad (void)
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

{- HLINT ignore "Use camelCase" -}

golden_shelleyGovernanceCreatePoll :: Property
golden_shelleyGovernanceCreatePoll =
propertyOnce . H.moduleWorkspace "tmp" $ \tempDir -> do
pollFile <- noteTempFile tempDir "poll.json"

stdout <- execCardanoCLI
[ "governance", "create-poll"
, "--question", "Pineapples on pizza?"
, "--answer", "yes"
, "--answer", "no"
, "--out-file", pollFile
]

void $ H.readFile pollFile
noteInputFile "test/data/golden/shelley/governance/create.json"
>>= H.readFile
>>= (H.===) stdout
H.assertFileOccurences 1 "GovernancePoll" pollFile
H.assertEndsWithSingleNewline pollFile

golden_shelleyGovernanceCreateLongPoll :: Property
golden_shelleyGovernanceCreateLongPoll =
propertyOnce . H.moduleWorkspace "tmp" $ \tempDir -> do
pollFile <- noteTempFile tempDir "poll.json"

stdout <- execCardanoCLI
[ "governance", "create-poll"
, "--question", "What is the most adequate topping to put on a pizza (please consider all possibilities and take time to answer)?"
, "--answer", "pineapples"
, "--answer", "only traditional topics should go on a pizza, this isn't room for jokes"
, "--out-file", pollFile
]

void $ H.readFile pollFile
noteInputFile "test/data/golden/shelley/governance/create-long.json"
>>= H.readFile
>>= (H.===) stdout
H.assertFileOccurences 1 "GovernancePoll" pollFile
H.assertEndsWithSingleNewline pollFile
64 changes: 64 additions & 0 deletions cardano-cli/test/Test/Golden/Shelley/Governance/VerifyPoll.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
{-# LANGUAGE OverloadedStrings #-}

module Test.Golden.Shelley.Governance.VerifyPoll
( golden_shelleyGovernanceVerifyPollVrf
, golden_shelleyGovernanceVerifyPollVrfTempered
, golden_shelleyGovernanceVerifyPollCold
, golden_shelleyGovernanceVerifyPollColdTempered
) where

import Control.Monad (void)
import Hedgehog (Property)
import Test.OptParse

import qualified Hedgehog as H

{- HLINT ignore "Use camelCase" -}

golden_shelleyGovernanceVerifyPollVrf :: Property
golden_shelleyGovernanceVerifyPollVrf = propertyOnce $ do
pollFile <- noteInputFile "test/data/golden/shelley/governance/poll.json"
metadataFile <- noteInputFile "test/data/golden/shelley/governance/answer-vrf.json"

void $ execCardanoCLI
[ "governance", "verify-poll"
, "--poll-file", pollFile
, "--metadata-file", metadataFile
]

golden_shelleyGovernanceVerifyPollCold :: Property
golden_shelleyGovernanceVerifyPollCold = propertyOnce $ do
pollFile <- noteInputFile "test/data/golden/shelley/governance/poll.json"
metadataFile <- noteInputFile "test/data/golden/shelley/governance/answer-cold.json"

void $ execCardanoCLI
[ "governance", "verify-poll"
, "--poll-file", pollFile
, "--metadata-file", metadataFile
]

golden_shelleyGovernanceVerifyPollVrfTempered :: Property
golden_shelleyGovernanceVerifyPollVrfTempered = propertyOnce $ do
pollFile <- noteInputFile "test/data/golden/shelley/governance/poll.json"
metadataFile <- noteInputFile "test/data/golden/shelley/governance/answer-vrf-tempered.json"

result <- tryExecCardanoCLI
[ "governance", "verify-poll"
, "--poll-file", pollFile
, "--metadata-file", metadataFile
]

either (const H.success) (const H.failure) result

golden_shelleyGovernanceVerifyPollColdTempered :: Property
golden_shelleyGovernanceVerifyPollColdTempered = propertyOnce $ do
pollFile <- noteInputFile "test/data/golden/shelley/governance/poll.json"
metadataFile <- noteInputFile "test/data/golden/shelley/governance/answer-cold-tempered.json"

result <- tryExecCardanoCLI
[ "governance", "verify-poll"
, "--poll-file", pollFile
, "--metadata-file", metadataFile
]

either (const H.success) (const H.failure) result
1 change: 1 addition & 0 deletions cardano-cli/test/cardano-cli-golden.hs
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,6 @@ main = do
, Test.Golden.Shelley.metadataTests
, Test.Golden.Shelley.multiSigTests
, Test.Golden.Shelley.txTests
, Test.Golden.Shelley.governancePollTests
, Test.Golden.TxView.txViewTests
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
{
"94": {
"map": [
{
"k": {
"int": 2
},
"v": {
"bytes": "820c311ced91f8c2bb9b5c7f446379063c9a077a1098d73498d17e9ea27045af"
}
},
{
"k": {
"int": 3
},
"v": {
"int": 1
}
},
{
"k": {
"int": 5
},
"v": {
"list": [
{
"bytes": "29ade2115fbcbc17f063eec41ec0d358ccc5b52c2bccb47c0918727695619a68"
},
{
"bytes": "6458ff100279aed89b0ea08a57ddbf3b77e7c6802b8c23840da7df80b60f37c0ddd445499d247d27d7e7adaa189db001d0f1eddc2229daa6be7509c43cc23501"
}
]
}
}
]
}
}
37 changes: 37 additions & 0 deletions cardano-cli/test/data/golden/shelley/governance/answer-cold.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
{
"94": {
"map": [
{
"k": {
"int": 2
},
"v": {
"bytes": "29093fd43fc30ba31e306af06ce8537390e1668ae7496fe53d53684683c3762c"
}
},
{
"k": {
"int": 3
},
"v": {
"int": 1
}
},
{
"k": {
"int": 5
},
"v": {
"list": [
{
"bytes": "29ade2115fbcbc17f063eec41ec0d358ccc5b52c2bccb47c0918727695619a68"
},
{
"bytes": "6458ff100279aed89b0ea08a57ddbf3b77e7c6802b8c23840da7df80b60f37c0ddd445499d247d27d7e7adaa189db001d0f1eddc2229daa6be7509c43cc23501"
}
]
}
}
]
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
{
"94": {
"map": [
{
"k": {
"int": 2
},
"v": {
"bytes": "29093fd43fc30ba31e306af06ce8537390e1668ae7496fe53d53684683c3762c"
}
},
{
"k": {
"int": 3
},
"v": {
"int": 1
}
},
{
"k": {
"int": 4
},
"v": {
"list": [
{
"bytes": "2dc2fa217af8b52251c4cdf538fa106cbf0b5beac3e74d05f97ceb33c0147a2c"
},
{
"list": [
{
"bytes": "c1c4d0cf60529f091431c456bf528b23d384f641afc536d1347b0889e9fd45d47e422249ac4bb5bdd75c205ea35c1ef2d89d96c0f06070590a98db7dba659647"
},
{
"bytes": "9a440df4e70830b22b86accbeab7bc07"
}
]
}
]
}
}
]
}
}
Loading

0 comments on commit e6a6e3d

Please sign in to comment.