-
Notifications
You must be signed in to change notification settings - Fork 2k
/
compatibility_test.go
197 lines (168 loc) · 7.33 KB
/
compatibility_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
195
196
197
// Copyright (c) The Thanos Authors.
// Licensed under the Apache License 2.0.
package e2e_test
import (
"bytes"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"testing"
"time"
"github.com/efficientgo/e2e"
e2edb "github.com/efficientgo/e2e/db"
common_cfg "github.com/prometheus/common/config"
"github.com/prometheus/prometheus/config"
"github.com/thanos-io/thanos/pkg/alert"
"github.com/thanos-io/thanos/pkg/httpconfig"
"github.com/thanos-io/thanos/pkg/testutil"
"github.com/thanos-io/thanos/test/e2e/e2ethanos"
)
// TestPromQLCompliance tests PromQL compatibility against https://github.com/prometheus/compliance/tree/main/promql.
// NOTE: This requires dockerization of compliance framework: https://github.com/prometheus/compliance/pull/46
// Test requires at least ~11m, so run this with `-test.timeout 9999m`.
func TestPromQLCompliance(t *testing.T) {
t.Skip("This is interactive test, it requires time to build up (scrape) the data. The data is also obtain from remote promlab servers.")
e, err := e2e.NewDockerEnvironment("compatibility")
testutil.Ok(t, err)
t.Cleanup(e.Close)
// Start receive + Querier.
receiverRunnable, err := e2ethanos.NewIngestingReceiver(e, "receive")
testutil.Ok(t, err)
queryReceive := e2edb.NewThanosQuerier(e, "query_receive", []string{receiverRunnable.InternalEndpoint("grpc")})
testutil.Ok(t, e2e.StartAndWaitReady(receiverRunnable, queryReceive))
// Start reference Prometheus.
prom := e2edb.NewPrometheus(e, "prom")
testutil.Ok(t, prom.SetConfig(`
global:
scrape_interval: 5s
evaluation_interval: 5s
external_labels:
prometheus: 1
remote_write:
- url: "`+e2ethanos.RemoteWriteEndpoint(receiverRunnable.InternalEndpoint("remote-write"))+`"
scrape_configs:
- job_name: 'demo'
static_configs:
- targets:
- 'demo.promlabs.com:10000'
- 'demo.promlabs.com:10001'
- 'demo.promlabs.com:10002'
`,
))
testutil.Ok(t, e2e.StartAndWaitReady(prom))
// Start sidecar + Querier
sidecar := e2edb.NewThanosSidecar(e, "sidecar", prom, e2edb.WithImage("thanos"))
querySidecar := e2edb.NewThanosQuerier(e, "query_sidecar", []string{sidecar.InternalEndpoint("grpc")}, e2edb.WithImage("thanos"))
testutil.Ok(t, e2e.StartAndWaitReady(sidecar, querySidecar))
// Start noop promql-compliance-tester. See https://github.com/prometheus/compliance/tree/main/promql on how to build local docker image.
compliance := e.Runnable("promql-compliance-tester").Init(e2e.StartOptions{
Image: "promql-compliance-tester:latest",
Command: e2e.NewCommandWithoutEntrypoint("tail", "-f", "/dev/null"),
})
testutil.Ok(t, e2e.StartAndWaitReady(compliance))
// Wait 10 minutes for Prometheus to scrape relevant data.
time.Sleep(10 * time.Minute)
t.Run("receive", func(t *testing.T) {
testutil.Ok(t, ioutil.WriteFile(filepath.Join(compliance.Dir(), "receive.yaml"),
[]byte(promQLCompatConfig(prom, queryReceive, []string{"prometheus", "receive", "tenant_id"})), os.ModePerm))
testutil.Ok(t, compliance.Exec(e2e.NewCommand(
"/promql-compliance-tester",
"-config-file", filepath.Join(compliance.InternalDir(), "receive.yaml"),
"-config-file", "/promql-test-queries.yml",
)))
})
t.Run("sidecar", func(t *testing.T) {
testutil.Ok(t, ioutil.WriteFile(filepath.Join(compliance.Dir(), "sidecar.yaml"),
[]byte(promQLCompatConfig(prom, querySidecar, []string{"prometheus"})), os.ModePerm))
testutil.Ok(t, compliance.Exec(e2e.NewCommand(
"/promql-compliance-tester",
"-config-file", filepath.Join(compliance.InternalDir(), "sidecar.yaml"),
"-config-file", "/promql-test-queries.yml",
)))
})
}
// nolint (it's still used in skipped test).
func promQLCompatConfig(reference *e2edb.Prometheus, target e2e.Runnable, dropLabels []string) string {
return `reference_target_config:
query_url: 'http://` + reference.InternalEndpoint("http") + `'
test_target_config:
query_url: 'http://` + target.InternalEndpoint("http") + `'
query_tweaks:
- note: 'Thanos requires adding "external_labels" to distinguish Prometheus servers, leading to extra labels in query results that need to be stripped before comparing results.'
no_bug: true
drop_result_labels:
` + func() (ret string) {
for _, l := range dropLabels {
ret += ` - ` + l + "\n"
}
return ret
}()
}
// nolint (it's still used in skipped test).
type alwaysReadyProbe struct{}
// nolint (it's still used in skipped test).
func (p alwaysReadyProbe) Ready(e2e.Runnable) error { return nil }
// TestAlertCompliance tests Alert compatibility against https://github.com/prometheus/compliance/blob/main/alert_generator.
// NOTE: This requires dockerization of compliance framework: https://github.com/prometheus/compliance/pull/46
func TestAlertCompliance_StatelessRuler(t *testing.T) {
t.Skip("This is an interactive test, using https://github.com/prometheus/compliance/tree/main/alert_generator testing is not optimized for CI runs")
e, err := e2e.NewDockerEnvironment("alert_compatibility")
testutil.Ok(t, err)
t.Cleanup(e.Close)
// Start receive + Querier.
receive, err := e2ethanos.NewIngestingReceiver(e, "receive")
testutil.Ok(t, err)
query := e2edb.NewThanosQuerier(e, "query_receive", []string{receive.InternalEndpoint("grpc")})
compliance := e.Runnable("alert_generator_compliance_tester").WithPorts(map[string]int{"http": 8080}).Init(e2e.StartOptions{
Image: "alert_generator_compliance_tester:latest",
// Batch job with HTTP port, we will start it with Exec, no need for readiness.
Readiness: alwaysReadyProbe{},
Command: e2e.NewCommandWithoutEntrypoint("tail", "-f", "/dev/null"),
})
ruler := e2ethanos.NewStatelessRuler(e, "1", "rules", []alert.AlertmanagerConfig{
{
EndpointsConfig: httpconfig.EndpointsConfig{
StaticAddresses: []string{compliance.InternalEndpoint("http")},
Scheme: "http",
},
Timeout: amTimeout,
APIVersion: alert.APIv1,
},
}, []httpconfig.Config{
{
EndpointsConfig: httpconfig.EndpointsConfig{
StaticAddresses: []string{
query.InternalEndpoint("http"),
},
Scheme: "http",
},
},
}, []*config.RemoteWriteConfig{
{URL: &common_cfg.URL{URL: urlParse(t, e2ethanos.RemoteWriteEndpoint(receive.InternalEndpoint("remote-write")))}, Name: "thanos-receiver"},
})
testutil.Ok(t, e2e.StartAndWaitReady(receive, query, ruler, compliance))
// Pull rules.yaml:
{
var stdout bytes.Buffer
testutil.Ok(t, compliance.Exec(e2e.NewCommand("cat", "/rules.yaml"), e2e.WithExecOptionStdout(&stdout)))
testutil.Ok(t, os.MkdirAll(filepath.Join(ruler.Dir(), "rules"), os.ModePerm))
testutil.Ok(t, os.WriteFile(filepath.Join(ruler.Dir(), "rules", "rules.yaml"), stdout.Bytes(), os.ModePerm))
}
testutil.Ok(t, ioutil.WriteFile(filepath.Join(compliance.Dir(), "test-thanos.yaml"),
[]byte(alertCompatConfig(receive, query)), os.ModePerm))
fmt.Println(alertCompatConfig(receive, query))
testutil.Ok(t, compliance.Exec(e2e.NewCommand(
"/alert_generator_compliance_tester", "-config-file", filepath.Join(compliance.InternalDir(), "test-thanos.yaml")),
))
}
// nolint (it's still used in skipped test).
func alertCompatConfig(receive e2e.Runnable, query e2e.Runnable) string {
return `settings:
remote_write_url: '` + e2ethanos.RemoteWriteEndpoint(receive.InternalEndpoint("http")) + `'
query_base_url: 'http://` + query.InternalEndpoint("http") + `'
rules_and_alerts_api_base_url: 'http://` + query.InternalEndpoint("http") + `'
alert_reception_server_port: 8080
alert_message_parser: default
`
}