diff --git a/clients/cmd/docker-driver/config_test.go b/clients/cmd/docker-driver/config_test.go index b20d40ee23..a3920778b6 100644 --- a/clients/cmd/docker-driver/config_test.go +++ b/clients/cmd/docker-driver/config_test.go @@ -81,7 +81,7 @@ var pipeline = PipelineConfig{ } func Test_parsePipeline(t *testing.T) { - f, err := os.CreateTemp("/tmp", "Test_parsePipeline") + f, err := os.CreateTemp("", "Test_parsePipeline") if err != nil { t.Fatal(err) } diff --git a/clients/cmd/fluent-bit/dque.go b/clients/cmd/fluent-bit/dque.go index ad4ec2318e..f7091de893 100644 --- a/clients/cmd/fluent-bit/dque.go +++ b/clients/cmd/fluent-bit/dque.go @@ -3,6 +3,7 @@ package main import ( "fmt" "os" + "path/filepath" "sync" "time" @@ -25,7 +26,7 @@ type dqueConfig struct { } var defaultDqueConfig = dqueConfig{ - queueDir: "/tmp/flb-storage/loki", + queueDir: filepath.Join(os.TempDir(), "flb-storage/loki"), queueSegmentSize: 500, queueSync: false, queueName: "dque", diff --git a/clients/pkg/promtail/positions/positions_test.go b/clients/pkg/promtail/positions/positions_test.go index 9bbff9fa17..1dce97b08e 100644 --- a/clients/pkg/promtail/positions/positions_test.go +++ b/clients/pkg/promtail/positions/positions_test.go @@ -40,7 +40,7 @@ func TestReadPositionsOK(t *testing.T) { }() yaml := []byte(`positions: - /tmp/random.log: "17623" + /log/path/random.log: "17623" `) err := os.WriteFile(temp, yaml, 0644) if err != nil { @@ -52,7 +52,7 @@ func TestReadPositionsOK(t *testing.T) { }, log.NewNopLogger()) require.NoError(t, err) - require.Equal(t, "17623", pos["/tmp/random.log"]) + require.Equal(t, "17623", pos["/log/path/random.log"]) } func TestReadPositionsEmptyFile(t *testing.T) { @@ -145,7 +145,7 @@ func Test_ReadOnly(t *testing.T) { _ = os.Remove(temp) }() yaml := []byte(`positions: - /tmp/random.log: "17623" + /log/path/random.log: "17623" `) err := os.WriteFile(temp, yaml, 0644) if err != nil { @@ -162,7 +162,7 @@ func Test_ReadOnly(t *testing.T) { defer p.Stop() p.Put("/foo/bar/f", 12132132) p.PutString("/foo/f", "100") - pos, err := p.Get("/tmp/random.log") + pos, err := p.Get("/log/path/random.log") if err != nil { t.Fatal(err) } @@ -176,7 +176,7 @@ func Test_ReadOnly(t *testing.T) { require.NoError(t, err) require.Equal(t, map[string]string{ - "/tmp/random.log": "17623", + "/log/path/random.log": "17623", }, out) } diff --git a/clients/pkg/promtail/promtail_test.go b/clients/pkg/promtail/promtail_test.go index 6f6501b59d..04a61cf25f 100644 --- a/clients/pkg/promtail/promtail_test.go +++ b/clients/pkg/promtail/promtail_test.go @@ -5,7 +5,6 @@ import ( "fmt" "io" "math" - "math/rand" "net" "net/http" "net/url" @@ -39,6 +38,7 @@ import ( "github.com/grafana/loki/clients/pkg/promtail/server" pserver "github.com/grafana/loki/clients/pkg/promtail/server" file2 "github.com/grafana/loki/clients/pkg/promtail/targets/file" + "github.com/grafana/loki/clients/pkg/promtail/targets/testutils" "github.com/grafana/loki/pkg/logproto" "github.com/grafana/loki/pkg/util" @@ -54,8 +54,8 @@ func TestPromtail(t *testing.T) { logger = level.NewFilter(logger, level.AllowInfo()) util_log.Logger = logger - initRandom() - dirName := "/tmp/promtail_test_" + randName() + testutils.InitRandom() + dirName := filepath.Join(os.TempDir(), "/promtail_test_"+testutils.RandName()) positionsFileName := dirName + "/positions.yml" err := os.MkdirAll(dirName, 0o750) @@ -641,22 +641,8 @@ func buildTestConfig(t *testing.T, positionsFileName string, logDirName string) return cfg } -func initRandom() { - rand.Seed(time.Now().UnixNano()) -} - -var letters = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ") - -func randName() string { - b := make([]rune, 10) - for i := range b { - b[i] = letters[rand.Intn(len(letters))] - } - return string(b) -} - func Test_DryRun(t *testing.T) { - f, err := os.CreateTemp("/tmp", "Test_DryRun") + f, err := os.CreateTemp("", "Test_DryRun") require.NoError(t, err) defer os.Remove(f.Name()) @@ -701,7 +687,7 @@ func Test_DryRun(t *testing.T) { } func Test_Reload(t *testing.T) { - f, err := os.CreateTemp("/tmp", "Test_Reload") + f, err := os.CreateTemp("", "Test_Reload") require.NoError(t, err) defer os.Remove(f.Name()) @@ -770,7 +756,7 @@ func Test_Reload(t *testing.T) { } func Test_ReloadFail_NotPanic(t *testing.T) { - f, err := os.CreateTemp("/tmp", "Test_Reload") + f, err := os.CreateTemp("", "Test_Reload") require.NoError(t, err) defer os.Remove(f.Name()) diff --git a/clients/pkg/promtail/targets/journal/journaltarget_test.go b/clients/pkg/promtail/targets/journal/journaltarget_test.go index 72220f5ca4..03d0dadae7 100644 --- a/clients/pkg/promtail/targets/journal/journaltarget_test.go +++ b/clients/pkg/promtail/targets/journal/journaltarget_test.go @@ -6,6 +6,7 @@ package journal import ( "io" "os" + "path/filepath" "strings" "testing" "time" @@ -70,7 +71,7 @@ func TestJournalTarget(t *testing.T) { logger := log.NewLogfmtLogger(w) testutils.InitRandom() - dirName := "/tmp/" + testutils.RandName() + dirName := filepath.Join(os.TempDir(), testutils.RandName()) positionsFileName := dirName + "/positions.yml" // Set the sync period to a really long value, to guarantee the sync timer @@ -132,7 +133,7 @@ func TestJournalTargetParsingErrors(t *testing.T) { logger := log.NewLogfmtLogger(w) testutils.InitRandom() - dirName := "/tmp/" + testutils.RandName() + dirName := filepath.Join(os.TempDir(), testutils.RandName()) positionsFileName := dirName + "/positions.yml" // Set the sync period to a really long value, to guarantee the sync timer @@ -200,7 +201,7 @@ func TestJournalTarget_JSON(t *testing.T) { logger := log.NewLogfmtLogger(w) testutils.InitRandom() - dirName := "/tmp/" + testutils.RandName() + dirName := filepath.Join(os.TempDir(), testutils.RandName()) positionsFileName := dirName + "/positions.yml" // Set the sync period to a really long value, to guarantee the sync timer @@ -260,7 +261,7 @@ func TestJournalTarget_Since(t *testing.T) { logger := log.NewLogfmtLogger(w) testutils.InitRandom() - dirName := "/tmp/" + testutils.RandName() + dirName := filepath.Join(os.TempDir(), testutils.RandName()) positionsFileName := dirName + "/positions.yml" // Set the sync period to a really long value, to guarantee the sync timer @@ -294,7 +295,7 @@ func TestJournalTarget_Cursor_TooOld(t *testing.T) { logger := log.NewLogfmtLogger(w) testutils.InitRandom() - dirName := "/tmp/" + testutils.RandName() + dirName := filepath.Join(os.TempDir(), testutils.RandName()) positionsFileName := dirName + "/positions.yml" // Set the sync period to a really long value, to guarantee the sync timer @@ -334,7 +335,7 @@ func TestJournalTarget_Cursor_NotTooOld(t *testing.T) { logger := log.NewLogfmtLogger(w) testutils.InitRandom() - dirName := "/tmp/" + testutils.RandName() + dirName := filepath.Join(os.TempDir(), testutils.RandName()) positionsFileName := dirName + "/positions.yml" // Set the sync period to a really long value, to guarantee the sync timer @@ -391,7 +392,7 @@ func TestJournalTarget_Matches(t *testing.T) { logger := log.NewLogfmtLogger(w) testutils.InitRandom() - dirName := "/tmp/" + testutils.RandName() + dirName := filepath.Join(os.TempDir(), testutils.RandName()) positionsFileName := dirName + "/positions.yml" // Set the sync period to a really long value, to guarantee the sync timer diff --git a/pkg/storage/stores/indexshipper/compactor/retention/marker.go b/pkg/storage/stores/indexshipper/compactor/retention/marker.go index 662e6a2eec..552f0af322 100644 --- a/pkg/storage/stores/indexshipper/compactor/retention/marker.go +++ b/pkg/storage/stores/indexshipper/compactor/retention/marker.go @@ -252,7 +252,7 @@ func (r *markerProcessor) processPath(path string, deleteFunc func(ctx context.C queue = make(chan *keyPair) ) // we use a copy to view the file so that we can read and update at the same time. - viewFile, err := os.CreateTemp("/tmp/", "marker-view-") + viewFile, err := os.CreateTemp("", "marker-view-") if err != nil { return err }