Skip to content

Commit

Permalink
refactor: use T.TempDir to create temporary test directory (#5253)
Browse files Browse the repository at this point in the history
* refactor: use `T.TempDir` to create temporary test directory

The directory created by `T.TempDir` is automatically removed when the
test and all its subtests complete.

Reference: https://pkg.go.dev/testing#T.TempDir
Signed-off-by: Eng Zer Jun <[email protected]>

* docs: update CHANGELOG.md

Signed-off-by: Eng Zer Jun <[email protected]>
  • Loading branch information
Juneezee authored Jan 27, 2022
1 parent 9fdcacf commit 392fde1
Show file tree
Hide file tree
Showing 30 changed files with 123 additions and 430 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
* [4857](https://github.com/grafana/loki/pull/4857) **jordanrushing**: New schema v12 changes chunk key structure
* [5077](https://github.com/grafana/loki/pull/5077) **trevorwhitney**: Change some default values for better out-of-the-box performance
* [5204](https://github.com/grafana/loki/pull/5204) **trevorwhitney**: Default `max_outstanding_per_tenant` to `2048`
* [5253](https://github.com/grafana/loki/pull/5253) **Juneezee**: refactor: use `T.TempDir` to create temporary test directory

# 2.4.1 (2021/11/07)

Expand Down
21 changes: 5 additions & 16 deletions pkg/ingester/checkpoint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"context"
fmt "fmt"
"io/ioutil"
"os"
"sort"
"testing"
"time"
Expand Down Expand Up @@ -55,9 +54,7 @@ func defaultIngesterTestConfigWithWAL(t *testing.T, walDir string) Config {
}

func TestIngesterWAL(t *testing.T) {
walDir, err := ioutil.TempDir(os.TempDir(), "loki-wal")
require.Nil(t, err)
defer os.RemoveAll(walDir)
walDir := t.TempDir()

ingesterConfig := defaultIngesterTestConfigWithWAL(t, walDir)

Expand Down Expand Up @@ -137,9 +134,7 @@ func TestIngesterWAL(t *testing.T) {
}

func TestIngesterWALIgnoresStreamLimits(t *testing.T) {
walDir, err := ioutil.TempDir(os.TempDir(), "loki-wal")
require.Nil(t, err)
defer os.RemoveAll(walDir)
walDir := t.TempDir()

ingesterConfig := defaultIngesterTestConfigWithWAL(t, walDir)

Expand Down Expand Up @@ -241,9 +236,7 @@ func TestUnflushedChunks(t *testing.T) {
}

func TestIngesterWALBackpressureSegments(t *testing.T) {
walDir, err := ioutil.TempDir(os.TempDir(), "loki-wal")
require.Nil(t, err)
defer os.RemoveAll(walDir)
walDir := t.TempDir()

ingesterConfig := defaultIngesterTestConfigWithWAL(t, walDir)
ingesterConfig.WAL.ReplayMemoryCeiling = 1000
Expand Down Expand Up @@ -285,9 +278,7 @@ func TestIngesterWALBackpressureSegments(t *testing.T) {
}

func TestIngesterWALBackpressureCheckpoint(t *testing.T) {
walDir, err := ioutil.TempDir(os.TempDir(), "loki-wal")
require.Nil(t, err)
defer os.RemoveAll(walDir)
walDir := t.TempDir()

ingesterConfig := defaultIngesterTestConfigWithWAL(t, walDir)
ingesterConfig.WAL.ReplayMemoryCeiling = 1000
Expand Down Expand Up @@ -580,9 +571,7 @@ func buildChunks(t testing.TB, size int) []Chunk {
func TestIngesterWALReplaysUnorderedToOrdered(t *testing.T) {
for _, waitForCheckpoint := range []bool{false, true} {
t.Run(fmt.Sprintf("checkpoint-%v", waitForCheckpoint), func(t *testing.T) {
walDir, err := ioutil.TempDir(os.TempDir(), "loki-wal")
require.Nil(t, err)
defer os.RemoveAll(walDir)
walDir := t.TempDir()

ingesterConfig := defaultIngesterTestConfigWithWAL(t, walDir)

Expand Down
5 changes: 1 addition & 4 deletions pkg/ingester/flush_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package ingester

import (
"fmt"
"io/ioutil"
"os"
"sort"
"sync"
Expand Down Expand Up @@ -131,9 +130,7 @@ func buildChunkDecs(t testing.TB) []*chunkDesc {
func TestWALFullFlush(t *testing.T) {
// technically replaced with a fake wal, but the ingester New() function creates a regular wal first,
// so we enable creation/cleanup even though it remains unused.
walDir, err := ioutil.TempDir(os.TempDir(), "loki-wal")
require.Nil(t, err)
defer os.RemoveAll(walDir)
walDir := t.TempDir()

store, ing := newTestStore(t, defaultIngesterTestConfigWithWAL(t, walDir), fullWAL{})
testData := pushTestSamples(t, ing)
Expand Down
6 changes: 1 addition & 5 deletions pkg/querier/base/chunk_tar_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"compress/gzip"
"context"
"io"
"io/ioutil"
"math"
"os"
"strconv"
Expand All @@ -15,7 +14,6 @@ import (
"github.com/go-kit/log"
"github.com/pkg/errors"
"github.com/prometheus/prometheus/promql"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/weaveworks/common/user"

Expand Down Expand Up @@ -52,9 +50,7 @@ func getTarDataFromEnv(t testing.TB) (query string, from, through time.Time, ste
}

func runRangeQuery(t testing.TB, query string, from, through time.Time, step time.Duration, store chunkstore.ChunkStore) {
dir, err := ioutil.TempDir("", t.Name())
assert.NoError(t, err)
defer os.RemoveAll(dir)
dir := t.TempDir()
queryTracker := promql.NewActiveQueryTracker(dir, 1, log.NewNopLogger())

if len(query) == 0 || store == nil {
Expand Down
21 changes: 4 additions & 17 deletions pkg/querier/base/querier_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ package base
import (
"context"
"fmt"
"io/ioutil"
"os"
"strconv"
"sync"
"testing"
Expand Down Expand Up @@ -168,12 +166,7 @@ func TestQuerier(t *testing.T) {
}

func mockTSDB(t *testing.T, mint model.Time, samples int, step, chunkOffset time.Duration, samplesPerChunk int) storage.Queryable {
dir, err := ioutil.TempDir("", "tsdb")
require.NoError(t, err)

t.Cleanup(func() {
_ = os.RemoveAll(dir)
})
dir := t.TempDir()

opts := tsdb.DefaultHeadOptions()
opts.ChunkDirRoot = dir
Expand Down Expand Up @@ -257,9 +250,7 @@ func TestNoHistoricalQueryToIngester(t *testing.T) {
},
}

dir, err := ioutil.TempDir("", t.Name())
assert.NoError(t, err)
defer os.RemoveAll(dir)
dir := t.TempDir()
queryTracker := promql.NewActiveQueryTracker(dir, 10, log.NewNopLogger())

engine := promql.NewEngine(promql.EngineOpts{
Expand Down Expand Up @@ -728,9 +719,7 @@ func mockDistibutorFor(t *testing.T, cs mockChunkStore, through model.Time) *Moc
}

func testRangeQuery(t testing.TB, queryable storage.Queryable, end model.Time, q query) *promql.Result {
dir, err := ioutil.TempDir("", "test_query")
assert.NoError(t, err)
defer os.RemoveAll(dir)
dir := t.TempDir()
queryTracker := promql.NewActiveQueryTracker(dir, 10, log.NewNopLogger())

from, through, step := time.Unix(0, 0), end.Time(), q.step
Expand Down Expand Up @@ -875,9 +864,7 @@ func TestShortTermQueryToLTS(t *testing.T) {
},
}

dir, err := ioutil.TempDir("", t.Name())
assert.NoError(t, err)
defer os.RemoveAll(dir)
dir := t.TempDir()
queryTracker := promql.NewActiveQueryTracker(dir, 10, log.NewNopLogger())

engine := promql.NewEngine(promql.EngineOpts{
Expand Down
42 changes: 14 additions & 28 deletions pkg/ruler/base/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,9 @@ import (
)

func TestRuler_rules(t *testing.T) {
cfg, cleanup := defaultRulerConfig(t, newMockRuleStore(mockRules))
defer cleanup()
cfg := defaultRulerConfig(t, newMockRuleStore(mockRules))

r, rcleanup := newTestRuler(t, cfg)
defer rcleanup()
r := newTestRuler(t, cfg)
defer services.StopAndAwaitTerminated(context.Background(), r) //nolint:errcheck

a := NewAPI(r, r.store, log.NewNopLogger())
Expand Down Expand Up @@ -78,11 +76,9 @@ func TestRuler_rules(t *testing.T) {
}

func TestRuler_rules_special_characters(t *testing.T) {
cfg, cleanup := defaultRulerConfig(t, newMockRuleStore(mockSpecialCharRules))
defer cleanup()
cfg := defaultRulerConfig(t, newMockRuleStore(mockSpecialCharRules))

r, rcleanup := newTestRuler(t, cfg)
defer rcleanup()
r := newTestRuler(t, cfg)
defer services.StopAndAwaitTerminated(context.Background(), r) //nolint:errcheck

a := NewAPI(r, r.store, log.NewNopLogger())
Expand Down Expand Up @@ -135,11 +131,9 @@ func TestRuler_rules_special_characters(t *testing.T) {
}

func TestRuler_alerts(t *testing.T) {
cfg, cleanup := defaultRulerConfig(t, newMockRuleStore(mockRules))
defer cleanup()
cfg := defaultRulerConfig(t, newMockRuleStore(mockRules))

r, rcleanup := newTestRuler(t, cfg)
defer rcleanup()
r := newTestRuler(t, cfg)
defer r.StopAsync()

a := NewAPI(r, r.store, log.NewNopLogger())
Expand Down Expand Up @@ -171,11 +165,9 @@ func TestRuler_alerts(t *testing.T) {
}

func TestRuler_Create(t *testing.T) {
cfg, cleanup := defaultRulerConfig(t, newMockRuleStore(make(map[string]rulespb.RuleGroupList)))
defer cleanup()
cfg := defaultRulerConfig(t, newMockRuleStore(make(map[string]rulespb.RuleGroupList)))

r, rcleanup := newTestRuler(t, cfg)
defer rcleanup()
r := newTestRuler(t, cfg)
defer services.StopAndAwaitTerminated(context.Background(), r) //nolint:errcheck

a := NewAPI(r, r.store, log.NewNopLogger())
Expand Down Expand Up @@ -262,11 +254,9 @@ rules:
}

func TestRuler_DeleteNamespace(t *testing.T) {
cfg, cleanup := defaultRulerConfig(t, newMockRuleStore(mockRulesNamespaces))
defer cleanup()
cfg := defaultRulerConfig(t, newMockRuleStore(mockRulesNamespaces))

r, rcleanup := newTestRuler(t, cfg)
defer rcleanup()
r := newTestRuler(t, cfg)
defer services.StopAndAwaitTerminated(context.Background(), r) //nolint:errcheck

a := NewAPI(r, r.store, log.NewNopLogger())
Expand Down Expand Up @@ -301,11 +291,9 @@ func TestRuler_DeleteNamespace(t *testing.T) {
}

func TestRuler_LimitsPerGroup(t *testing.T) {
cfg, cleanup := defaultRulerConfig(t, newMockRuleStore(make(map[string]rulespb.RuleGroupList)))
defer cleanup()
cfg := defaultRulerConfig(t, newMockRuleStore(make(map[string]rulespb.RuleGroupList)))

r, rcleanup := newTestRuler(t, cfg)
defer rcleanup()
r := newTestRuler(t, cfg)
defer services.StopAndAwaitTerminated(context.Background(), r) //nolint:errcheck

r.limits = ruleLimits{maxRuleGroups: 1, maxRulesPerRuleGroup: 1}
Expand Down Expand Up @@ -356,11 +344,9 @@ rules:
}

func TestRuler_RulerGroupLimits(t *testing.T) {
cfg, cleanup := defaultRulerConfig(t, newMockRuleStore(make(map[string]rulespb.RuleGroupList)))
defer cleanup()
cfg := defaultRulerConfig(t, newMockRuleStore(make(map[string]rulespb.RuleGroupList)))

r, rcleanup := newTestRuler(t, cfg)
defer rcleanup()
r := newTestRuler(t, cfg)
defer services.StopAndAwaitTerminated(context.Background(), r) //nolint:errcheck

r.limits = ruleLimits{maxRuleGroups: 1, maxRulesPerRuleGroup: 1}
Expand Down
12 changes: 4 additions & 8 deletions pkg/ruler/base/lifecycle_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,11 @@ import (
func TestRulerShutdown(t *testing.T) {
ctx := context.Background()

config, cleanup := defaultRulerConfig(t, newMockRuleStore(mockRules))
defer cleanup()
config := defaultRulerConfig(t, newMockRuleStore(mockRules))

m := storage.NewClientMetrics()
defer m.Unregister()
r, rcleanup := buildRuler(t, config, nil, m, nil)
defer rcleanup()
r := buildRuler(t, config, nil, m, nil)

r.cfg.EnableSharding = true
ringStore, closer := consul.NewInMemoryClient(ring.GetCodec(), log.NewNopLogger(), nil)
Expand Down Expand Up @@ -60,12 +58,10 @@ func TestRuler_RingLifecyclerShouldAutoForgetUnhealthyInstances(t *testing.T) {
const heartbeatTimeout = time.Minute

ctx := context.Background()
config, cleanup := defaultRulerConfig(t, newMockRuleStore(mockRules))
defer cleanup()
config := defaultRulerConfig(t, newMockRuleStore(mockRules))
m := storage.NewClientMetrics()
defer m.Unregister()
r, rcleanup := buildRuler(t, config, nil, m, nil)
defer rcleanup()
r := buildRuler(t, config, nil, m, nil)
r.cfg.EnableSharding = true
r.cfg.Ring.HeartbeatPeriod = 100 * time.Millisecond
r.cfg.Ring.HeartbeatTimeout = heartbeatTimeout
Expand Down
8 changes: 1 addition & 7 deletions pkg/ruler/base/manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ package base

import (
"context"
"io/ioutil"
"os"
"testing"
"time"

Expand All @@ -20,11 +18,7 @@ import (
)

func TestSyncRuleGroups(t *testing.T) {
dir, err := ioutil.TempDir("", "rules")
require.NoError(t, err)
t.Cleanup(func() {
_ = os.RemoveAll(dir)
})
dir := t.TempDir()

m, err := NewDefaultMultiTenantManager(Config{RulePath: dir}, factory, nil, log.NewNopLogger())
require.NoError(t, err)
Expand Down
Loading

0 comments on commit 392fde1

Please sign in to comment.