From 2404a267b9447ac8c51560d435ca20c2aaa74311 Mon Sep 17 00:00:00 2001
From: Evgenii Stratonikov <evgeniy@morphbits.ru>
Date: Mon, 24 Oct 2022 15:41:55 +0300
Subject: [PATCH] [#1959] neofs-adm: Move testdata generation to a separate
 function

Later we could make it a separate tests an run neofs-adm tests on CI.

Signed-off-by: Evgenii Stratonikov <evgeniy@morphbits.ru>
---
 .../internal/modules/morph/generate_test.go   |  2 +-
 .../internal/modules/morph/initialize_test.go | 73 +++++++++++--------
 2 files changed, 44 insertions(+), 31 deletions(-)

diff --git a/cmd/neofs-adm/internal/modules/morph/generate_test.go b/cmd/neofs-adm/internal/modules/morph/generate_test.go
index f3acae949..da4a7c738 100644
--- a/cmd/neofs-adm/internal/modules/morph/generate_test.go
+++ b/cmd/neofs-adm/internal/modules/morph/generate_test.go
@@ -112,7 +112,7 @@ func setupTestTerminal(t *testing.T) *bytes.Buffer {
 }
 
 func newTempDir(t *testing.T) string {
-	dir := filepath.Join(os.TempDir(), "neofs-adm.test."+strconv.FormatUint(rand.Uint64(), 10))
+	dir := filepath.Join(t.TempDir(), "neofs-adm.test."+strconv.FormatUint(rand.Uint64(), 10))
 	require.NoError(t, os.Mkdir(dir, os.ModePerm))
 	t.Cleanup(func() {
 		require.NoError(t, os.RemoveAll(dir))
diff --git a/cmd/neofs-adm/internal/modules/morph/initialize_test.go b/cmd/neofs-adm/internal/modules/morph/initialize_test.go
index e4125c933..ef79ca020 100644
--- a/cmd/neofs-adm/internal/modules/morph/initialize_test.go
+++ b/cmd/neofs-adm/internal/modules/morph/initialize_test.go
@@ -16,12 +16,16 @@ import (
 	"gopkg.in/yaml.v3"
 )
 
-const contractsPath = "../../../../../../neofs-contract/neofs-contract-v0.16.0.tar.gz"
+const (
+	contractsPath = "../../../../../../neofs-contract/neofs-contract-v0.16.0.tar.gz"
+	protoFileName = "proto.yml"
+)
 
 func TestInitialize(t *testing.T) {
 	// This test needs neofs-contract tarball, so it is skipped by default.
 	// It is here for performing local testing after the changes.
 	t.Skip()
+
 	t.Run("4 nodes", func(t *testing.T) {
 		testInitialize(t, 4)
 	})
@@ -31,25 +35,43 @@ func TestInitialize(t *testing.T) {
 }
 
 func testInitialize(t *testing.T, committeeSize int) {
-	validatorCount := committeeSize
-
-	walletDir := newTempDir(t)
+	testdataDir := newTempDir(t)
 	v := viper.GetViper()
 
-	v.Set(alphabetWalletsFlag, walletDir)
+	generateTestData(t, testdataDir, committeeSize)
+	v.Set(protoConfigPath, filepath.Join(testdataDir, protoFileName))
 
-	sizeStr := strconv.FormatUint(uint64(committeeSize), 10)
+	// Set to the path or remove the next statement to download from the network.
+	require.NoError(t, initCmd.Flags().Set(contractsInitFlag, contractsPath))
+	v.Set(localDumpFlag, filepath.Join(testdataDir, "out"))
+	v.Set(alphabetWalletsFlag, testdataDir)
+	v.Set(epochDurationInitFlag, 1)
+	v.Set(maxObjectSizeInitFlag, 1024)
+
+	setTestCredentials(v, committeeSize)
+	require.NoError(t, initializeSideChainCmd(initCmd, nil))
+
+	t.Run("force-new-epoch", func(t *testing.T) {
+		require.NoError(t, forceNewEpochCmd(forceNewEpoch, nil))
+	})
+	t.Run("set-config", func(t *testing.T) {
+		require.NoError(t, setConfigCmd(setConfig, []string{"MaintenanceModeAllowed=true"}))
+	})
+}
+
+func generateTestData(t *testing.T, dir string, size int) {
+	v := viper.GetViper()
+	v.Set(alphabetWalletsFlag, dir)
+
+	sizeStr := strconv.FormatUint(uint64(size), 10)
 	require.NoError(t, generateAlphabetCmd.Flags().Set(alphabetSizeFlag, sizeStr))
 
-	for i := 0; i < committeeSize; i++ {
-		v.Set("credentials."+innerring.GlagoliticLetter(i).String(), strconv.FormatUint(uint64(i), 10))
-	}
-	v.Set("credentials.contract", testContractPassword)
+	setTestCredentials(v, size)
 	require.NoError(t, generateAlphabetCreds(generateAlphabetCmd, nil))
 
 	var pubs []string
-	for i := 0; i < committeeSize; i++ {
-		p := filepath.Join(walletDir, innerring.GlagoliticLetter(i).String()+".json")
+	for i := 0; i < size; i++ {
+		p := filepath.Join(dir, innerring.GlagoliticLetter(i).String()+".json")
 		w, err := wallet.NewWalletFromFile(p)
 		require.NoError(t, err, "wallet doesn't exist")
 		for _, acc := range w.Accounts {
@@ -63,7 +85,7 @@ func testInitialize(t *testing.T, committeeSize int) {
 	}
 
 	cfg := config.Config{}
-	cfg.ProtocolConfiguration.ValidatorsCount = validatorCount
+	cfg.ProtocolConfiguration.ValidatorsCount = size
 	cfg.ProtocolConfiguration.SecondsPerBlock = 1
 	cfg.ProtocolConfiguration.StandbyCommittee = pubs // sorted by glagolic letters
 	cfg.ProtocolConfiguration.P2PSigExtensions = true
@@ -71,22 +93,13 @@ func testInitialize(t *testing.T, committeeSize int) {
 	data, err := yaml.Marshal(cfg)
 	require.NoError(t, err)
 
-	protoPath := filepath.Join(walletDir, "proto.yml")
+	protoPath := filepath.Join(dir, protoFileName)
 	require.NoError(t, os.WriteFile(protoPath, data, os.ModePerm))
-
-	v.Set(protoConfigPath, protoPath)
-	// Set to the path or remove the next statement to download from the network.
-	require.NoError(t, initCmd.Flags().Set(contractsInitFlag, contractsPath))
-	v.Set(localDumpFlag, filepath.Join(walletDir, "out"))
-	v.Set(alphabetWalletsFlag, walletDir)
-	v.Set(epochDurationInitFlag, 1)
-	v.Set(maxObjectSizeInitFlag, 1024)
-	require.NoError(t, initializeSideChainCmd(initCmd, nil))
-
-	t.Run("force-new-epoch", func(t *testing.T) {
-		require.NoError(t, forceNewEpochCmd(forceNewEpoch, nil))
-	})
-	t.Run("set-config", func(t *testing.T) {
-		require.NoError(t, setConfigCmd(setConfig, []string{"MaintenanceModeAllowed=true"}))
-	})
+}
+
+func setTestCredentials(v *viper.Viper, size int) {
+	for i := 0; i < size; i++ {
+		v.Set("credentials."+innerring.GlagoliticLetter(i).String(), strconv.FormatUint(uint64(i), 10))
+	}
+	v.Set("credentials.contract", testContractPassword)
 }