Skip to content

Commit

Permalink
e2e: add multiversion flag to generator (tendermint#9829)
Browse files Browse the repository at this point in the history
* add multiversion flag to generator

* clarify flag comment
  • Loading branch information
williambanfield authored Dec 6, 2022
1 parent 49502da commit ac48630
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 5 deletions.
10 changes: 8 additions & 2 deletions test/e2e/generator/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ var (
},
"validators": {"genesis", "initchain"},
}

nodeVersions = weightedChoice{
"": 2,
}
// The following specify randomly chosen values for testnet nodes.
nodeDatabases = uniformChoice{"goleveldb", "cleveldb", "rocksdb", "boltdb", "badgerdb"}
ipv6 = uniformChoice{false, true}
Expand Down Expand Up @@ -50,7 +52,10 @@ var (
)

// Generate generates random testnets using the given RNG.
func Generate(r *rand.Rand) ([]e2e.Manifest, error) {
func Generate(r *rand.Rand, multiversion string) ([]e2e.Manifest, error) {
if multiversion != "" {
nodeVersions[multiversion] = 1
}
manifests := []e2e.Manifest{}
for _, opt := range combinations(testnetCombinations) {
manifest, err := generateTestnet(r, opt)
Expand Down Expand Up @@ -224,6 +229,7 @@ func generateNode(
r *rand.Rand, mode e2e.Mode, syncApp bool, startAt int64, initialHeight int64, forceArchive bool,
) *e2e.ManifestNode {
node := e2e.ManifestNode{
Version: nodeVersions.Choose(r).(string),
Mode: string(mode),
SyncApp: syncApp,
StartAt: startAt,
Expand Down
12 changes: 9 additions & 3 deletions test/e2e/generator/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,25 +44,31 @@ func NewCLI() *CLI {
if err != nil {
return err
}
return cli.generate(dir, groups)
multiversion, err := cmd.Flags().GetString("multi-version")
if err != nil {
return err
}
return cli.generate(dir, groups, multiversion)
},
}

cli.root.PersistentFlags().StringP("dir", "d", "", "Output directory for manifests")
_ = cli.root.MarkPersistentFlagRequired("dir")
cli.root.PersistentFlags().StringP("multi-version", "m", "", "Include multi-version testing."+
"If multi-version is not specified, then only the current Tendermint version will be used in generated testnets.")
cli.root.PersistentFlags().IntP("groups", "g", 0, "Number of groups")

return cli
}

// generate generates manifests in a directory.
func (cli *CLI) generate(dir string, groups int) error {
func (cli *CLI) generate(dir string, groups int, multiversion string) error {
err := os.MkdirAll(dir, 0o755)
if err != nil {
return err
}

manifests, err := Generate(rand.New(rand.NewSource(randomSeed))) //nolint:gosec
manifests, err := Generate(rand.New(rand.NewSource(randomSeed)), multiversion) //nolint:gosec
if err != nil {
return err
}
Expand Down
22 changes: 22 additions & 0 deletions test/e2e/generator/random.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,3 +83,25 @@ func (usc uniformSetChoice) Choose(r *rand.Rand) []string {
}
return choices
}

// weightedChoice chooses a single random key from a map of keys and weights.
type weightedChoice map[interface{}]uint

func (wc weightedChoice) Choose(r *rand.Rand) interface{} {
total := 0
choices := make([]interface{}, 0, len(wc))
for choice, weight := range wc {
total += int(weight)
choices = append(choices, choice)
}

rem := r.Intn(total)
for _, choice := range choices {
rem -= int(wc[choice])
if rem <= 0 {
return choice
}
}

return nil
}

0 comments on commit ac48630

Please sign in to comment.