mirror of
https://github.com/prometheus/prometheus.git
synced 2025-01-11 13:57:36 -08:00
Test we get back out the data we put in.
This also adds a utility that'll aid future tests.
This commit is contained in:
parent
2fcdd1a5cb
commit
2742fc380b
65
db_test.go
65
db_test.go
|
@ -19,43 +19,59 @@ import (
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/prometheus/tsdb/labels"
|
"github.com/prometheus/tsdb/labels"
|
||||||
|
"github.com/stretchr/testify/require"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// Convert a SeriesSet into a form useable with reflect.DeepEqual.
|
||||||
|
func readSeriesSet(ss SeriesSet) (map[string][]sample, error) {
|
||||||
|
result := map[string][]sample{}
|
||||||
|
|
||||||
|
for ss.Next() {
|
||||||
|
series := ss.At()
|
||||||
|
|
||||||
|
samples := []sample{}
|
||||||
|
it := series.Iterator()
|
||||||
|
for it.Next() {
|
||||||
|
t, v := it.At()
|
||||||
|
samples = append(samples, sample{t: t, v: v})
|
||||||
|
}
|
||||||
|
|
||||||
|
name := series.Labels().String()
|
||||||
|
result[name] = samples
|
||||||
|
if err := ss.Err(); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return result, nil
|
||||||
|
}
|
||||||
|
|
||||||
func TestDataAvailableOnlyAfterCommit(t *testing.T) {
|
func TestDataAvailableOnlyAfterCommit(t *testing.T) {
|
||||||
tmpdir, _ := ioutil.TempDir("", "test")
|
tmpdir, _ := ioutil.TempDir("", "test")
|
||||||
defer os.RemoveAll(tmpdir)
|
defer os.RemoveAll(tmpdir)
|
||||||
|
|
||||||
db, err := Open(tmpdir, nil, nil, nil)
|
db, err := Open(tmpdir, nil, nil, nil)
|
||||||
if err != nil {
|
require.NoError(t, err)
|
||||||
t.Fatalf("Error opening database: %q", err)
|
|
||||||
}
|
|
||||||
defer db.Close()
|
defer db.Close()
|
||||||
|
|
||||||
app := db.Appender()
|
app := db.Appender()
|
||||||
_, err = app.Add(labels.FromStrings("foo", "bar"), 0, 0)
|
_, err = app.Add(labels.FromStrings("foo", "bar"), 0, 0)
|
||||||
if err != nil {
|
require.NoError(t, err)
|
||||||
t.Fatalf("Error adding sample: %q", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
querier := db.Querier(0, 1)
|
querier := db.Querier(0, 1)
|
||||||
defer querier.Close()
|
defer querier.Close()
|
||||||
seriesSet := querier.Select(labels.NewEqualMatcher("foo", "bar"))
|
seriesSet, err := readSeriesSet(querier.Select(labels.NewEqualMatcher("foo", "bar")))
|
||||||
if seriesSet.Next() {
|
require.NoError(t, err)
|
||||||
t.Fatalf("Error, was expecting no matching series.")
|
require.Equal(t, seriesSet, map[string][]sample{})
|
||||||
}
|
|
||||||
|
|
||||||
err = app.Commit()
|
err = app.Commit()
|
||||||
if err != nil {
|
require.NoError(t, err)
|
||||||
t.Fatalf("Error committing: %q", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
querier = db.Querier(0, 1)
|
querier = db.Querier(0, 1)
|
||||||
defer querier.Close()
|
defer querier.Close()
|
||||||
|
|
||||||
seriesSet = querier.Select(labels.NewEqualMatcher("foo", "bar"))
|
seriesSet, err = readSeriesSet(querier.Select(labels.NewEqualMatcher("foo", "bar")))
|
||||||
if !seriesSet.Next() {
|
require.NoError(t, err)
|
||||||
t.Fatalf("Error, was expecting a matching series.")
|
require.Equal(t, seriesSet, map[string][]sample{`{foo="bar"}`: []sample{{t: 0, v: 0}}})
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestDataNotAvailableAfterRollback(t *testing.T) {
|
func TestDataNotAvailableAfterRollback(t *testing.T) {
|
||||||
|
@ -70,20 +86,15 @@ func TestDataNotAvailableAfterRollback(t *testing.T) {
|
||||||
|
|
||||||
app := db.Appender()
|
app := db.Appender()
|
||||||
_, err = app.Add(labels.FromStrings("foo", "bar"), 0, 0)
|
_, err = app.Add(labels.FromStrings("foo", "bar"), 0, 0)
|
||||||
if err != nil {
|
require.NoError(t, err)
|
||||||
t.Fatalf("Error adding sample: %q", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
err = app.Rollback()
|
err = app.Rollback()
|
||||||
if err != nil {
|
require.NoError(t, err)
|
||||||
t.Fatalf("Error rolling back: %q", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
querier := db.Querier(0, 1)
|
querier := db.Querier(0, 1)
|
||||||
defer querier.Close()
|
defer querier.Close()
|
||||||
|
|
||||||
seriesSet := querier.Select(labels.NewEqualMatcher("foo", "bar"))
|
seriesSet, err := readSeriesSet(querier.Select(labels.NewEqualMatcher("foo", "bar")))
|
||||||
if seriesSet.Next() {
|
require.NoError(t, err)
|
||||||
t.Fatalf("Error, was expecting no matching series.")
|
require.Equal(t, seriesSet, map[string][]sample{})
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue