This repository has been archived by the owner on Dec 29, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
e2e_test.go
92 lines (87 loc) · 3.19 KB
/
e2e_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
/*
Copyright 2017 Google Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package integration
import (
"context"
"flag"
"testing"
"golang.org/x/oauth2/google"
monitoring "google.golang.org/api/monitoring/v3"
)
var (
clusterLocation = flag.String("cluster-location", "", "the location of the cluster used by kubectl in the context where this test runs")
clusterName = flag.String("cluster-name", "", "the name of the cluster used by kubectl in the context where this test runs")
namespaceName = flag.String("namespace-name", "", "the name of the namespace used by kubectl in the context where this test runs")
projectID = flag.String("project-id", "", "the project ID used by kubectl in the context where this test runs")
integration = flag.Bool("integration", false, "whether to run integration tests")
)
func TestE2E(t *testing.T) {
if !*integration {
t.Skip("skipping integration test: disabled")
}
if *clusterLocation == "" {
t.Fatalf("the cluster location must not be empty")
}
t.Logf("Cluster location: %s", *clusterLocation)
if *clusterName == "" {
t.Fatalf("the cluster name must not be empty")
}
t.Logf("Cluster name: %s", *clusterName)
if *namespaceName == "" {
t.Fatalf("the namespace name must not be empty")
}
t.Logf("Namespace name: %s", *namespaceName)
if *projectID == "" {
t.Fatalf("the project ID must not be empty")
}
t.Logf("Project ID: %s", *projectID)
// Container name launched by stackdriver-prometheus-sidecar/kube/full/deploy.sh.
const containerName = "kube-state-metrics"
t.Run("gke_container", func(t *testing.T) {
client, err := google.DefaultClient(
context.Background(), monitoring.MonitoringReadScope)
if err != nil {
t.Fatalf("Failed to get Google OAuth2 credentials: %v", err)
}
stackdriverService, err := monitoring.New(client)
if err != nil {
t.Fatalf("Failed to create Stackdriver client: %v", err)
}
t.Logf("Successfully created Stackdriver client")
// We don't provide "instance_id" and "pod_id" labels because
// they're generated automatically by the managed
// deployment. "namespace_name" should be sufficient to uniquely
// identify the time series.
value, err := fetchFloat64Metric(
stackdriverService,
*projectID,
&monitoring.MonitoredResource{
Type: "k8s_container",
Labels: map[string]string{
"project_id": *projectID,
"cluster_name": *clusterName,
"namespace_name": *namespaceName,
"container_name": containerName,
"location": *clusterLocation,
},
}, &monitoring.Metric{
Type: "external.googleapis.com/prometheus/up",
})
if err != nil {
t.Fatalf("Failed to fetch metric: %v", err)
}
if value != 1 {
t.Errorf("expected metric value %v, got %v", 1, value)
}
})
}