diff --git a/CHANGELOG-developer.next.asciidoc b/CHANGELOG-developer.next.asciidoc index 649cf5cd424..8d9da05617f 100644 --- a/CHANGELOG-developer.next.asciidoc +++ b/CHANGELOG-developer.next.asciidoc @@ -146,6 +146,7 @@ The list below covers the major changes between 7.0.0-rc2 and main only. - Improve compatibility and reduce flakyness of Python tests {pull}31588[31588] - Added `.python-version` file {pull}32323[32323] - Add support for multiple regions in GCP {pull}32964[32964] +- Use `T.TempDir` to create temporary test directory {pull}33082[33082] ==== Deprecated diff --git a/libbeat/publisher/queue/diskqueue/benchmark_test.go b/libbeat/publisher/queue/diskqueue/benchmark_test.go index bf2a2ab1e13..111c849cd42 100644 --- a/libbeat/publisher/queue/diskqueue/benchmark_test.go +++ b/libbeat/publisher/queue/diskqueue/benchmark_test.go @@ -31,7 +31,6 @@ package diskqueue import ( "math/rand" - "os" "testing" "time" @@ -61,7 +60,7 @@ var ( } ) -//makePublisherEvent creates a sample publisher.Event, using a random message from msgs list +// makePublisherEvent creates a sample publisher.Event, using a random message from msgs list func makePublisherEvent() publisher.Event { return publisher.Event{ Content: beat.Event{ @@ -73,7 +72,7 @@ func makePublisherEvent() publisher.Event { } } -//makeMessagesEvent creates a sample *messages.Event, using a random message from msgs list +// makeMessagesEvent creates a sample *messages.Event, using a random message from msgs list func makeMessagesEvent() *messages.Event { return &messages.Event{ Timestamp: timestamppb.New(eventTime), @@ -89,17 +88,13 @@ func makeMessagesEvent() *messages.Event { } } -//setup creates the disk queue, including a temporary directory to +// setup creates the disk queue, including a temporary directory to // hold the queue. Location of the temporary directory is stored in // the queue settings. Call `cleanup` when done with the queue to // close the queue and remove the temp dir. -func setup(encrypt bool, compress bool, protobuf bool) (*diskQueue, queue.Producer) { - dir, err := os.MkdirTemp("", "benchmark") - if err != nil { - panic(err) - } +func setup(b *testing.B, encrypt bool, compress bool, protobuf bool) (*diskQueue, queue.Producer) { s := DefaultSettings() - s.Path = dir + s.Path = b.TempDir() if encrypt { s.EncryptionKey = []byte("testtesttesttest") } @@ -107,21 +102,18 @@ func setup(encrypt bool, compress bool, protobuf bool) (*diskQueue, queue.Produc s.UseProtobuf = protobuf q, err := NewQueue(logp.L(), s) if err != nil { - os.RemoveAll(dir) panic(err) } p := q.Producer(queue.ProducerConfig{}) - return q, p -} -//clean closes the queue and deletes the temporory directory that -// holds the queue. -func cleanup(q *diskQueue) { - err := q.Close() - os.RemoveAll(q.settings.directoryPath()) - if err != nil { - panic(err) - } + b.Cleanup(func() { + err := q.Close() + if err != nil { + panic(err) + } + }) + + return q, p } func publishEvents(p queue.Producer, num int, protobuf bool) { @@ -154,7 +146,7 @@ func getAndAckEvents(q *diskQueue, num_events int, batch_size int) error { } } -//produceAndConsume generates and publishes events in a go routine, in +// produceAndConsume generates and publishes events in a go routine, in // the main go routine it consumes and acks them. This interleaves // publish and consume. func produceAndConsume(p queue.Producer, q *diskQueue, num_events int, batch_size int, protobuf bool) error { @@ -162,14 +154,14 @@ func produceAndConsume(p queue.Producer, q *diskQueue, num_events int, batch_siz return getAndAckEvents(q, num_events, batch_size) } -//produceThenConsume generates and publishes events, when all events +// produceThenConsume generates and publishes events, when all events // are published it consumes and acks them. func produceThenConsume(p queue.Producer, q *diskQueue, num_events int, batch_size int, protobuf bool) error { publishEvents(p, num_events, protobuf) return getAndAckEvents(q, num_events, batch_size) } -//benchmarkQueue is a wrapper for produceAndConsume, it tries to limit +// benchmarkQueue is a wrapper for produceAndConsume, it tries to limit // timers to just produceAndConsume func benchmarkQueue(num_events int, batch_size int, encrypt bool, compress bool, async bool, protobuf bool, b *testing.B) { b.ResetTimer() @@ -178,20 +170,17 @@ func benchmarkQueue(num_events int, batch_size int, encrypt bool, compress bool, for n := 0; n < b.N; n++ { b.StopTimer() rand.Seed(1) - q, p := setup(encrypt, compress, protobuf) + q, p := setup(b, encrypt, compress, protobuf) b.StartTimer() if async { if err = produceAndConsume(p, q, num_events, batch_size, protobuf); err != nil { - cleanup(q) break } } else { if err = produceThenConsume(p, q, num_events, batch_size, protobuf); err != nil { - cleanup(q) break } } - cleanup(q) } if err != nil { b.Errorf("Error producing/consuming events: %v", err) diff --git a/libbeat/publisher/queue/diskqueue/segments_test.go b/libbeat/publisher/queue/diskqueue/segments_test.go index 3aa30adbcae..b80bba22e89 100644 --- a/libbeat/publisher/queue/diskqueue/segments_test.go +++ b/libbeat/publisher/queue/diskqueue/segments_test.go @@ -19,7 +19,6 @@ package diskqueue import ( "io" - "os" "testing" "github.com/stretchr/testify/assert" @@ -57,9 +56,7 @@ func TestSegmentsRoundTrip(t *testing.T) { plaintext: []byte("encryption and compression"), }, } - dir, err := os.MkdirTemp("", t.Name()) - assert.Nil(t, err) - defer os.RemoveAll(dir) + dir := t.TempDir() for name, tc := range tests { dst := make([]byte, len(tc.plaintext)) settings := DefaultSettings() @@ -133,9 +130,7 @@ func TestSegmentReaderSeek(t *testing.T) { plaintexts: [][]byte{[]byte("abc"), []byte("defg")}, }, } - dir, err := os.MkdirTemp("", t.Name()) - assert.Nil(t, err) - defer os.RemoveAll(dir) + dir := t.TempDir() for name, tc := range tests { settings := DefaultSettings() settings.Path = dir @@ -210,9 +205,7 @@ func TestSegmentReaderSeekLocations(t *testing.T) { location: 2, }, } - dir, err := os.MkdirTemp("", t.Name()) - assert.Nil(t, err) - defer os.RemoveAll(dir) + dir := t.TempDir() for name, tc := range tests { settings := DefaultSettings() settings.Path = dir diff --git a/packetbeat/npcap/npcap_test.go b/packetbeat/npcap/npcap_test.go index 4b2d7ebd4c7..bd5c499820b 100644 --- a/packetbeat/npcap/npcap_test.go +++ b/packetbeat/npcap/npcap_test.go @@ -19,7 +19,6 @@ package npcap import ( "context" - "os" "os/exec" "path/filepath" "runtime" @@ -36,12 +35,7 @@ func TestNpcap(t *testing.T) { obs := logp.ObserverLogs() // Working space. - dir, err := os.MkdirTemp("", "packetbeat-npcap-*") - if err != nil { - t.Fatalf("failed to create working directory: %v", err) - } - defer os.RemoveAll(dir) - path := filepath.Join(dir, "installer") + path := filepath.Join(t.TempDir(), "installer") if runtime.GOOS == "windows" { path += ".exe" } diff --git a/x-pack/osquerybeat/internal/osqd/osqueryd_test.go b/x-pack/osquerybeat/internal/osqd/osqueryd_test.go index 420a4612d99..a831001563e 100644 --- a/x-pack/osquerybeat/internal/osqd/osqueryd_test.go +++ b/x-pack/osquerybeat/internal/osqd/osqueryd_test.go @@ -72,15 +72,11 @@ func TestPrepareAutoloadFile(t *testing.T) { validLogger := logp.NewLogger("osqueryd_test") // Prepare the directory with extension - dir, err := os.MkdirTemp("", "") - if err != nil { - t.Fatal(err) - } - defer os.RemoveAll(dir) + dir := t.TempDir() mandatoryExtensionPath := filepath.Join(dir, extensionName) // Write fake extension file for testing - err = ioutil.WriteFile(mandatoryExtensionPath, nil, 0600) + err := ioutil.WriteFile(mandatoryExtensionPath, nil, 0600) if err != nil { t.Fatal(err) } @@ -123,14 +119,7 @@ func TestPrepareAutoloadFile(t *testing.T) { t.Run(tc.Name, func(t *testing.T) { // Setup - dir, err := os.MkdirTemp("", "") - if err != nil { - t.Fatal(err) - } - - defer os.RemoveAll(dir) - - extensionAutoloadPath := filepath.Join(dir, osqueryAutoload) + extensionAutoloadPath := filepath.Join(t.TempDir(), osqueryAutoload) err = ioutil.WriteFile(extensionAutoloadPath, tc.FileContent, 0600) if err != nil {