Skip to content

Commit

Permalink
[chore][exporter/file] Do not leak temp files during tests (open-tele…
Browse files Browse the repository at this point in the history
…metry#29554)

Tests are leaking temporary files since they were not created under the
`t.TempDir()`. This change moves all temporary files to `t.TemDir()` and
add the needed `Shutdown` and `Close` calls to ensure that the files are
closed before the test clean-up.

**Testing:**
Local Windows and Linux `make` of `exporter\fileexporter`
  • Loading branch information
pjanotti committed Nov 30, 2023
1 parent 554dee1 commit 451e539
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 6 deletions.
6 changes: 6 additions & 0 deletions exporter/fileexporter/factory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ func TestCreateMetricsExporter(t *testing.T) {
cfg)
assert.NoError(t, err)
require.NotNil(t, exp)
assert.NoError(t, exp.Shutdown(context.Background()))
}

func TestCreateTracesExporter(t *testing.T) {
Expand All @@ -56,6 +57,7 @@ func TestCreateTracesExporter(t *testing.T) {
cfg)
assert.NoError(t, err)
require.NotNil(t, exp)
assert.NoError(t, exp.Shutdown(context.Background()))
}

func TestCreateTracesExporterError(t *testing.T) {
Expand All @@ -80,6 +82,7 @@ func TestCreateLogsExporter(t *testing.T) {
cfg)
assert.NoError(t, err)
require.NotNil(t, exp)
assert.NoError(t, exp.Shutdown(context.Background()))
}

func TestCreateLogsExporterError(t *testing.T) {
Expand Down Expand Up @@ -157,6 +160,9 @@ func TestBuildFileWriter(t *testing.T) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := buildFileWriter(tt.args.cfg)
defer func() {
assert.NoError(t, got.Close())
}()
assert.NoError(t, err)
tt.validate(t, got)
})
Expand Down
8 changes: 2 additions & 6 deletions exporter/fileexporter/file_exporter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"errors"
"io"
"os"
"path/filepath"
"sync"
"testing"
"time"
Expand Down Expand Up @@ -484,12 +485,7 @@ func TestExportMessageAsBuffer(t *testing.T) {

// tempFileName provides a temporary file name for testing.
func tempFileName(t *testing.T) string {
tmpfile, err := os.CreateTemp("", "*")
require.NoError(t, err)
require.NoError(t, tmpfile.Close())
socket := tmpfile.Name()
require.NoError(t, os.Remove(socket))
return socket
return filepath.Join(t.TempDir(), "fileexporter_test.tmp")
}

// errorWriter is an io.Writer that will return an error all ways
Expand Down

0 comments on commit 451e539

Please sign in to comment.