forked from thanos-io/thanos
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpromclient_e2e_test.go
More file actions
149 lines (122 loc) · 4.35 KB
/
promclient_e2e_test.go
File metadata and controls
149 lines (122 loc) · 4.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
package promclient
import (
"context"
"fmt"
"io/ioutil"
"net/url"
"os"
"path"
"testing"
"time"
"github.com/go-kit/kit/log"
"github.com/improbable-eng/thanos/pkg/runutil"
"github.com/improbable-eng/thanos/pkg/testutil"
"github.com/oklog/ulid"
"github.com/prometheus/common/model"
"github.com/prometheus/prometheus/pkg/timestamp"
"github.com/prometheus/tsdb/labels"
)
func TestIsWALFileAccesible_e2e(t *testing.T) {
testutil.ForeachPrometheus(t, func(t testing.TB, p *testutil.Prometheus) {
testutil.Ok(t, p.Start())
defer func() { testutil.Ok(t, p.Stop()) }()
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Minute)
defer cancel()
testutil.Ok(t, runutil.Retry(time.Second, ctx.Done(), func() error { return IsWALDirAccesible(p.Dir()) }))
testutil.NotOk(t, IsWALDirAccesible(path.Join(p.Dir(), "/non-existing")))
testutil.NotOk(t, IsWALDirAccesible(path.Join(p.Dir(), "/../")))
})
}
func TestExternalLabels_e2e(t *testing.T) {
testutil.ForeachPrometheus(t, func(t testing.TB, p *testutil.Prometheus) {
testutil.Ok(t, p.SetConfig(`
global:
external_labels:
region: eu-west
az: 1
`))
testutil.Ok(t, p.Start())
defer func() { testutil.Ok(t, p.Stop()) }()
u, err := url.Parse(fmt.Sprintf("http://%s", p.Addr()))
testutil.Ok(t, err)
ext, err := ExternalLabels(context.Background(), log.NewNopLogger(), u)
testutil.Ok(t, err)
testutil.Equals(t, 2, len(ext))
testutil.Equals(t, "eu-west", ext.Get("region"))
testutil.Equals(t, "1", ext.Get("az"))
})
}
func TestConfiguredFlags_e2e(t *testing.T) {
testutil.ForeachPrometheus(t, func(t testing.TB, p *testutil.Prometheus) {
testutil.Ok(t, p.Start())
defer func() { testutil.Ok(t, p.Stop()) }()
u, err := url.Parse(fmt.Sprintf("http://%s", p.Addr()))
testutil.Ok(t, err)
flags, err := ConfiguredFlags(context.Background(), log.NewNopLogger(), u)
testutil.Ok(t, err)
testutil.Assert(t, flags.WebEnableAdminAPI, "")
testutil.Assert(t, !flags.WebEnableLifecycle, "")
testutil.Equals(t, p.Dir(), flags.TSDBPath)
testutil.Equals(t, int64(2*time.Hour), int64(flags.TSDBMinTime))
testutil.Equals(t, int64(4.8*float64(time.Hour)), int64(flags.TSDBMaxTime))
testutil.Equals(t, int64(2*24*time.Hour), int64(flags.TSDBRetention))
})
}
func TestSnapshot_e2e(t *testing.T) {
testutil.ForeachPrometheus(t, func(t testing.TB, p *testutil.Prometheus) {
now := time.Now()
ctx := context.Background()
// Create artificial block.
id, err := testutil.CreateBlockWithTombstone(
ctx,
p.Dir(),
[]labels.Labels{labels.FromStrings("a", "b")},
10,
timestamp.FromTime(now.Add(-6*time.Hour)),
timestamp.FromTime(now.Add(-4*time.Hour)),
nil,
0,
)
testutil.Ok(t, err)
testutil.Ok(t, p.Start())
defer func() { testutil.Ok(t, p.Stop()) }()
u, err := url.Parse(fmt.Sprintf("http://%s", p.Addr()))
testutil.Ok(t, err)
// Prometheus since 2.7.0 don't write empty blocks even if it's head block. So it's no matter passing skip_head true or false here
// Pass skipHead = true to support all prometheus versions and assert that snapshot creates only one file
// https://github.com/prometheus/tsdb/pull/374
dir, err := Snapshot(ctx, log.NewNopLogger(), u, true)
testutil.Ok(t, err)
_, err = os.Stat(path.Join(p.Dir(), dir, id.String()))
testutil.Ok(t, err)
files, err := ioutil.ReadDir(path.Join(p.Dir(), dir))
testutil.Ok(t, err)
for _, f := range files {
_, err := ulid.Parse(f.Name())
testutil.Ok(t, err)
}
testutil.Equals(t, 1, len(files))
})
}
func TestRule_UnmarshalScalarResponse(t *testing.T) {
var (
scalarJSONResult = []byte(`[1541196373.677,"1"]`)
invalidLengthScalarJSONResult = []byte(`[1541196373.677,"1", "nonsence"]`)
invalidDataScalarJSONResult = []byte(`["foo","bar"]`)
vectorResult model.Vector
expectedVector = model.Vector{&model.Sample{
Metric: model.Metric{},
Value: 1,
Timestamp: model.Time(1541196373677)}}
)
// Test valid input.
vectorResult, err := convertScalarJSONToVector(scalarJSONResult)
testutil.Ok(t, err)
testutil.Equals(t, vectorResult.String(), expectedVector.String())
// Test invalid length of scalar data structure.
vectorResult, err = convertScalarJSONToVector(invalidLengthScalarJSONResult)
testutil.NotOk(t, err)
// Test invalid format of scalar data.
vectorResult, err = convertScalarJSONToVector(invalidDataScalarJSONResult)
testutil.NotOk(t, err)
}