Skip to content

Commit

Permalink
Include block-generator and validator as algorand-indexer subcommands. (
Browse files Browse the repository at this point in the history
  • Loading branch information
winder authored Feb 28, 2022
1 parent 2aad7db commit 8d85bf4
Show file tree
Hide file tree
Showing 9 changed files with 291 additions and 242 deletions.
38 changes: 26 additions & 12 deletions cmd/algorand-indexer/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ import (
log "github.com/sirupsen/logrus"
"github.com/spf13/viper"

"github.com/algorand/indexer/cmd/import-validator/core"
bg "github.com/algorand/indexer/cmd/block-generator/core"
iv "github.com/algorand/indexer/cmd/import-validator/core"
v "github.com/algorand/indexer/cmd/validator/core"
"github.com/algorand/indexer/config"
"github.com/algorand/indexer/idb"
"github.com/algorand/indexer/idb/dummy"
Expand Down Expand Up @@ -114,9 +116,16 @@ func indexerDbFromFlags(opts idb.IndexerDbOptions) (idb.IndexerDb, chan struct{}
}

func init() {
// Add hidden subcommands
core.ImportValidatorCmd.Hidden = true
rootCmd.AddCommand(core.ImportValidatorCmd)
// Utilities subcommand for more convenient access to useful testing utilities.
utilsCmd := &cobra.Command{
Use: "util",
Short: "Utilities for testing Indexer operation and correctness.",
Long: "Utilities used for Indexer development. These are low level tools that may require low level knowledge of Indexer deployment and operation. They are included as part of this binary for ease of deployment and automation, and to publicize their existance to people who may find them useful. More detailed documention may be found on github in README files located the different 'cmd' directories.",
}
utilsCmd.AddCommand(iv.ImportValidatorCmd)
utilsCmd.AddCommand(v.ValidatorCmd)
utilsCmd.AddCommand(bg.BlockGenerator)
rootCmd.AddCommand(utilsCmd)

logger = log.New()
logger.SetFormatter(&log.JSONFormatter{
Expand All @@ -129,14 +138,19 @@ func init() {
importCmd.Hidden = true
rootCmd.AddCommand(daemonCmd)

rootCmd.PersistentFlags().StringVarP(&logLevel, "loglevel", "l", "info", "verbosity of logs: [error, warn, info, debug, trace]")
rootCmd.PersistentFlags().StringVarP(&logFile, "logfile", "f", "", "file to write logs to, if unset logs are written to standard out")
rootCmd.PersistentFlags().StringVarP(&postgresAddr, "postgres", "P", "", "connection string for postgres database")
rootCmd.PersistentFlags().BoolVarP(&dummyIndexerDb, "dummydb", "n", false, "use dummy indexer db")
rootCmd.PersistentFlags().StringVarP(&cpuProfile, "cpuprofile", "", "", "file to record cpu profile to")
rootCmd.PersistentFlags().StringVarP(&pidFilePath, "pidfile", "", "", "file to write daemon's process id to")
rootCmd.PersistentFlags().StringVarP(&configFile, "configfile", "c", "", "file path to configuration file (indexer.yml)")
rootCmd.PersistentFlags().BoolVarP(&doVersion, "version", "v", false, "print version and exit")
// Not applied globally to avoid adding to utility commands.
addFlags := func(cmd *cobra.Command) {
cmd.Flags().StringVarP(&logLevel, "loglevel", "l", "info", "verbosity of logs: [error, warn, info, debug, trace]")
cmd.Flags().StringVarP(&logFile, "logfile", "f", "", "file to write logs to, if unset logs are written to standard out")
cmd.Flags().StringVarP(&postgresAddr, "postgres", "P", "", "connection string for postgres database")
cmd.Flags().BoolVarP(&dummyIndexerDb, "dummydb", "n", false, "use dummy indexer db")
cmd.Flags().StringVarP(&cpuProfile, "cpuprofile", "", "", "file to record cpu profile to")
cmd.Flags().StringVarP(&pidFilePath, "pidfile", "", "", "file to write daemon's process id to")
cmd.Flags().StringVarP(&configFile, "configfile", "c", "", "file path to configuration file (indexer.yml)")
cmd.Flags().BoolVarP(&doVersion, "version", "v", false, "print version and exit")
}
addFlags(daemonCmd)
addFlags(importCmd)

viper.RegisterAlias("postgres", "postgres-connection-string")

Expand Down
20 changes: 20 additions & 0 deletions cmd/block-generator/core/commands.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package core

import (
"github.com/spf13/cobra"

"github.com/algorand/indexer/cmd/block-generator/generator"
"github.com/algorand/indexer/cmd/block-generator/runner"
)

// BlockGenerator related cobra commands, ready to be executed or included as subcommands.
var BlockGenerator *cobra.Command

func init() {
BlockGenerator = &cobra.Command{
Use: `block-generator`,
Short: `Block generator testing tools.`,
}
BlockGenerator.AddCommand(runner.RunnerCmd)
BlockGenerator.AddCommand(generator.DaemonCmd)
}
Original file line number Diff line number Diff line change
@@ -1,34 +1,33 @@
package main
package generator

import (
"fmt"
"math/rand"

"github.com/spf13/cobra"

"github.com/algorand/indexer/cmd/block-generator/generator"
)

// DaemonCmd starts a block generator daemon.
var DaemonCmd *cobra.Command

func init() {
rand.Seed(12345)

var configFile string
var port uint64

var daemonCmd = &cobra.Command{
DaemonCmd = &cobra.Command{
Use: "daemon",
Short: "Start the generator daemon in standalone mode.",
Run: func(cmd *cobra.Command, args []string) {
addr := fmt.Sprintf(":%d", port)
srv, _ := generator.MakeServer(configFile, addr)
srv, _ := MakeServer(configFile, addr)
srv.ListenAndServe()
},
}

daemonCmd.Flags().StringVarP(&configFile, "config", "c", "", "Specify the block configuration yaml file.")
daemonCmd.Flags().Uint64VarP(&port, "port", "p", 4010, "Port to start the server at.")

daemonCmd.MarkFlagRequired("config")
DaemonCmd.Flags().StringVarP(&configFile, "config", "c", "", "Specify the block configuration yaml file.")
DaemonCmd.Flags().Uint64VarP(&port, "port", "p", 4010, "Port to start the server at.")

rootCmd.AddCommand(daemonCmd)
DaemonCmd.MarkFlagRequired("config")
}
9 changes: 2 additions & 7 deletions cmd/block-generator/main.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
package main

import "github.com/spf13/cobra"

var rootCmd = &cobra.Command{
Use: `block-generator`,
Short: `Block generator testing tools.`,
}
import "github.com/algorand/indexer/cmd/block-generator/core"

func main() {
rootCmd.Execute()
core.BlockGenerator.Execute()
}
44 changes: 0 additions & 44 deletions cmd/block-generator/runner.go

This file was deleted.

44 changes: 44 additions & 0 deletions cmd/block-generator/runner/runner.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package runner

import (
"fmt"
"math/rand"
"time"

"github.com/spf13/cobra"
)

// RunnerCmd launches the block-generator test suite runner.
var RunnerCmd *cobra.Command

func init() {
rand.Seed(12345)
var runnerArgs Args

RunnerCmd = &cobra.Command{
Use: "runner",
Short: "Run test suite and collect results.",
Long: "Run an automated test suite using the block-generator daemon and a provided algorand-indexer binary. Results are captured to a specified output directory.",
Run: func(cmd *cobra.Command, args []string) {
if err := Run(runnerArgs); err != nil {
fmt.Println(err)
}
},
}

RunnerCmd.Flags().StringVarP(&runnerArgs.Path, "scenario", "s", "", "Directory containing scenarios, or specific scenario file.")
RunnerCmd.Flags().StringVarP(&runnerArgs.IndexerBinary, "indexer-binary", "i", "", "Path to indexer binary.")
RunnerCmd.Flags().Uint64VarP(&runnerArgs.IndexerPort, "indexer-port", "p", 4010, "Port to start the server at. This is useful if you have a prometheus server for collecting additional data.")
RunnerCmd.Flags().StringVarP(&runnerArgs.PostgresConnectionString, "postgres-connection-string", "c", "", "Postgres connection string.")
RunnerCmd.Flags().DurationVarP(&runnerArgs.RunDuration, "test-duration", "d", 5*time.Minute, "Duration to use for each scenario.")
RunnerCmd.Flags().StringVarP(&runnerArgs.ReportDirectory, "report-directory", "r", "", "Location to place test reports.")
RunnerCmd.Flags().StringVarP(&runnerArgs.LogLevel, "log-level", "l", "error", "LogLevel to use when starting Indexer. [error, warn, info, debug, trace]")
RunnerCmd.Flags().StringVarP(&runnerArgs.CPUProfilePath, "cpuprofile", "", "", "Path where Indexer writes its CPU profile.")
RunnerCmd.Flags().BoolVarP(&runnerArgs.ResetReportDir, "reset", "", false, "If set any existing report directory will be deleted before running tests.")
RunnerCmd.Flags().BoolVarP(&runnerArgs.RunValidation, "validate", "", false, "If set the validator will run after test-duration has elapsed to verify data is correct. An extra line in each report indicates validator success or failure.")

RunnerCmd.MarkFlagRequired("scenario")
RunnerCmd.MarkFlagRequired("indexer-binary")
RunnerCmd.MarkFlagRequired("postgres-connection-string")
RunnerCmd.MarkFlagRequired("report-directory")
}
2 changes: 1 addition & 1 deletion cmd/import-validator/core/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package core

import "github.com/spf13/cobra"

// ImportValidatorCmd is a configured cobra command to be executed, or included as a subcommand.
// ImportValidatorCmd is the real-time import validator command.
var ImportValidatorCmd *cobra.Command

func init() {
Expand Down
Loading

0 comments on commit 8d85bf4

Please sign in to comment.