From ae3ad5137c5fc367f48bce7279a5301530ec8b0f Mon Sep 17 00:00:00 2001 From: algochoi <86622919+algochoi@users.noreply.github.com> Date: Wed, 16 Aug 2023 16:28:41 -0400 Subject: [PATCH 01/15] Add export and import options for genesis files during network creation --- cmd/goal/network.go | 63 ++++++++++++++++++++++++++++++ netdeploy/network.go | 4 +- netdeploy/networkTemplate.go | 6 +-- netdeploy/networkTemplates_test.go | 3 +- 4 files changed, 69 insertions(+), 7 deletions(-) diff --git a/cmd/goal/network.go b/cmd/goal/network.go index 5c32b68e36..51ad68c31a 100644 --- a/cmd/goal/network.go +++ b/cmd/goal/network.go @@ -28,6 +28,7 @@ import ( "github.com/algorand/go-algorand/cmd/util/datadir" "github.com/algorand/go-algorand/config" + "github.com/algorand/go-algorand/gen" "github.com/algorand/go-algorand/netdeploy" "github.com/algorand/go-algorand/util" ) @@ -40,6 +41,7 @@ var noImportKeys bool var noClean bool var devModeOverride bool var startOnCreation bool +var genesisDir string func init() { networkCmd.AddCommand(networkCreateCmd) @@ -52,14 +54,18 @@ func init() { networkCreateCmd.Flags().BoolVar(&noClean, "noclean", false, "Prevents auto-cleanup on error - for diagnosing problems") networkCreateCmd.Flags().BoolVar(&devModeOverride, "devMode", false, "Forces the configuration to enable DevMode, returns an error if the template is not compatible with DevMode.") networkCreateCmd.Flags().BoolVarP(&startOnCreation, "start", "s", false, "Automatically start the network after creating it.") + networkCreateCmd.Flags().StringVar(&genesisDir, "genesisDir", "", "Specify the path to the directory with existing genesis.json, root and partkeys. By default, the genesis.json and keys will be generated on start. This should only be used on private networks.") networkStartCmd.Flags().StringVarP(&startNode, "node", "n", "", "Specify the name of a specific node to start") + networkGenesisCmd.Flags().StringVarP(&networkTemplateFile, "template", "t", "", "Specify the path to the template file for the network") + networkCmd.AddCommand(networkStartCmd) networkCmd.AddCommand(networkRestartCmd) networkCmd.AddCommand(networkStopCmd) networkCmd.AddCommand(networkStatusCmd) networkCmd.AddCommand(networkDeleteCmd) + networkCmd.AddCommand(networkGenesisCmd) } var networkCmd = &cobra.Command{ @@ -112,6 +118,15 @@ var networkCreateCmd = &cobra.Command{ reportErrorf(infoNetworkAlreadyExists, networkRootDir) } + // If genesisDir is specified, copy files over + if genesisDir != "" { + genesisDir, err = filepath.Abs(genesisDir) + if err != nil { + panic(err) + } + util.CopyFolder(genesisDir, networkRootDir) + } + binDir, err := util.ExeDir() if err != nil { panic(err) @@ -246,3 +261,51 @@ var networkDeleteCmd = &cobra.Command{ reportInfof(infoNetworkDeleted, networkRootDir) }, } + +var networkGenesisCmd = &cobra.Command{ + Use: "genesis", + Short: "Creates the genesis.json, root and participation keys for a wallet", + Args: validateNoPosArgsFn, + Run: func(cmd *cobra.Command, _ []string) { + networkRootDir, err := filepath.Abs(networkRootDir) + if err != nil { + panic(err) + } + + var templateReader io.Reader + + if networkTemplateFile == "" { + templateReader = strings.NewReader(defaultNetworkTemplate) + } else { + networkTemplateFile, err = filepath.Abs(networkTemplateFile) + if err != nil { + panic(err) + } + file, osErr := os.Open(networkTemplateFile) + if osErr != nil { + reportErrorf(errorCreateNetwork, osErr) + } + + defer file.Close() + templateReader = file + } + + var template netdeploy.NetworkTemplate + err = netdeploy.LoadTemplateFromReader(templateReader, &template) + if err != nil { + reportErrorf("Error in loading template: %v\n", err) + } + + dataDir := datadir.MaybeSingleDataDir() + var consensus config.ConsensusProtocols + if dataDir != "" { + // try to load the consensus from there. If there is none, we can just use the built in one. + consensus, _ = config.PreloadConfigurableConsensusProtocols(dataDir) + } + if err = template.Validate(); err != nil { + reportErrorf("Error in template validation: %v\n", err) + } + + gen.GenerateGenesisFiles(template.Genesis, consensus, networkRootDir, os.Stdout) + }, +} diff --git a/netdeploy/network.go b/netdeploy/network.go index 02202d5559..4e86bd831f 100644 --- a/netdeploy/network.go +++ b/netdeploy/network.go @@ -70,7 +70,7 @@ func CreateNetworkFromTemplate(name, rootDir string, templateReader io.Reader, b var err error template := defaultNetworkTemplate - err = loadTemplateFromReader(templateReader, &template) + err = LoadTemplateFromReader(templateReader, &template) if err == nil { if overrideDevMode { @@ -100,7 +100,7 @@ func CreateNetworkFromTemplate(name, rootDir string, templateReader io.Reader, b return n, err } template.Consensus = consensus - err = template.generateGenesisAndWallets(rootDir, n.cfg.Name, binDir) + err = template.generateGenesisAndWallets(rootDir, n.cfg.Name) if err != nil { return n, err } diff --git a/netdeploy/networkTemplate.go b/netdeploy/networkTemplate.go index 7d8b12bf92..c6e770c7f2 100644 --- a/netdeploy/networkTemplate.go +++ b/netdeploy/networkTemplate.go @@ -47,7 +47,7 @@ var defaultNetworkTemplate = NetworkTemplate{ Genesis: gen.DefaultGenesis, } -func (t NetworkTemplate) generateGenesisAndWallets(targetFolder, networkName, binDir string) error { +func (t NetworkTemplate) generateGenesisAndWallets(targetFolder, networkName string) error { genesisData := t.Genesis genesisData.NetworkName = networkName mergedConsensus := config.Consensus.Merge(t.Consensus) @@ -165,11 +165,11 @@ func loadTemplate(templateFile string) (NetworkTemplate, error) { } defer f.Close() - err = loadTemplateFromReader(f, &template) + err = LoadTemplateFromReader(f, &template) return template, err } -func loadTemplateFromReader(reader io.Reader, template *NetworkTemplate) error { +func LoadTemplateFromReader(reader io.Reader, template *NetworkTemplate) error { if runtime.GOARCH == "arm" || runtime.GOARCH == "arm64" { // for arm machines, use smaller key dilution diff --git a/netdeploy/networkTemplates_test.go b/netdeploy/networkTemplates_test.go index f21c37ace9..80f2c2a43c 100644 --- a/netdeploy/networkTemplates_test.go +++ b/netdeploy/networkTemplates_test.go @@ -69,9 +69,8 @@ func TestGenerateGenesis(t *testing.T) { targetFolder := t.TempDir() networkName := "testGenGen" - binDir := os.ExpandEnv("${GOPATH}/bin") - err := template.generateGenesisAndWallets(targetFolder, networkName, binDir) + err := template.generateGenesisAndWallets(targetFolder, networkName) a.NoError(err) _, err = os.Stat(filepath.Join(targetFolder, config.GenesisJSONFile)) fileExists := err == nil From be0ec7dfa9de929722664cc45fcda8e6e2f7f4cb Mon Sep 17 00:00:00 2001 From: algochoi <86622919+algochoi@users.noreply.github.com> Date: Wed, 16 Aug 2023 16:55:18 -0400 Subject: [PATCH 02/15] Fix lint errors --- cmd/goal/network.go | 13 ++++++++++--- netdeploy/networkTemplate.go | 1 + 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/cmd/goal/network.go b/cmd/goal/network.go index 51ad68c31a..f4ae2b3219 100644 --- a/cmd/goal/network.go +++ b/cmd/goal/network.go @@ -124,7 +124,10 @@ var networkCreateCmd = &cobra.Command{ if err != nil { panic(err) } - util.CopyFolder(genesisDir, networkRootDir) + err = util.CopyFolder(genesisDir, networkRootDir) + if err != nil { + panic(err) + } } binDir, err := util.ExeDir() @@ -267,7 +270,8 @@ var networkGenesisCmd = &cobra.Command{ Short: "Creates the genesis.json, root and participation keys for a wallet", Args: validateNoPosArgsFn, Run: func(cmd *cobra.Command, _ []string) { - networkRootDir, err := filepath.Abs(networkRootDir) + var err error + networkRootDir, err = filepath.Abs(networkRootDir) if err != nil { panic(err) } @@ -306,6 +310,9 @@ var networkGenesisCmd = &cobra.Command{ reportErrorf("Error in template validation: %v\n", err) } - gen.GenerateGenesisFiles(template.Genesis, consensus, networkRootDir, os.Stdout) + err = gen.GenerateGenesisFiles(template.Genesis, consensus, networkRootDir, os.Stdout) + if err != nil { + reportErrorf("Cannot write genesis files: %s", err) + } }, } diff --git a/netdeploy/networkTemplate.go b/netdeploy/networkTemplate.go index c6e770c7f2..a036784758 100644 --- a/netdeploy/networkTemplate.go +++ b/netdeploy/networkTemplate.go @@ -169,6 +169,7 @@ func loadTemplate(templateFile string) (NetworkTemplate, error) { return template, err } +// LoadTemplateFromReader loads and decodes a network template func LoadTemplateFromReader(reader io.Reader, template *NetworkTemplate) error { if runtime.GOARCH == "arm" || runtime.GOARCH == "arm64" { From 9cdd3d2e5a172efd86ab720169a7a9a0a3cc7795 Mon Sep 17 00:00:00 2001 From: algochoi <86622919+algochoi@users.noreply.github.com> Date: Fri, 18 Aug 2023 14:20:51 -0400 Subject: [PATCH 03/15] Add e2e test for creating genesis files --- cmd/goal/network.go | 5 +++++ test/scripts/test_private_network.sh | 26 ++++++++++++++++++++++++++ 2 files changed, 31 insertions(+) diff --git a/cmd/goal/network.go b/cmd/goal/network.go index f4ae2b3219..b3aa9debbe 100644 --- a/cmd/goal/network.go +++ b/cmd/goal/network.go @@ -294,6 +294,11 @@ var networkGenesisCmd = &cobra.Command{ templateReader = file } + // Make sure target directory does not exist or is empty + if util.FileExists(networkRootDir) && !util.IsEmpty(networkRootDir) { + reportErrorf(infoNetworkAlreadyExists, networkRootDir) + } + var template netdeploy.NetworkTemplate err = netdeploy.LoadTemplateFromReader(templateReader, &template) if err != nil { diff --git a/test/scripts/test_private_network.sh b/test/scripts/test_private_network.sh index f1adc7f62b..a4ba17021d 100755 --- a/test/scripts/test_private_network.sh +++ b/test/scripts/test_private_network.sh @@ -2,7 +2,10 @@ echo "######################################################################" echo " test_private_network" echo "######################################################################" + set -e +set -o pipefail +set -o nounset # Suppress telemetry reporting for tests export ALGOTEST=1 @@ -11,9 +14,12 @@ export GOPATH=$(go env GOPATH) SRCPATH=$(pwd) NETROOTPATH=${SRCPATH}/tmp/test_private_network +GENFILESPATH=${SRCPATH}/tmp/test_private_network_genesis_files + # purge if it already exists ${GOPATH}/bin/goal network delete -r ${NETROOTPATH} || true rm -rf ${NETROOTPATH} +rm -rf ${GENFILESPATH} ${GOPATH}/bin/goal network create -r ${NETROOTPATH} -n net1 -t ./test/testdata/nettemplates/TwoNodes50Each.json @@ -35,6 +41,26 @@ ${GOPATH}/bin/goal network stop -r ${NETROOTPATH} ${GOPATH}/bin/goal network delete -r ${NETROOTPATH} +# Test that genesis generation works correctly +${GOPATH}/bin/goal network genesis -r ${GENFILESPATH} -t ./test/testdata/nettemplates/TwoNodes50Each.json +# Try importing genesis files from same template +${GOPATH}/bin/goal network create -r ${NETROOTPATH} -n net1 -t ./test/testdata/nettemplates/TwoNodes50Each.json + +${GOPATH}/bin/goal network start -r ${NETROOTPATH} + +${GOPATH}/bin/goal network stop -r ${NETROOTPATH} + +${GOPATH}/bin/goal network delete -r ${NETROOTPATH} + +# Creating genesis files should fail if directory is not empty +# Capture output upon failure +RES=$(${GOPATH}/bin/goal network genesis -r ${GENFILESPATH} 2>&1 || true) +EXPERROR="already exists and is not empty" +if [[ $RES != *"${EXPERROR}"* ]]; then + date '+test_private_network FAIL goal network genesis did not fail even though specified directory was not empty %Y%m%d_%H%M%S' + exit 1 +fi + echo "----------------------------------------------------------------------" echo " DONE: test_private_network" echo "----------------------------------------------------------------------" From b2a5ea7222dc3cf4e704c6f9b057c42f2b520099 Mon Sep 17 00:00:00 2001 From: algochoi <86622919+algochoi@users.noreply.github.com> Date: Fri, 18 Aug 2023 15:54:29 -0400 Subject: [PATCH 04/15] Reuse genesis files --- test/scripts/test_private_network.sh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/scripts/test_private_network.sh b/test/scripts/test_private_network.sh index a4ba17021d..96bceab45e 100755 --- a/test/scripts/test_private_network.sh +++ b/test/scripts/test_private_network.sh @@ -43,8 +43,8 @@ ${GOPATH}/bin/goal network delete -r ${NETROOTPATH} # Test that genesis generation works correctly ${GOPATH}/bin/goal network genesis -r ${GENFILESPATH} -t ./test/testdata/nettemplates/TwoNodes50Each.json -# Try importing genesis files from same template -${GOPATH}/bin/goal network create -r ${NETROOTPATH} -n net1 -t ./test/testdata/nettemplates/TwoNodes50Each.json +# Try importing genesis files from same template -- should reuse the root and partkey files +${GOPATH}/bin/goal network create -r ${NETROOTPATH} -n net1 -t ./test/testdata/nettemplates/TwoNodes50Each.json --genesisDir ${GENFILESPATH} ${GOPATH}/bin/goal network start -r ${NETROOTPATH} From 96ae6fb85837785e0a2b8a2429e30b5ccc06c74a Mon Sep 17 00:00:00 2001 From: algochoi <86622919+algochoi@users.noreply.github.com> Date: Mon, 21 Aug 2023 17:15:46 -0400 Subject: [PATCH 05/15] Some UX improvements to genesis command --- cmd/goal/network.go | 20 +++++++++++++++----- test/scripts/test_private_network.sh | 2 +- 2 files changed, 16 insertions(+), 6 deletions(-) diff --git a/cmd/goal/network.go b/cmd/goal/network.go index b3aa9debbe..83f930a1ad 100644 --- a/cmd/goal/network.go +++ b/cmd/goal/network.go @@ -46,7 +46,6 @@ var genesisDir string func init() { networkCmd.AddCommand(networkCreateCmd) networkCmd.PersistentFlags().StringVarP(&networkRootDir, "rootdir", "r", "", "Root directory for the private network directories") - networkCmd.MarkPersistentFlagRequired("rootdir") networkCreateCmd.Flags().StringVarP(&networkName, "network", "n", "", "Specify the name to use for the private network") networkCreateCmd.Flags().StringVarP(&networkTemplateFile, "template", "t", "", "Specify the path to the template file for the network") @@ -54,18 +53,29 @@ func init() { networkCreateCmd.Flags().BoolVar(&noClean, "noclean", false, "Prevents auto-cleanup on error - for diagnosing problems") networkCreateCmd.Flags().BoolVar(&devModeOverride, "devMode", false, "Forces the configuration to enable DevMode, returns an error if the template is not compatible with DevMode.") networkCreateCmd.Flags().BoolVarP(&startOnCreation, "start", "s", false, "Automatically start the network after creating it.") - networkCreateCmd.Flags().StringVar(&genesisDir, "genesisDir", "", "Specify the path to the directory with existing genesis.json, root and partkeys. By default, the genesis.json and keys will be generated on start. This should only be used on private networks.") + networkCreateCmd.Flags().StringVarP(&genesisDir, "genesisdir", "g", "", "Specify the path to the directory with existing genesis.json, root and partkeys to import into the network directory. By default, the genesis.json and keys will be generated on start. This should only be used on private networks.") + networkCreateCmd.MarkFlagRequired("rootdir") + networkCmd.AddCommand(networkStartCmd) networkStartCmd.Flags().StringVarP(&startNode, "node", "n", "", "Specify the name of a specific node to start") + networkStartCmd.MarkFlagRequired("rootdir") - networkGenesisCmd.Flags().StringVarP(&networkTemplateFile, "template", "t", "", "Specify the path to the template file for the network") - - networkCmd.AddCommand(networkStartCmd) networkCmd.AddCommand(networkRestartCmd) + networkRestartCmd.MarkFlagRequired("rootdir") + networkCmd.AddCommand(networkStopCmd) + networkStopCmd.MarkFlagRequired("rootdir") + networkCmd.AddCommand(networkStatusCmd) + networkStatusCmd.MarkFlagRequired("rootdir") + networkCmd.AddCommand(networkDeleteCmd) + networkDeleteCmd.MarkFlagRequired("rootdir") + networkCmd.AddCommand(networkGenesisCmd) + networkGenesisCmd.Flags().StringVarP(&networkTemplateFile, "template", "t", "", "Specify the path to the template file for the network") + networkGenesisCmd.Flags().StringVarP(&genesisDir, "genesisdir", "g", "", "Specify the path to the directory to export genesis.json, root and partkey files. This should only be used on private networks.") + networkStopCmd.MarkFlagRequired("genesisdir") } var networkCmd = &cobra.Command{ diff --git a/test/scripts/test_private_network.sh b/test/scripts/test_private_network.sh index 96bceab45e..cede841980 100755 --- a/test/scripts/test_private_network.sh +++ b/test/scripts/test_private_network.sh @@ -44,7 +44,7 @@ ${GOPATH}/bin/goal network delete -r ${NETROOTPATH} # Test that genesis generation works correctly ${GOPATH}/bin/goal network genesis -r ${GENFILESPATH} -t ./test/testdata/nettemplates/TwoNodes50Each.json # Try importing genesis files from same template -- should reuse the root and partkey files -${GOPATH}/bin/goal network create -r ${NETROOTPATH} -n net1 -t ./test/testdata/nettemplates/TwoNodes50Each.json --genesisDir ${GENFILESPATH} +${GOPATH}/bin/goal network create -r ${NETROOTPATH} -n net1 -t ./test/testdata/nettemplates/TwoNodes50Each.json --genesisdir ${GENFILESPATH} ${GOPATH}/bin/goal network start -r ${NETROOTPATH} From 196e0d1768034eebc83ba4d15c486050ca6bfa4c Mon Sep 17 00:00:00 2001 From: algochoi <86622919+algochoi@users.noreply.github.com> Date: Thu, 24 Aug 2023 16:28:36 -0400 Subject: [PATCH 06/15] PR comment fixes regarding UX --- cmd/goal/network.go | 19 ++++++++++++++----- test/scripts/test_private_network.sh | 4 ++-- 2 files changed, 16 insertions(+), 7 deletions(-) diff --git a/cmd/goal/network.go b/cmd/goal/network.go index 83f930a1ad..df34c218e6 100644 --- a/cmd/goal/network.go +++ b/cmd/goal/network.go @@ -75,7 +75,12 @@ func init() { networkCmd.AddCommand(networkGenesisCmd) networkGenesisCmd.Flags().StringVarP(&networkTemplateFile, "template", "t", "", "Specify the path to the template file for the network") networkGenesisCmd.Flags().StringVarP(&genesisDir, "genesisdir", "g", "", "Specify the path to the directory to export genesis.json, root and partkey files. This should only be used on private networks.") - networkStopCmd.MarkFlagRequired("genesisdir") + networkGenesisCmd.MarkFlagRequired("genesisdir") + // Hide rootdir flag as it is unused and will error if used with this command. + networkGenesisCmd.SetHelpFunc(func(command *cobra.Command, strings []string) { + _ = command.Flags().MarkHidden("rootdir") + command.Parent().HelpFunc()(command, strings) + }) } var networkCmd = &cobra.Command{ @@ -281,7 +286,11 @@ var networkGenesisCmd = &cobra.Command{ Args: validateNoPosArgsFn, Run: func(cmd *cobra.Command, _ []string) { var err error - networkRootDir, err = filepath.Abs(networkRootDir) + if networkRootDir != "" { + reportErrorf("This command does not take a network directory as an argument. Use --genesisdir flag instead.") + } + + genesisDir, err = filepath.Abs(genesisDir) if err != nil { panic(err) } @@ -305,8 +314,8 @@ var networkGenesisCmd = &cobra.Command{ } // Make sure target directory does not exist or is empty - if util.FileExists(networkRootDir) && !util.IsEmpty(networkRootDir) { - reportErrorf(infoNetworkAlreadyExists, networkRootDir) + if util.FileExists(genesisDir) && !util.IsEmpty(genesisDir) { + reportErrorf(infoNetworkAlreadyExists, genesisDir) } var template netdeploy.NetworkTemplate @@ -325,7 +334,7 @@ var networkGenesisCmd = &cobra.Command{ reportErrorf("Error in template validation: %v\n", err) } - err = gen.GenerateGenesisFiles(template.Genesis, consensus, networkRootDir, os.Stdout) + err = gen.GenerateGenesisFiles(template.Genesis, consensus, genesisDir, os.Stdout) if err != nil { reportErrorf("Cannot write genesis files: %s", err) } diff --git a/test/scripts/test_private_network.sh b/test/scripts/test_private_network.sh index cede841980..4f99d5c5d8 100755 --- a/test/scripts/test_private_network.sh +++ b/test/scripts/test_private_network.sh @@ -42,7 +42,7 @@ ${GOPATH}/bin/goal network stop -r ${NETROOTPATH} ${GOPATH}/bin/goal network delete -r ${NETROOTPATH} # Test that genesis generation works correctly -${GOPATH}/bin/goal network genesis -r ${GENFILESPATH} -t ./test/testdata/nettemplates/TwoNodes50Each.json +${GOPATH}/bin/goal network genesis -g ${GENFILESPATH} -t ./test/testdata/nettemplates/TwoNodes50Each.json # Try importing genesis files from same template -- should reuse the root and partkey files ${GOPATH}/bin/goal network create -r ${NETROOTPATH} -n net1 -t ./test/testdata/nettemplates/TwoNodes50Each.json --genesisdir ${GENFILESPATH} @@ -54,7 +54,7 @@ ${GOPATH}/bin/goal network delete -r ${NETROOTPATH} # Creating genesis files should fail if directory is not empty # Capture output upon failure -RES=$(${GOPATH}/bin/goal network genesis -r ${GENFILESPATH} 2>&1 || true) +RES=$(${GOPATH}/bin/goal network genesis -g ${GENFILESPATH} 2>&1 || true) EXPERROR="already exists and is not empty" if [[ $RES != *"${EXPERROR}"* ]]; then date '+test_private_network FAIL goal network genesis did not fail even though specified directory was not empty %Y%m%d_%H%M%S' From f97851b977bef6055afe81527ccc5e2f5c1481f4 Mon Sep 17 00:00:00 2001 From: algochoi <86622919+algochoi@users.noreply.github.com> Date: Fri, 25 Aug 2023 12:05:43 -0400 Subject: [PATCH 07/15] Add private network tests in e2e.sh --- test/scripts/e2e.sh | 4 ++++ test/scripts/test_private_network.sh | 18 +++++++++++------- 2 files changed, 15 insertions(+), 7 deletions(-) diff --git a/test/scripts/e2e.sh b/test/scripts/e2e.sh index 760c3170ad..5f8a5a7cfa 100755 --- a/test/scripts/e2e.sh +++ b/test/scripts/e2e.sh @@ -167,6 +167,10 @@ if [ -z "$E2E_TEST_FILTER" ] || [ "$E2E_TEST_FILTER" == "SCRIPTS" ]; then goal network delete -r $NETWORKDIR + # Run private network tests + ./timeout 200 ./test_private_network.sh $SRCROOT + duration "test_private_network.sh" + KEEP_TEMPS_CMD_STR="" # If the platform is arm64, we want to pass "--keep-temps" into e2e_client_runner.py diff --git a/test/scripts/test_private_network.sh b/test/scripts/test_private_network.sh index 4f99d5c5d8..b4256cfb59 100755 --- a/test/scripts/test_private_network.sh +++ b/test/scripts/test_private_network.sh @@ -1,17 +1,21 @@ #!/usr/bin/env bash + +set -euf -o pipefail + echo "######################################################################" echo " test_private_network" echo "######################################################################" -set -e -set -o pipefail -set -o nounset +if [ "$#" -eq 0 ]; then + echo "Usage: test_private_network.sh " + exit 1 +fi # Suppress telemetry reporting for tests export ALGOTEST=1 export GOPATH=$(go env GOPATH) -SRCPATH=$(pwd) +SRCPATH="$1" NETROOTPATH=${SRCPATH}/tmp/test_private_network GENFILESPATH=${SRCPATH}/tmp/test_private_network_genesis_files @@ -21,7 +25,7 @@ ${GOPATH}/bin/goal network delete -r ${NETROOTPATH} || true rm -rf ${NETROOTPATH} rm -rf ${GENFILESPATH} -${GOPATH}/bin/goal network create -r ${NETROOTPATH} -n net1 -t ./test/testdata/nettemplates/TwoNodes50Each.json +${GOPATH}/bin/goal network create -r ${NETROOTPATH} -n net1 -t ${SRCPATH}/test/testdata/nettemplates/TwoNodes50Each.json ${GOPATH}/bin/goal network start -r ${NETROOTPATH} @@ -42,9 +46,9 @@ ${GOPATH}/bin/goal network stop -r ${NETROOTPATH} ${GOPATH}/bin/goal network delete -r ${NETROOTPATH} # Test that genesis generation works correctly -${GOPATH}/bin/goal network genesis -g ${GENFILESPATH} -t ./test/testdata/nettemplates/TwoNodes50Each.json +${GOPATH}/bin/goal network genesis -g ${GENFILESPATH} -t ${SRCPATH}/test/testdata/nettemplates/TwoNodes50Each.json # Try importing genesis files from same template -- should reuse the root and partkey files -${GOPATH}/bin/goal network create -r ${NETROOTPATH} -n net1 -t ./test/testdata/nettemplates/TwoNodes50Each.json --genesisdir ${GENFILESPATH} +${GOPATH}/bin/goal network create -r ${NETROOTPATH} -n net1 -t ${SRCPATH}/test/testdata/nettemplates/TwoNodes50Each.json --genesisdir ${GENFILESPATH} ${GOPATH}/bin/goal network start -r ${NETROOTPATH} From 5af9189b76bb5726f54fdc641b79012be3175395 Mon Sep 17 00:00:00 2001 From: algochoi <86622919+algochoi@users.noreply.github.com> Date: Fri, 25 Aug 2023 12:59:25 -0400 Subject: [PATCH 08/15] Merge default consensus --- cmd/goal/network.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmd/goal/network.go b/cmd/goal/network.go index df34c218e6..f0472661e8 100644 --- a/cmd/goal/network.go +++ b/cmd/goal/network.go @@ -334,7 +334,7 @@ var networkGenesisCmd = &cobra.Command{ reportErrorf("Error in template validation: %v\n", err) } - err = gen.GenerateGenesisFiles(template.Genesis, consensus, genesisDir, os.Stdout) + err = gen.GenerateGenesisFiles(template.Genesis, config.Consensus.Merge(consensus), genesisDir, os.Stdout) if err != nil { reportErrorf("Cannot write genesis files: %s", err) } From 4ab1aadffa6433499a74bdd425845825546fec29 Mon Sep 17 00:00:00 2001 From: algochoi <86622919+algochoi@users.noreply.github.com> Date: Mon, 28 Aug 2023 13:28:42 -0400 Subject: [PATCH 09/15] Add go test for generating network --- .../features/privatenet/privatenet_test.go | 69 ++++++++++++++++++ test/framework/fixtures/goalFixture.go | 73 +++++++++++++++++++ 2 files changed, 142 insertions(+) create mode 100644 test/e2e-go/features/privatenet/privatenet_test.go diff --git a/test/e2e-go/features/privatenet/privatenet_test.go b/test/e2e-go/features/privatenet/privatenet_test.go new file mode 100644 index 0000000000..3cc5345a2f --- /dev/null +++ b/test/e2e-go/features/privatenet/privatenet_test.go @@ -0,0 +1,69 @@ +// Copyright (C) 2019-2023 Algorand, Inc. +// This file is part of go-algorand +// +// go-algorand is free software: you can redistribute it and/or modify +// it under the terms of the GNU Affero General Public License as +// published by the Free Software Foundation, either version 3 of the +// License, or (at your option) any later version. +// +// go-algorand is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Affero General Public License for more details. +// +// You should have received a copy of the GNU Affero General Public License +// along with go-algorand. If not, see . + +// Check that private networks are started as designed. +package privatenet + +import ( + "os" + "testing" + + "github.com/algorand/go-algorand/test/framework/fixtures" + "github.com/algorand/go-algorand/test/partitiontest" + "github.com/stretchr/testify/require" +) + +// TestPrivateNetworkImportKeys tests that part keys can be exported and +// imported when starting a private network. +func TestPrivateNetworkImportKeys(t *testing.T) { + partitiontest.PartitionTest(t) + + // This test takes 5~10 seconds. + if testing.Short() { + t.Skip() + } + + // First test that keys can be exported by using `goal network genesis ...` + // Don't start up network, just create genesis files. + var goalFixture fixtures.GoalFixture + tmpGenDir := "tmpGen" + tmpNetDir := "tmpNet" + defaultTemplate := "" + os.RemoveAll(tmpGenDir) // clean up any tmp directories + os.RemoveAll(tmpNetDir) // clean up any tmp directories + + _, err := goalFixture.NetworkGenesis(defaultTemplate, tmpGenDir) + require.NoError(t, err) + + // Check that if there is an existing directory with same name, test fails. + errStr, err := goalFixture.NetworkGenesis(defaultTemplate, tmpGenDir) + require.Error(t, err) + require.Contains(t, errStr, "already exists and is not empty") + + // Then try importing files from same template. + err = goalFixture.NetworkCreate(tmpNetDir, "", defaultTemplate, tmpGenDir) + require.NoError(t, err) + + err = goalFixture.NetworkStart(tmpNetDir) + require.NoError(t, err) + + err = goalFixture.NetworkStop(tmpNetDir) + require.NoError(t, err) + + // Clean up + os.RemoveAll(tmpGenDir) + os.RemoveAll(tmpNetDir) +} diff --git a/test/framework/fixtures/goalFixture.go b/test/framework/fixtures/goalFixture.go index ef52b51b63..df24528ea7 100644 --- a/test/framework/fixtures/goalFixture.go +++ b/test/framework/fixtures/goalFixture.go @@ -58,6 +58,10 @@ const ( nodeCmd = "node" startCmd = "start" stopCmd = "stop" + + networkCmd = "network" + genesisCmd = "genesis" + createCmd = "create" ) func (f *GoalFixture) executeCommand(args ...string) (retStdout string, retStderr string, err error) { @@ -71,6 +75,15 @@ func (f *GoalFixture) executeCommand(args ...string) (retStdout string, retStder return } +func (f *GoalFixture) executeRawCommand(args ...string) (retStdout string, retStderr string, err error) { + cmd := filepath.Join(f.binDir, goalCmd) + retStdout, retStderr, err = util.ExecAndCaptureOutput(cmd, args...) + retStdout = strings.TrimRight(retStdout, "\n") + retStderr = strings.TrimRight(retStderr, "\n") + //fmt.Printf("command: %v %v\nret: %v\n", cmd, args, ret) + return +} + // combine the error and the output so that we could return it as a single error object. func combineExecuteError(retStdout string, retStderr string, err error) error { if err == nil { @@ -227,3 +240,63 @@ func (f *GoalFixture) AccountImportRootKey(wallet string, createDefaultUnencrypt _, _, err = f.executeCommand(args...) return } + +// NetworkGenesis exposes the `goal network genesis` command +func (f *GoalFixture) NetworkGenesis(template, genesisdir string) (stdErr string, err error) { + args := []string{ + networkCmd, + genesisCmd, + "-g", + genesisdir, + } + if template != "" { + args = append(args, "-t", template) + } + _, stdErr, err = f.executeRawCommand(args...) + return +} + +// NetworkCreate exposes the `goal network create` command +func (f *GoalFixture) NetworkCreate(networkdir, networkName, template, genesisdir string) (err error) { + args := []string{ + networkCmd, + createCmd, + "-r", + networkdir, + } + if networkName != "" { + args = append(args, "-n", networkName) + } + if template != "" { + args = append(args, "-t", template) + } + if genesisdir != "" { + args = append(args, "-g", genesisdir) + } + _, _, err = f.executeRawCommand(args...) + return +} + +// NetworkStart exposes the `goal network start` command +func (f *GoalFixture) NetworkStart(networkdir string) (err error) { + args := []string{ + networkCmd, + startCmd, + "-r", + networkdir, + } + _, _, err = f.executeRawCommand(args...) + return +} + +// NetworkStop exposes the `goal network stop` command +func (f *GoalFixture) NetworkStop(networkdir string) (err error) { + args := []string{ + networkCmd, + stopCmd, + "-r", + networkdir, + } + _, _, err = f.executeRawCommand(args...) + return +} From c797f87766ad5b06e2c2c6a26fe08c6a5e33ed08 Mon Sep 17 00:00:00 2001 From: algochoi <86622919+algochoi@users.noreply.github.com> Date: Mon, 28 Aug 2023 13:36:40 -0400 Subject: [PATCH 10/15] Change command name to pregen --- cmd/goal/network.go | 14 +++++++------- test/e2e-go/features/privatenet/privatenet_test.go | 6 +++--- test/framework/fixtures/goalFixture.go | 8 ++++---- test/scripts/e2e.sh | 4 ---- test/scripts/test_private_network.sh | 6 +++--- 5 files changed, 17 insertions(+), 21 deletions(-) diff --git a/cmd/goal/network.go b/cmd/goal/network.go index f0472661e8..0b7f61559d 100644 --- a/cmd/goal/network.go +++ b/cmd/goal/network.go @@ -72,12 +72,12 @@ func init() { networkCmd.AddCommand(networkDeleteCmd) networkDeleteCmd.MarkFlagRequired("rootdir") - networkCmd.AddCommand(networkGenesisCmd) - networkGenesisCmd.Flags().StringVarP(&networkTemplateFile, "template", "t", "", "Specify the path to the template file for the network") - networkGenesisCmd.Flags().StringVarP(&genesisDir, "genesisdir", "g", "", "Specify the path to the directory to export genesis.json, root and partkey files. This should only be used on private networks.") - networkGenesisCmd.MarkFlagRequired("genesisdir") + networkCmd.AddCommand(networkPregenCmd) + networkPregenCmd.Flags().StringVarP(&networkTemplateFile, "template", "t", "", "Specify the path to the template file for the network") + networkPregenCmd.Flags().StringVarP(&genesisDir, "genesisdir", "g", "", "Specify the path to the directory to export genesis.json, root and partkey files. This should only be used on private networks.") + networkPregenCmd.MarkFlagRequired("genesisdir") // Hide rootdir flag as it is unused and will error if used with this command. - networkGenesisCmd.SetHelpFunc(func(command *cobra.Command, strings []string) { + networkPregenCmd.SetHelpFunc(func(command *cobra.Command, strings []string) { _ = command.Flags().MarkHidden("rootdir") command.Parent().HelpFunc()(command, strings) }) @@ -280,8 +280,8 @@ var networkDeleteCmd = &cobra.Command{ }, } -var networkGenesisCmd = &cobra.Command{ - Use: "genesis", +var networkPregenCmd = &cobra.Command{ + Use: "pregen", Short: "Creates the genesis.json, root and participation keys for a wallet", Args: validateNoPosArgsFn, Run: func(cmd *cobra.Command, _ []string) { diff --git a/test/e2e-go/features/privatenet/privatenet_test.go b/test/e2e-go/features/privatenet/privatenet_test.go index 3cc5345a2f..279adab2e7 100644 --- a/test/e2e-go/features/privatenet/privatenet_test.go +++ b/test/e2e-go/features/privatenet/privatenet_test.go @@ -36,7 +36,7 @@ func TestPrivateNetworkImportKeys(t *testing.T) { t.Skip() } - // First test that keys can be exported by using `goal network genesis ...` + // First test that keys can be exported by using `goal network pregen ...` // Don't start up network, just create genesis files. var goalFixture fixtures.GoalFixture tmpGenDir := "tmpGen" @@ -45,11 +45,11 @@ func TestPrivateNetworkImportKeys(t *testing.T) { os.RemoveAll(tmpGenDir) // clean up any tmp directories os.RemoveAll(tmpNetDir) // clean up any tmp directories - _, err := goalFixture.NetworkGenesis(defaultTemplate, tmpGenDir) + _, err := goalFixture.NetworkPregen(defaultTemplate, tmpGenDir) require.NoError(t, err) // Check that if there is an existing directory with same name, test fails. - errStr, err := goalFixture.NetworkGenesis(defaultTemplate, tmpGenDir) + errStr, err := goalFixture.NetworkPregen(defaultTemplate, tmpGenDir) require.Error(t, err) require.Contains(t, errStr, "already exists and is not empty") diff --git a/test/framework/fixtures/goalFixture.go b/test/framework/fixtures/goalFixture.go index df24528ea7..ed9ca0fa39 100644 --- a/test/framework/fixtures/goalFixture.go +++ b/test/framework/fixtures/goalFixture.go @@ -60,7 +60,7 @@ const ( stopCmd = "stop" networkCmd = "network" - genesisCmd = "genesis" + pregenCmd = "pregen" createCmd = "create" ) @@ -241,11 +241,11 @@ func (f *GoalFixture) AccountImportRootKey(wallet string, createDefaultUnencrypt return } -// NetworkGenesis exposes the `goal network genesis` command -func (f *GoalFixture) NetworkGenesis(template, genesisdir string) (stdErr string, err error) { +// NetworkPregen exposes the `goal network pregen` command +func (f *GoalFixture) NetworkPregen(template, genesisdir string) (stdErr string, err error) { args := []string{ networkCmd, - genesisCmd, + pregenCmd, "-g", genesisdir, } diff --git a/test/scripts/e2e.sh b/test/scripts/e2e.sh index 5f8a5a7cfa..760c3170ad 100755 --- a/test/scripts/e2e.sh +++ b/test/scripts/e2e.sh @@ -167,10 +167,6 @@ if [ -z "$E2E_TEST_FILTER" ] || [ "$E2E_TEST_FILTER" == "SCRIPTS" ]; then goal network delete -r $NETWORKDIR - # Run private network tests - ./timeout 200 ./test_private_network.sh $SRCROOT - duration "test_private_network.sh" - KEEP_TEMPS_CMD_STR="" # If the platform is arm64, we want to pass "--keep-temps" into e2e_client_runner.py diff --git a/test/scripts/test_private_network.sh b/test/scripts/test_private_network.sh index b4256cfb59..61e78fcbc5 100755 --- a/test/scripts/test_private_network.sh +++ b/test/scripts/test_private_network.sh @@ -46,7 +46,7 @@ ${GOPATH}/bin/goal network stop -r ${NETROOTPATH} ${GOPATH}/bin/goal network delete -r ${NETROOTPATH} # Test that genesis generation works correctly -${GOPATH}/bin/goal network genesis -g ${GENFILESPATH} -t ${SRCPATH}/test/testdata/nettemplates/TwoNodes50Each.json +${GOPATH}/bin/goal network pregen -g ${GENFILESPATH} -t ${SRCPATH}/test/testdata/nettemplates/TwoNodes50Each.json # Try importing genesis files from same template -- should reuse the root and partkey files ${GOPATH}/bin/goal network create -r ${NETROOTPATH} -n net1 -t ${SRCPATH}/test/testdata/nettemplates/TwoNodes50Each.json --genesisdir ${GENFILESPATH} @@ -58,10 +58,10 @@ ${GOPATH}/bin/goal network delete -r ${NETROOTPATH} # Creating genesis files should fail if directory is not empty # Capture output upon failure -RES=$(${GOPATH}/bin/goal network genesis -g ${GENFILESPATH} 2>&1 || true) +RES=$(${GOPATH}/bin/goal network pregen -g ${GENFILESPATH} 2>&1 || true) EXPERROR="already exists and is not empty" if [[ $RES != *"${EXPERROR}"* ]]; then - date '+test_private_network FAIL goal network genesis did not fail even though specified directory was not empty %Y%m%d_%H%M%S' + date '+test_private_network FAIL goal network pregen did not fail even though specified directory was not empty %Y%m%d_%H%M%S' exit 1 fi From 0ee860d69f79361077c9b42d83be2b7571a00528 Mon Sep 17 00:00:00 2001 From: algochoi <86622919+algochoi@users.noreply.github.com> Date: Wed, 30 Aug 2023 10:14:36 -0400 Subject: [PATCH 11/15] Revert private network script test --- test/scripts/test_private_network.sh | 32 ++-------------------------- 1 file changed, 2 insertions(+), 30 deletions(-) diff --git a/test/scripts/test_private_network.sh b/test/scripts/test_private_network.sh index 61e78fcbc5..72f0bd9160 100755 --- a/test/scripts/test_private_network.sh +++ b/test/scripts/test_private_network.sh @@ -6,26 +6,18 @@ echo "######################################################################" echo " test_private_network" echo "######################################################################" -if [ "$#" -eq 0 ]; then - echo "Usage: test_private_network.sh " - exit 1 -fi - # Suppress telemetry reporting for tests export ALGOTEST=1 export GOPATH=$(go env GOPATH) -SRCPATH="$1" +SRCPATH=$(pwd) NETROOTPATH=${SRCPATH}/tmp/test_private_network -GENFILESPATH=${SRCPATH}/tmp/test_private_network_genesis_files - # purge if it already exists ${GOPATH}/bin/goal network delete -r ${NETROOTPATH} || true rm -rf ${NETROOTPATH} -rm -rf ${GENFILESPATH} -${GOPATH}/bin/goal network create -r ${NETROOTPATH} -n net1 -t ${SRCPATH}/test/testdata/nettemplates/TwoNodes50Each.json +${GOPATH}/bin/goal network create -r ${NETROOTPATH} -n net1 -t ./test/testdata/nettemplates/TwoNodes50Each.json ${GOPATH}/bin/goal network start -r ${NETROOTPATH} @@ -45,26 +37,6 @@ ${GOPATH}/bin/goal network stop -r ${NETROOTPATH} ${GOPATH}/bin/goal network delete -r ${NETROOTPATH} -# Test that genesis generation works correctly -${GOPATH}/bin/goal network pregen -g ${GENFILESPATH} -t ${SRCPATH}/test/testdata/nettemplates/TwoNodes50Each.json -# Try importing genesis files from same template -- should reuse the root and partkey files -${GOPATH}/bin/goal network create -r ${NETROOTPATH} -n net1 -t ${SRCPATH}/test/testdata/nettemplates/TwoNodes50Each.json --genesisdir ${GENFILESPATH} - -${GOPATH}/bin/goal network start -r ${NETROOTPATH} - -${GOPATH}/bin/goal network stop -r ${NETROOTPATH} - -${GOPATH}/bin/goal network delete -r ${NETROOTPATH} - -# Creating genesis files should fail if directory is not empty -# Capture output upon failure -RES=$(${GOPATH}/bin/goal network pregen -g ${GENFILESPATH} 2>&1 || true) -EXPERROR="already exists and is not empty" -if [[ $RES != *"${EXPERROR}"* ]]; then - date '+test_private_network FAIL goal network pregen did not fail even though specified directory was not empty %Y%m%d_%H%M%S' - exit 1 -fi - echo "----------------------------------------------------------------------" echo " DONE: test_private_network" echo "----------------------------------------------------------------------" From bd49e532d7872f76a3df8f627f0dba689e5961e1 Mon Sep 17 00:00:00 2001 From: algochoi <86622919+algochoi@users.noreply.github.com> Date: Wed, 30 Aug 2023 10:22:00 -0400 Subject: [PATCH 12/15] Rename genesis to pregen --- cmd/goal/network.go | 28 +++++++++---------- .../features/privatenet/privatenet_test.go | 6 ++-- test/framework/fixtures/goalFixture.go | 10 +++---- 3 files changed, 22 insertions(+), 22 deletions(-) diff --git a/cmd/goal/network.go b/cmd/goal/network.go index 0b7f61559d..efc7117edb 100644 --- a/cmd/goal/network.go +++ b/cmd/goal/network.go @@ -41,7 +41,7 @@ var noImportKeys bool var noClean bool var devModeOverride bool var startOnCreation bool -var genesisDir string +var pregenDir string func init() { networkCmd.AddCommand(networkCreateCmd) @@ -53,7 +53,7 @@ func init() { networkCreateCmd.Flags().BoolVar(&noClean, "noclean", false, "Prevents auto-cleanup on error - for diagnosing problems") networkCreateCmd.Flags().BoolVar(&devModeOverride, "devMode", false, "Forces the configuration to enable DevMode, returns an error if the template is not compatible with DevMode.") networkCreateCmd.Flags().BoolVarP(&startOnCreation, "start", "s", false, "Automatically start the network after creating it.") - networkCreateCmd.Flags().StringVarP(&genesisDir, "genesisdir", "g", "", "Specify the path to the directory with existing genesis.json, root and partkeys to import into the network directory. By default, the genesis.json and keys will be generated on start. This should only be used on private networks.") + networkCreateCmd.Flags().StringVarP(&pregenDir, "pregendir", "p", "", "Specify the path to the directory with pregenerated genesis.json, root and partkeys to import into the network directory. By default, the genesis.json and keys will be generated on start. This should only be used on private networks.") networkCreateCmd.MarkFlagRequired("rootdir") networkCmd.AddCommand(networkStartCmd) @@ -74,8 +74,8 @@ func init() { networkCmd.AddCommand(networkPregenCmd) networkPregenCmd.Flags().StringVarP(&networkTemplateFile, "template", "t", "", "Specify the path to the template file for the network") - networkPregenCmd.Flags().StringVarP(&genesisDir, "genesisdir", "g", "", "Specify the path to the directory to export genesis.json, root and partkey files. This should only be used on private networks.") - networkPregenCmd.MarkFlagRequired("genesisdir") + networkPregenCmd.Flags().StringVarP(&pregenDir, "pregendir", "p", "", "Specify the path to the directory to export genesis.json, root and partkey files. This should only be used on private networks.") + networkPregenCmd.MarkFlagRequired("pregendir") // Hide rootdir flag as it is unused and will error if used with this command. networkPregenCmd.SetHelpFunc(func(command *cobra.Command, strings []string) { _ = command.Flags().MarkHidden("rootdir") @@ -133,13 +133,13 @@ var networkCreateCmd = &cobra.Command{ reportErrorf(infoNetworkAlreadyExists, networkRootDir) } - // If genesisDir is specified, copy files over - if genesisDir != "" { - genesisDir, err = filepath.Abs(genesisDir) + // If pregendir is specified, copy files over + if pregenDir != "" { + pregenDir, err = filepath.Abs(pregenDir) if err != nil { panic(err) } - err = util.CopyFolder(genesisDir, networkRootDir) + err = util.CopyFolder(pregenDir, networkRootDir) if err != nil { panic(err) } @@ -282,15 +282,15 @@ var networkDeleteCmd = &cobra.Command{ var networkPregenCmd = &cobra.Command{ Use: "pregen", - Short: "Creates the genesis.json, root and participation keys for a wallet", + Short: "Pregenerates the genesis.json, root and participation keys for a wallet", Args: validateNoPosArgsFn, Run: func(cmd *cobra.Command, _ []string) { var err error if networkRootDir != "" { - reportErrorf("This command does not take a network directory as an argument. Use --genesisdir flag instead.") + reportErrorf("This command does not take a network directory as an argument. Use --pregendir flag instead.") } - genesisDir, err = filepath.Abs(genesisDir) + pregenDir, err = filepath.Abs(pregenDir) if err != nil { panic(err) } @@ -314,8 +314,8 @@ var networkPregenCmd = &cobra.Command{ } // Make sure target directory does not exist or is empty - if util.FileExists(genesisDir) && !util.IsEmpty(genesisDir) { - reportErrorf(infoNetworkAlreadyExists, genesisDir) + if util.FileExists(pregenDir) && !util.IsEmpty(pregenDir) { + reportErrorf(infoNetworkAlreadyExists, pregenDir) } var template netdeploy.NetworkTemplate @@ -334,7 +334,7 @@ var networkPregenCmd = &cobra.Command{ reportErrorf("Error in template validation: %v\n", err) } - err = gen.GenerateGenesisFiles(template.Genesis, config.Consensus.Merge(consensus), genesisDir, os.Stdout) + err = gen.GenerateGenesisFiles(template.Genesis, config.Consensus.Merge(consensus), pregenDir, os.Stdout) if err != nil { reportErrorf("Cannot write genesis files: %s", err) } diff --git a/test/e2e-go/features/privatenet/privatenet_test.go b/test/e2e-go/features/privatenet/privatenet_test.go index 279adab2e7..96009df88d 100644 --- a/test/e2e-go/features/privatenet/privatenet_test.go +++ b/test/e2e-go/features/privatenet/privatenet_test.go @@ -32,9 +32,9 @@ func TestPrivateNetworkImportKeys(t *testing.T) { partitiontest.PartitionTest(t) // This test takes 5~10 seconds. - if testing.Short() { - t.Skip() - } + // if testing.Short() { + // t.Skip() + // } // First test that keys can be exported by using `goal network pregen ...` // Don't start up network, just create genesis files. diff --git a/test/framework/fixtures/goalFixture.go b/test/framework/fixtures/goalFixture.go index ed9ca0fa39..cbf8f64884 100644 --- a/test/framework/fixtures/goalFixture.go +++ b/test/framework/fixtures/goalFixture.go @@ -242,12 +242,12 @@ func (f *GoalFixture) AccountImportRootKey(wallet string, createDefaultUnencrypt } // NetworkPregen exposes the `goal network pregen` command -func (f *GoalFixture) NetworkPregen(template, genesisdir string) (stdErr string, err error) { +func (f *GoalFixture) NetworkPregen(template, pregendir string) (stdErr string, err error) { args := []string{ networkCmd, pregenCmd, "-g", - genesisdir, + pregendir, } if template != "" { args = append(args, "-t", template) @@ -257,7 +257,7 @@ func (f *GoalFixture) NetworkPregen(template, genesisdir string) (stdErr string, } // NetworkCreate exposes the `goal network create` command -func (f *GoalFixture) NetworkCreate(networkdir, networkName, template, genesisdir string) (err error) { +func (f *GoalFixture) NetworkCreate(networkdir, networkName, template, pregendir string) (err error) { args := []string{ networkCmd, createCmd, @@ -270,8 +270,8 @@ func (f *GoalFixture) NetworkCreate(networkdir, networkName, template, genesisdi if template != "" { args = append(args, "-t", template) } - if genesisdir != "" { - args = append(args, "-g", genesisdir) + if pregendir != "" { + args = append(args, "-g", pregendir) } _, _, err = f.executeRawCommand(args...) return From 5eae0110c4d8b9979f0f7373b6fd28506d6d3196 Mon Sep 17 00:00:00 2001 From: algochoi <86622919+algochoi@users.noreply.github.com> Date: Wed, 30 Aug 2023 10:48:49 -0400 Subject: [PATCH 13/15] Use tempdir in tests --- .../features/privatenet/privatenet_test.go | 13 +++---------- test/framework/fixtures/goalFixture.go | 17 ++++++----------- 2 files changed, 9 insertions(+), 21 deletions(-) diff --git a/test/e2e-go/features/privatenet/privatenet_test.go b/test/e2e-go/features/privatenet/privatenet_test.go index 96009df88d..5200245369 100644 --- a/test/e2e-go/features/privatenet/privatenet_test.go +++ b/test/e2e-go/features/privatenet/privatenet_test.go @@ -18,7 +18,6 @@ package privatenet import ( - "os" "testing" "github.com/algorand/go-algorand/test/framework/fixtures" @@ -39,11 +38,9 @@ func TestPrivateNetworkImportKeys(t *testing.T) { // First test that keys can be exported by using `goal network pregen ...` // Don't start up network, just create genesis files. var goalFixture fixtures.GoalFixture - tmpGenDir := "tmpGen" - tmpNetDir := "tmpNet" - defaultTemplate := "" - os.RemoveAll(tmpGenDir) // clean up any tmp directories - os.RemoveAll(tmpNetDir) // clean up any tmp directories + tmpGenDir := t.TempDir() + tmpNetDir := t.TempDir() + defaultTemplate := "" // Use the default template by omitting the filepath. _, err := goalFixture.NetworkPregen(defaultTemplate, tmpGenDir) require.NoError(t, err) @@ -62,8 +59,4 @@ func TestPrivateNetworkImportKeys(t *testing.T) { err = goalFixture.NetworkStop(tmpNetDir) require.NoError(t, err) - - // Clean up - os.RemoveAll(tmpGenDir) - os.RemoveAll(tmpNetDir) } diff --git a/test/framework/fixtures/goalFixture.go b/test/framework/fixtures/goalFixture.go index cbf8f64884..81c8aafc18 100644 --- a/test/framework/fixtures/goalFixture.go +++ b/test/framework/fixtures/goalFixture.go @@ -64,24 +64,19 @@ const ( createCmd = "create" ) -func (f *GoalFixture) executeCommand(args ...string) (retStdout string, retStderr string, err error) { +func (f *GoalFixture) executeRawCommand(args ...string) (retStdout string, retStderr string, err error) { + // Executes a command without a specified data directory cmd := filepath.Join(f.binDir, goalCmd) - // We always execute goal against the PrimaryDataDir() instance - args = append(args, "-d", f.PrimaryDataDir()) retStdout, retStderr, err = util.ExecAndCaptureOutput(cmd, args...) retStdout = strings.TrimRight(retStdout, "\n") retStderr = strings.TrimRight(retStderr, "\n") - //fmt.Printf("command: %v %v\nret: %v\n", cmd, args, ret) return } -func (f *GoalFixture) executeRawCommand(args ...string) (retStdout string, retStderr string, err error) { - cmd := filepath.Join(f.binDir, goalCmd) - retStdout, retStderr, err = util.ExecAndCaptureOutput(cmd, args...) - retStdout = strings.TrimRight(retStdout, "\n") - retStderr = strings.TrimRight(retStderr, "\n") - //fmt.Printf("command: %v %v\nret: %v\n", cmd, args, ret) - return +func (f *GoalFixture) executeCommand(args ...string) (retStdout string, retStderr string, err error) { + // We always execute goal against the PrimaryDataDir() instance + args = append(args, "-d", f.PrimaryDataDir()) + return f.executeRawCommand(args...) } // combine the error and the output so that we could return it as a single error object. From 0a989a2f9cb86431610ba0022bb7dd1d752df1b4 Mon Sep 17 00:00:00 2001 From: algochoi <86622919+algochoi@users.noreply.github.com> Date: Wed, 30 Aug 2023 11:12:04 -0400 Subject: [PATCH 14/15] Fix flags in testing utility --- test/framework/fixtures/goalFixture.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/framework/fixtures/goalFixture.go b/test/framework/fixtures/goalFixture.go index 81c8aafc18..69e43e784a 100644 --- a/test/framework/fixtures/goalFixture.go +++ b/test/framework/fixtures/goalFixture.go @@ -241,7 +241,7 @@ func (f *GoalFixture) NetworkPregen(template, pregendir string) (stdErr string, args := []string{ networkCmd, pregenCmd, - "-g", + "-p", pregendir, } if template != "" { @@ -266,7 +266,7 @@ func (f *GoalFixture) NetworkCreate(networkdir, networkName, template, pregendir args = append(args, "-t", template) } if pregendir != "" { - args = append(args, "-g", pregendir) + args = append(args, "-p", pregendir) } _, _, err = f.executeRawCommand(args...) return From e054b23bb95c92606c1a204e3972f6c72173782a Mon Sep 17 00:00:00 2001 From: algochoi <86622919+algochoi@users.noreply.github.com> Date: Wed, 30 Aug 2023 11:33:24 -0400 Subject: [PATCH 15/15] Uncomment short test skips --- test/e2e-go/features/privatenet/privatenet_test.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/test/e2e-go/features/privatenet/privatenet_test.go b/test/e2e-go/features/privatenet/privatenet_test.go index 5200245369..312abed618 100644 --- a/test/e2e-go/features/privatenet/privatenet_test.go +++ b/test/e2e-go/features/privatenet/privatenet_test.go @@ -31,9 +31,9 @@ func TestPrivateNetworkImportKeys(t *testing.T) { partitiontest.PartitionTest(t) // This test takes 5~10 seconds. - // if testing.Short() { - // t.Skip() - // } + if testing.Short() { + t.Skip() + } // First test that keys can be exported by using `goal network pregen ...` // Don't start up network, just create genesis files.