Skip to content

Commit 7851aa3

Browse files
committed
kube-prometheus: Add e2e tests
1 parent 5321022 commit 7851aa3

File tree

13 files changed

+2354
-1
lines changed

13 files changed

+2354
-1
lines changed

Diff for: .travis.yml

+2
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ jobs:
2222
# E2e tests
2323
- script: ./scripts/travis-e2e.sh
2424
- script: ./scripts/travis-e2e-helm.sh
25+
# kube-prometheus e2e tests
26+
- script: ./contrib/kube-prometheus/tests/e2e/travis-e2e.sh
2527

2628
- stage: deploy
2729
script: skip

Diff for: Gopkg.lock

+9
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: Gopkg.toml

+4
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@
2020
# version = "2.4.0"
2121

2222

23+
[[constraint]]
24+
name = "github.com/Jeffail/gabs"
25+
version = "1.0.0"
26+
2327
[[constraint]]
2428
branch = "master"
2529
name = "github.com/ant31/crd-validation"

Diff for: Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ K8S_GEN_DEPS+=$(foreach bin,$(K8S_GEN_BINARIES),$(FIRST_GOPATH)/bin/$(bin))
3232
K8S_GEN_DEPS+=$(OPENAPI_GEN_BINARY)
3333

3434
GOLANG_FILES:=$(shell find . -name \*.go -print)
35-
pkgs = $(shell go list ./... | grep -v /vendor/ | grep -v /test/)
35+
pkgs = $(shell go list ./... | grep -v /vendor/ | grep -v /test/ | grep -v /contrib/)
3636

3737
.PHONY: all
3838
all: format generate build test

Diff for: contrib/kube-prometheus/Makefile

+3
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,9 @@ test: $(JB_BINARY)
4242
$(JB_BINARY) install
4343
./test.sh
4444

45+
test-e2e:
46+
go test -timeout 55m -v ./tests/e2e
47+
4548
test-in-docker: ../../hack/jsonnet-docker-image
4649
@echo ">> Compiling assets and generating Kubernetes manifests"
4750
docker run \

Diff for: contrib/kube-prometheus/tests/e2e/main_test.go

+118
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
// Copyright 2019 The prometheus-operator Authors
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package e2e
16+
17+
import (
18+
"log"
19+
"os"
20+
"testing"
21+
"time"
22+
23+
"github.com/pkg/errors"
24+
"k8s.io/apimachinery/pkg/util/wait"
25+
"k8s.io/client-go/kubernetes"
26+
"k8s.io/client-go/tools/clientcmd"
27+
)
28+
29+
var promClient *prometheusClient
30+
31+
func TestMain(m *testing.M) {
32+
os.Exit(testMain(m))
33+
}
34+
35+
// testMain circumvents the issue, that one can not call `defer` in TestMain, as
36+
// `os.Exit` does not honor `defer` statements. For more details see:
37+
// http://blog.englund.nu/golang,/testing/2017/03/12/using-defer-in-testmain.html
38+
func testMain(m *testing.M) int {
39+
kubeConfigPath, ok := os.LookupEnv("KUBECONFIG")
40+
if !ok {
41+
log.Fatal("failed to retrieve KUBECONFIG env var")
42+
}
43+
44+
config, err := clientcmd.BuildConfigFromFlags("", kubeConfigPath)
45+
if err != nil {
46+
log.Fatal(err)
47+
}
48+
49+
kubeClient, err := kubernetes.NewForConfig(config)
50+
if err != nil {
51+
log.Fatal(errors.Wrap(err, "creating kubeClient failed"))
52+
}
53+
54+
promClient = newPrometheusClient(kubeClient)
55+
56+
return m.Run()
57+
}
58+
59+
func TestQueryPrometheus(t *testing.T) {
60+
t.Parallel()
61+
queries := []struct {
62+
query string
63+
expectN int
64+
}{
65+
{
66+
// query: `up{job="node-exporter"} == 1`,
67+
// expectN: 1,
68+
// }, {
69+
// query: `up{job="kubelet"} == 1`,
70+
// expectN: 1,
71+
// }, {
72+
query: `up{job="apiserver"} == 1`,
73+
expectN: 1,
74+
// }, {
75+
// query: `up{job="kube-state-metrics"} == 1`,
76+
// expectN: 1,
77+
}, {
78+
query: `up{job="prometheus-k8s"} == 1`,
79+
expectN: 1,
80+
}, {
81+
query: `up{job="prometheus-operator"} == 1`,
82+
expectN: 1,
83+
}, {
84+
query: `up{job="alertmanager-main"} == 1`,
85+
expectN: 2,
86+
},
87+
}
88+
89+
// Wait for pod to respond at queries at all. Then start verifying their results.
90+
err := wait.Poll(5*time.Second, 1*time.Minute, func() (bool, error) {
91+
_, err := promClient.query("up")
92+
return err == nil, nil
93+
})
94+
if err != nil {
95+
t.Fatal(errors.Wrap(err, "wait for prometheus-k8s"))
96+
}
97+
98+
err = wait.Poll(5*time.Second, 1*time.Minute, func() (bool, error) {
99+
defer t.Log("---------------------------\n")
100+
101+
for _, q := range queries {
102+
n, err := promClient.query(q.query)
103+
if err != nil {
104+
return false, err
105+
}
106+
if n < q.expectN {
107+
// Don't return an error as targets may only become visible after a while.
108+
t.Logf("expected at least %d results for %q but got %d", q.expectN, q.query, n)
109+
return false, nil
110+
}
111+
t.Logf("query %q succeeded", q.query)
112+
}
113+
return true, nil
114+
})
115+
if err != nil {
116+
t.Fatal(err)
117+
}
118+
}
+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
// Copyright 2019 The prometheus-operator Authors
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package e2e
16+
17+
import (
18+
"k8s.io/client-go/kubernetes"
19+
20+
"github.com/Jeffail/gabs"
21+
)
22+
23+
type prometheusClient struct {
24+
kubeClient kubernetes.Interface
25+
}
26+
27+
func newPrometheusClient(kubeClient kubernetes.Interface) *prometheusClient {
28+
return &prometheusClient{kubeClient}
29+
}
30+
31+
// Query makes a request against the Prometheus /api/v1/query endpoint.
32+
func (c *prometheusClient) query(query string) (int, error) {
33+
req := c.kubeClient.CoreV1().RESTClient().Get().
34+
Namespace("monitoring").
35+
Resource("pods").
36+
SubResource("proxy").
37+
Name("prometheus-k8s-0:9090").
38+
Suffix("/api/v1/query").Param("query", query)
39+
40+
b, err := req.DoRaw()
41+
if err != nil {
42+
return 0, err
43+
}
44+
45+
res, err := gabs.ParseJSON(b)
46+
if err != nil {
47+
return 0, err
48+
}
49+
50+
n, err := res.ArrayCountP("data.result")
51+
return n, err
52+
}

Diff for: contrib/kube-prometheus/tests/e2e/travis-e2e.sh

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#!/usr/bin/env bash
2+
# exit immediately when a command fails
3+
set -e
4+
# only exit with zero if all commands of the pipeline exit successfully
5+
set -o pipefail
6+
# error on unset variables
7+
set -u
8+
# print each command before executing it
9+
set -x
10+
11+
SCRIPT_DIR=$(dirname "${BASH_SOURCE[0]}")
12+
13+
"${SCRIPT_DIR}"/../../../../scripts/create-minikube.sh
14+
15+
# waiting for kube-dns to be ready
16+
JSONPATH='{range .items[*]}{@.metadata.name}:{range @.status.conditions[*]}{@.type}={@.status};{end}{end}'; until kubectl -n kube-system get pods -lk8s-app=kube-dns -o jsonpath="$JSONPATH" 2>&1 | grep -q "Ready=True"; do sleep 1;echo "waiting for kube-dns to be available"; kubectl get pods --all-namespaces; done
17+
18+
(
19+
cd "${SCRIPT_DIR}"/../.. || exit
20+
kubectl apply -f manifests
21+
KUBECONFIG=~/.kube/config make test-e2e
22+
)
23+
24+
"${SCRIPT_DIR}"/../../../../scripts/delete-minikube.sh

Diff for: vendor/github.com/Jeffail/gabs/LICENSE

+19
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)