From 0680e1f1781dc1184182e0ceeda79f36a82a2016 Mon Sep 17 00:00:00 2001 From: Gabriel <45315755+JustAGabriel@users.noreply.github.com> Date: Fri, 27 Oct 2023 20:48:15 +0200 Subject: [PATCH] test: add logging to helper methods --- internal/agent/agent_test.go | 2 +- internal/log/index_test.go | 2 +- internal/log/log_test.go | 2 +- internal/log/segment_test.go | 2 +- internal/log/store_test.go | 2 +- internal/server/server_test.go | 8 ++++---- internal/testutils.go | 14 ++++++++++---- 7 files changed, 19 insertions(+), 13 deletions(-) diff --git a/internal/agent/agent_test.go b/internal/agent/agent_test.go index 70fcd30..4a05c4b 100644 --- a/internal/agent/agent_test.go +++ b/internal/agent/agent_test.go @@ -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 { diff --git a/internal/log/index_test.go b/internal/log/index_test.go index cf96a43..59045f2 100644 --- a/internal/log/index_test.go +++ b/internal/log/index_test.go @@ -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{} diff --git a/internal/log/log_test.go b/internal/log/log_test.go index 25b1988..32266c8 100644 --- a/internal/log/log_test.go +++ b/internal/log/log_test.go @@ -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) diff --git a/internal/log/segment_test.go b/internal/log/segment_test.go index dfcff64..09f4f34 100644 --- a/internal/log/segment_test.go +++ b/internal/log/segment_test.go @@ -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{ diff --git a/internal/log/store_test.go b/internal/log/store_test.go index f964d8e..74bc0e8 100644 --- a/internal/log/store_test.go +++ b/internal/log/store_test.go @@ -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) diff --git a/internal/server/server_test.go b/internal/server/server_test.go index 36c91a5..c03c1e1 100644 --- a/internal/server/server_test.go +++ b/internal/server/server_test.go @@ -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) @@ -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{ @@ -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) diff --git a/internal/testutils.go b/internal/testutils.go index 55e8d23..392400a 100644 --- a/internal/testutils.go +++ b/internal/testutils.go @@ -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) } @@ -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 } @@ -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")