-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathautoscale_test.go
224 lines (190 loc) · 6.52 KB
/
autoscale_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
215
216
217
218
219
220
221
222
223
224
// +build e2e
/*
Copyright 2018 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 (
"strings"
"testing"
"github.com/knative/serving/test"
"go.uber.org/zap"
"k8s.io/api/core/v1"
"k8s.io/api/extensions/v1beta1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
const (
autoscaleExpectedOutput = "39999983"
)
var (
initialScaleToZeroThreshold string
)
func isDeploymentScaledUp() func(d *v1beta1.Deployment) (bool, error) {
return func(d *v1beta1.Deployment) (bool, error) {
return d.Status.ReadyReplicas >= 1, nil
}
}
func isDeploymentScaledToZero() func(d *v1beta1.Deployment) (bool, error) {
return func(d *v1beta1.Deployment) (bool, error) {
return d.Status.ReadyReplicas == 0, nil
}
}
func generateTrafficBurst(clients *test.Clients, logger *zap.SugaredLogger, num int, domain string) {
concurrentRequests := make(chan bool, num)
logger.Infof("Performing %d concurrent requests.", num)
for i := 0; i < num; i++ {
go func() {
test.WaitForEndpointState(clients.Kube,
logger,
test.Flags.ResolvableDomain,
domain,
test.EventuallyMatchesBody(autoscaleExpectedOutput),
"MakingConcurrentRequests")
concurrentRequests <- true
}()
}
logger.Infof("Waiting for all requests to complete.")
for i := 0; i < num; i++ {
<-concurrentRequests
}
}
func getAutoscalerConfigMap(clients *test.Clients) (*v1.ConfigMap, error) {
return clients.Kube.CoreV1().ConfigMaps("knative-serving").Get("config-autoscaler", metav1.GetOptions{})
}
func setScaleToZeroThreshold(clients *test.Clients, threshold string) error {
configMap, err := getAutoscalerConfigMap(clients)
if err != nil {
return err
}
configMap.Data["scale-to-zero-threshold"] = threshold
_, err = clients.Kube.CoreV1().ConfigMaps("knative-serving").Update(configMap)
return err
}
func setup(t *testing.T, logger *zap.SugaredLogger) *test.Clients {
clients := Setup(t)
configMap, err := getAutoscalerConfigMap(clients)
if err != nil {
logger.Infof("Unable to retrieve the autoscale configMap. Assuming a ScaleToZero value of '5m'. %v", err)
initialScaleToZeroThreshold = "5m"
} else {
initialScaleToZeroThreshold = configMap.Data["scale-to-zero-threshold"]
}
err = setScaleToZeroThreshold(clients, "1m")
if err != nil {
t.Fatalf(`Unable to set ScaleToZeroThreshold to '1m'. This will
cause the test to time out. Failing fast instead. %v`, err)
}
return clients
}
func tearDown(clients *test.Clients, names test.ResourceNames, logger *zap.SugaredLogger) {
setScaleToZeroThreshold(clients, initialScaleToZeroThreshold)
TearDown(clients, names, logger)
}
func TestAutoscaleUpDownUp(t *testing.T) {
//add test case specific name to its own logger
logger := test.GetContextLogger("TestAutoscaleUpDownUp")
clients := setup(t, logger)
imagePath := strings.Join(
[]string{
test.Flags.DockerRepo,
"autoscale"},
"/")
logger.Infof("Creating a new Route and Configuration")
names, err := CreateRouteAndConfig(clients, logger, imagePath)
if err != nil {
t.Fatalf("Failed to create Route and Configuration: %v", err)
}
test.CleanupOnInterrupt(func() { tearDown(clients, names, logger) }, logger)
defer tearDown(clients, names, logger)
logger.Infof(`When the Revision can have traffic routed to it,
the Route is marked as Ready.`)
err = test.WaitForRouteState(
clients.Routes,
names.Route,
test.IsRouteReady,
"RouteIsReady")
if err != nil {
t.Fatalf(`The Route %s was not marked as Ready to serve traffic:
%v`, names.Route, err)
}
logger.Infof("Serves the expected data at the endpoint")
config, err := clients.Configs.Get(names.Config, metav1.GetOptions{})
if err != nil {
t.Fatalf(`Configuration %s was not updated with the new
revision: %v`, names.Config, err)
}
deploymentName :=
config.Status.LatestCreatedRevisionName + "-deployment"
route, err := clients.Routes.Get(names.Route, metav1.GetOptions{})
if err != nil {
t.Fatalf("Error fetching Route %s: %v", names.Route, err)
}
domain := route.Status.Domain
err = test.WaitForEndpointState(
clients.Kube,
logger,
test.Flags.ResolvableDomain,
domain,
test.EventuallyMatchesBody(autoscaleExpectedOutput),
"CheckingEndpointAfterUpdating")
if err != nil {
t.Fatalf(`The endpoint for Route %s at domain %s didn't serve
the expected text \"%v\": %v`,
names.Route, domain, autoscaleExpectedOutput, err)
}
logger.Infof(`The autoscaler spins up additional replicas when traffic
increases.`)
generateTrafficBurst(clients, logger, 5, domain)
err = test.WaitForDeploymentState(
clients.Kube.ExtensionsV1beta1().Deployments(test.Flags.Namespace),
deploymentName,
isDeploymentScaledUp(),
"DeploymentIsScaledUp")
if err != nil {
logger.Fatalf(`Unable to observe the Deployment named %s scaling
up. %s`, deploymentName, err)
}
logger.Infof(`The autoscaler successfully scales down when devoid of
traffic.`)
logger.Infof(`Manually setting ScaleToZeroThreshold to '1m' to facilitate
faster testing.`)
err = test.WaitForDeploymentState(
clients.Kube.ExtensionsV1beta1().Deployments(test.Flags.Namespace),
deploymentName,
isDeploymentScaledToZero(),
"DeploymentScaledToZero")
if err != nil {
logger.Fatalf(`Unable to observe the Deployment named %s scaling
down. %s`, deploymentName, err)
}
// Account for the case where scaling up uses all available pods.
logger.Infof("Wait until there are pods available to scale into.")
pc := clients.Kube.CoreV1().Pods(test.Flags.Namespace)
err = test.WaitForPodListState(
pc,
func(p *v1.PodList) (bool, error) {
return len(p.Items) == 0, nil
},
"WaitForAvailablePods")
logger.Infof("Scaled down.")
logger.Infof(`The autoscaler spins up additional replicas once again when
traffic increases.`)
generateTrafficBurst(clients, logger, 8, domain)
err = test.WaitForDeploymentState(
clients.Kube.ExtensionsV1beta1().Deployments(test.Flags.Namespace),
deploymentName,
isDeploymentScaledUp(),
"DeploymentScaledUp")
if err != nil {
logger.Fatalf(`Unable to observe the Deployment named %s scaling
up. %s`, deploymentName, err)
}
}