Skip to content

Commit

Permalink
test: add test defending against regression in testnet genesis genera…
Browse files Browse the repository at this point in the history
…tion (#4741)
  • Loading branch information
michaeldiamant authored Nov 3, 2022
1 parent 06d146b commit 7fcfe26
Showing 1 changed file with 50 additions and 9 deletions.
59 changes: 50 additions & 9 deletions gen/generate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package gen
import (
"encoding/json"
"fmt"
"github.com/algorand/go-algorand/data/basics"
"io"
"os"
"path/filepath"
Expand Down Expand Up @@ -131,27 +132,37 @@ func TestGenesisJsonCreation(t *testing.T) {
t.Parallel()

type testCase struct {
name string
gd GenesisData
name string
gd GenesisData
expectedOverride string
}

// `base` is a canonical test confirming `devnet.json` generates the intended `genesis.json`.
base := func() testCase {
jsonBytes, err := os.ReadFile("devnet.json")
defaultGenesisFromFile := func(filename string) GenesisData {
jsonBytes, err := os.ReadFile(filename)
require.NoError(t, err)

gd := DefaultGenesis
err = json.Unmarshal(jsonBytes, &gd)
require.NoError(t, err)

return testCase{"base", gd}
return gd
}

// `base` is a canonical test confirming `devnet.json` generates the intended `genesis.json`.
base := func() testCase {
return testCase{"base", defaultGenesisFromFile("devnet.json"), ""}
}

// `balance` extends `base` to confirm overriding the rewards pool balance works.
balance := func() testCase {
gd := base().gd
gd.RewardsPoolBalance = 0 // Expect generated balance == MinBalance
return testCase{"balance", gd}
return testCase{"balance", gd, ""}
}

// `testnet` confirms the generated genesis.json conforms to a previously generated _installer_ artifact.
testnet := func() testCase {
return testCase{"testnet", defaultGenesisFromFile("testnet.json"), "../installer/genesis/testnet/genesis.json"}
}

// `blotOutRandomValues` replaces random values with static values to support equality checks.
Expand Down Expand Up @@ -180,6 +191,24 @@ func TestGenesisJsonCreation(t *testing.T) {
}
}

const quickLastPartKeyRound = basics.Round(10) // Ensure quick test execution by reducing rounds.

// `blotOutFixedValues` replaces values from actual genesis values in order to be compatible with artifacts generated by tests.
blotOutFixedValues := func(g *bookkeeping.Genesis) {
for i := range g.Allocation {
if g.Allocation[i].State.Status == basics.Online {
require.Greater(t, g.Allocation[i].State.VoteLastValid, quickLastPartKeyRound)
g.Allocation[i].State.VoteLastValid = quickLastPartKeyRound
}
}

require.NotZero(t, g.Timestamp)
g.Timestamp = 0

require.NotEmpty(t, g.Network)
g.Network = ""
}

saveGeneratedGenesisJSON := func(filename, artifactName string) {
src, err := os.Open(filename)
require.NoError(t, err)
Expand All @@ -206,10 +235,11 @@ func TestGenesisJsonCreation(t *testing.T) {
for _, tc := range []testCase{
base(),
balance(),
testnet(),
} {
t.Run(fmt.Sprintf("name=%v", tc.name), func(t *testing.T) {
gd := tc.gd
gd.LastPartKeyRound = 10 // Ensure quick test execution by reducing rounds.
gd.LastPartKeyRound = uint64(quickLastPartKeyRound)

outDir := t.TempDir()
err := GenerateGenesisFiles(gd, config.Consensus, outDir, nil)
Expand All @@ -224,11 +254,22 @@ func TestGenesisJsonCreation(t *testing.T) {
roundtrip, err := bookkeeping.LoadGenesisFromFile(generatedFilename)
require.NoError(t, err)

expected, err := bookkeeping.LoadGenesisFromFile("resources/" + artifactName)
expectedFilepath := func() string {
if len(tc.expectedOverride) == 0 {
return "resources/" + artifactName
}
return tc.expectedOverride
}
expected, err := bookkeeping.LoadGenesisFromFile(expectedFilepath())
saveOnFailure(assert.NoError(t, err))

blotOutRandomValues(expected.Allocation)
blotOutRandomValues(roundtrip.Allocation)

if len(tc.expectedOverride) > 0 {
blotOutFixedValues(&expected)
}

saveOnFailure(assert.Equal(t, expected, roundtrip))
})
}
Expand Down

0 comments on commit 7fcfe26

Please sign in to comment.