testutil cleanup and reorg (#6658)

Prepare migrating testing auxiliary functions from tests
to testutil.

Remove local duplicates on testutil.WriteToNewTempFile().

Always favor testutil.NewTestCaseDir() over ioutil.TempDir().

Add test cases for the testing auxiliary functions.
This commit is contained in:
Alessio Treglia
2020-07-09 14:21:20 +02:00
committed by GitHub
parent f8df05f6f1
commit a940214a49
44 changed files with 281 additions and 243 deletions
+46
View File
@@ -0,0 +1,46 @@
package testutil
import (
"bytes"
"io/ioutil"
"os"
"strings"
"testing"
"github.com/spf13/cobra"
"github.com/stretchr/testify/require"
)
// NewTestCaseDir creates a new temporary directory for a test case.
// Returns the directory path and a cleanup function.
// nolint: errcheck
func NewTestCaseDir(t testing.TB) (string, func()) {
dir, err := ioutil.TempDir("", t.Name()+"_")
require.NoError(t, err)
return dir, func() { os.RemoveAll(dir) }
}
// ApplyMockIO replaces stdin/out/err with buffers that can be used during testing.
func ApplyMockIO(c *cobra.Command) (*strings.Reader, *bytes.Buffer, *bytes.Buffer) {
mockIn := strings.NewReader("")
mockOut := bytes.NewBufferString("")
mockErr := bytes.NewBufferString("")
c.SetIn(mockIn)
c.SetOut(mockOut)
c.SetErr(mockErr)
return mockIn, mockOut, mockErr
}
// Write the given string to a new temporary file.
// Returns an open file and a clean up function that
// the caller must call to remove the file when it is
// no longer needed.
func WriteToNewTempFile(t testing.TB, s string) (*os.File, func()) {
fp, err := ioutil.TempFile("", t.Name()+"_")
require.Nil(t, err)
_, err = fp.WriteString(s)
require.Nil(t, err)
return fp, func() { os.Remove(fp.Name()) }
}
+55
View File
@@ -0,0 +1,55 @@
package testutil_test
import (
"io/ioutil"
"testing"
"github.com/spf13/cobra"
"github.com/stretchr/testify/require"
"github.com/cosmos/cosmos-sdk/testutil"
)
func TestNewTestCaseDir(t *testing.T) {
dir1, cleanup1 := testutil.NewTestCaseDir(t)
dir2, cleanup2 := testutil.NewTestCaseDir(t)
require.NotEqual(t, dir1, dir2)
require.DirExists(t, dir1)
require.DirExists(t, dir2)
cleanup1()
require.NoDirExists(t, dir1)
require.DirExists(t, dir2)
cleanup2()
require.NoDirExists(t, dir2)
}
func TestApplyMockIO(t *testing.T) {
cmd := &cobra.Command{}
oldStdin := cmd.InOrStdin()
oldStdout := cmd.OutOrStdout()
oldStderr := cmd.ErrOrStderr()
testutil.ApplyMockIO(cmd)
require.NotEqual(t, cmd.InOrStdin(), oldStdin)
require.NotEqual(t, cmd.OutOrStdout(), oldStdout)
require.NotEqual(t, cmd.ErrOrStderr(), oldStderr)
}
func TestWriteToNewTempFile(t *testing.T) {
tempfile, cleanup := testutil.WriteToNewTempFile(t, "test string")
tempfile.Close()
bs, err := ioutil.ReadFile(tempfile.Name())
require.NoError(t, err)
require.Equal(t, "test string", string(bs))
cleanup()
require.NoFileExists(t, tempfile.Name())
}
+6
View File
@@ -0,0 +1,6 @@
package testutil
const (
// Tests expect a ledger device initialized to the following mnemonic
TestMnemonic = "equip will roof matter pink blind book anxiety banner elbow sun young"
)
+5 -5
View File
@@ -1,14 +1,14 @@
/*
Package testutil implements and exposes a fully operational in-process Tendermint
Package network implements and exposes a fully operational in-process Tendermint
test network that consists of at least one or potentially many validators. This
test network can be used primarily for integration tests or unit test suites.
The testnetwork utilizes SimApp as the ABCI application and uses all the modules
The test network utilizes SimApp as the ABCI application and uses all the modules
defined in the Cosmos SDK. An in-process test network can be configured with any
number of validators as well as account funds and even custom genesis state.
When creating a test network, a series of Validator objects are returned. Each
Validator object has useful information such as their address and pubkey. A
Validator object has useful information such as their address and public key. A
Validator will also provide its RPC, P2P, and API addresses that can be useful
for integration testing. In addition, a Tendermint local RPC client is also provided
which can be handy for making direct RPC calls to Tendermint.
@@ -35,7 +35,7 @@ A typical testing flow might look like the following:
cfg.NumValidators = 1
s.cfg = cfg
s.network = testutil.NewTestNetwork(s.T(), cfg)
s.network = testutil.New(s.T(), cfg)
_, err := s.network.WaitForHeight(1)
s.Require().NoError(err)
@@ -62,4 +62,4 @@ A typical testing flow might look like the following:
suite.Run(t, new(IntegrationTestSuite))
}
*/
package testutil
package network
@@ -1,4 +1,4 @@
package testutil
package network
import (
"bufio"
@@ -144,7 +144,7 @@ type (
}
)
func NewTestNetwork(t *testing.T, cfg Config) *Network {
func New(t *testing.T, cfg Config) *Network {
// only one caller/test can create and use a network at a time
t.Log("acquiring test network lock")
lock.Lock()
@@ -1,22 +1,24 @@
package testutil
package network_test
import (
"testing"
"time"
"github.com/stretchr/testify/suite"
"github.com/cosmos/cosmos-sdk/testutil/network"
)
type IntegrationTestSuite struct {
suite.Suite
network *Network
network *network.Network
}
func (s *IntegrationTestSuite) SetupSuite() {
s.T().Log("setting up integration test suite")
s.network = NewTestNetwork(s.T(), DefaultConfig())
s.network = network.New(s.T(), network.DefaultConfig())
s.Require().NotNil(s.network)
_, err := s.network.WaitForHeight(1)
@@ -1,4 +1,4 @@
package testutil
package network
import (
"path/filepath"