forked from knative/serving
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathminscale_readiness_test.go
More file actions
152 lines (121 loc) · 4.66 KB
/
minscale_readiness_test.go
File metadata and controls
152 lines (121 loc) · 4.66 KB
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
// +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 (
"strconv"
"time"
"testing"
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"
"knative.dev/serving/pkg/apis/serving/v1alpha1"
"knative.dev/serving/test"
v1a1test "knative.dev/serving/test/v1alpha1"
)
func TestMinScale(t *testing.T) {
t.Parallel()
cancel := logstream.Start(t)
defer cancel()
const minScale = 4
clients := Setup(t)
name := test.ObjectNameForTest(t)
names := test.ResourceNames{
Config: name,
Route: name,
Image: "helloworld",
}
t.Log("Creating configuration")
if _, err := v1a1test.CreateConfiguration(t, clients, names, withMinScale(minScale)); err != nil {
t.Fatalf("Failed to create Configuration: %v", err)
}
test.CleanupOnInterrupt(func() { test.TearDown(clients, names) })
defer test.TearDown(clients, names)
revName := latestRevisionName(t, clients, names.Config)
deploymentName := revName + "-deployment"
// Before becoming ready, observe minScale
t.Log("Waiting for revision to scale to minScale before becoming ready")
if err := waitForDesiredScale(t, clients, deploymentName, gte(minScale)); err != nil {
t.Fatalf("The deployment %q did not scale >= %d before becoming ready: %v", deploymentName, minScale, err)
}
// Revision becomes ready
if err := v1a1test.WaitForRevisionState(
clients.ServingAlphaClient, revName, v1a1test.IsRevisionReady, "RevisionIsReady",
); err != nil {
t.Fatalf("The Revision %q did not become ready: %v", revName, err)
}
// Without a route, ignore minScale
t.Log("Waiting for revision to scale below minScale after becoming ready")
if err := waitForDesiredScale(t, clients, deploymentName, lt(minScale)); err != nil {
t.Fatalf("The deployment %q did not scale < minScale after becoming ready: %v", deploymentName, err)
}
// Create route
t.Log("Creating route")
if _, err := v1a1test.CreateRoute(t, clients, names); err != nil {
t.Fatalf("Failed to create Route: %v", err)
}
// Route becomes ready
if err := v1a1test.WaitForRouteState(
clients.ServingAlphaClient, names.Route, v1a1test.IsRouteReady, "RouteIsReady",
); err != nil {
t.Fatalf("The Route %q is not ready: %v", names.Route, err)
}
// With a route, observe minScale
t.Log("Waiting for revision to scale to minScale after creating route")
if err := waitForDesiredScale(t, clients, deploymentName, gte(minScale)); err != nil {
t.Fatalf("The deployment %q did not scale >= %d after creating route: %v", deploymentName, minScale, err)
}
}
func gte(m int) func(int32) bool {
return func(n int32) bool {
return n >= int32(m)
}
}
func lt(m int) func(int32) bool {
return func(n int32) bool {
return n < int32(m)
}
}
func withMinScale(minScale int) func(cfg *v1alpha1.Configuration) {
return func(cfg *v1alpha1.Configuration) {
if cfg.Spec.Template.Annotations == nil {
cfg.Spec.Template.Annotations = make(map[string]string)
}
cfg.Spec.Template.Annotations[autoscaling.MinScaleAnnotationKey] = strconv.Itoa(minScale)
}
}
func latestRevisionName(t *testing.T, clients *test.Clients, configName string) string {
// Wait for the Config have a LatestCreatedRevisionName
if err := v1a1test.WaitForConfigurationState(
clients.ServingAlphaClient, configName,
v1a1test.ConfigurationHasCreatedRevision, "ConfigurationHasCreatedRevision",
); err != nil {
t.Fatalf("The Configuration %q does not have a LatestCreatedRevisionName: %v", configName, err)
}
config, err := clients.ServingAlphaClient.Configs.Get(configName, metav1.GetOptions{})
if err != nil {
t.Fatalf("Failed to get Configuration after it was seen to be live: %v", err)
}
return config.Status.LatestCreatedRevisionName
}
func waitForDesiredScale(t *testing.T, clients *test.Clients, deploymentName string, cond func(int32) bool) error {
deployments := clients.KubeClient.Kube.AppsV1().Deployments(test.ServingNamespace)
return wait.PollImmediate(time.Second, 1*time.Minute, func() (bool, error) {
deployment, err := deployments.Get(deploymentName, metav1.GetOptions{})
if err != nil {
return false, nil
}
return cond(*deployment.Spec.Replicas), nil
})
}