forked from GoogleCloudPlatform/prometheus-engine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain_test.go
189 lines (163 loc) · 5.9 KB
/
main_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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
// Copyright 2022 Google LLC
//
// 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
//
// https://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 e2e contains tests that validate the behavior of gmp-operator against a cluster.
// To make tests simple and fast, the test suite runs the operator internally. The CRDs
// are expected to be installed out of band (along with the operator deployment itself in
// a real world setup).
package e2e
import (
"context"
"flag"
"fmt"
"os"
"testing"
"time"
"go.uber.org/zap/zapcore"
metav1 "k8s.io/api/core/v1"
apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/client-go/kubernetes/scheme"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/log/zap"
"github.com/GoogleCloudPlatform/prometheus-engine/e2e/deploy"
"github.com/GoogleCloudPlatform/prometheus-engine/e2e/kube"
"github.com/GoogleCloudPlatform/prometheus-engine/pkg/operator"
)
const (
// Arbitrary amount of time to let tests exit cleanly before the main process
// terminates.
timeoutGracePeriod = 10 * time.Second
)
var (
projectID, location, cluster string
skipGCM bool
pollDuration time.Duration
gcpServiceAccount string
)
// TestMain injects custom flags to tests.
func TestMain(m *testing.M) {
ctrl.SetLogger(zap.New(zap.Level(zapcore.DebugLevel)))
flag.StringVar(&projectID, "project-id", "", "The GCP project to write metrics to.")
flag.StringVar(&location, "location", "", "The location of the Kubernetes cluster that's tested against.")
flag.StringVar(&cluster, "cluster", "", "The name of the Kubernetes cluster that's tested against.")
flag.BoolVar(&skipGCM, "skip-gcm", false, "Skip validating GCM ingested points.")
flag.DurationVar(&pollDuration, "duration", 3*time.Second, "How often to poll and retry for resources.")
flag.StringVar(&gcpServiceAccount, "gcp-service-account", "", "Path to GCP service account file for usage by deployed containers.")
flag.Parse()
os.Exit(m.Run())
}
func setupCluster(ctx context.Context, t testing.TB) (client.Client, *rest.Config, error) {
t.Log(">>> deploying static resources")
restConfig, err := newRestConfig()
if err != nil {
return nil, nil, err
}
kubeClient, err := newKubeClient(restConfig)
if err != nil {
return nil, nil, err
}
if err := createResources(ctx, kubeClient); err != nil {
return nil, nil, err
}
t.Log(">>> waiting for operator to be deployed")
if err := kube.WaitForDeploymentReady(ctx, kubeClient, operator.DefaultOperatorNamespace, operator.NameOperator); err != nil {
return nil, nil, err
}
t.Log(">>> waiting for operator to be ready")
if err := deploy.WaitForOperatorReady(ctx, kubeClient); err != nil {
return nil, nil, err
}
t.Log(">>> operator started successfully")
return kubeClient, restConfig, nil
}
func setRESTConfigDefaults(restConfig *rest.Config) error {
// https://github.com/kubernetes/client-go/issues/657
// https://github.com/kubernetes/client-go/issues/1159
// https://github.com/kubernetes/kubectl/blob/6fb6697c77304b7aaf43a520d30cb17563c69886/pkg/cmd/util/kubectl_match_version.go#L115
defaultGroupVersion := &schema.GroupVersion{Group: "", Version: "v1"}
if restConfig.GroupVersion == nil {
restConfig.GroupVersion = defaultGroupVersion
}
if restConfig.APIPath == "" {
restConfig.APIPath = "/api"
}
if restConfig.NegotiatedSerializer == nil {
restConfig.NegotiatedSerializer = scheme.Codecs.WithoutConversion()
}
return rest.SetKubernetesDefaults(restConfig)
}
func newRestConfig() (*rest.Config, error) {
rules := clientcmd.NewDefaultClientConfigLoadingRules()
restConfig, err := clientcmd.NewNonInteractiveDeferredLoadingClientConfig(rules, nil).ClientConfig()
if err != nil {
return nil, err
}
if err := setRESTConfigDefaults(restConfig); err != nil {
return nil, err
}
return restConfig, nil
}
func newKubeClient(restConfig *rest.Config) (client.Client, error) {
scheme, err := newScheme()
if err != nil {
return nil, err
}
return client.New(restConfig, client.Options{
Scheme: scheme,
})
}
func newScheme() (*runtime.Scheme, error) {
scheme, err := operator.NewScheme()
if err != nil {
return nil, err
}
if err := apiextensionsv1.AddToScheme(scheme); err != nil {
return nil, err
}
return scheme, nil
}
func createResources(ctx context.Context, kubeClient client.Client) error {
if err := deploy.CreateResources(ctx, kubeClient, deploy.WithMeta(projectID, cluster, location), deploy.WithDisableGCM(skipGCM)); err != nil {
return err
}
if gcpServiceAccount == "" {
gcpServiceAccount, _ = os.LookupEnv("GOOGLE_APPLICATION_CREDENTIALS")
}
if gcpServiceAccount != "" {
b, err := os.ReadFile(gcpServiceAccount)
if err != nil {
return fmt.Errorf("read service account file %q: %w", gcpServiceAccount, err)
}
if err := deploy.CreateGCPSecretResources(context.Background(), kubeClient, metav1.NamespaceDefault, b); err != nil {
return err
}
}
return nil
}
// contextWithDeadline returns a context that will timeout before t.Deadline().
// See: https://github.com/golang/go/issues/36532
func contextWithDeadline(t *testing.T) context.Context {
t.Helper()
deadline, ok := t.Deadline()
if !ok {
return context.Background()
}
ctx, cancel := context.WithDeadline(context.Background(), deadline.Truncate(timeoutGracePeriod))
t.Cleanup(cancel)
return ctx
}