Skip to content
This repository was archived by the owner on Aug 13, 2019. It is now read-only.

Commit 67c92d8

Browse files
committed
Add tests for analyzeBlock CLI command
Signed-off-by: obitech <[email protected]>
1 parent 7a61b29 commit 67c92d8

File tree

3 files changed

+71
-31
lines changed

3 files changed

+71
-31
lines changed

cmd/tsdb/main.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,16 +32,18 @@ import (
3232

3333
"github.com/go-kit/kit/log"
3434
"github.com/pkg/errors"
35+
"gopkg.in/alecthomas/kingpin.v2"
36+
3537
"github.com/prometheus/tsdb"
3638
"github.com/prometheus/tsdb/chunks"
3739
tsdb_errors "github.com/prometheus/tsdb/errors"
3840
"github.com/prometheus/tsdb/labels"
39-
"gopkg.in/alecthomas/kingpin.v2"
4041
)
4142

4243
const (
4344
printBlocksTableHeader = "BLOCK ULID\tMIN TIME\tMAX TIME\tNUM SAMPLES\tNUM CHUNKS\tNUM SERIES"
4445
defaultAnalyzeLimit = "20"
46+
timeDelta = 30000
4547
)
4648

4749
func main() {
@@ -250,8 +252,6 @@ func (b *writeBenchmark) run() error {
250252
return nil
251253
}
252254

253-
const timeDelta = 30000
254-
255255
func (b *writeBenchmark) ingestScrapes(lbls []labels.Labels, scrapeCount int) (uint64, error) {
256256
var mu sync.Mutex
257257
var total uint64

cmd/tsdb/main_test.go

Lines changed: 43 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"strings"
2323
"testing"
2424
"text/tabwriter"
25+
"time"
2526

2627
"github.com/prometheus/tsdb"
2728
testutildb "github.com/prometheus/tsdb/testutil/db"
@@ -112,7 +113,7 @@ func TestPrintBlocks(t *testing.T) {
112113
expected = strings.Replace(expected, "\n", "", -1)
113114

114115
if expected != actual {
115-
t.Errorf("expected (%#v) != actual (%#v)", expected, actual)
116+
t.Errorf("expected (%#v) != actual (%#v)", b.String(), actualStdout.String())
116117
}
117118
}
118119

@@ -150,7 +151,7 @@ func TestExtractBlock(t *testing.T) {
150151
analyzeBlockID = "foo"
151152
block, err = extractBlock(blocks, &analyzeBlockID)
152153
if err == nil {
153-
t.Errorf("Analyzing block %q should throw error", analyzeBlockID)
154+
t.Errorf("Analyzing block ID %q should throw error", analyzeBlockID)
154155
}
155156
if block != nil {
156157
t.Error("block should be nil")
@@ -180,9 +181,48 @@ func TestAnalyzeBlocks(t *testing.T) {
180181
t.Error(err)
181182
}
182183

183-
var actual bytes.Buffer
184+
var (
185+
expected bytes.Buffer
186+
actual bytes.Buffer
187+
)
188+
189+
// Actual output.
184190
err = analyzeBlock(&actual, block, dal)
185191
if err != nil {
186192
t.Error(err)
187193
}
194+
195+
act := actual.String()
196+
act = strings.Replace(act, " ", "", -1)
197+
act = strings.Replace(act, "\t", "", -1)
198+
act = strings.Replace(act, "\n", "", -1)
199+
200+
// Expected output.
201+
meta := block.Meta()
202+
fmt.Fprintf(&expected, "Block ID: %s\n", meta.ULID)
203+
fmt.Fprintf(&expected, "Duration: %s\n", (time.Duration(meta.MaxTime-meta.MinTime) * 1e6).String())
204+
fmt.Fprintf(&expected, "Series: %d\n", 1)
205+
fmt.Fprintf(&expected, "Label names: %d\n", 1)
206+
fmt.Fprintf(&expected, "Postings (unique label pairs): %d\n", 1)
207+
fmt.Fprintf(&expected, "Postings entries (total label pairs): %d\n", 1)
208+
fmt.Fprintf(&expected, "\nLabel pairs most involved in churning:\n")
209+
fmt.Fprintf(&expected, "1 %s=0", testutildb.DefaultLabelName)
210+
fmt.Fprintf(&expected, "\nLabel names most involved in churning:\n")
211+
fmt.Fprintf(&expected, "1 %s", testutildb.DefaultLabelName)
212+
fmt.Fprintf(&expected, "\nMost common label pairs:\n")
213+
fmt.Fprintf(&expected, "1 %s=0", testutildb.DefaultLabelName)
214+
fmt.Fprintf(&expected, "\nLabel names with highest cumulative label value length:\n")
215+
fmt.Fprintf(&expected, "1 %s", testutildb.DefaultLabelName)
216+
fmt.Fprintf(&expected, "\nHighest cardinality labels:\n")
217+
fmt.Fprintf(&expected, "1 %s", testutildb.DefaultLabelName)
218+
fmt.Fprintf(&expected, "\nHighest cardinality metric names:\n")
219+
220+
exp := expected.String()
221+
exp = strings.Replace(exp, " ", "", -1)
222+
exp = strings.Replace(exp, "\t", "", -1)
223+
exp = strings.Replace(exp, "\n", "", -1)
224+
225+
if exp != act {
226+
t.Errorf("expected (%#v) != actual (%#v)", expected.String(), actual.String())
227+
}
188228
}

testutil/db/db.go

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,29 @@ import (
3030
"github.com/prometheus/tsdb/tsdbutil"
3131
)
3232

33+
const (
34+
DefaultLabelName = "labelName"
35+
defaultLabelValue = "labelValue"
36+
)
37+
38+
type sample struct {
39+
t int64
40+
v float64
41+
}
42+
43+
type mockSeries struct {
44+
labels func() labels.Labels
45+
iterator func() tsdb.SeriesIterator
46+
}
47+
48+
func (s sample) T() int64 {
49+
return s.t
50+
}
51+
52+
func (s sample) V() float64 {
53+
return s.v
54+
}
55+
3356
// CreateBlock creates a block with given set of series and returns its dir.
3457
func CreateBlock(tb testing.TB, dir string, series []tsdb.Series) string {
3558
head := createHead(tb, series)
@@ -72,24 +95,6 @@ func createHead(tb testing.TB, series []tsdb.Series) *tsdb.Head {
7295
return head
7396
}
7497

75-
const (
76-
defaultLabelName = "labelName"
77-
defaultLabelValue = "labelValue"
78-
)
79-
80-
type sample struct {
81-
t int64
82-
v float64
83-
}
84-
85-
func (s sample) T() int64 {
86-
return s.t
87-
}
88-
89-
func (s sample) V() float64 {
90-
return s.v
91-
}
92-
9398
// GenSeries generates series with a given number of labels and values.
9499
func GenSeries(totalSeries, labelCount int, mint, maxt int64) []tsdb.Series {
95100
if totalSeries == 0 || labelCount == 0 {
@@ -100,9 +105,9 @@ func GenSeries(totalSeries, labelCount int, mint, maxt int64) []tsdb.Series {
100105

101106
for i := 0; i < totalSeries; i++ {
102107
lbls := make(map[string]string, labelCount)
103-
lbls[defaultLabelName] = strconv.Itoa(i)
108+
lbls[DefaultLabelName] = strconv.Itoa(i)
104109
for j := 1; len(lbls) < labelCount; j++ {
105-
lbls[defaultLabelName+strconv.Itoa(j)] = defaultLabelValue + strconv.Itoa(j)
110+
lbls[DefaultLabelName+strconv.Itoa(j)] = defaultLabelValue + strconv.Itoa(j)
106111
}
107112
samples := make([]tsdbutil.Sample, 0, maxt-mint+1)
108113
for t := mint; t < maxt; t++ {
@@ -113,11 +118,6 @@ func GenSeries(totalSeries, labelCount int, mint, maxt int64) []tsdb.Series {
113118
return series
114119
}
115120

116-
type mockSeries struct {
117-
labels func() labels.Labels
118-
iterator func() tsdb.SeriesIterator
119-
}
120-
121121
func newSeries(l map[string]string, s []tsdbutil.Sample) tsdb.Series {
122122
return &mockSeries{
123123
labels: func() labels.Labels { return labels.FromMap(l) },

0 commit comments

Comments
 (0)