/
matchers.go
142 lines (124 loc) · 4.33 KB
/
matchers.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
// Copyright 2020 SAP SE or an SAP affiliate company. All rights reserved. This file is licensed under the Apache Software License, v. 2 except as noted otherwise in the LICENSE file
//
// 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 matchers
import (
"errors"
"github.com/onsi/gomega/format"
"github.com/onsi/gomega/types"
apierrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/api/meta"
"k8s.io/apimachinery/pkg/runtime"
kubernetescache "github.com/gardener/gardener/pkg/client/kubernetes/cache"
)
func init() {
// if CharactersAroundMismatchToInclude is too small, then format.MessageWithDiff will be unable to output our
// mismatch message
// set the variable in init func, otherwise the race detector will complain when matchers are used concurrently in
// multiple goroutines
if format.CharactersAroundMismatchToInclude < 50 {
format.CharactersAroundMismatchToInclude = 50
}
}
// DeepEqual returns a Gomega matcher which checks whether the expected object is deeply equal with the object it is
// being compared against.
func DeepEqual(expected interface{}) types.GomegaMatcher {
return newDeepEqualMatcher(expected)
}
// DeepDerivativeEqual is similar to DeepEqual except that unset fields in actual are
// ignored (not compared). This allows us to focus on the fields that matter to
// the semantic comparison.
func DeepDerivativeEqual(expected interface{}) types.GomegaMatcher {
return newDeepDerivativeMatcher(expected)
}
// BeNotFoundError checks if error is NotFound.
func BeNotFoundError() types.GomegaMatcher {
return &kubernetesErrors{
checkFunc: apierrors.IsNotFound,
message: "NotFound",
}
}
// BeNotRegisteredError checks if error is NotRegistered.
func BeNotRegisteredError() types.GomegaMatcher {
return &kubernetesErrors{
checkFunc: runtime.IsNotRegisteredError,
message: "NotRegistered",
}
}
// BeAlreadyExistsError checks if error is AlreadyExists.
func BeAlreadyExistsError() types.GomegaMatcher {
return &kubernetesErrors{
checkFunc: apierrors.IsAlreadyExists,
message: "AlreadyExists",
}
}
// BeForbiddenError checks if error is Forbidden.
func BeForbiddenError() types.GomegaMatcher {
return &kubernetesErrors{
checkFunc: apierrors.IsForbidden,
message: "Forbidden",
}
}
// BeBadRequestError checks if error is BadRequest.
func BeBadRequestError() types.GomegaMatcher {
return &kubernetesErrors{
checkFunc: apierrors.IsBadRequest,
message: "BadRequest",
}
}
// BeNoMatchError checks if error is a NoMatchError.
func BeNoMatchError() types.GomegaMatcher {
return &kubernetesErrors{
checkFunc: meta.IsNoMatchError,
message: "NoMatch",
}
}
// BeMissingKindError checks if error is a MissingKindError.
func BeMissingKindError() types.GomegaMatcher {
return &kubernetesErrors{
checkFunc: runtime.IsMissingKind,
message: "Object 'Kind' is missing",
}
}
// BeInternalServerError checks if error is a InternalServerError.
func BeInternalServerError() types.GomegaMatcher {
return &kubernetesErrors{
checkFunc: apierrors.IsInternalError,
message: "",
}
}
// BeInvalidError checks if error is an InvalidError.
func BeInvalidError() types.GomegaMatcher {
return &kubernetesErrors{
checkFunc: apierrors.IsInvalid,
message: "Invalid",
}
}
// BeCacheError checks if error is a CacheError.
func BeCacheError() types.GomegaMatcher {
return &kubernetesErrors{
checkFunc: func(err error) bool {
cacheErr := &kubernetescache.CacheError{}
return errors.As(err, &cacheErr)
},
message: "",
}
}
// ShareSameReferenceAs checks if objects shares the same underlying reference as the passed object.
// This can be used to check if maps or slices have the same underlying data store.
// Only objects that work for 'reflect.ValueOf(x).Pointer' can be compared.
func ShareSameReferenceAs(expected interface{}) types.GomegaMatcher {
return &referenceMatcher{
expected: expected,
}
}