forked from knative/serving
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathroute_service_test.go
182 lines (150 loc) · 6.14 KB
/
route_service_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
// +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 (
"testing"
netpkg "knative.dev/networking/pkg"
"knative.dev/pkg/ptr"
v1 "knative.dev/serving/pkg/apis/serving/v1"
serviceresourcenames "knative.dev/serving/pkg/reconciler/service/resources/names"
rtesting "knative.dev/serving/pkg/testing/v1"
"knative.dev/serving/test"
v1test "knative.dev/serving/test/v1"
)
// TestRoutesNotReady tests the scenario that when Route's status is
// Ready == False, the Service's RoutesReady value should change from
// Unknown to False
func TestRoutesNotReady(t *testing.T) {
t.Parallel()
clients := Setup(t)
names := test.ResourceNames{
Service: test.ObjectNameForTest(t),
Image: test.PizzaPlanet1,
}
test.EnsureTearDown(t, clients, &names)
withTrafficSpec := rtesting.WithRouteSpec(v1.RouteSpec{
Traffic: []v1.TrafficTarget{
{
RevisionName: "foobar", // Invalid revision name. This allows Revision creation to succeed and Route configuration to fail
Percent: ptr.Int64(100),
},
},
})
t.Log("Creating a new Service with an invalid traffic target.")
svc, err := v1test.CreateService(t, clients, names, withTrafficSpec)
if err != nil {
t.Fatalf("Failed to create initial Service %q: %#v", names.Service, err)
}
t.Logf("Waiting for Service %q ObservedGeneration to match Generation, and status transition to Ready == False.", names.Service)
if err := v1test.WaitForServiceState(clients.ServingClient, names.Service, v1test.IsServiceFailed, "ServiceIsNotReady"); err != nil {
t.Fatalf("Failed waiting for Service %q to transition to Ready == False: %#v", names.Service, err)
}
t.Logf("Validating Route %q has reconciled to Ready == False.", serviceresourcenames.Route(svc))
// Check Route is not ready
if err = v1test.CheckRouteState(clients.ServingClient, serviceresourcenames.Route(svc), v1test.IsRouteFailed); err != nil {
t.Fatalf("The Route %q was marked as Ready to serve traffic but it should not be: %#v", serviceresourcenames.Route(svc), err)
}
// Wait for RoutesReady to become False
t.Logf("Validating Service %q has reconciled to RoutesReady == False.", names.Service)
if err = v1test.CheckServiceState(clients.ServingClient, names.Service, v1test.IsServiceRoutesNotReady); err != nil {
t.Fatalf("Service %q was not marked RoutesReady == False: %#v", names.Service, err)
}
}
func TestRouteVisibilityChanges(t *testing.T) {
t.Parallel()
testCases := []struct {
name string
withTrafficSpec rtesting.ServiceOption
}{
{
name: "Route visibility changes from public to private with single traffic",
withTrafficSpec: rtesting.WithRouteSpec(v1.RouteSpec{
Traffic: []v1.TrafficTarget{
{
Percent: ptr.Int64(100),
},
},
}),
},
{
name: "Route visibility changes from public to private with tag only",
withTrafficSpec: rtesting.WithRouteSpec(v1.RouteSpec{
Traffic: []v1.TrafficTarget{
{
Percent: ptr.Int64(100),
Tag: "cow",
},
},
}),
},
{
name: "Route visibility changes from public to private with both tagged and non-tagged traffic",
withTrafficSpec: rtesting.WithRouteSpec(v1.RouteSpec{
Traffic: []v1.TrafficTarget{
{
Percent: ptr.Int64(60),
},
{
Percent: ptr.Int64(40),
Tag: "cow",
},
},
}),
},
}
hasPublicRoute := func(r *v1.Route) (b bool, e error) {
return !isRouteClusterLocal(r.Status), nil
}
hasPrivateRoute := func(r *v1.Route) (b bool, e error) {
return isRouteClusterLocal(r.Status), nil
}
for _, testCase := range testCases {
testCase := testCase
t.Run(testCase.name, func(st *testing.T) {
st.Parallel()
clients := Setup(st)
names := test.ResourceNames{
Service: test.ObjectNameForTest(t),
Image: test.PizzaPlanet1,
}
test.EnsureTearDown(t, clients, &names)
st.Log("Creating a new Service")
svc, err := v1test.CreateService(st, clients, names, testCase.withTrafficSpec)
if err != nil {
st.Fatalf("Failed to create initial Service %q: %#v", names.Service, err)
}
st.Logf("Waiting for Service %q ObservedGeneration to match Generation, and status transition to Ready == True", names.Service)
if err := v1test.WaitForServiceState(clients.ServingClient, names.Service, v1test.IsServiceReady, "ServiceIsReady"); err != nil {
st.Fatalf("Failed waiting for Service %q to transition to Ready == True: %#v", names.Service, err)
}
st.Logf("Validating Route %q has non cluster-local address", serviceresourcenames.Route(svc))
// Check Route is not ready
if err = v1test.CheckRouteState(clients.ServingClient, serviceresourcenames.Route(svc), hasPublicRoute); err != nil {
st.Fatalf("The Route %q should be publicly visible but it was not: %#v", serviceresourcenames.Route(svc), err)
}
v1test.PatchService(st, clients, svc, func(s *v1.Service) {
s.SetLabels(map[string]string{netpkg.VisibilityLabelKey: "cluster-local"})
})
st.Logf("Waiting for Service %q ObservedGeneration to match Generation, and status transition to Ready == True", names.Service)
if err := v1test.WaitForServiceState(clients.ServingClient, names.Service, v1test.IsServiceReady, "ServiceIsReady"); err != nil {
st.Fatalf("Failed waiting for Service %q to transition to Ready == True: %#v", names.Service, err)
}
st.Logf("Validating Route %q has cluster-local address", serviceresourcenames.Route(svc))
// Check Route is not ready
if err = v1test.WaitForRouteState(clients.ServingClient, serviceresourcenames.Route(svc), hasPrivateRoute, "RouteIsClusterLocal"); err != nil {
st.Fatalf("The Route %q should be privately visible but it was not: %#v", serviceresourcenames.Route(svc), err)
}
})
}
}