Skip to content

Commit e697ad0

Browse files
committed
Add metrics test.
1 parent 40d0e7c commit e697ad0

File tree

2 files changed

+171
-0
lines changed

2 files changed

+171
-0
lines changed

tests/metrics_test.go

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
//
2+
// DISCLAIMER
3+
//
4+
// Copyright 2019 ArangoDB GmbH, Cologne, Germany
5+
//
6+
// Licensed under the Apache License, Version 2.0 (the "License");
7+
// you may not use this file except in compliance with the License.
8+
// You may obtain a copy of the License at
9+
//
10+
// http://www.apache.org/licenses/LICENSE-2.0
11+
//
12+
// Unless required by applicable law or agreed to in writing, software
13+
// distributed under the License is distributed on an "AS IS" BASIS,
14+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
// See the License for the specific language governing permissions and
16+
// limitations under the License.
17+
//
18+
// Copyright holder is ArangoDB GmbH, Cologne, Germany
19+
//
20+
// Author Max Neunhoeffer
21+
//
22+
package tests
23+
24+
import (
25+
"context"
26+
"fmt"
27+
"testing"
28+
"time"
29+
30+
api "github.com/arangodb/kube-arangodb/pkg/apis/deployment/v1alpha"
31+
kubeArangoClient "github.com/arangodb/kube-arangodb/pkg/client"
32+
"github.com/arangodb/kube-arangodb/pkg/util"
33+
"github.com/dchest/uniuri"
34+
"github.com/pkg/errors"
35+
corev1 "k8s.io/api/core/v1"
36+
)
37+
38+
func TestAddingMetrics(t *testing.T) {
39+
longOrSkip(t)
40+
41+
ns := getNamespace(t)
42+
kubecli := mustNewKubeClient(t)
43+
c := kubeArangoClient.MustNewInCluster()
44+
45+
depl := newDeployment(fmt.Sprintf("%s-%s", "arangodb-metrics-test", uniuri.NewLen(4)))
46+
depl.Spec.Mode = api.NewMode(api.DeploymentModeCluster)
47+
depl.Spec.StorageEngine = api.NewStorageEngine(api.StorageEngineRocksDB)
48+
depl.Spec.TLS = api.TLSSpec{} // should auto-generate cert
49+
depl.Spec.SetDefaults(depl.GetName()) // this must be last
50+
51+
// Create deployment
52+
deployment, err := c.DatabaseV1alpha().ArangoDeployments(ns).Create(depl)
53+
if err != nil {
54+
t.Fatalf("Create deployment failed: %v", err)
55+
}
56+
defer deferedCleanupDeployment(c, depl.GetName(), ns)
57+
58+
// Wait for deployment to be ready
59+
deployment, err = waitUntilDeployment(c, depl.GetName(), ns, deploymentIsReady())
60+
if err != nil {
61+
t.Fatalf("Deployment not running in time: %v", err)
62+
}
63+
64+
// Create a database client
65+
ctx := context.Background()
66+
DBClient := mustNewArangodDatabaseClient(ctx, kubecli, deployment, t, nil)
67+
if err := waitUntilArangoDeploymentHealthy(deployment, DBClient, kubecli, ""); err != nil {
68+
t.Fatalf("Deployment not healthy in time: %v", err)
69+
}
70+
71+
// Try to switch on metrics:
72+
deployment, err = updateDeployment(c, depl.GetName(), ns,
73+
func(depl *api.DeploymentSpec) {
74+
depl.Metrics = api.MetricsSpec{
75+
Enabled: util.NewBool(true),
76+
Image: util.NewString("arangodb/arangodb-exporter:0.1.6"),
77+
}
78+
})
79+
if err != nil {
80+
t.Fatalf("Failed to add metrics")
81+
} else {
82+
t.Log("Updated deployment by adding metrics")
83+
}
84+
85+
if err := waitUntilArangoDeploymentHealthy(deployment, DBClient, kubecli, ""); err != nil {
86+
t.Errorf("Deployment not healthy in time: %v", err)
87+
} else {
88+
t.Log("Deployment healthy")
89+
}
90+
91+
_, err = waitUntilService(kubecli, depl.GetName()+"-exporter", ns,
92+
func(service *corev1.Service) error {
93+
return nil
94+
}, time.Second*30)
95+
if err != nil {
96+
t.Errorf("Exporter service did not show up in time")
97+
} else {
98+
t.Log("Found exporter service")
99+
}
100+
101+
_, err = waitUntilEndpoints(kubecli, depl.GetName()+"-exporter", ns,
102+
func(endpoints *corev1.Endpoints) error {
103+
count := 0
104+
for _, subset := range endpoints.Subsets {
105+
count += len(subset.Addresses)
106+
}
107+
t.Logf("Found %d endpoints in the Endpoints resource", count)
108+
if count < 6 {
109+
return errors.New("did not find enough endpoints in Endpoints resource")
110+
}
111+
return nil
112+
}, time.Second*360) // needs a full rotation with extra containers
113+
if err != nil {
114+
t.Errorf("Exporter endpoints did not show up in time")
115+
} else {
116+
t.Log("Found exporter endpoints")
117+
}
118+
119+
// Cleanup
120+
removeDeployment(c, depl.GetName(), ns)
121+
}

tests/test_util.go

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -341,6 +341,56 @@ func waitUntilSecret(cli kubernetes.Interface, secretName, ns string, predicate
341341
return result, nil
342342
}
343343

344+
// waitUntilService waits until a service with given name in given
345+
// namespace exists and has reached a state where the given predicate
346+
// returns nil.
347+
func waitUntilService(cli kubernetes.Interface, serviceName, ns string, predicate func(*v1.Service) error, timeout time.Duration) (*v1.Service, error) {
348+
var result *v1.Service
349+
op := func() error {
350+
obj, err := cli.CoreV1().Services(ns).Get(serviceName, metav1.GetOptions{})
351+
if err != nil {
352+
result = nil
353+
return maskAny(err)
354+
}
355+
result = obj
356+
if predicate != nil {
357+
if err := predicate(obj); err != nil {
358+
return maskAny(err)
359+
}
360+
}
361+
return nil
362+
}
363+
if err := retry.Retry(op, timeout); err != nil {
364+
return nil, maskAny(err)
365+
}
366+
return result, nil
367+
}
368+
369+
// waitUntilEndpoints waits until an endpoints resource with given name
370+
// in given namespace exists and has reached a state where the given
371+
// predicate returns nil.
372+
func waitUntilEndpoints(cli kubernetes.Interface, serviceName, ns string, predicate func(*v1.Endpoints) error, timeout time.Duration) (*v1.Endpoints, error) {
373+
var result *v1.Endpoints
374+
op := func() error {
375+
obj, err := cli.CoreV1().Endpoints(ns).Get(serviceName, metav1.GetOptions{})
376+
if err != nil {
377+
result = nil
378+
return maskAny(err)
379+
}
380+
result = obj
381+
if predicate != nil {
382+
if err := predicate(obj); err != nil {
383+
return maskAny(err)
384+
}
385+
}
386+
return nil
387+
}
388+
if err := retry.Retry(op, timeout); err != nil {
389+
return nil, maskAny(err)
390+
}
391+
return result, nil
392+
}
393+
344394
// waitUntilSecretNotFound waits until a secret with given name in given namespace
345395
// is no longer found.
346396
func waitUntilSecretNotFound(cli kubernetes.Interface, secretName, ns string, timeout time.Duration) error {

0 commit comments

Comments
 (0)