|
| 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 | +} |
0 commit comments