forked from knative/serving
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrollback_byo_test.go
132 lines (114 loc) · 4.43 KB
/
rollback_byo_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
//go:build e2e
// +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"
"knative.dev/pkg/ptr"
v1 "knative.dev/serving/pkg/apis/serving/v1"
rtesting "knative.dev/serving/pkg/testing/v1"
"knative.dev/serving/test"
v1test "knative.dev/serving/test/v1"
)
func TestRollbackBYOName(t *testing.T) {
t.Parallel()
clients := Setup(t)
serviceName := test.ObjectNameForTest(t)
byoNameOld := serviceName + "-byo-foo"
byoNameNew := serviceName + "-byo-foo-new"
names := test.ResourceNames{
Service: serviceName,
Image: test.HelloWorld,
}
test.EnsureTearDown(t, clients, &names)
withTrafficSpecOld := rtesting.WithRouteSpec(v1.RouteSpec{
Traffic: []v1.TrafficTarget{{
RevisionName: byoNameOld,
Percent: ptr.Int64(100),
}},
})
withTrafficSpecNew := rtesting.WithRouteSpec(v1.RouteSpec{
Traffic: []v1.TrafficTarget{{
RevisionName: byoNameNew,
Percent: ptr.Int64(100),
}},
})
t.Logf("Creating a new Service with byo config name %q.", byoNameOld)
resources, err := v1test.CreateServiceReady(t, clients, &names,
withTrafficSpecOld,
func(svc *v1.Service) {
svc.Spec.ConfigurationSpec.Template.ObjectMeta.Name = byoNameOld
})
if err != nil {
t.Fatalf("Failed to create initial Service: %v: %v", names.Service, err)
}
originalServiceSpec := resources.Service.Spec
revisionName := resources.Revision.ObjectMeta.Name
if revisionName != byoNameOld {
t.Fatalf("Expect configuration name in revision label %q but got %q ", byoNameOld, revisionName)
}
// Update service to use a new byo name
t.Logf("Updating the Service to a new revision with a new byo name %q.", byoNameNew)
svc, err := v1test.PatchService(t, clients, resources.Service, func(s *v1.Service) {
s.Spec.Template.Name = byoNameNew
withTrafficSpecNew(s)
})
resources.Service = svc
if err != nil {
t.Fatalf("Patch update for Service (new byo name %q) failed: %v", byoNameNew, err)
}
t.Log("Since the Service was updated a new Revision will be created and the Service will be updated")
newRevision, err := v1test.WaitForServiceLatestRevision(clients, names)
if err != nil {
t.Fatalf("Service %s was not updated with the Revision for new byo name %s: %v", names.Service, byoNameNew, err)
}
if newRevision != byoNameNew {
t.Fatalf("Expect configuration name in revision label %q but got %q ", byoNameNew, newRevision)
}
// Now, rollback to the first RevisionSpec
svc, err = v1test.PatchService(t, clients, resources.Service, func(s *v1.Service) {
s.Spec = originalServiceSpec
})
resources.Service = svc
if err != nil {
t.Fatalf("Patch update for Service (rollback to byo name %q) failed: %v", byoNameOld, err)
}
t.Logf("We are rolling back to the previous revision (byoNameOld %q).", byoNameOld)
// Wait for the route to become ready, and check that the traffic split between the byoNameOld
// and byoNameNew is 100 and 0, respectively
err = v1test.WaitForServiceState(clients.ServingClient, names.Service, func(s *v1.Service) (bool, error) {
for _, tr := range s.Status.Traffic {
if tr.RevisionName != byoNameOld {
return false, nil
}
if tr.Percent == nil || *tr.Percent != 100 {
return false, nil
}
}
return true, nil
}, "ServiceRollbackRevision")
if err != nil {
t.Fatalf("Service %s was not rolled back with byo name %s: %v", names.Service, byoNameOld, err)
}
// Verify that the latest ready revision and latest created revision are both rolled back to byoNameOld,
// which means no new revision is created in the rollback.
//
// This is a special case when LatestReady and LatestCreated move backwards
err = v1test.WaitForServiceState(clients.ServingClient, names.Service, func(s *v1.Service) (bool, error) {
return (s.Status.LatestReadyRevisionName == byoNameOld && s.Status.LatestCreatedRevisionName == byoNameOld), nil
}, "ServiceNoNewRevisionCreated")
if err != nil {
t.Fatalf("Service %s was not rolled back with byo name %s: %v", names.Service, byoNameOld, err)
}
}