Skip to content

Commit

Permalink
test: use T.TempDir to create temporary test directory (#33082)
Browse files Browse the repository at this point in the history
This commit replaces `os.MkdirTemp` with `t.TempDir` in tests. The
directory created by `t.TempDir` is automatically removed when the test
and all its subtests complete.

Prior to this commit, temporary directory created using `os.MkdirTemp`
needs to be removed manually by calling `os.RemoveAll`, which is omitted
in some tests. The error handling boilerplate e.g.

	defer func() {
		if err := os.RemoveAll(dir); err != nil {
			t.Fatal(err)
		}
	}

is also tedious, but `t.TempDir` handles this for us nicely.

Reference: https://pkg.go.dev/testing#T.TempDir

Signed-off-by: Eng Zer Jun <[email protected]>
  • Loading branch information
Juneezee authored Oct 25, 2022
1 parent 4cac5d8 commit d04da48
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 59 deletions.
1 change: 1 addition & 0 deletions CHANGELOG-developer.next.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
45 changes: 17 additions & 28 deletions libbeat/publisher/queue/diskqueue/benchmark_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ package diskqueue

import (
"math/rand"
"os"
"testing"
"time"

Expand Down Expand Up @@ -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{
Expand All @@ -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),
Expand All @@ -89,39 +88,32 @@ 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")
}
s.UseCompression = compress
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) {
Expand Down Expand Up @@ -154,22 +146,22 @@ 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 {
go publishEvents(p, num_events, protobuf)
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()
Expand All @@ -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)
Expand Down
13 changes: 3 additions & 10 deletions libbeat/publisher/queue/diskqueue/segments_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ package diskqueue

import (
"io"
"os"
"testing"

"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
8 changes: 1 addition & 7 deletions packetbeat/npcap/npcap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ package npcap

import (
"context"
"os"
"os/exec"
"path/filepath"
"runtime"
Expand All @@ -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"
}
Expand Down
17 changes: 3 additions & 14 deletions x-pack/osquerybeat/internal/osqd/osqueryd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit d04da48

Please sign in to comment.