Skip to content

Commit

Permalink
use t.TempDir instead of os.MkdirTemp
Browse files Browse the repository at this point in the history
  • Loading branch information
mauri870 committed Oct 1, 2024
1 parent f310b03 commit 4d5d1da
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 46 deletions.
11 changes: 2 additions & 9 deletions api/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,7 @@ func TestSocket(t *testing.T) {
}

t.Run("socket doesn't exist before", func(t *testing.T) {
tmpDir, err := os.MkdirTemp("", "testsocket")
require.NoError(t, err)
defer os.RemoveAll(tmpDir)

tmpDir := t.TempDir()
sockFile := tmpDir + "/test.sock"

cfg := config.MustNewConfigFrom(map[string]interface{}{
Expand Down Expand Up @@ -101,11 +98,7 @@ func TestSocket(t *testing.T) {
})

t.Run("starting beat and recover a dangling socket file", func(t *testing.T) {
tmpDir, err := os.MkdirTemp("", "testsocket")
require.NoError(t, err)
defer os.RemoveAll(tmpDir)

sockFile := tmpDir + "/test.sock"
sockFile := t.TempDir() + "/test.sock"

// Create the socket before the server.
f, err := os.Create(sockFile)
Expand Down
8 changes: 2 additions & 6 deletions file/helper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,10 @@ import (
)

func TestSafeFileRotateExistingFile(t *testing.T) {
tempdir, err := os.MkdirTemp("", "")
assert.NoError(t, err)
defer func() {
assert.NoError(t, os.RemoveAll(tempdir))
}()
tempdir := t.TempDir()

// create an existing registry file
err = os.WriteFile(filepath.Join(tempdir, "registry"),
err := os.WriteFile(filepath.Join(tempdir, "registry"),
[]byte("existing filebeat"), 0x777)
assert.NoError(t, err)

Expand Down
35 changes: 16 additions & 19 deletions keystore/file_keystore_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ var keyValue = "output.elasticsearch.password"
var secretValue = []byte(",s3cRet~`! @#$%^&*()_-+={[}]|\\:;\"'<,>.?/")

func TestCanCreateAKeyStore(t *testing.T) {
path := GetTemporaryKeystoreFile()
path := GetTemporaryKeystoreFile(t)
defer os.Remove(path)

keyStore, err := NewFileKeystore(path)
Expand All @@ -50,7 +50,7 @@ func TestCanCreateAKeyStore(t *testing.T) {
}

func TestCanReadAnExistingKeyStoreWithEmptyString(t *testing.T) {
path := GetTemporaryKeystoreFile()
path := GetTemporaryKeystoreFile(t)
defer os.Remove(path)

CreateAnExistingKeystore(path)
Expand All @@ -67,7 +67,7 @@ func TestCanReadAnExistingKeyStoreWithEmptyString(t *testing.T) {
}

func TestCanDeleteAKeyFromTheStoreAndPersistChanges(t *testing.T) {
path := GetTemporaryKeystoreFile()
path := GetTemporaryKeystoreFile(t)
defer os.Remove(path)

CreateAnExistingKeystore(path)
Expand Down Expand Up @@ -97,7 +97,7 @@ func TestFilePermissionOnCreate(t *testing.T) {
t.Skip("Permission check is not running on windows")
}

path := GetTemporaryKeystoreFile()
path := GetTemporaryKeystoreFile(t)
defer os.Remove(path)
CreateAnExistingKeystore(path)

Expand All @@ -115,7 +115,7 @@ func TestFilePermissionOnUpdate(t *testing.T) {
t.Skip("Permission check is not running on windows")
}

path := GetTemporaryKeystoreFile()
path := GetTemporaryKeystoreFile(t)
defer os.Remove(path)

keyStore := CreateAnExistingKeystore(path)
Expand All @@ -141,7 +141,7 @@ func TestFilePermissionOnLoadWhenStrictIsOn(t *testing.T) {
t.Skip("Permission check is not running on windows")
}

path := GetTemporaryKeystoreFile()
path := GetTemporaryKeystoreFile(t)
defer os.Remove(path)

// Create a world readable keystore file
Expand All @@ -154,7 +154,7 @@ func TestFilePermissionOnLoadWhenStrictIsOn(t *testing.T) {
}

func TestReturnsUsedKeysInTheStore(t *testing.T) {
path := GetTemporaryKeystoreFile()
path := GetTemporaryKeystoreFile(t)
defer os.Remove(path)

keyStore := CreateAnExistingKeystore(path)
Expand All @@ -170,7 +170,7 @@ func TestReturnsUsedKeysInTheStore(t *testing.T) {
}

func TestCannotDecryptKeyStoreWithWrongPassword(t *testing.T) {
path := GetTemporaryKeystoreFile()
path := GetTemporaryKeystoreFile(t)
defer os.Remove(path)

keyStore, err := NewFileKeystoreWithPassword(path, NewSecureString([]byte("password")))
Expand Down Expand Up @@ -211,7 +211,7 @@ func TestSecretWithASCIIEncodedSecret(t *testing.T) {
}

func TestGetConfig(t *testing.T) {
path := GetTemporaryKeystoreFile()
path := GetTemporaryKeystoreFile(t)
defer os.Remove(path)

keyStore := CreateAnExistingKeystore(path)
Expand Down Expand Up @@ -239,7 +239,7 @@ func TestGetConfig(t *testing.T) {
}

func TestShouldRaiseAndErrorWhenVersionDontMatch(t *testing.T) {
temporaryPath := GetTemporaryKeystoreFile()
temporaryPath := GetTemporaryKeystoreFile(t)
defer os.Remove(temporaryPath)

badVersion := `v2D/EQwnDNO7yZsjsRFVWGgbkZudhPxVhBkaQAVud66+tK4HRdfPrNrNNgSmhioDGrQ0z/VZpvbw68gb0G
Expand All @@ -258,7 +258,7 @@ func TestShouldRaiseAndErrorWhenVersionDontMatch(t *testing.T) {
}

func TestMissingEncryptedBlock(t *testing.T) {
temporaryPath := GetTemporaryKeystoreFile()
temporaryPath := GetTemporaryKeystoreFile(t)
defer os.Remove(temporaryPath)

badVersion := "v1"
Expand All @@ -276,7 +276,7 @@ func TestMissingEncryptedBlock(t *testing.T) {
}

func createAndReadKeystoreSecret(t *testing.T, password []byte, key string, value []byte) {
path := GetTemporaryKeystoreFile()
path := GetTemporaryKeystoreFile(t)
defer os.Remove(path)

keyStore, err := NewFileKeystoreWithPassword(path, NewSecureString(password))
Expand All @@ -298,7 +298,7 @@ func createAndReadKeystoreSecret(t *testing.T, password []byte, key string, valu
}

func createAndReadKeystoreWithPassword(t *testing.T, password []byte) {
path := GetTemporaryKeystoreFile()
path := GetTemporaryKeystoreFile(t)
defer os.Remove(path)

keyStore, err := NewFileKeystoreWithPassword(path, NewSecureString(password))
Expand Down Expand Up @@ -346,12 +346,9 @@ func CreateAnExistingKeystore(path string) Keystore {
}

// GetTemporaryKeystoreFile create a temporary file on disk to save the keystore.
func GetTemporaryKeystoreFile() string {
path, err := os.MkdirTemp("", "testing")
if err != nil {
panic(err)
}
return filepath.Join(path, "keystore")
func GetTemporaryKeystoreFile(t *testing.T) string {
t.Helper()
return filepath.Join(t.TempDir(), "keystore")
}

func TestRandomBytesLength(t *testing.T) {
Expand Down
4 changes: 2 additions & 2 deletions keystore/keystore_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import (
)

func TestResolverWhenTheKeyDoesntExist(t *testing.T) {
path := GetTemporaryKeystoreFile()
path := GetTemporaryKeystoreFile(t)
defer os.Remove(path)

keystore := CreateAnExistingKeystore(path)
Expand All @@ -40,7 +40,7 @@ func TestResolverWhenTheKeyDoesntExist(t *testing.T) {
}

func TestResolverWhenTheKeyExist(t *testing.T) {
path := GetTemporaryKeystoreFile()
path := GetTemporaryKeystoreFile(t)
defer os.Remove(path)

keystore := CreateAnExistingKeystore(path)
Expand Down
10 changes: 2 additions & 8 deletions loader/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,7 @@ func TestInputsResolveNOOP(t *testing.T) {
},
}

tmp, err := os.MkdirTemp("", "config")
require.NoError(t, err)
defer os.RemoveAll(tmp)

cfgPath := filepath.Join(tmp, "config.yml")
cfgPath := filepath.Join(t.TempDir(), "config.yml")
dumpToYAML(t, cfgPath, contents)

cfg, err := LoadFile(cfgPath)
Expand All @@ -86,9 +82,7 @@ func testToMapStr(t *testing.T) {
}

func testLoadFiles(t *testing.T) {
tmp, err := os.MkdirTemp("", "watch")
require.NoError(t, err)
defer os.RemoveAll(tmp)
tmp := t.TempDir()

f1 := filepath.Join(tmp, "1.yml")
dumpToYAML(t, f1, map[string]interface{}{
Expand Down
3 changes: 1 addition & 2 deletions loader/discover_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,7 @@ func TestDiscover(t *testing.T) {

func withFiles(files []string, fn func(dst string, t *testing.T)) func(t *testing.T) {
return func(t *testing.T) {
tmp, _ := os.MkdirTemp("", "watch")
defer os.RemoveAll(tmp)
tmp := t.TempDir()

for _, file := range files {
path := filepath.Join(tmp, file)
Expand Down

0 comments on commit 4d5d1da

Please sign in to comment.