From 2fd7c926ed3b6393e013c63385953e2535a82c6d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?H=C3=A5vard=20Anda=20Estensen?= Date: Thu, 21 Apr 2022 22:45:44 +0200 Subject: [PATCH] Use the T.TempDir and B.TempDir to create temp dirs for testing (#10560) * Use t.TempDir and b.TempDir for temporary test dirs * Remove redundant dir cleanup --- beacon-chain/node/node_test.go | 3 -- cmd/beacon-chain/powchain/options_test.go | 29 +++---------------- .../slashing-protection/import_export_test.go | 5 ++-- io/file/fileutil_test.go | 7 +---- tools/http-request-sink/main_test.go | 5 +--- tools/interop/split-keys/main_test.go | 6 +--- validator/db/kv/attester_protection_test.go | 3 +- validator/rpc/auth_token_test.go | 4 --- validator/rpc/wallet_test.go | 10 ------- 9 files changed, 10 insertions(+), 62 deletions(-) diff --git a/beacon-chain/node/node_test.go b/beacon-chain/node/node_test.go index 6156df85f..c6299371a 100644 --- a/beacon-chain/node/node_test.go +++ b/beacon-chain/node/node_test.go @@ -3,7 +3,6 @@ package node import ( "flag" "fmt" - "os" "path/filepath" "testing" @@ -41,7 +40,6 @@ func TestNodeClose_OK(t *testing.T) { node.Close() require.LogsContain(t, hook, "Stopping beacon node") - require.NoError(t, os.RemoveAll(tmp)) } // TestClearDB tests clearing the database @@ -67,5 +65,4 @@ func TestClearDB(t *testing.T) { require.NoError(t, err) require.LogsContain(t, hook, "Removing database") - require.NoError(t, os.RemoveAll(tmp)) } diff --git a/cmd/beacon-chain/powchain/options_test.go b/cmd/beacon-chain/powchain/options_test.go index b26ab3182..645032ca8 100644 --- a/cmd/beacon-chain/powchain/options_test.go +++ b/cmd/beacon-chain/powchain/options_test.go @@ -3,7 +3,6 @@ package powchaincmd import ( "flag" "fmt" - "os" "path/filepath" "testing" @@ -53,13 +52,8 @@ func Test_parseJWTSecretFromFile(t *testing.T) { t.Run("empty string in file", func(t *testing.T) { app := cli.App{} set := flag.NewFlagSet("test", 0) - fullPath := filepath.Join(os.TempDir(), "foohex") + fullPath := filepath.Join(t.TempDir(), "foohex") require.NoError(t, file.WriteFile(fullPath, []byte{})) - t.Cleanup(func() { - if err := os.RemoveAll(fullPath); err != nil { - t.Fatalf("Could not delete temp dir: %v", err) - } - }) set.String(flags.ExecutionJWTSecretFlag.Name, fullPath, "") ctx := cli.NewContext(&app, set, nil) _, err := parseJWTSecretFromFile(ctx) @@ -68,15 +62,10 @@ func Test_parseJWTSecretFromFile(t *testing.T) { t.Run("less than 32 bytes", func(t *testing.T) { app := cli.App{} set := flag.NewFlagSet("test", 0) - fullPath := filepath.Join(os.TempDir(), "foohex") + fullPath := filepath.Join(t.TempDir(), "foohex") secret := bytesutil.PadTo([]byte("foo"), 31) hexData := fmt.Sprintf("%#x", secret) require.NoError(t, file.WriteFile(fullPath, []byte(hexData))) - t.Cleanup(func() { - if err := os.RemoveAll(fullPath); err != nil { - t.Fatalf("Could not delete temp dir: %v", err) - } - }) set.String(flags.ExecutionJWTSecretFlag.Name, fullPath, "") ctx := cli.NewContext(&app, set, nil) _, err := parseJWTSecretFromFile(ctx) @@ -85,14 +74,9 @@ func Test_parseJWTSecretFromFile(t *testing.T) { t.Run("bad data", func(t *testing.T) { app := cli.App{} set := flag.NewFlagSet("test", 0) - fullPath := filepath.Join(os.TempDir(), "foohex") + fullPath := filepath.Join(t.TempDir(), "foohex") secret := []byte("foo") require.NoError(t, file.WriteFile(fullPath, secret)) - t.Cleanup(func() { - if err := os.RemoveAll(fullPath); err != nil { - t.Fatalf("Could not delete temp dir: %v", err) - } - }) set.String(flags.ExecutionJWTSecretFlag.Name, fullPath, "") ctx := cli.NewContext(&app, set, nil) _, err := parseJWTSecretFromFile(ctx) @@ -101,15 +85,10 @@ func Test_parseJWTSecretFromFile(t *testing.T) { t.Run("correct format", func(t *testing.T) { app := cli.App{} set := flag.NewFlagSet("test", 0) - fullPath := filepath.Join(os.TempDir(), "foohex") + fullPath := filepath.Join(t.TempDir(), "foohex") secret := bytesutil.ToBytes32([]byte("foo")) secretHex := fmt.Sprintf("%#x", secret) require.NoError(t, file.WriteFile(fullPath, []byte(secretHex))) - t.Cleanup(func() { - if err := os.RemoveAll(fullPath); err != nil { - t.Fatalf("Could not delete temp dir: %v", err) - } - }) set.String(flags.ExecutionJWTSecretFlag.Name, fullPath, "") ctx := cli.NewContext(&app, set, nil) got, err := parseJWTSecretFromFile(ctx) diff --git a/cmd/validator/slashing-protection/import_export_test.go b/cmd/validator/slashing-protection/import_export_test.go index 147230f7e..6e128c12a 100644 --- a/cmd/validator/slashing-protection/import_export_test.go +++ b/cmd/validator/slashing-protection/import_export_test.go @@ -3,7 +3,6 @@ package historycmd import ( "encoding/json" "flag" - "os" "path/filepath" "testing" @@ -38,7 +37,7 @@ func setupCliCtx( func TestImportExportSlashingProtectionCli_RoundTrip(t *testing.T) { numValidators := 10 - outputPath := filepath.Join(os.TempDir(), "slashing-exports") + outputPath := filepath.Join(t.TempDir(), "slashing-exports") err := file.MkdirAll(outputPath) require.NoError(t, err) protectionFileName := "slashing_history_import.json" @@ -111,7 +110,7 @@ func TestImportExportSlashingProtectionCli_RoundTrip(t *testing.T) { func TestImportExportSlashingProtectionCli_EmptyData(t *testing.T) { numValidators := 10 - outputPath := filepath.Join(os.TempDir(), "slashing-exports") + outputPath := filepath.Join(t.TempDir(), "slashing-exports") err := file.MkdirAll(outputPath) require.NoError(t, err) protectionFileName := "slashing_history_import.json" diff --git a/io/file/fileutil_test.go b/io/file/fileutil_test.go index 69c5ac1ab..6a3de9169 100644 --- a/io/file/fileutil_test.go +++ b/io/file/fileutil_test.go @@ -413,13 +413,8 @@ func TestHasReadWritePermissions(t *testing.T) { } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - fullPath := filepath.Join(os.TempDir(), tt.args.itemPath) + fullPath := filepath.Join(t.TempDir(), tt.args.itemPath) require.NoError(t, os.WriteFile(fullPath, []byte("foo"), tt.args.perms)) - t.Cleanup(func() { - if err := os.RemoveAll(fullPath); err != nil { - t.Fatalf("Could not delete temp dir: %v", err) - } - }) got, err := file.HasReadWritePermissions(fullPath) if (err != nil) != tt.wantErr { t.Errorf("HasReadWritePermissions() error = %v, wantErr %v", err, tt.wantErr) diff --git a/tools/http-request-sink/main_test.go b/tools/http-request-sink/main_test.go index 5ee840d59..115564ebb 100644 --- a/tools/http-request-sink/main_test.go +++ b/tools/http-request-sink/main_test.go @@ -20,10 +20,7 @@ type sampleRPCRequest struct { } func Test_parseAndCaptureRequest(t *testing.T) { - tmpFile := filepath.Join(os.TempDir(), "faketest.log") - t.Cleanup(func() { - require.NoError(t, os.RemoveAll(tmpFile)) - }) + tmpFile := filepath.Join(t.TempDir(), "faketest.log") body := &sampleRPCRequest{ Name: "eth2", ETHMethod: "eth2_produceBlock", diff --git a/tools/interop/split-keys/main_test.go b/tools/interop/split-keys/main_test.go index f13817804..0c51f3c74 100644 --- a/tools/interop/split-keys/main_test.go +++ b/tools/interop/split-keys/main_test.go @@ -5,7 +5,6 @@ import ( "context" "encoding/hex" "fmt" - "os" "path/filepath" "strings" "testing" @@ -55,10 +54,7 @@ func Test_generateKeysFromMnemonicList(t *testing.T) { func Test_spreadKeysAcrossImportedWallets(t *testing.T) { walletPassword := "Sr0ngPass0q0z929301" - tmpDir := filepath.Join(os.TempDir(), "testwallets") - t.Cleanup(func() { - require.NoError(t, os.RemoveAll(tmpDir)) - }) + tmpDir := filepath.Join(t.TempDir(), "testwallets") // Spread 5 keys across 5 wallets, meaning there is 1 // key per wallet stored on disk. diff --git a/validator/db/kv/attester_protection_test.go b/validator/db/kv/attester_protection_test.go index 62859717c..f39530af7 100644 --- a/validator/db/kv/attester_protection_test.go +++ b/validator/db/kv/attester_protection_test.go @@ -3,7 +3,6 @@ package kv import ( "context" "fmt" - "os" "path/filepath" "sync" "testing" @@ -501,7 +500,7 @@ func benchCheckSurroundVote( shouldSurround bool, ) { ctx := context.Background() - validatorDB, err := NewKVStore(ctx, filepath.Join(os.TempDir(), "benchsurroundvote"), &Config{ + validatorDB, err := NewKVStore(ctx, filepath.Join(b.TempDir(), "benchsurroundvote"), &Config{ PubKeys: pubKeys, }) require.NoError(b, err, "Failed to instantiate DB") diff --git a/validator/rpc/auth_token_test.go b/validator/rpc/auth_token_test.go index 13d4f17a6..87ec8850b 100644 --- a/validator/rpc/auth_token_test.go +++ b/validator/rpc/auth_token_test.go @@ -16,9 +16,6 @@ import ( func setupWalletDir(t testing.TB) string { walletDir := filepath.Join(t.TempDir(), "wallet") require.NoError(t, os.MkdirAll(walletDir, os.ModePerm)) - t.Cleanup(func() { - require.NoError(t, os.RemoveAll(walletDir)) - }) return walletDir } @@ -102,7 +99,6 @@ func Test_initializeAuthToken(t *testing.T) { // Deleting the auth token and re-initializing means we create a jwt token // and secret from scratch again. - require.NoError(t, os.RemoveAll(walletDir)) srv3 := &Server{} walletDir = setupWalletDir(t) token3, err := srv3.initializeAuthToken(walletDir) diff --git a/validator/rpc/wallet_test.go b/validator/rpc/wallet_test.go index 71d2e718a..61340e617 100644 --- a/validator/rpc/wallet_test.go +++ b/validator/rpc/wallet_test.go @@ -4,7 +4,6 @@ import ( "context" "encoding/json" "fmt" - "os" "path/filepath" "testing" @@ -62,9 +61,6 @@ func TestServer_CreateWallet_Local(t *testing.T) { Keymanager: pb.KeymanagerKind_IMPORTED, WalletPassword: strongPass, } - // We delete the directory at defaultWalletPath as CreateWallet will return an error if it tries to create a wallet - // where a directory already exists - require.NoError(t, os.RemoveAll(defaultWalletPath)) _, err = s.CreateWallet(ctx, req) require.NoError(t, err) @@ -113,9 +109,6 @@ func TestServer_CreateWallet_Local_PasswordTooWeak(t *testing.T) { Keymanager: pb.KeymanagerKind_IMPORTED, WalletPassword: "", // Weak password, empty string } - // We delete the directory at defaultWalletPath as CreateWallet will return an error if it tries to create a wallet - // where a directory already exists - require.NoError(t, os.RemoveAll(defaultWalletPath)) _, err := s.CreateWallet(ctx, req) require.ErrorContains(t, "Password too weak", err) @@ -138,9 +131,6 @@ func TestServer_RecoverWallet_Derived(t *testing.T) { WalletPassword: strongPass, NumAccounts: 0, } - // We delete the directory at defaultWalletPath as RecoverWallet will return an error if it tries to create a wallet - // where a directory already exists - require.NoError(t, os.RemoveAll(localWalletDir)) _, err := s.RecoverWallet(ctx, req) require.ErrorContains(t, "Must create at least 1 validator account", err)