Merge pull request #32027 from m1093782566/m109-petset-fix-test-err

Automatic merge from submit-queue

[BUG FIX] Fix bug of UT in Pet Set

<!--  Thanks for sending a pull request!  Here are some tips for you:
1. If this is your first time, read our contributor guidelines https://github.com/kubernetes/kubernetes/blob/master/CONTRIBUTING.md and developer guide https://github.com/kubernetes/kubernetes/blob/master/docs/devel/development.md
2. If you want *faster* PR reviews, read how: https://github.com/kubernetes/kubernetes/blob/master/docs/devel/faster_reviews.md
3. Follow the instructions for writing a release note: https://github.com/kubernetes/kubernetes/blob/master/docs/devel/pull-requests.md#release-notes
-->

**What this PR does / why we need it**:

Fix bug of UT in Pet Set.

[1] https://github.com/kubernetes/kubernetes/blob/master/pkg/controller/petset/pet_set_test.go#L74-L75, 

I think` len(pl)` is not equal to `len(fc.pets)`, see [here](https://github.com/kubernetes/kubernetes/blob/master/pkg/controller/petset/fakes.go#L229-L233)

[2] https://github.com/kubernetes/kubernetes/blob/master/pkg/controller/petset/fakes.go#L249

I think should change to 

```
if len(f.pets) <= index {
```

because when `len(f.pets)==index`, then [here](https://github.com/kubernetes/kubernetes/blob/master/pkg/controller/petset/fakes.go#L252-L254) will cause `index out of range` panic!

[3] https://github.com/kubernetes/kubernetes/blob/master/pkg/controller/petset/fakes.go#L271

same reason with [2]

[4] https://github.com/kubernetes/kubernetes/blob/master/pkg/controller/petset/pet_set_test.go#L79

which doesn't make use of the error returned by [setHealthy](https://github.com/kubernetes/kubernetes/blob/master/pkg/controller/petset/fakes.go#L248) and has a risk of letting the error out.

Should we catch the error and use `t.Errorf()` to stop the test?
This commit is contained in:
Kubernetes Submit Queue 2016-09-26 21:13:14 -07:00 committed by GitHub
commit 6c1c0b9842
2 changed files with 4 additions and 4 deletions

View File

@ -248,7 +248,7 @@ func (f *fakePetClient) deletePetAtIndex(index int) {
}
func (f *fakePetClient) setHealthy(index int) error {
if len(f.pets) < index {
if len(f.pets) <= index {
return fmt.Errorf("Index out of range, len %v index %v", len(f.pets), index)
}
f.pets[index].pod.Status.Phase = api.PodRunning
@ -270,7 +270,7 @@ func (f *fakePetClient) isHealthy(pod *api.Pod) bool {
}
func (f *fakePetClient) setDeletionTimestamp(index int) error {
if len(f.pets) < index {
if len(f.pets) <= index {
return fmt.Errorf("Index out of range, len %v index %v", len(f.pets), index)
}
f.pets[index].pod.DeletionTimestamp = &unversioned.Time{Time: time.Now()}

View File

@ -73,7 +73,7 @@ func scalePetSet(t *testing.T, ps *apps.PetSet, psc *PetSetController, fc *fakeP
for i := 0; i < scale; i++ {
pl := fc.getPodList()
if len(pl) != i {
t.Errorf("Unexpected number of pets, expected %d found %d", i, len(fc.pets))
t.Errorf("Unexpected number of pets, expected %d found %d", i, len(pl))
}
if _, syncErr := psc.syncPetSet(ps, pl); syncErr != nil {
errs = append(errs, syncErr)
@ -120,7 +120,7 @@ func TestPetSetControllerDeletes(t *testing.T) {
knownPods := fc.getPodList()
for i := replicas - 1; i >= 0; i-- {
if len(fc.pets) != i+1 {
t.Errorf("Unexpected number of pets, expected %d found %d", i, len(fc.pets))
t.Errorf("Unexpected number of pets, expected %d found %d", i+1, len(fc.pets))
}
if _, syncErr := psc.syncPetSet(ps, knownPods); syncErr != nil {
errs = append(errs, syncErr)