MCPcopy Create free account
hub / github.com/efficientgo/e2e / TestExample

Function TestExample

examples/thanos/e2etest_test.go:21–95  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

19)
20
21func TestExample(t *testing.T) {
22 t.Parallel() // We can run those tests in parallel (as long as host has enough CPU time).
23
24 // Start isolated environment with given ref.
25 e, err := e2e.New()
26 testutil.Ok(t, err)
27 // Make sure resources (e.g docker containers, network, dir) are cleaned.
28 t.Cleanup(e.Close)
29
30 // Create structs for Prometheus containers scraping itself.
31 p1 := e2edb.NewPrometheus(e, "prometheus-1")
32 s1 := e2edb.NewThanosSidecar(e, "sidecar-1", p1)
33
34 p2 := e2edb.NewPrometheus(e, "prometheus-2")
35 s2 := e2edb.NewThanosSidecar(e, "sidecar-2", p2)
36
37 // Create Thanos Query container. We can point the peer network addresses of both Prometheus instance
38 // using InternalEndpoint methods, even before they started.
39 t1 := e2edb.NewThanosQuerier(e, "query-1", []string{s1.InternalEndpoint("grpc"), s2.InternalEndpoint("grpc")})
40
41 // Start them.
42 testutil.Ok(t, e2e.StartAndWaitReady(p1, s1, p2, s2, t1))
43
44 // To ensure query should have access we can check its Prometheus metric using WaitSumMetrics method. Since the metric we are looking for
45 // only appears after init, we add option to wait for it.
46 testutil.Ok(t, t1.WaitSumMetricsWithOptions(e2emon.Equals(2), []string{"thanos_store_nodes_grpc_connections"}, e2emon.WaitMissingMetrics()))
47
48 // To ensure Prometheus scraped already something ensure number of scrapes.
49 testutil.Ok(t, p1.WaitSumMetrics(e2emon.Greater(50), "prometheus_tsdb_head_samples_appended_total"))
50 testutil.Ok(t, p2.WaitSumMetrics(e2emon.Greater(50), "prometheus_tsdb_head_samples_appended_total"))
51
52 // We can now query Thanos Querier directly from here, using it's host address thanks to Endpoint method.
53 a, err := api.NewClient(api.Config{Address: "http://" + t1.Endpoint("http")})
54 testutil.Ok(t, err)
55
56 {
57 now := model.Now()
58 v, w, err := v1.NewAPI(a).Query(context.Background(), "up{}", now.Time())
59 testutil.Ok(t, err)
60 testutil.Equals(t, 0, len(w))
61 testutil.Equals(
62 t,
63 fmt.Sprintf(`up{instance="%v", job="myself", prometheus="prometheus-1"} => 1 @[%v]
64up{instance="%v", job="myself", prometheus="prometheus-2"} => 1 @[%v]`, p1.InternalEndpoint(e2edb.AccessPortName), now, p2.InternalEndpoint(e2edb.AccessPortName), now),
65 v.String(),
66 )
67 }
68
69 // Stop first Prometheus and sidecar.
70 testutil.Ok(t, s1.Stop())
71 testutil.Ok(t, p1.Stop())
72
73 // Wait a bit until Thanos drops connection to stopped Prometheus.
74 testutil.Ok(t, t1.WaitSumMetricsWithOptions(e2emon.Equals(1), []string{"thanos_store_nodes_grpc_connections"}, e2emon.WaitMissingMetrics()))
75
76 {
77 now := model.Now()
78 v, w, err := v1.NewAPI(a).Query(context.Background(), "up{}", now.Time())

Callers

nothing calls this directly

Calls 15

NewFunction · 0.92
StartAndWaitReadyFunction · 0.92
NewCommandRunUntilStopFunction · 0.92
NewCommandFunction · 0.92
WithExecOptionStdoutFunction · 0.92
InternalEndpointMethod · 0.65
WaitSumMetricsMethod · 0.65
EndpointMethod · 0.65
StopMethod · 0.65
InitMethod · 0.65
RunnableMethod · 0.65

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…