Skip to content

Commit

Permalink
Remove testutil temp file/dir, use testing.TB.TempDir instead (open-t…
Browse files Browse the repository at this point in the history
…elemetry#10862)

Signed-off-by: Bogdan Drutu <[email protected]>
  • Loading branch information
bogdandrutu committed Jun 9, 2022
1 parent 935197f commit 55304f6
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 74 deletions.
29 changes: 3 additions & 26 deletions internal/common/testutil/testutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,12 @@ package testutil // import "github.com/open-telemetry/opentelemetry-collector-co

import (
"net"
"os"
"os/exec"
"runtime"
"strconv"
"strings"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

Expand All @@ -41,7 +39,9 @@ func GetAvailableLocalAddress(t *testing.T) string {
require.NoError(t, err, "Failed to get a free local port")
// There is a possible race if something else takes this same port before
// the test uses it, however, that is unlikely in practice.
defer ln.Close()
defer func() {
require.NoError(t, ln.Close())
}()
return ln.Addr().String()
}

Expand Down Expand Up @@ -111,26 +111,3 @@ func createExclusionsList(exclusionsText string, t *testing.T) []portpair {
}
return exclusions
}

// NewTemporaryFile creates a file that can be used within the scope of the test
// and will be closed then removed from the file system during the test cleanup
func NewTemporaryFile(tb testing.TB) *os.File {
file, err := os.CreateTemp("", "otelcol_defaults_file_exporter_test*.tmp")
require.NoError(tb, err, "Must not error when creating a temporary file")
tb.Cleanup(func() {
assert.NoError(tb, file.Close(), "Must not error when closing the file")
assert.NoError(tb, os.Remove(file.Name()), "Must not fail removing temporary file used for testing")
})
return file
}

// NewTemporaryDirectory creates a new temporary directory that can be used within the scope of
// test or benchmark and the directory will be cleaned up with all files contained within directory.
func NewTemporaryDirectory(tb testing.TB) (absolutePath string) {
name, err := os.MkdirTemp("", "open-telemetry-test-dir-*")
require.NoError(tb, err, "Must not error when creating a test dir")
tb.Cleanup(func() {
assert.NoError(tb, os.RemoveAll(name), "Must not error when removing temporary directory")
})
return name
}
32 changes: 0 additions & 32 deletions internal/common/testutil/testutil_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,9 @@ package testutil

import (
"net"
"os"
"strconv"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

Expand Down Expand Up @@ -75,33 +73,3 @@ Start Port End Port
emptyExclusions := createExclusionsList(emptyExclusionsText, t)
require.Equal(t, len(emptyExclusions), 0)
}

func TestTemporaryFile(t *testing.T) {
var filename string

t.Run("scoped lifetime", func(t *testing.T) {
f := NewTemporaryFile(t)
filename = f.Name()

_, err := os.Stat(filename)
assert.ErrorIs(t, err, nil)
})

_, err := os.Stat(filename)
assert.ErrorIs(t, err, os.ErrNotExist)
}

func TestTemporaryDirectory(t *testing.T) {
var tmp string

t.Run("scoped lifetime", func(t *testing.T) {
tmp = NewTemporaryDirectory(t)

stat, err := os.Stat(tmp)
assert.NoError(t, err)
assert.True(t, stat.IsDir(), "Must have the directory permissions set")
})

_, err := os.Stat(tmp)
assert.ErrorIs(t, err, os.ErrNotExist)
}
11 changes: 4 additions & 7 deletions internal/components/exporters_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package components
import (
"context"
"errors"
"path/filepath"
"runtime"
"testing"

Expand Down Expand Up @@ -86,9 +87,7 @@ func TestDefaultExporters(t *testing.T) {
exporter: "file",
getConfigFn: func() config.Exporter {
cfg := expFactories["file"].CreateDefaultConfig().(*fileexporter.Config)
f := testutil.NewTemporaryFile(t)
assert.NoError(t, f.Close())
cfg.Path = f.Name()
cfg.Path = filepath.Join(t.TempDir(), "random.file")
return cfg
},
},
Expand Down Expand Up @@ -154,7 +153,7 @@ func TestDefaultExporters(t *testing.T) {
exporter: "parquet",
getConfigFn: func() config.Exporter {
cfg := expFactories["parquet"].CreateDefaultConfig().(*parquetexporter.Config)
cfg.Path = testutil.NewTemporaryDirectory(t)
cfg.Path = t.TempDir()
return cfg
},
},
Expand Down Expand Up @@ -326,12 +325,10 @@ func TestDefaultExporters(t *testing.T) {
{
exporter: "f5cloud",
getConfigFn: func() config.Exporter {
f := testutil.NewTemporaryFile(t)

cfg := expFactories["f5cloud"].CreateDefaultConfig().(*f5cloudexporter.Config)
cfg.Endpoint = "http:https://" + endpoint
cfg.Source = "magic-source"
cfg.AuthConfig.CredentialFile = f.Name()
cfg.AuthConfig.CredentialFile = filepath.Join(t.TempDir(), "random.file")

return cfg
},
Expand Down
15 changes: 9 additions & 6 deletions internal/components/extensions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ package components

import (
"context"
"os"
"path/filepath"
"testing"
"time"

Expand Down Expand Up @@ -91,10 +93,12 @@ func TestDefaultExtensions(t *testing.T) {
extension: "basicauth",
getConfigFn: func() config.Extension {
cfg := extFactories["basicauth"].CreateDefaultConfig().(*basicauthextension.Config)
f := testutil.NewTemporaryFile(t)
f.WriteString("username:password")
// No need to clean up, t.TempDir will be deleted entirely.
fileName := filepath.Join(t.TempDir(), "random.file")
require.NoError(t, os.WriteFile(fileName, []byte("username:password"), 0600))

cfg.Htpasswd = &basicauthextension.HtpasswdSettings{
File: f.Name(),
File: fileName,
Inline: "username:password",
}
return cfg
Expand Down Expand Up @@ -181,16 +185,15 @@ func TestDefaultExtensions(t *testing.T) {
getConfigFn: func() config.Extension {
cfg := extFactories["db_storage"].CreateDefaultConfig().(*dbstorage.Config)
cfg.DriverName = "sqlite3"
tempFolder := testutil.NewTemporaryDirectory(t)
cfg.DataSource = tempFolder + "/foo.db"
cfg.DataSource = filepath.Join(t.TempDir(), "foo.db")
return cfg
},
},
{
extension: "file_storage",
getConfigFn: func() config.Extension {
cfg := extFactories["file_storage"].CreateDefaultConfig().(*filestorage.Config)
cfg.Directory = testutil.NewTemporaryDirectory(t)
cfg.Directory = t.TempDir()
return cfg
},
},
Expand Down
5 changes: 2 additions & 3 deletions internal/components/receivers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ package components
import (
"context"
"errors"
"path"
"path/filepath"
"runtime"
"testing"

Expand All @@ -29,7 +29,6 @@ import (
"go.opentelemetry.io/collector/config"
"go.opentelemetry.io/collector/consumer/consumertest"

"github.com/open-telemetry/opentelemetry-collector-contrib/internal/common/testutil"
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/adapter"
"github.com/open-telemetry/opentelemetry-collector-contrib/receiver/carbonreceiver"
"github.com/open-telemetry/opentelemetry-collector-contrib/receiver/filelogreceiver"
Expand Down Expand Up @@ -115,7 +114,7 @@ func TestDefaultReceivers(t *testing.T) {
cfg := rcvrFactories["filelog"].CreateDefaultConfig().(*filelogreceiver.FileLogConfig)
cfg.Input = adapter.InputConfig{
"include": []string{
path.Join(testutil.NewTemporaryDirectory(t), "*"),
filepath.Join(t.TempDir(), "*"),
},
}
return cfg
Expand Down

0 comments on commit 55304f6

Please sign in to comment.