diff --git a/internal/pkg/agent/application/filelock/locker_test.go b/internal/pkg/agent/application/filelock/locker_test.go index aff860e1fe8..fea473b15df 100644 --- a/internal/pkg/agent/application/filelock/locker_test.go +++ b/internal/pkg/agent/application/filelock/locker_test.go @@ -5,7 +5,6 @@ package filelock import ( - "os" "testing" "github.com/stretchr/testify/assert" @@ -15,8 +14,7 @@ import ( const testLockFile = "test.lock" func TestAppLocker(t *testing.T) { - tmp, _ := os.MkdirTemp("", "locker") - defer os.RemoveAll(tmp) + tmp := t.TempDir() locker1 := NewAppLocker(tmp, testLockFile) locker2 := NewAppLocker(tmp, testLockFile) diff --git a/internal/pkg/agent/application/gateway/fleet/fleet_gateway_test.go b/internal/pkg/agent/application/gateway/fleet/fleet_gateway_test.go index 3cf5be91216..4fcbc51a231 100644 --- a/internal/pkg/agent/application/gateway/fleet/fleet_gateway_test.go +++ b/internal/pkg/agent/application/gateway/fleet/fleet_gateway_test.go @@ -12,7 +12,6 @@ import ( "io" "net/http" "net/url" - "os" "path/filepath" "sync" "testing" @@ -498,8 +497,7 @@ func runFleetGateway(ctx context.Context, g coordinator.FleetGateway) <-chan err } func newStateStore(t *testing.T, log *logger.Logger) *store.StateStore { - dir, err := os.MkdirTemp("", "fleet-gateway-unit-test") - require.NoError(t, err) + dir := t.TempDir() filename := filepath.Join(dir, "state.enc") diskStore, err := storage.NewDiskStore(filename) @@ -507,10 +505,6 @@ func newStateStore(t *testing.T, log *logger.Logger) *store.StateStore { stateStore, err := store.NewStateStore(log, diskStore) require.NoError(t, err) - t.Cleanup(func() { - os.RemoveAll(dir) - }) - return stateStore } diff --git a/internal/pkg/agent/application/secret/secret_test.go b/internal/pkg/agent/application/secret/secret_test.go index 6004205c21b..080adc9844f 100644 --- a/internal/pkg/agent/application/secret/secret_test.go +++ b/internal/pkg/agent/application/secret/secret_test.go @@ -6,7 +6,6 @@ package secret import ( "context" - "os" "path/filepath" "testing" "time" @@ -67,6 +66,4 @@ func TestCreate(t *testing.T) { t.Fatal(err) } } - - os.RemoveAll(filepath.Dir(getTestVaultPath(t))) } diff --git a/internal/pkg/agent/storage/storage_test.go b/internal/pkg/agent/storage/storage_test.go index 0f867ae4df2..9bb1841c7e1 100644 --- a/internal/pkg/agent/storage/storage_test.go +++ b/internal/pkg/agent/storage/storage_test.go @@ -28,10 +28,9 @@ func TestReplaceOrRollbackStore(t *testing.T) { failure := NewHandlerStore(func(_ io.Reader) error { return errors.New("fail") }) t.Run("when the save is successful with target and source don't match", func(t *testing.T) { - target, err := genFile(oldContent) + target, err := genFile(t, oldContent) require.NoError(t, err) dir := filepath.Dir(target) - defer os.RemoveAll(dir) requireFilesCount(t, dir, 1) @@ -53,10 +52,9 @@ func TestReplaceOrRollbackStore(t *testing.T) { }) t.Run("when save is not successful", func(t *testing.T) { - target, err := genFile(oldContent) + target, err := genFile(t, oldContent) require.NoError(t, err) dir := filepath.Dir(target) - defer os.RemoveAll(dir) requireFilesCount(t, dir, 1) @@ -77,10 +75,9 @@ func TestReplaceOrRollbackStore(t *testing.T) { }) t.Run("when save is successful with target and source content match", func(t *testing.T) { - target, err := genFile(replaceWith) + target, err := genFile(t, replaceWith) require.NoError(t, err) dir := filepath.Dir(target) - defer os.RemoveAll(dir) requireFilesCount(t, dir, 1) @@ -104,11 +101,10 @@ func TestReplaceOrRollbackStore(t *testing.T) { t.Run("when replace is skipped due to target already containing source content", func(t *testing.T) { yamlTarget := []byte("fleet:\n enabled: true\nother: value\n") yamlReplaceWith := []byte("#This comment is left out\nfleet:\n enabled: true\n") - target, err := genFile(yamlTarget) + target, err := genFile(t, yamlTarget) require.NoError(t, err) dir := filepath.Dir(target) - defer os.RemoveAll(dir) requireFilesCount(t, dir, 1) @@ -141,9 +137,8 @@ func TestReplaceOrRollbackStore(t *testing.T) { func TestDiskStore(t *testing.T) { t.Run("when the target file already exists", func(t *testing.T) { - target, err := genFile([]byte("hello world")) + target, err := genFile(t, []byte("hello world")) require.NoError(t, err) - defer os.Remove(target) d, err := NewDiskStore(target) require.NoError(t, err) @@ -159,9 +154,7 @@ func TestDiskStore(t *testing.T) { }) t.Run("when the target do no exist", func(t *testing.T) { - dir, err := os.MkdirTemp("", "configs") - require.NoError(t, err) - defer os.Remove(dir) + dir := t.TempDir() target := filepath.Join(dir, "hello.txt") d, err := NewDiskStore(target) @@ -180,7 +173,7 @@ func TestDiskStore(t *testing.T) { t.Run("return an io.ReadCloser to the target file", func(t *testing.T) { msg := []byte("bonjour la famille") - target, err := genFile(msg) + target, err := genFile(t, msg) require.NoError(t, err) d, err := NewDiskStore(target) @@ -197,11 +190,8 @@ func TestDiskStore(t *testing.T) { }) } -func genFile(b []byte) (string, error) { - dir, err := os.MkdirTemp("", "configs") - if err != nil { - return "", err - } +func genFile(t *testing.T, b []byte) (string, error) { + dir := t.TempDir() f, err := os.CreateTemp(dir, "config-") if err != nil { diff --git a/internal/pkg/config/config_test.go b/internal/pkg/config/config_test.go index 00ebbf0b6e7..cfbf8c76394 100644 --- a/internal/pkg/config/config_test.go +++ b/internal/pkg/config/config_test.go @@ -42,9 +42,7 @@ func TestInputsResolveNOOP(t *testing.T) { }, } - tmp, err := os.MkdirTemp("", "config") - require.NoError(t, err) - defer os.RemoveAll(tmp) + tmp := t.TempDir() cfgPath := filepath.Join(tmp, "config.yml") dumpToYAML(t, cfgPath, contents) @@ -95,9 +93,7 @@ func TestCommaParsing(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{}{ diff --git a/internal/pkg/dir/discover_test.go b/internal/pkg/dir/discover_test.go index 3a86401bbd8..476f4122f23 100644 --- a/internal/pkg/dir/discover_test.go +++ b/internal/pkg/dir/discover_test.go @@ -58,8 +58,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) diff --git a/internal/pkg/filewatcher/watcher_test.go b/internal/pkg/filewatcher/watcher_test.go index 05fa134cc4d..ac76060445a 100644 --- a/internal/pkg/filewatcher/watcher_test.go +++ b/internal/pkg/filewatcher/watcher_test.go @@ -23,9 +23,7 @@ func TestWatch(t *testing.T) { })) t.Run("newly added files are discovered", withWatch(func(t *testing.T, w *Watch) { - tmp, err := os.MkdirTemp("", "watch") - require.NoError(t, err) - defer os.RemoveAll(tmp) + tmp := t.TempDir() path := filepath.Join(tmp, "hello.txt") empty, err := os.Create(path) @@ -42,9 +40,7 @@ func TestWatch(t *testing.T) { })) t.Run("ignore old files", withWatch(func(t *testing.T, w *Watch) { - tmp, err := os.MkdirTemp("", "watch") - require.NoError(t, err) - defer os.RemoveAll(tmp) + tmp := t.TempDir() path := filepath.Join(tmp, "hello.txt") empty, err := os.Create(path) @@ -67,9 +63,7 @@ func TestWatch(t *testing.T) { })) t.Run("can unwatch a watched file", withWatch(func(t *testing.T, w *Watch) { - tmp, err := os.MkdirTemp("", "watch") - require.NoError(t, err) - defer os.RemoveAll(tmp) + tmp := t.TempDir() path := filepath.Join(tmp, "hello.txt") empty, err := os.Create(path) @@ -96,7 +90,7 @@ func TestWatch(t *testing.T) { w.Unwatch(path) // Add new content to the file. - os.WriteFile(path, []byte("heeeelo"), 0644) + assert.NoError(t, os.WriteFile(path, []byte("heeeelo"), 0644)) // Should not find the file. r, u, err = w.scan() @@ -106,9 +100,7 @@ func TestWatch(t *testing.T) { })) t.Run("can returns the list of watched files", withWatch(func(t *testing.T, w *Watch) { - tmp, err := os.MkdirTemp("", "watch") - require.NoError(t, err) - defer os.RemoveAll(tmp) + tmp := t.TempDir() path := filepath.Join(tmp, "hello.txt") empty, err := os.Create(path) @@ -124,9 +116,7 @@ func TestWatch(t *testing.T) { })) t.Run("update returns updated, unchanged and watched files", withWatch(func(t *testing.T, w *Watch) { - tmp, err := os.MkdirTemp("", "watch") - require.NoError(t, err) - defer os.RemoveAll(tmp) + tmp := t.TempDir() path1 := filepath.Join(tmp, "hello-1.txt") empty, err := os.Create(path1) @@ -151,7 +141,8 @@ func TestWatch(t *testing.T) { w.Watch(path3) // Set initial state - w.Update() + _, err = w.Update() + require.NoError(t, err) // Reset watched files. w.Reset() @@ -165,7 +156,8 @@ func TestWatch(t *testing.T) { // Add new content to the file. f, err := os.OpenFile(path3, os.O_APPEND|os.O_WRONLY, 0600) require.NoError(t, err) - f.Write([]byte("more-hello")) + _, err = f.Write([]byte("more-hello")) + require.NoError(t, err) require.NoError(t, f.Sync()) f.Close() @@ -183,9 +175,7 @@ func TestWatch(t *testing.T) { })) t.Run("should cleanup files that disapear", withWatch(func(t *testing.T, w *Watch) { - tmp, err := os.MkdirTemp("", "watch") - require.NoError(t, err) - defer os.RemoveAll(tmp) + tmp := t.TempDir() path1 := filepath.Join(tmp, "hello.txt") empty, err := os.Create(path1) @@ -200,9 +190,7 @@ func TestWatch(t *testing.T) { })) t.Run("should allow to invalidate the cache ", withWatch(func(t *testing.T, w *Watch) { - tmp, err := os.MkdirTemp("", "watch") - require.NoError(t, err) - defer os.RemoveAll(tmp) + tmp := t.TempDir() path1 := filepath.Join(tmp, "hello.txt") empty, err := os.Create(path1) diff --git a/pkg/component/runtime/conn_info_server_test.go b/pkg/component/runtime/conn_info_server_test.go index d1e6d6fbd3c..9978f301e8a 100644 --- a/pkg/component/runtime/conn_info_server_test.go +++ b/pkg/component/runtime/conn_info_server_test.go @@ -11,7 +11,6 @@ import ( "io" "net" "net/url" - "os" "runtime" "syscall" "testing" @@ -92,11 +91,7 @@ func getAddress(dir string, isLocal bool) string { } func runTests(t *testing.T, fn func(*testing.T, string)) { - sockdir, err := os.MkdirTemp("", "") - if err != nil { - t.Fatal(err) - } - defer os.RemoveAll(sockdir) + sockdir := t.TempDir() tests := []struct { name string diff --git a/pkg/packer/packer_test.go b/pkg/packer/packer_test.go index 10682d01410..a391158e823 100644 --- a/pkg/packer/packer_test.go +++ b/pkg/packer/packer_test.go @@ -24,9 +24,7 @@ func TestPacker(t *testing.T) { withFiles := func(test tt, fn func(pattern []string, t *testing.T)) func(t *testing.T) { return func(t *testing.T) { - d, err := os.MkdirTemp("", "packer") - require.NoError(t, err) - defer os.RemoveAll(d) + d := t.TempDir() for f, v := range test.content { path := filepath.Join(d, f)