Skip to content
This repository was archived by the owner on Apr 2, 2024. It is now read-only.

Commit 56b2368

Browse files
Blagoj Atanasovskiatanasovskib
authored andcommitted
Add end-2-end tests for label endpoints
1 parent ecf092a commit 56b2368

File tree

3 files changed

+38
-16
lines changed

3 files changed

+38
-16
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
FROM prom/prometheus
2+
ADD prometheus.yml /etc/prometheus/prometheus.yml

pkg/internal/testhelpers/containers.go

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,9 @@ import (
88
"context"
99
"fmt"
1010
"io/ioutil"
11-
"runtime"
12-
1311
"os"
1412
"path/filepath"
13+
"runtime"
1514
"testing"
1615

1716
"github.com/docker/go-connections/nat"
@@ -26,8 +25,9 @@ const (
2625
defaultDB = "postgres"
2726
connectTemplate = "postgres://%s:password@%s:%d/%s"
2827

29-
postgresUser = "postgres"
30-
promUser = "prom"
28+
postgresUser = "postgres"
29+
promUser = "prom"
30+
emptyPromConfig = "global:\n scrape_interval: 10s"
3131

3232
Superuser = true
3333
NoSuperuser = false
@@ -208,14 +208,19 @@ func StartPromContainer(storagePath string, ctx context.Context) (testcontainers
208208
return nil, err
209209
}
210210

211+
promConfigFile := filepath.Join(storagePath, "prometheus.yml")
212+
err = ioutil.WriteFile(promConfigFile, []byte(emptyPromConfig), 0777)
213+
if err != nil {
214+
return nil, err
215+
}
211216
prometheusPort := nat.Port("9090/tcp")
212-
213217
req := testcontainers.ContainerRequest{
214218
Image: "prom/prometheus",
215219
ExposedPorts: []string{string(prometheusPort)},
216220
WaitingFor: wait.ForListeningPort(prometheusPort),
217221
BindMounts: map[string]string{
218-
storagePath: "/prometheus",
222+
storagePath: "/prometheus",
223+
promConfigFile: "/etc/prometheus/prometheus.yml",
219224
},
220225
}
221226
container, err := testcontainers.GenericContainer(ctx, testcontainers.GenericContainerRequest{
@@ -225,7 +230,6 @@ func StartPromContainer(storagePath string, ctx context.Context) (testcontainers
225230
if err != nil {
226231
return nil, err
227232
}
228-
229233
PromHost, err = container.Host(ctx)
230234
if err != nil {
231235
return nil, err

pkg/pgmodel/end_to_end_tests/promql_endpoint_integration_test.go

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,20 @@ func getLabelsRequest(apiUrl string) (*http.Request, error) {
102102
)
103103
}
104104

105+
func getLabelNamesRequest(apiUrl string) (*http.Request, error) {
106+
u, err := url.Parse(fmt.Sprintf("%s/labels", apiUrl))
107+
108+
if err != nil {
109+
return nil, err
110+
}
111+
112+
return http.NewRequest(
113+
"GET",
114+
u.String(),
115+
nil,
116+
)
117+
}
118+
105119
func getLabelValuesRequest(apiUrl string, labelName string) (*http.Request, error) {
106120
u, err := url.Parse(fmt.Sprintf("%s/label/%s/values", apiUrl, labelName))
107121

@@ -369,7 +383,7 @@ func TestPromQLQueryEndpoint(t *testing.T) {
369383
})
370384
}
371385

372-
func TestPromQLLabelsEndpoint(t *testing.T) {
386+
func TestPromQLLabelEndpoints(t *testing.T) {
373387
if testing.Short() || !*useDocker {
374388
t.Skip("skipping integration test")
375389
}
@@ -390,24 +404,26 @@ func TestPromQLLabelsEndpoint(t *testing.T) {
390404
r := pgmodel.NewPgxReader(readOnly, nil)
391405
queryable := query.NewQueryable(r.GetQuerier())
392406

393-
//labelNamesHandler := api.Labels(queryable)
407+
labelNamesHandler := api.Labels(queryable)
394408
labelValuesHandler := api.LabelValues(queryable)
395409
router := route.New()
396410
router.Get("/api/v1/label/:name/values", labelValuesHandler.ServeHTTP)
397-
411+
router.Get("/api/v1/labels", labelNamesHandler.ServeHTTP)
398412
apiURL := fmt.Sprintf("http://%s:%d/api/v1", testhelpers.PromHost, testhelpers.PromPort.Int())
399413
client := &http.Client{Timeout: 10 * time.Second}
400414

401415
var (
402416
req *http.Request
403417
err error
404418
)
405-
//req, err = getLabelsRequest(apiURL)
406-
//if err != nil {
407-
// t.Fatalf("unable to create PromQL labels request: %v", err)
408-
//}
409-
//testMethod := testRequest(req, labelNamesHandler, client, labelsResultComparator)
410-
//tester.Run("get all labels", testMethod)
419+
req, err = getLabelNamesRequest(apiURL)
420+
if err != nil {
421+
t.Fatalf("unable to create PromQL label names request: %v", err)
422+
}
423+
424+
testMethod := testRequest(req, router, client, labelsResultComparator)
425+
tester.Run("get label names", testMethod)
426+
411427
labelNames, err := r.GetQuerier().LabelNames()
412428
if err != nil {
413429
t.Fatalf("could not get label names from querier")

0 commit comments

Comments
 (0)