Skip to content

Commit cbbc31a

Browse files
Addressed PR comments
1 parent 9671b1d commit cbbc31a

File tree

2 files changed

+29
-30
lines changed

2 files changed

+29
-30
lines changed

controllers/topology/blueprint_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,7 @@ func TestGetBlueprint(t *testing.T) {
262262
g.Expect(got).To(BeNil())
263263
return
264264
}
265-
// Use EqualObject where an object is created and passed through the fake client. Elsewhere the Equal methoc
265+
// Use EqualObject where an object is created and passed through the fake client. Elsewhere the Equal method
266266
// is enough to establish inequality.
267267
g.Expect(tt.want.ClusterClass).To(EqualObject(got.ClusterClass, IgnoreAutogeneratedMetadata))
268268
g.Expect(tt.want.InfrastructureClusterTemplate).To(EqualObject(got.InfrastructureClusterTemplate))

internal/testtypes/matchers.go

Lines changed: 28 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ import (
3434
// These package variables hold pre-created commonly used options that can be used to reduce the manual work involved in
3535
// identifying the paths that need to be compared for testing equality between objects.
3636
var (
37-
3837
// IgnoreAutogeneratedMetadata contains the paths for all the metadata fields that are commonly set by the
3938
// client and APIServer. This is used as a MatchOption for situations when only user-provided metadata is relevant.
4039
IgnoreAutogeneratedMetadata = IgnorePaths{
@@ -45,7 +44,6 @@ var (
4544
{"metadata", "managedFields"},
4645
{"metadata", "deletionGracePeriodSeconds"},
4746
{"metadata", "deletionTimestamp"},
48-
{"metadata", "ownerReferences"},
4947
{"metadata", "selfLink"},
5048
{"metadata", "generateName"},
5149
}
@@ -63,8 +61,8 @@ type Matcher struct {
6361
options *MatchOptions
6462
}
6563

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.
6866
func EqualObject(original runtime.Object, opts ...MatchOption) *Matcher {
6967
matchOptions := &MatchOptions{}
7068
matchOptions = matchOptions.ApplyOptions(opts)
@@ -79,56 +77,57 @@ func EqualObject(original runtime.Object, opts ...MatchOption) *Matcher {
7977
}
8078
}
8179

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) {
8583
// Nil checks required first here for:
8684
// 1) Nil equality which returns true
8785
// 2) One object nil which returns an error
88-
otherIsNil := reflect.ValueOf(other).IsNil()
86+
actualIsNil := reflect.ValueOf(actual).IsNil()
8987
originalIsNil := reflect.ValueOf(m.original).IsNil()
9088

91-
if otherIsNil && originalIsNil {
89+
if actualIsNil && originalIsNil {
9290
return true, nil
9391
}
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)
9694
}
9795

9896
// Calculate diff returns a json diff between the two objects.
99-
m.diff, err = m.calculateDiff(other)
97+
m.diff, err = m.calculateDiff(actual)
10098
if err != nil {
10199
return false, err
102100
}
103101
return bytes.Equal(m.diff, []byte("{}")), nil
104102
}
105103

106104
// 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) {
108106
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))
110108
}
111109

112110
// 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))
115114
}
116115

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.
120119
originalJSON, err := json.Marshal(m.original)
121120
if err != nil {
122121
return nil, err
123122
}
124123

125-
modifiedJSON, err := json.Marshal(other)
124+
actualJSON, err := json.Marshal(actual)
126125
if err != nil {
127126
return nil, err
128127
}
129128

130129
// 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)
132131
if err != nil {
133132
return nil, err
134133
}
@@ -182,16 +181,16 @@ func (i AllowPaths) ApplyToMatcher(opts *MatchOptions) {
182181
}
183182

184183
// 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) {
186185
// converts the diff into a Map
187186
diffMap := make(map[string]interface{})
188187
err := json.Unmarshal(diff, &diffMap)
189188
if err != nil {
190189
return nil, errors.Wrap(err, "failed to unmarshal merge diff")
191190
}
192191

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)
195194

196195
// Removes from diffs everything in the ignore paths.
197196
for _, path := range ignorePaths {
@@ -207,17 +206,17 @@ func filterDiff(diff []byte, allowedPaths, ignorePaths [][]string) ([]byte, erro
207206
}
208207

209208
// 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) {
211210
// 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] == "*" {
213212
return
214213
}
215214

216215
// Loop through the entries in the map.
217216
for k, m := range diffMap {
218-
// Check if item is in the allowed paths.
217+
// Check if item is in the allowPaths.
219218
allowed := false
220-
for _, path := range allowedPaths {
219+
for _, path := range allowPaths {
221220
if k == path[0] {
222221
allowed = true
223222
break
@@ -234,7 +233,7 @@ func filterDiffMap(diffMap map[string]interface{}, allowedPaths [][]string) {
234233
continue
235234
}
236235
nestedPaths := make([][]string, 0)
237-
for _, path := range allowedPaths {
236+
for _, path := range allowPaths {
238237
if k == path[0] && len(path) > 1 {
239238
nestedPaths = append(nestedPaths, path[1:])
240239
}

0 commit comments

Comments
 (0)