-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathe2etest_test.go
95 lines (77 loc) · 3.55 KB
/
e2etest_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
// Copyright (c) The EfficientGo Authors.
// Licensed under the Apache License 2.0.
package main
import (
"bytes"
"context"
"fmt"
"testing"
"github.com/efficientgo/core/testutil"
"github.com/efficientgo/e2e"
e2edb "github.com/efficientgo/e2e/db"
e2emon "github.com/efficientgo/e2e/monitoring"
"github.com/prometheus/client_golang/api"
v1 "github.com/prometheus/client_golang/api/prometheus/v1"
"github.com/prometheus/common/model"
)
func TestExample(t *testing.T) {
t.Parallel() // We can run those tests in parallel (as long as host has enough CPU time).
// Start isolated environment with given ref.
e, err := e2e.New()
testutil.Ok(t, err)
// Make sure resources (e.g docker containers, network, dir) are cleaned.
t.Cleanup(e.Close)
// Create structs for Prometheus containers scraping itself.
p1 := e2edb.NewPrometheus(e, "prometheus-1")
s1 := e2edb.NewThanosSidecar(e, "sidecar-1", p1)
p2 := e2edb.NewPrometheus(e, "prometheus-2")
s2 := e2edb.NewThanosSidecar(e, "sidecar-2", p2)
// Create Thanos Query container. We can point the peer network addresses of both Prometheus instance
// using InternalEndpoint methods, even before they started.
t1 := e2edb.NewThanosQuerier(e, "query-1", []string{s1.InternalEndpoint("grpc"), s2.InternalEndpoint("grpc")})
// Start them.
testutil.Ok(t, e2e.StartAndWaitReady(p1, s1, p2, s2, t1))
// To ensure query should have access we can check its Prometheus metric using WaitSumMetrics method. Since the metric we are looking for
// only appears after init, we add option to wait for it.
testutil.Ok(t, t1.WaitSumMetricsWithOptions(e2emon.Equals(2), []string{"thanos_store_nodes_grpc_connections"}, e2emon.WaitMissingMetrics()))
// To ensure Prometheus scraped already something ensure number of scrapes.
testutil.Ok(t, p1.WaitSumMetrics(e2emon.Greater(50), "prometheus_tsdb_head_samples_appended_total"))
testutil.Ok(t, p2.WaitSumMetrics(e2emon.Greater(50), "prometheus_tsdb_head_samples_appended_total"))
// We can now query Thanos Querier directly from here, using it's host address thanks to Endpoint method.
a, err := api.NewClient(api.Config{Address: "http://" + t1.Endpoint("http")})
testutil.Ok(t, err)
{
now := model.Now()
v, w, err := v1.NewAPI(a).Query(context.Background(), "up{}", now.Time())
testutil.Ok(t, err)
testutil.Equals(t, 0, len(w))
testutil.Equals(
t,
fmt.Sprintf(`up{instance="%v", job="myself", prometheus="prometheus-1"} => 1 @[%v]
up{instance="%v", job="myself", prometheus="prometheus-2"} => 1 @[%v]`, p1.InternalEndpoint(e2edb.AccessPortName), now, p2.InternalEndpoint(e2edb.AccessPortName), now),
v.String(),
)
}
// Stop first Prometheus and sidecar.
testutil.Ok(t, s1.Stop())
testutil.Ok(t, p1.Stop())
// Wait a bit until Thanos drops connection to stopped Prometheus.
testutil.Ok(t, t1.WaitSumMetricsWithOptions(e2emon.Equals(1), []string{"thanos_store_nodes_grpc_connections"}, e2emon.WaitMissingMetrics()))
{
now := model.Now()
v, w, err := v1.NewAPI(a).Query(context.Background(), "up{}", now.Time())
testutil.Ok(t, err)
testutil.Equals(t, 0, len(w))
testutil.Equals(
t,
fmt.Sprintf(`up{instance="%v", job="myself", prometheus="prometheus-2"} => 1 @[%v]`, p2.InternalEndpoint(e2edb.AccessPortName), now),
v.String(),
)
}
// Batch job example.
batch := e.Runnable("batch").Init(e2e.StartOptions{Image: "ubuntu:20.04", Command: e2e.NewCommandRunUntilStop()})
testutil.Ok(t, batch.Start())
var out bytes.Buffer
testutil.Ok(t, batch.Exec(e2e.NewCommand("echo", "it works"), e2e.WithExecOptionStdout(&out)))
testutil.Equals(t, "it works\n", out.String())
}