@@ -34,7 +34,6 @@ import (
34
34
// These package variables hold pre-created commonly used options that can be used to reduce the manual work involved in
35
35
// identifying the paths that need to be compared for testing equality between objects.
36
36
var (
37
-
38
37
// IgnoreAutogeneratedMetadata contains the paths for all the metadata fields that are commonly set by the
39
38
// client and APIServer. This is used as a MatchOption for situations when only user-provided metadata is relevant.
40
39
IgnoreAutogeneratedMetadata = IgnorePaths {
45
44
{"metadata" , "managedFields" },
46
45
{"metadata" , "deletionGracePeriodSeconds" },
47
46
{"metadata" , "deletionTimestamp" },
48
- {"metadata" , "ownerReferences" },
49
47
{"metadata" , "selfLink" },
50
48
{"metadata" , "generateName" },
51
49
}
@@ -63,8 +61,8 @@ type Matcher struct {
63
61
options * MatchOptions
64
62
}
65
63
66
- // EqualObject applies the MatchOptions and returns a Matcher for the passed Kubernetes runtime.Object. This function
67
- // can be used directly as a Gomega Matcher in Gomega Assertions.
64
+ // EqualObject returns a Matcher for the passed Kubernetes runtime.Object with the passed Options . This function can be
65
+ // used as a Gomega Matcher in Gomega Assertions.
68
66
func EqualObject (original runtime.Object , opts ... MatchOption ) * Matcher {
69
67
matchOptions := & MatchOptions {}
70
68
matchOptions = matchOptions .ApplyOptions (opts )
@@ -79,56 +77,57 @@ func EqualObject(original runtime.Object, opts ...MatchOption) *Matcher {
79
77
}
80
78
}
81
79
82
- // Match compares the object it's based on to the passed object and returns true if the objects are the same according to
83
- // the Matcher MatchOptions.
84
- func (m * Matcher ) Match (other interface {}) (success bool , err error ) {
80
+ // Match compares the current object to the passed object and returns true if the objects are the same according to
81
+ // the Matcher and MatchOptions.
82
+ func (m * Matcher ) Match (actual interface {}) (success bool , err error ) {
85
83
// Nil checks required first here for:
86
84
// 1) Nil equality which returns true
87
85
// 2) One object nil which returns an error
88
- otherIsNil := reflect .ValueOf (other ).IsNil ()
86
+ actualIsNil := reflect .ValueOf (actual ).IsNil ()
89
87
originalIsNil := reflect .ValueOf (m .original ).IsNil ()
90
88
91
- if otherIsNil && originalIsNil {
89
+ if actualIsNil && originalIsNil {
92
90
return true , nil
93
91
}
94
- if otherIsNil || originalIsNil {
95
- return false , fmt .Errorf ("can not compare an object with a nil. original : %v , other %v" , m .original , other )
92
+ if actualIsNil || originalIsNil {
93
+ return false , fmt .Errorf ("can not compare an object with a nil. original %v , actual %v" , m .original , actual )
96
94
}
97
95
98
96
// Calculate diff returns a json diff between the two objects.
99
- m .diff , err = m .calculateDiff (other )
97
+ m .diff , err = m .calculateDiff (actual )
100
98
if err != nil {
101
99
return false , err
102
100
}
103
101
return bytes .Equal (m .diff , []byte ("{}" )), nil
104
102
}
105
103
106
104
// FailureMessage returns a message comparing the full objects after an unexpected failure to match has occurred.
107
- func (m * Matcher ) FailureMessage (other interface {}) (message string ) {
105
+ func (m * Matcher ) FailureMessage (actual interface {}) (message string ) {
108
106
return fmt .Sprintf ("the following fields were expected to match but did not:\n %s\n %s" , string (m .diff ),
109
- format .Message (other , "expected to match" , m .original ))
107
+ format .Message (actual , "expected to match" , m .original ))
110
108
}
111
109
112
110
// NegatedFailureMessage returns a string comparing the full objects after an unexpected match has occurred.
113
- func (m * Matcher ) NegatedFailureMessage (other interface {}) (message string ) {
114
- return format .Message (other , "not to match" , m .original )
111
+ func (m * Matcher ) NegatedFailureMessage (actual interface {}) (message string ) {
112
+ return format .Message ("the following fields were not expected to match \n %s\n %s" , string (m .diff ),
113
+ format .Message (actual , "expected to match" , m .original ))
115
114
}
116
115
117
- // calculateDiff applies the MatchOptions and identified the diff between the Matcher object and the passed object.
118
- func (m * Matcher ) calculateDiff (other interface {}) ([]byte , error ) {
119
- // Convert the original and other objects to json.
116
+ // calculateDiff applies the MatchOptions and identifies the diff between the Matcher object and the actual object.
117
+ func (m * Matcher ) calculateDiff (actual interface {}) ([]byte , error ) {
118
+ // Convert the original and actual objects to json.
120
119
originalJSON , err := json .Marshal (m .original )
121
120
if err != nil {
122
121
return nil , err
123
122
}
124
123
125
- modifiedJSON , err := json .Marshal (other )
124
+ actualJSON , err := json .Marshal (actual )
126
125
if err != nil {
127
126
return nil , err
128
127
}
129
128
130
129
// Use a mergePatch to produce a diff between the two objects.
131
- originalWithModifiedJSON , err := jsonpatch .MergePatch (originalJSON , modifiedJSON )
130
+ originalWithModifiedJSON , err := jsonpatch .MergePatch (originalJSON , actualJSON )
132
131
if err != nil {
133
132
return nil , err
134
133
}
@@ -182,16 +181,16 @@ func (i AllowPaths) ApplyToMatcher(opts *MatchOptions) {
182
181
}
183
182
184
183
// filterDiff limits the diff to allowPaths if given and excludes ignorePaths if given. It returns the altered diff.
185
- func filterDiff (diff []byte , allowedPaths , ignorePaths [][]string ) ([]byte , error ) {
184
+ func filterDiff (diff []byte , allowPaths , ignorePaths [][]string ) ([]byte , error ) {
186
185
// converts the diff into a Map
187
186
diffMap := make (map [string ]interface {})
188
187
err := json .Unmarshal (diff , & diffMap )
189
188
if err != nil {
190
189
return nil , errors .Wrap (err , "failed to unmarshal merge diff" )
191
190
}
192
191
193
- // Removes from diffs everything not in the allowed paths .
194
- filterDiffMap (diffMap , allowedPaths )
192
+ // Removes from diffs everything not in the allowpaths .
193
+ filterDiffMap (diffMap , allowPaths )
195
194
196
195
// Removes from diffs everything in the ignore paths.
197
196
for _ , path := range ignorePaths {
@@ -207,17 +206,17 @@ func filterDiff(diff []byte, allowedPaths, ignorePaths [][]string) ([]byte, erro
207
206
}
208
207
209
208
// filterDiffMap limits the diffMap to those paths allowed by the MatchOptions.
210
- func filterDiffMap (diffMap map [string ]interface {}, allowedPaths [][]string ) {
209
+ func filterDiffMap (diffMap map [string ]interface {}, allowPaths [][]string ) {
211
210
// if the allowPaths only contains "*" return the full diffmap.
212
- if len (allowedPaths ) == 1 && allowedPaths [0 ][0 ] == "*" {
211
+ if len (allowPaths ) == 1 && allowPaths [0 ][0 ] == "*" {
213
212
return
214
213
}
215
214
216
215
// Loop through the entries in the map.
217
216
for k , m := range diffMap {
218
- // Check if item is in the allowed paths .
217
+ // Check if item is in the allowPaths .
219
218
allowed := false
220
- for _ , path := range allowedPaths {
219
+ for _ , path := range allowPaths {
221
220
if k == path [0 ] {
222
221
allowed = true
223
222
break
@@ -234,7 +233,7 @@ func filterDiffMap(diffMap map[string]interface{}, allowedPaths [][]string) {
234
233
continue
235
234
}
236
235
nestedPaths := make ([][]string , 0 )
237
- for _ , path := range allowedPaths {
236
+ for _ , path := range allowPaths {
238
237
if k == path [0 ] && len (path ) > 1 {
239
238
nestedPaths = append (nestedPaths , path [1 :])
240
239
}
0 commit comments