-
Notifications
You must be signed in to change notification settings - Fork 18
/
assert.go
168 lines (133 loc) · 4.91 KB
/
assert.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
/*
SPDX-License-Identifier: Apache-2.0
Copyright Contributors to the Submariner project.
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 test
import (
"context"
"fmt"
"strings"
"time"
. "github.com/onsi/gomega"
"github.com/submariner-io/admiral/pkg/resource"
apierrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/client-go/testing"
"k8s.io/utils/set"
)
func EnsureNoActionsForResource(f *testing.Fake, resourceType string, expectedVerbs ...string) {
Consistently(func() []string {
return GetOccurredActionVerbs(f, resourceType, expectedVerbs...)
}).Should(BeEmpty())
}
func EnsureActionsForResource(f *testing.Fake, resourceType string, expectedVerbs ...string) {
Eventually(func() []string {
return GetOccurredActionVerbs(f, resourceType, expectedVerbs...)
}).ShouldNot(BeEmpty())
}
func GetOccurredActionVerbs(f *testing.Fake, resourceType string, expectedVerbs ...string) []string {
expSet := set.New(expectedVerbs...)
verbs := []string{}
subresource := ""
if index := strings.Index(resourceType, "/"); index != -1 {
subresource = resourceType[index+1:]
resourceType = resourceType[:index]
}
actualActions := f.Actions()
for i := range actualActions {
verb := actualActions[i].GetVerb()
if actualActions[i].GetResource().Resource == resourceType && actualActions[i].GetSubresource() == subresource && expSet.Has(verb) {
verbs = append(verbs, verb)
}
}
return verbs
}
func AwaitFinalizer[T runtime.Object](client resource.Interface[T], name, finalizer string) {
Eventually(func() []string {
return GetFinalizers(client, name)
}).Should(ContainElement(finalizer))
}
func AwaitNoFinalizer[T runtime.Object](client resource.Interface[T], name, finalizer string) {
Eventually(func() []string {
return GetFinalizers(client, name)
}).ShouldNot(ContainElement(finalizer))
}
func AssertFinalizers[T runtime.Object](client resource.Interface[T], name string, finalizers ...string) {
if finalizers == nil {
finalizers = []string{}
}
Expect(GetFinalizers(client, name)).To(Equal(finalizers))
}
func AwaitStatusCondition(expCond *metav1.Condition, get func() ([]metav1.Condition, error)) {
var found *metav1.Condition
Eventually(func() bool {
conditions, err := get()
Expect(err).To(Succeed())
found = meta.FindStatusCondition(conditions, expCond.Type)
if found == nil {
return false
}
return found.Status == expCond.Status && found.Reason == expCond.Reason
}, 5*time.Second, 50*time.Millisecond).Should(BeTrue(), "Status condition not found: %s", resource.ToJSON(expCond))
Expect(found.LastTransitionTime).To(Not(BeNil()))
}
func AwaitResource[T runtime.Object](client resource.Interface[T], name string) T {
return AwaitAndVerifyResource(client, name, nil)
}
func AwaitAndVerifyResource[T runtime.Object](client resource.Interface[T], name string, verify func(T) bool) T {
var found T
Eventually(func() error {
obj, err := client.Get(context.TODO(), name, metav1.GetOptions{})
if err != nil {
return err
}
if verify == nil || verify(obj) {
found = obj
return nil
}
return fmt.Errorf("resource %q was found but not verified", name)
}, 3*time.Second, 50*time.Millisecond).Should(Succeed())
return found
}
func AwaitNoResource[T runtime.Object](client resource.Interface[T], name string) {
Eventually(func() bool {
_, err := client.Get(context.TODO(), name, metav1.GetOptions{})
return apierrors.IsNotFound(err)
}, 3).Should(BeTrue(), "Found unexpected resource %q", name)
}
func EnsureNoResource[T runtime.Object](client resource.Interface[T], name string) {
Consistently(func() bool {
_, err := client.Get(context.TODO(), name, metav1.GetOptions{})
return apierrors.IsNotFound(err)
}, 300*time.Millisecond).Should(BeTrue(), "Found unexpected resource %q", name)
}
func AwaitUpdateAction(f *testing.Fake, resourceType, name string) runtime.Object {
var retObj runtime.Object
Eventually(func() bool {
actions := f.Actions()
for i := range actions {
if actions[i].GetVerb() == "update" && actions[i].GetResource().Resource == resourceType {
update := actions[i].(testing.UpdateAction)
obj, err := meta.Accessor(update.GetObject())
Expect(err).To(Succeed())
if obj.GetName() == name {
retObj = update.GetObject()
return true
}
}
}
return false
}).Should(BeTrue(), "Expected update action for resource %q of type %q", name, resourceType)
return retObj
}