-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
promclient_e2e_test.go
194 lines (159 loc) · 5.62 KB
/
promclient_e2e_test.go
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
// Copyright (c) The Thanos Authors.
// Licensed under the Apache License 2.0.
package promclient
import (
"context"
"fmt"
"net/url"
"os"
"path"
"testing"
"time"
"github.com/go-kit/log"
"github.com/oklog/ulid"
"github.com/prometheus/common/model"
"github.com/prometheus/prometheus/config"
"github.com/prometheus/prometheus/model/labels"
"github.com/prometheus/prometheus/model/timestamp"
"gopkg.in/yaml.v3"
"github.com/efficientgo/core/testutil"
"github.com/thanos-io/thanos/pkg/block/metadata"
"github.com/thanos-io/thanos/pkg/runutil"
"github.com/thanos-io/thanos/pkg/testutil/e2eutil"
)
func TestIsWALFileAccessible_e2e(t *testing.T) {
e2eutil.ForeachPrometheus(t, func(t testing.TB, p *e2eutil.Prometheus) {
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Minute)
defer cancel()
testutil.Ok(t, p.Start(ctx, log.NewNopLogger()))
testutil.Ok(t, runutil.Retry(time.Second, ctx.Done(), func() error { return IsWALDirAccessible(p.Dir()) }))
testutil.NotOk(t, IsWALDirAccessible(path.Join(p.Dir(), "/non-existing")))
testutil.NotOk(t, IsWALDirAccessible(path.Join(p.Dir(), "/../")))
})
}
func TestExternalLabels_e2e(t *testing.T) {
e2eutil.ForeachPrometheus(t, func(t testing.TB, p *e2eutil.Prometheus) {
// Keep consistent with the config processing in function (*Client).ExternalLabels.
cfg := config.Config{GlobalConfig: config.GlobalConfig{ExternalLabels: labels.FromMap(map[string]string{
"region": "eu-west",
"az": "1",
})}}
cfgData, err := yaml.Marshal(cfg)
testutil.Ok(t, err)
p.SetConfig(string(cfgData))
testutil.Ok(t, p.Start(context.Background(), log.NewNopLogger()))
u, err := url.Parse(fmt.Sprintf("http://%s", p.Addr()))
testutil.Ok(t, err)
ext, err := NewDefaultClient().ExternalLabels(context.Background(), u)
testutil.Ok(t, err)
testutil.Equals(t, 2, ext.Len())
testutil.Equals(t, "eu-west", ext.Get("region"))
testutil.Equals(t, "1", ext.Get("az"))
})
}
func TestConfiguredFlags_e2e(t *testing.T) {
e2eutil.ForeachPrometheus(t, func(t testing.TB, p *e2eutil.Prometheus) {
testutil.Ok(t, p.Start(context.Background(), log.NewNopLogger()))
u, err := url.Parse(fmt.Sprintf("http://%s", p.Addr()))
testutil.Ok(t, err)
flags, err := NewDefaultClient().ConfiguredFlags(context.Background(), 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) {
e2eutil.ForeachPrometheus(t, func(t testing.TB, p *e2eutil.Prometheus) {
now := time.Now()
ctx := context.Background()
// Create artificial block.
id, err := e2eutil.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)),
labels.EmptyLabels(),
0,
metadata.NoneFunc,
)
testutil.Ok(t, err)
testutil.Ok(t, p.Start(context.Background(), log.NewNopLogger()))
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 := NewDefaultClient().Snapshot(ctx, u, true)
testutil.Ok(t, err)
_, err = os.Stat(path.Join(p.Dir(), dir, id.String()))
testutil.Ok(t, err)
files, err := os.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", "nonsense"]`)
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.
_, err = convertScalarJSONToVector(invalidLengthScalarJSONResult)
testutil.NotOk(t, err)
// Test invalid format of scalar data.
_, err = convertScalarJSONToVector(invalidDataScalarJSONResult)
testutil.NotOk(t, err)
}
func TestQueryRange_e2e(t *testing.T) {
e2eutil.ForeachPrometheus(t, func(t testing.TB, p *e2eutil.Prometheus) {
now := time.Now()
ctx := context.Background()
// Create artificial block.
_, err := e2eutil.CreateBlock(
ctx,
p.Dir(),
[]labels.Labels{labels.FromStrings("a", "b")},
10,
timestamp.FromTime(now.Add(-2*time.Hour)),
timestamp.FromTime(now),
labels.EmptyLabels(),
0,
metadata.NoneFunc,
)
testutil.Ok(t, err)
testutil.Ok(t, p.Start(context.Background(), log.NewNopLogger()))
u, err := url.Parse(fmt.Sprintf("http://%s", p.Addr()))
testutil.Ok(t, err)
res, _, _, err := NewDefaultClient().QueryRange(
ctx,
u,
`{a="b"}`,
timestamp.FromTime(now.Add(-2*time.Hour)),
timestamp.FromTime(now),
14,
QueryOptions{},
)
testutil.Ok(t, err)
testutil.Equals(t, len(res) > 0, true)
})
}