Debian Codegraph @

tutorials/whatsup/internal/acceptance_test.go

raw
1 // Copyright 2023 The Prometheus Authors
2 // Licensed under the Apache License, Version 2.0 (the "License");
3 // you may not use this file except in compliance with the License.
4 // You may obtain a copy of the License at
5 //
6 // http://www.apache.org/licenses/LICENSE-2.0
7 //
8 // Unless required by applicable law or agreed to in writing, software
9 // distributed under the License is distributed on an "AS IS" BASIS,
10 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 // See the License for the specific language governing permissions and
12 // limitations under the License.
13
14 //go:build interactive
15 // +build interactive
16
17 package internal
18
19 import (
20 "fmt"
21 "io"
22 "net/http"
23 "strings"
24 "testing"
25
26 "github.com/efficientgo/core/testutil"
27 )
28
29 func TestAcceptance(t *testing.T) {
30 resp, err := http.Get(whatsupAddr(fmt.Sprintf("http://localhost:%v", WhatsupPort)) + "/metrics")
31 testutil.Ok(t, err)
32 defer resp.Body.Close()
33
34 b, err := io.ReadAll(resp.Body)
35 testutil.Ok(t, err)
36
37 metrics := string(b)
38
39 gotErr := false
40 for _, tcase := range []struct {
41 expectMetricName string
42 expectMetricType string
43 expectExemplar bool // TODO
44 }{
45 {
46 expectMetricName: "whatsup_queries_handled_total",
47 expectMetricType: "counter",
48 },
49 {
50 expectMetricName: "whatsup_last_response_elements",
51 expectMetricType: "gauge",
52 },
53 {
54 expectMetricName: "build_info",
55 expectMetricType: "gauge",
56 },
57 {
58 expectMetricName: "whatsup_queries_duration_seconds",
59 expectMetricType: "histogram",
60 },
61 {
62 expectMetricName: "go_goroutines",
63 expectMetricType: "gauge",
64 },
65 } {
66 if !t.Run(fmt.Sprintf("Metric %v type %v with exemplar %v", tcase.expectMetricName, tcase.expectMetricType, tcase.expectExemplar), func(t *testing.T) {
67 // Yolo.
68 expLine := fmt.Sprintf("# TYPE %v %v", tcase.expectMetricName, tcase.expectMetricType)
69 if strings.Index(metrics, expLine) == -1 {
70 t.Error(expLine, "not found!")
71 }
72 // TODO(bwplotka): Check all properly.
73 }) {
74 gotErr = true
75 }
76 }
77
78 if gotErr {
79 fmt.Println("Got this response from ", fmt.Sprintf("http://localhost:%v", WhatsupPort), ":", metrics)
80 }
81
82 }