forked from knative/serving
/
e2e.go
144 lines (125 loc) · 4.8 KB
/
e2e.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
/*
Copyright 2019 The Knative Authors
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 e2e
import (
"fmt"
"testing"
"time"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/wait"
// Mysteriously required to support GCP auth (required by k8s libs).
// Apparently just importing it is enough. @_@ side effects @_@.
// https://github.com/kubernetes/client-go/issues/242
_ "k8s.io/client-go/plugin/pkg/client/auth/gcp"
"github.com/google/go-cmp/cmp"
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
"knative.dev/pkg/system"
pkgTest "knative.dev/pkg/test"
"knative.dev/serving/pkg/apis/networking"
autoscalerconfig "knative.dev/serving/pkg/autoscaler/config"
"knative.dev/serving/test"
v1a1test "knative.dev/serving/test/v1alpha1"
)
// Setup creates the client objects needed in the e2e tests.
func Setup(t *testing.T) *test.Clients {
return SetupWithNamespace(t, test.ServingNamespace)
}
// SetupAlternativeNamespace creates the client objects needed in e2e tests
// under the alternative namespace.
func SetupAlternativeNamespace(t *testing.T) *test.Clients {
return SetupWithNamespace(t, test.AlternativeServingNamespace)
}
//SetupServingNamespaceforSecurityTesting creates the client objects needed in e2e tests
// under the security testing namespace.
func SetupServingNamespaceforSecurityTesting(t *testing.T) *test.Clients {
return SetupWithNamespace(t, test.ServingNamespaceforSecurityTesting)
}
// SetupWithNamespace creates the client objects needed in the e2e tests under the specified namespace.
func SetupWithNamespace(t *testing.T, namespace string) *test.Clients {
pkgTest.SetupLoggingFlags()
clients, err := test.NewClients(
pkgTest.Flags.Kubeconfig,
pkgTest.Flags.Cluster,
namespace)
if err != nil {
t.Fatalf("Couldn't initialize clients: %v", err)
}
return clients
}
// autoscalerCM returns the current autoscaler config map deployed to the
// test cluster.
func autoscalerCM(clients *test.Clients) (*autoscalerconfig.Config, error) {
autoscalerCM, err := clients.KubeClient.Kube.CoreV1().ConfigMaps("knative-serving").Get(
autoscalerconfig.ConfigName,
metav1.GetOptions{})
if err != nil {
return nil, err
}
return autoscalerconfig.NewConfigFromMap(autoscalerCM.Data)
}
// rawCM returns the raw knative config map for the given name
func rawCM(clients *test.Clients, name string) (*corev1.ConfigMap, error) {
return clients.KubeClient.Kube.CoreV1().ConfigMaps("knative-serving").Get(
name,
metav1.GetOptions{})
}
// patchCM updates the existing config map with the supplied value.
func patchCM(clients *test.Clients, cm *corev1.ConfigMap) (*corev1.ConfigMap, error) {
return clients.KubeClient.Kube.CoreV1().ConfigMaps("knative-serving").Update(cm)
}
// WaitForScaleToZero will wait for the specified deployment to scale to 0 replicas.
// Will wait up to 6 times the configured ScaleToZeroGracePeriod before failing.
func WaitForScaleToZero(t *testing.T, deploymentName string, clients *test.Clients) error {
t.Helper()
t.Logf("Waiting for %q to scale to zero", deploymentName)
cfg, err := autoscalerCM(clients)
if err != nil {
return fmt.Errorf("failed to get autoscaler configmap: %w", err)
}
return pkgTest.WaitForDeploymentState(
clients.KubeClient,
deploymentName,
func(d *appsv1.Deployment) (bool, error) {
return d.Status.ReadyReplicas == 0, nil
},
"DeploymentIsScaledDown",
test.ServingNamespace,
cfg.ScaleToZeroGracePeriod*6,
)
}
// waitForActivatorEndpoints waits for the Service endpoints to match that of activator.
func waitForActivatorEndpoints(resources *v1a1test.ResourceObjects, clients *test.Clients) error {
return wait.Poll(250*time.Millisecond, time.Minute, func() (bool, error) {
// We need to fetch the activator endpoints at every check, since it can change.
aeps, err := clients.KubeClient.Kube.CoreV1().Endpoints(
system.Namespace()).Get(networking.ActivatorServiceName, metav1.GetOptions{})
if err != nil {
return false, nil
}
svcEps, err := clients.KubeClient.Kube.CoreV1().Endpoints(test.ServingNamespace).Get(
resources.Revision.Status.ServiceName, metav1.GetOptions{})
if err != nil {
return false, err
}
if len(svcEps.Subsets) != len(aeps.Subsets) {
return false, nil
}
for i, ss := range svcEps.Subsets {
if !cmp.Equal(ss.Addresses, aeps.Subsets[i].Addresses) {
return false, nil
}
}
return true, nil
})
}