Merge pull request #64216 from misterikkit/nil-or-empty

Automatic merge from submit-queue (batch tested with PRs 64174, 64187, 64216, 63265, 64223). If you want to cherry-pick this change to another branch, please follow the instructions <a href="https://github.com/kubernetes/community/blob/master/contributors/devel/cherry-picks.md">here</a>.

Do not use DeepEqual to compare slices in test.

This wraps DeepEqual with a helper that considers nil slices and empty
slices to be equal.

Scheduler code might use a nil slice or empty slice to represent an
empty list, so tests should not be sensitive to the difference.  Tests
could fail because DeepEqual considers nil to be different from an empty
slice.



**What this PR does / why we need it**:
Avoid breaking tests in cases where application behavior is not changed.

**Special notes for your reviewer**:
This brittle test keeps breaking in a number of my PRs. Hoping to get this fix merged independently.

**Release note**:

```release-note
NONE
```

/sig scheduling
/kind cleanup
This commit is contained in:
Kubernetes Submit Queue
2018-05-24 09:41:17 -07:00
committed by GitHub

View File

@@ -378,6 +378,14 @@ func TestUpdateResult(t *testing.T) {
}
}
// slicesEqual wraps reflect.DeepEqual, but returns true when comparing nil and empty slice.
func slicesEqual(a, b []algorithm.PredicateFailureReason) bool {
if len(a) == 0 && len(b) == 0 {
return true
}
return reflect.DeepEqual(a, b)
}
func TestLookupResult(t *testing.T) {
tests := []struct {
name string
@@ -504,9 +512,9 @@ func TestLookupResult(t *testing.T) {
if fit != test.expectedPredicateItem.fit {
t.Errorf("Failed: %s, expected fit: %v, but got: %v", test.name, test.cachedItem.fit, fit)
}
if !reflect.DeepEqual(reasons, test.expectedPredicateItem.reasons) {
if !slicesEqual(reasons, test.expectedPredicateItem.reasons) {
t.Errorf("Failed: %s, expected reasons: %v, but got: %v",
test.name, test.cachedItem.reasons, reasons)
test.name, test.expectedPredicateItem.reasons, reasons)
}
}
}