forked from knative/serving
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathminscale_readiness_test.go
214 lines (173 loc) · 7.1 KB
/
minscale_readiness_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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
// +build e2e
/*
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"
"strconv"
"testing"
"time"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/wait"
"knative.dev/pkg/test/logstream"
"knative.dev/serving/pkg/apis/autoscaling"
v1 "knative.dev/serving/pkg/apis/serving/v1"
"knative.dev/serving/pkg/resources"
rtesting "knative.dev/serving/pkg/testing/v1"
"knative.dev/serving/test"
v1test "knative.dev/serving/test/v1"
)
func TestMinScale(t *testing.T) {
t.Parallel()
cancel := logstream.Start(t)
defer cancel()
const minScale = 4
clients := Setup(t)
names := test.ResourceNames{
// Config and Route have different names to avoid false positives
Config: test.ObjectNameForTest(t),
Route: test.ObjectNameForTest(t),
Image: "helloworld",
}
test.CleanupOnInterrupt(func() { test.TearDown(clients, names) })
defer test.TearDown(clients, names)
t.Log("Creating route")
if _, err := v1test.CreateRoute(t, clients, names); err != nil {
t.Fatal("Failed to create Route:", err)
}
t.Log("Creating configuration")
cfg, err := v1test.CreateConfiguration(t, clients, names, withMinScale(minScale))
if err != nil {
t.Fatal("Failed to create Configuration:", err)
}
revName := latestRevisionName(t, clients, names.Config, "")
serviceName := privateServiceName(t, clients, revName)
t.Log("Waiting for revision to scale to minScale before becoming ready")
if err := waitForDesiredScale(clients, serviceName, gte(minScale)); err != nil {
t.Fatalf("The revision %q did not scale >= %d before becoming ready: %v", revName, minScale, err)
}
t.Log("Waiting for revision to become ready")
if err := v1test.WaitForRevisionState(
clients.ServingClient, revName, v1test.IsRevisionReady, "RevisionIsReady",
); err != nil {
t.Fatalf("The Revision %q did not become ready: %v", revName, err)
}
t.Log("Holding revision at minScale after becoming ready")
if !ensureDesiredScale(clients, serviceName, gte(minScale)) {
t.Fatalf("The revision %q did not stay at scale >= %d after becoming ready", revName, minScale)
}
t.Log("Updating configuration")
if _, err := v1test.PatchConfig(t, clients, cfg, rtesting.WithConfigEnv(corev1.EnvVar{Name: "FOO", Value: "BAR"})); err != nil {
t.Fatal("Failed to update Configuration:", err)
}
newRevName := latestRevisionName(t, clients, names.Config, revName)
newServiceName := privateServiceName(t, clients, newRevName)
t.Log("Waiting for new revision to scale to minScale after update")
if err := waitForDesiredScale(clients, newServiceName, gte(minScale)); err != nil {
t.Fatalf("The revision %q did not scale >= %d after creating route: %v", newRevName, minScale, err)
}
t.Log("Waiting for new revision to become ready")
if err := v1test.WaitForRevisionState(
clients.ServingClient, newRevName, v1test.IsRevisionReady, "RevisionIsReady",
); err != nil {
t.Fatalf("The Revision %q did not become ready: %v", newRevName, err)
}
t.Log("Holding new revision at minScale after becoming ready")
if !ensureDesiredScale(clients, newServiceName, gte(minScale)) {
t.Fatalf("The new revision %q did not stay at scale >= %d after becoming ready", newRevName, minScale)
}
t.Log("Waiting for old revision to scale below minScale after being replaced")
if err := waitForDesiredScale(clients, serviceName, lt(minScale)); err != nil {
t.Fatalf("The revision %q did not scale < minScale after being replaced: %v", revName, err)
}
t.Log("Deleting route")
if err := clients.ServingClient.Routes.Delete(names.Route, &metav1.DeleteOptions{}); err != nil {
t.Fatalf("Failed to delete route %q: %v", names.Route, err)
}
t.Log("Waiting for new revision to scale below minScale when there is no route")
if err := waitForDesiredScale(clients, newServiceName, lt(minScale)); err != nil {
t.Fatalf("The revision %q did not scale < minScale after being replaced: %v", newRevName, err)
}
}
func gte(m int) func(int) bool {
return func(n int) bool {
return n >= m
}
}
func lt(m int) func(int) bool {
return func(n int) bool {
return n < m
}
}
func withMinScale(minScale int) func(cfg *v1.Configuration) {
return func(cfg *v1.Configuration) {
if cfg.Spec.Template.Annotations == nil {
cfg.Spec.Template.Annotations = make(map[string]string, 1)
}
cfg.Spec.Template.Annotations[autoscaling.MinScaleAnnotationKey] = strconv.Itoa(minScale)
}
}
func latestRevisionName(t *testing.T, clients *test.Clients, configName, oldRevName string) string {
// Wait for the Config have a LatestCreatedRevisionName
if err := v1test.WaitForConfigurationState(
clients.ServingClient, configName,
func(c *v1.Configuration) (bool, error) {
return c.Status.LatestCreatedRevisionName != oldRevName, nil
}, "ConfigurationHasUpdatedCreatedRevision",
); err != nil {
t.Fatalf("The Configuration %q has not updated LatestCreatedRevisionName from %q: %v", configName, oldRevName, err)
}
config, err := clients.ServingClient.Configs.Get(configName, metav1.GetOptions{})
if err != nil {
t.Fatal("Failed to get Configuration after it was seen to be live:", err)
}
return config.Status.LatestCreatedRevisionName
}
func privateServiceName(t *testing.T, clients *test.Clients, revisionName string) string {
var privateServiceName string
if err := wait.PollImmediate(time.Second, 1*time.Minute, func() (bool, error) {
sks, err := clients.NetworkingClient.ServerlessServices.Get(revisionName, metav1.GetOptions{})
if err != nil {
return false, nil
}
privateServiceName = sks.Status.PrivateServiceName
return privateServiceName != "", nil
}); err != nil {
t.Fatalf("Error retrieving sks %q: %v", revisionName, err)
}
return privateServiceName
}
func waitForDesiredScale(clients *test.Clients, serviceName string, cond func(int) bool) error {
endpoints := clients.KubeClient.Kube.CoreV1().Endpoints(test.ServingNamespace)
return wait.PollImmediate(250*time.Millisecond, 1*time.Minute, func() (bool, error) {
endpoint, err := endpoints.Get(serviceName, metav1.GetOptions{})
if err != nil {
return false, nil
}
return cond(resources.ReadyAddressCount(endpoint)), nil
})
}
func ensureDesiredScale(clients *test.Clients, serviceName string, cond func(int) bool) bool {
endpoints := clients.KubeClient.Kube.CoreV1().Endpoints(test.ServingNamespace)
return wait.PollImmediate(250*time.Millisecond, 5*time.Second, func() (bool, error) {
endpoint, err := endpoints.Get(serviceName, metav1.GetOptions{})
if err != nil {
return false, nil
}
if scale := resources.ReadyAddressCount(endpoint); !cond(scale) {
return false, fmt.Errorf("scale %d didn't meet condition", scale)
}
return false, nil
}) == wait.ErrWaitTimeout
}