forked from knative/serving
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhelloworld_test.go
More file actions
205 lines (181 loc) · 7.11 KB
/
helloworld_test.go
File metadata and controls
205 lines (181 loc) · 7.11 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
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
// +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 (
"fmt"
"strings"
"testing"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/resource"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
pkgTest "knative.dev/pkg/test"
"knative.dev/pkg/test/logstream"
"knative.dev/serving/pkg/apis/serving"
v1a1opts "knative.dev/serving/pkg/testing/v1alpha1"
"knative.dev/serving/test"
v1a1test "knative.dev/serving/test/v1alpha1"
)
func TestHelloWorld(t *testing.T) {
t.Parallel()
cancel := logstream.Start(t)
defer cancel()
clients := Setup(t)
names := test.ResourceNames{
Service: test.ObjectNameForTest(t),
Image: "helloworld",
}
if test.ServingFlags.Https {
// Save the current Gateway to restore it after the test
oldGateway, err := clients.IstioClient.NetworkingV1alpha3().Gateways(v1a1test.Namespace).Get(v1a1test.GatewayName, metav1.GetOptions{})
if err != nil {
t.Fatalf("Failed to get Gateway %s/%s", v1a1test.Namespace, v1a1test.GatewayName)
}
test.CleanupOnInterrupt(func() { v1a1test.RestoreGateway(t, clients, *oldGateway) })
defer v1a1test.RestoreGateway(t, clients, *oldGateway)
}
test.CleanupOnInterrupt(func() { test.TearDown(clients, names) })
defer test.TearDown(clients, names)
t.Log("Creating a new Service")
resources, httpsTransportOption, err := v1a1test.CreateRunLatestServiceReady(t, clients, &names, test.ServingFlags.Https)
if err != nil {
t.Fatalf("Failed to create initial Service: %v: %v", names.Service, err)
}
url := resources.Route.Status.URL.URL()
var opt interface{}
if test.ServingFlags.Https {
url.Scheme = "https"
if httpsTransportOption == nil {
t.Fatalf("Https transport option is nil")
}
opt = *httpsTransportOption
}
if _, err := pkgTest.WaitForEndpointState(
clients.KubeClient,
t.Logf,
url,
v1a1test.RetryingRouteInconsistency(pkgTest.MatchesAllOf(pkgTest.IsStatusOK, pkgTest.MatchesBody(test.HelloWorldText))),
"HelloWorldServesText",
test.ServingFlags.ResolvableDomain,
opt); err != nil {
t.Fatalf("The endpoint %s for Route %s didn't serve the expected text %q: %v", url, names.Route, test.HelloWorldText, err)
}
revision := resources.Revision
if val, ok := revision.Labels["serving.knative.dev/configuration"]; ok {
if val != names.Config {
t.Fatalf("Expect configuration name in revision label %q but got %q ", names.Config, val)
}
} else {
t.Fatalf("Failed to get configuration name from Revision label")
}
if val, ok := revision.Labels["serving.knative.dev/service"]; ok {
if val != names.Service {
t.Fatalf("Expect Service name in revision label %q but got %q ", names.Service, val)
}
} else {
t.Fatalf("Failed to get Service name from Revision label")
}
}
func TestQueueSideCarResourceLimit(t *testing.T) {
t.Parallel()
cancel := logstream.Start(t)
defer cancel()
clients := Setup(t)
names := test.ResourceNames{
Service: test.ObjectNameForTest(t),
Image: "helloworld",
}
test.CleanupOnInterrupt(func() { test.TearDown(clients, names) })
defer test.TearDown(clients, names)
t.Log("Creating a new Service")
resources, _, err := v1a1test.CreateRunLatestServiceReady(t, clients, &names,
false, /* https TODO(taragu) turn this on after helloworld test running with https */
v1a1opts.WithResourceRequirements(corev1.ResourceRequirements{
Requests: corev1.ResourceList{
corev1.ResourceName("cpu"): resource.MustParse("50m"),
corev1.ResourceName("memory"): resource.MustParse("128Mi"),
},
Limits: corev1.ResourceList{
corev1.ResourceName("cpu"): resource.MustParse("100m"),
corev1.ResourceName("memory"): resource.MustParse("258Mi"),
},
}), v1a1opts.WithConfigAnnotations(map[string]string{
serving.QueueSideCarResourcePercentageAnnotation: "0.2",
}))
if err != nil {
t.Fatalf("Failed to create initial Service: %v: %v", names.Service, err)
}
url := resources.Route.Status.URL.URL()
if _, err = pkgTest.WaitForEndpointState(
clients.KubeClient,
t.Logf,
url,
v1a1test.RetryingRouteInconsistency(pkgTest.MatchesAllOf(pkgTest.IsStatusOK, pkgTest.MatchesBody(test.HelloWorldText))),
"HelloWorldServesText",
test.ServingFlags.ResolvableDomain); err != nil {
t.Fatalf("The endpoint for Route %s at %s didn't serve the expected text %q: %v", names.Route, url, test.HelloWorldText, err)
}
revision := resources.Revision
if val, ok := revision.Labels["serving.knative.dev/configuration"]; ok {
if val != names.Config {
t.Fatalf("Expect configuration name in revision label %q but got %q ", names.Config, val)
}
} else {
t.Fatalf("Failed to get configuration name from Revision label")
}
if val, ok := revision.Labels["serving.knative.dev/service"]; ok {
if val != names.Service {
t.Fatalf("Expect Service name in revision label %q but got %q ", names.Service, val)
}
} else {
t.Fatalf("Failed to get Service name from Revision label")
}
container, err := getContainer(clients.KubeClient, resources.Service.Name, "queue-proxy", resources.Service.Namespace)
if err != nil {
t.Fatalf("Failed to get queue-proxy container in the pod %v in namespace %v: %v", resources.Service.Name, resources.Service.Namespace, err)
}
if container.Resources.Limits.Cpu().Cmp(resource.MustParse("40m")) != 0 {
t.Fatalf("queue-proxy should have limit.cpu set to 40m got %v", container.Resources.Limits.Cpu())
}
if container.Resources.Limits.Memory().Cmp(resource.MustParse("200Mi")) != 0 {
t.Fatalf("queue-proxy should have limit.memory set to 200Mi got %v", container.Resources.Limits.Memory())
}
if container.Resources.Requests.Cpu().Cmp(resource.MustParse("25m")) != 0 {
t.Fatalf("queue-proxy should have request.cpu set to 25m got %v", container.Resources.Requests.Cpu())
}
if container.Resources.Requests.Memory().Cmp(resource.MustParse("50Mi")) != 0 {
t.Fatalf("queue-proxy should have request.memory set to 50Mi got %v", container.Resources.Requests.Memory())
}
}
// Container returns container for given Pod and Container in the namespace
func getContainer(client *pkgTest.KubeClient, podName, containerName, namespace string) (corev1.Container, error) {
pods := client.Kube.CoreV1().Pods(namespace)
podList, err := pods.List(metav1.ListOptions{})
if err != nil {
return corev1.Container{}, err
}
for _, pod := range podList.Items {
if strings.Contains(pod.Name, podName) {
result, err := pods.Get(pod.Name, metav1.GetOptions{})
if err != nil {
return corev1.Container{}, err
}
for _, container := range result.Spec.Containers {
if strings.Contains(container.Name, containerName) {
return container, nil
}
}
}
}
return corev1.Container{}, fmt.Errorf("Could not find container for %s/%s", podName, containerName)
}