mirror of
https://github.com/tendermint/tendermint.git
synced 2026-01-08 14:21:14 +00:00
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:
@@ -21,9 +21,9 @@ import (
|
||||
)
|
||||
|
||||
func TestGenLoadValidator(t *testing.T) {
|
||||
tempKeyFile, err := os.CreateTemp("", "priv_validator_key_")
|
||||
tempKeyFile, err := os.CreateTemp(t.TempDir(), "priv_validator_key_")
|
||||
require.NoError(t, err)
|
||||
tempStateFile, err := os.CreateTemp("", "priv_validator_state_")
|
||||
tempStateFile, err := os.CreateTemp(t.TempDir(), "priv_validator_state_")
|
||||
require.NoError(t, err)
|
||||
|
||||
privVal, err := GenFilePV(tempKeyFile.Name(), tempStateFile.Name(), "")
|
||||
@@ -44,9 +44,9 @@ func TestResetValidator(t *testing.T) {
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
defer cancel()
|
||||
|
||||
tempKeyFile, err := os.CreateTemp("", "priv_validator_key_")
|
||||
tempKeyFile, err := os.CreateTemp(t.TempDir(), "priv_validator_key_")
|
||||
require.NoError(t, err)
|
||||
tempStateFile, err := os.CreateTemp("", "priv_validator_state_")
|
||||
tempStateFile, err := os.CreateTemp(t.TempDir(), "priv_validator_state_")
|
||||
require.NoError(t, err)
|
||||
|
||||
privVal, err := GenFilePV(tempKeyFile.Name(), tempStateFile.Name(), "")
|
||||
@@ -74,9 +74,9 @@ func TestResetValidator(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestLoadOrGenValidator(t *testing.T) {
|
||||
tempKeyFile, err := os.CreateTemp("", "priv_validator_key_")
|
||||
tempKeyFile, err := os.CreateTemp(t.TempDir(), "priv_validator_key_")
|
||||
require.NoError(t, err)
|
||||
tempStateFile, err := os.CreateTemp("", "priv_validator_state_")
|
||||
tempStateFile, err := os.CreateTemp(t.TempDir(), "priv_validator_state_")
|
||||
require.NoError(t, err)
|
||||
|
||||
tempKeyFilePath := tempKeyFile.Name()
|
||||
@@ -160,9 +160,9 @@ func TestSignVote(t *testing.T) {
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
defer cancel()
|
||||
|
||||
tempKeyFile, err := os.CreateTemp("", "priv_validator_key_")
|
||||
tempKeyFile, err := os.CreateTemp(t.TempDir(), "priv_validator_key_")
|
||||
require.NoError(t, err)
|
||||
tempStateFile, err := os.CreateTemp("", "priv_validator_state_")
|
||||
tempStateFile, err := os.CreateTemp(t.TempDir(), "priv_validator_state_")
|
||||
require.NoError(t, err)
|
||||
|
||||
privVal, err := GenFilePV(tempKeyFile.Name(), tempStateFile.Name(), "")
|
||||
@@ -215,9 +215,9 @@ func TestSignProposal(t *testing.T) {
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
defer cancel()
|
||||
|
||||
tempKeyFile, err := os.CreateTemp("", "priv_validator_key_")
|
||||
tempKeyFile, err := os.CreateTemp(t.TempDir(), "priv_validator_key_")
|
||||
require.NoError(t, err)
|
||||
tempStateFile, err := os.CreateTemp("", "priv_validator_state_")
|
||||
tempStateFile, err := os.CreateTemp(t.TempDir(), "priv_validator_state_")
|
||||
require.NoError(t, err)
|
||||
|
||||
privVal, err := GenFilePV(tempKeyFile.Name(), tempStateFile.Name(), "")
|
||||
@@ -263,9 +263,9 @@ func TestDifferByTimestamp(t *testing.T) {
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
defer cancel()
|
||||
|
||||
tempKeyFile, err := os.CreateTemp("", "priv_validator_key_")
|
||||
tempKeyFile, err := os.CreateTemp(t.TempDir(), "priv_validator_key_")
|
||||
require.NoError(t, err)
|
||||
tempStateFile, err := os.CreateTemp("", "priv_validator_state_")
|
||||
tempStateFile, err := os.CreateTemp(t.TempDir(), "priv_validator_state_")
|
||||
require.NoError(t, err)
|
||||
|
||||
privVal, err := GenFilePV(tempKeyFile.Name(), tempStateFile.Name(), "")
|
||||
|
||||
@@ -23,7 +23,7 @@ func getFreeLocalhostAddrPort(t *testing.T) string {
|
||||
|
||||
func getDialerTestCases(t *testing.T) []dialerTestCase {
|
||||
tcpAddr := getFreeLocalhostAddrPort(t)
|
||||
unixFilePath, err := testUnixAddr()
|
||||
unixFilePath, err := testUnixAddr(t)
|
||||
require.NoError(t, err)
|
||||
unixAddr := fmt.Sprintf("unix://%s", unixFilePath)
|
||||
|
||||
|
||||
@@ -28,14 +28,17 @@ type listenerTestCase struct {
|
||||
|
||||
// testUnixAddr will attempt to obtain a platform-independent temporary file
|
||||
// name for a Unix socket
|
||||
func testUnixAddr() (string, error) {
|
||||
f, err := os.CreateTemp("", "tendermint-privval-test-*")
|
||||
func testUnixAddr(t *testing.T) (string, error) {
|
||||
// N.B. We can't use t.TempDir here because socket filenames have a
|
||||
// restrictive length limit (~100 bytes) for silly historical reasons.
|
||||
f, err := os.CreateTemp("", "tendermint-privval-test-*.sock")
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
addr := f.Name()
|
||||
f.Close()
|
||||
os.Remove(addr)
|
||||
os.Remove(addr) // remove so the test can bind it
|
||||
t.Cleanup(func() { os.Remove(addr) }) // clean up after the test
|
||||
return addr, nil
|
||||
}
|
||||
|
||||
@@ -56,7 +59,7 @@ func tcpListenerTestCase(t *testing.T, timeoutAccept, timeoutReadWrite time.Dura
|
||||
}
|
||||
|
||||
func unixListenerTestCase(t *testing.T, timeoutAccept, timeoutReadWrite time.Duration) listenerTestCase {
|
||||
addr, err := testUnixAddr()
|
||||
addr, err := testUnixAddr(t)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user