Clean up temp files more thoroughly after testing. (#7815)

Our test cases spew a lot of files and directories around $TMPDIR.  Make more
thorough use of the testing package's TempDir methods to ensure these are
cleaned up.

In a few cases, this required plumbing test contexts through existing helper
code. In a couple places an explicit path was required, to work around cases
where we do global setup during a TestMain function. Those cases probably
deserve more thorough cleansing (preferably with fire), but for now I have just
worked around it to keep focused on the cleanup.
This commit is contained in:
M. J. Fromberger
2022-02-14 06:32:07 -08:00
committed by GitHub
parent 824960c565
commit 7e09c2ef43
39 changed files with 151 additions and 215 deletions
+15 -21
View File
@@ -2,8 +2,7 @@ package light_test
import (
"context"
stdlog "log"
"os"
"testing"
"time"
dbm "github.com/tendermint/tm-db"
@@ -17,17 +16,17 @@ import (
)
// Manually getting light blocks and verifying them.
func ExampleClient() {
func TestExampleClient(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
conf, err := rpctest.CreateConfig("ExampleClient_VerifyLightBlockAtHeight")
conf, err := rpctest.CreateConfig(t, "ExampleClient_VerifyLightBlockAtHeight")
if err != nil {
stdlog.Fatal(err)
t.Fatal(err)
}
logger, err := log.NewDefaultLogger(log.LogFormatPlain, log.LogLevelInfo)
if err != nil {
stdlog.Fatal(err)
t.Fatal(err)
}
// Start a test application
@@ -35,21 +34,16 @@ func ExampleClient() {
_, closer, err := rpctest.StartTendermint(ctx, conf, app, rpctest.SuppressStdout)
if err != nil {
stdlog.Fatal(err)
t.Fatal(err)
}
defer func() { _ = closer(ctx) }()
dbDir, err := os.MkdirTemp("", "light-client-example")
if err != nil {
stdlog.Fatal(err)
}
defer os.RemoveAll(dbDir)
dbDir := t.TempDir()
chainID := conf.ChainID()
primary, err := httpp.New(chainID, conf.RPC.ListenAddress)
if err != nil {
stdlog.Fatal(err)
t.Fatal(err)
}
// give Tendermint time to generate some blocks
@@ -57,12 +51,12 @@ func ExampleClient() {
block, err := primary.LightBlock(ctx, 2)
if err != nil {
stdlog.Fatal(err)
t.Fatal(err)
}
db, err := dbm.NewGoLevelDB("light-client-db", dbDir)
if err != nil {
stdlog.Fatal(err)
t.Fatal(err)
}
c, err := light.NewClient(ctx,
@@ -78,11 +72,11 @@ func ExampleClient() {
light.Logger(logger),
)
if err != nil {
stdlog.Fatal(err)
t.Fatal(err)
}
defer func() {
if err := c.Cleanup(); err != nil {
stdlog.Fatal(err)
t.Fatal(err)
}
}()
@@ -92,19 +86,19 @@ func ExampleClient() {
// veify the block at height 3
_, err = c.VerifyLightBlockAtHeight(ctx, 3, time.Now())
if err != nil {
stdlog.Fatal(err)
t.Fatal(err)
}
// retrieve light block at height 3
_, err = c.TrustedLightBlock(3)
if err != nil {
stdlog.Fatal(err)
t.Fatal(err)
}
// update to the latest height
lb, err := c.Update(ctx, time.Now())
if err != nil {
stdlog.Fatal(err)
t.Fatal(err)
}
logger.Info("verified light block", "light-block", lb)
+6 -16
View File
@@ -2,7 +2,6 @@ package light_test
import (
"context"
"os"
"testing"
"time"
@@ -28,7 +27,7 @@ func TestClientIntegration_Update(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
conf, err := rpctest.CreateConfig(t.Name())
conf, err := rpctest.CreateConfig(t, t.Name())
require.NoError(t, err)
logger := log.NewTestingLogger(t)
@@ -42,10 +41,7 @@ func TestClientIntegration_Update(t *testing.T) {
// give Tendermint time to generate some blocks
time.Sleep(5 * time.Second)
dbDir, err := os.MkdirTemp("", "light-client-test-update-example")
require.NoError(t, err)
defer os.RemoveAll(dbDir)
dbDir := t.TempDir()
chainID := conf.ChainID()
primary, err := httpp.New(chainID, conf.RPC.ListenAddress)
@@ -91,7 +87,7 @@ func TestClientIntegration_VerifyLightBlockAtHeight(t *testing.T) {
t.Parallel()
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
conf, err := rpctest.CreateConfig(t.Name())
conf, err := rpctest.CreateConfig(t, t.Name())
require.NoError(t, err)
logger := log.NewTestingLogger(t)
@@ -103,10 +99,7 @@ func TestClientIntegration_VerifyLightBlockAtHeight(t *testing.T) {
require.NoError(t, err)
defer func() { require.NoError(t, closer(ctx)) }()
dbDir, err := os.MkdirTemp("", "light-client-test-verify-example")
require.NoError(t, err)
defer os.RemoveAll(dbDir)
dbDir := t.TempDir()
chainID := conf.ChainID()
primary, err := httpp.New(chainID, conf.RPC.ListenAddress)
@@ -171,7 +164,7 @@ func waitForBlock(ctx context.Context, p provider.Provider, height int64) (*type
func TestClientStatusRPC(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
conf, err := rpctest.CreateConfig(t.Name())
conf, err := rpctest.CreateConfig(t, t.Name())
require.NoError(t, err)
// Start a test application
@@ -181,10 +174,7 @@ func TestClientStatusRPC(t *testing.T) {
require.NoError(t, err)
defer func() { require.NoError(t, closer(ctx)) }()
dbDir, err := os.MkdirTemp("", "light-client-test-status-example")
require.NoError(t, err)
t.Cleanup(func() { os.RemoveAll(dbDir) })
dbDir := t.TempDir()
chainID := conf.ChainID()
primary, err := httpp.New(chainID, conf.RPC.ListenAddress)
+1 -1
View File
@@ -35,7 +35,7 @@ func TestNewProvider(t *testing.T) {
func TestProvider(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
cfg, err := rpctest.CreateConfig(t.Name())
cfg, err := rpctest.CreateConfig(t, t.Name())
require.NoError(t, err)
// start a tendermint node in the background to test against