Skip to content

Commit

Permalink
test: add logging to helper methods
Browse files Browse the repository at this point in the history
  • Loading branch information
gm0stache committed Oct 27, 2023
1 parent 9df364a commit 0680e1f
Show file tree
Hide file tree
Showing 7 changed files with 19 additions and 13 deletions.
2 changes: 1 addition & 1 deletion internal/agent/agent_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func TestAgent(t *testing.T) {
bindAddr := fmt.Sprintf("%s:%d", "127.0.0.1", bindPort)
rpcPort := internal.FreePort(t)

dataDir := internal.GetTempDir("agent-test-log")
dataDir := internal.GetTempDir(t, "agent-test-log")

var startJoinAddr []string
if i != 0 {
Expand Down
2 changes: 1 addition & 1 deletion internal/log/index_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
)

func TestIndex(t *testing.T) {
f := internal.GetTempFile("", "index_test")
f := internal.GetTempFile(t, "", "index_test")
defer os.Remove(f.Name())

c := Config{}
Expand Down
2 changes: 1 addition & 1 deletion internal/log/log_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func TestLog(t *testing.T) {

for scenario, fn := range scenarios {
testFn := func(t *testing.T) {
dir := internal.GetTempDir("store-test")
dir := internal.GetTempDir(t, "store-test")
defer os.RemoveAll(dir)

log, err := NewLog(dir, config)
Expand Down
2 changes: 1 addition & 1 deletion internal/log/segment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
)

func TestSegment(t *testing.T) {
dir := internal.GetTempDir("segment-test")
dir := internal.GetTempDir(t, "segment-test")
defer os.RemoveAll(dir)

want := &api.Record{
Expand Down
2 changes: 1 addition & 1 deletion internal/log/store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ func testReadAt(t *testing.T, s *store) {
}

func TestStoreClose(t *testing.T) {
f := internal.GetTempFile("", "store_close_test")
f := internal.GetTempFile(t, "", "store_close_test")
defer os.Remove(f.Name())

s, err := newStore(f)
Expand Down
8 changes: 4 additions & 4 deletions internal/server/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ func setupTest(t *testing.T, fn func(*Config)) (rootClient api.LogClient, nobody
require.NoError(t, err)
serverCreds := credentials.NewTLS(serverTLSConfig)

dir := internal.GetTempDir("server-test")
dir := internal.GetTempDir(t, "server-test")
clog, err := log.NewLog(dir, log.Config{})
require.NoError(t, err)

Expand All @@ -81,10 +81,10 @@ func setupTest(t *testing.T, fn func(*Config)) (rootClient api.LogClient, nobody

var telemetryExporter *exporter.LogExporter
if *debug {
metricsLogFile := internal.GetTempFile("", "metrics-*.log")
metricsLogFile := internal.GetTempFile(t, "", "metrics-*.log")
t.Logf("metrics log file: %q", metricsLogFile.Name())

tracesLogFile := internal.GetTempFile("", "traces-*.log")
tracesLogFile := internal.GetTempFile(t, "", "traces-*.log")
t.Logf("traces log file: %q", metricsLogFile.Name())

telemetryExporter, err = exporter.NewLogExporter(exporter.Options{
Expand Down Expand Up @@ -306,7 +306,7 @@ func TestServerRequiresClientTLSCert(t *testing.T) {
require.NoError(t, err)
serverCreds := credentials.NewTLS(serverTLSConfig)

dir := internal.GetTempDir("server-test")
dir := internal.GetTempDir(t, "server-test")
clog, err := log.NewLog(dir, log.Config{})
require.NoError(t, err)

Expand Down
14 changes: 10 additions & 4 deletions internal/testutils.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@ func testRootDir() string {

// GetTempFile creates a file in the temporary 'dir'.
// If 'dir' is "", a random dir name will be assigned.
func GetTempFile(dir, filename string) *os.File {
func GetTempFile(t *testing.T, dir, filename string) *os.File {
// make sure that a random dir is generated if none specified
if dir == "" {
dirName := randDirName()
dir = GetTempDir(dirName)
dir = GetTempDir(t, dirName)
} else {
dir = path.Join(testRootDir(), dir)
}
Expand All @@ -34,16 +34,20 @@ func GetTempFile(dir, filename string) *os.File {
if err != nil {
panic(err)
}

t.Logf("created file %q", f.Name())
return f
}

// GetTempDir creates a temporary directory 'dir'.
// if the 'dir' string includes '*', those will be replaced with a random string.
func GetTempDir(dir string) string {
func GetTempDir(t *testing.T, dir string) string {
dirPath, err := os.MkdirTemp(testRootDir(), dir)
if err != nil {
panic(err)
}

t.Logf("testutil: created dir %q", dirPath)
return dirPath
}

Expand All @@ -62,7 +66,9 @@ func FreePort(t *testing.T) int {
continue
}

return l.Addr().(*net.TCPAddr).Port
port := l.Addr().(*net.TCPAddr).Port
t.Logf("testutil: returned free TCP port '%d'", port)
return port
}

t.Error("could not determine a free port")
Expand Down

0 comments on commit 0680e1f

Please sign in to comment.