Skip to content

Commit

Permalink
Third round of fixing controller tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mvshao committed Aug 26, 2024
1 parent a884581 commit d0c21b7
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 30 deletions.
5 changes: 2 additions & 3 deletions internal/controller/runtime/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,8 +154,7 @@ func setupGardenerTestClientForUpdate() {
func setupGardenerTestClientForDelete() {
baseShoot := getBaseShootForTestingSequence()
shoots := fixShootsSequenceForDelete(&baseShoot)
seeds := fixSeedsSequence()
setupGardenerClientWithSequence(shoots, seeds)
setupGardenerClientWithSequence(shoots, nil)
}

func setupGardenerClientWithSequence(shoots []*gardener_api.Shoot, seeds []*gardener_api.Seed) {
Expand Down Expand Up @@ -268,7 +267,7 @@ func fixShootsSequenceForDelete(shoot *gardener_api.Shoot) []*gardener_api.Shoot
pendingDeleteShoot.Status.LastOperation.Type = gardener_api.LastOperationTypeDelete
pendingDeleteShoot.Status.LastOperation.State = gardener_api.LastOperationStatePending

return []*gardener_api.Shoot{currentShoot, currentShoot, currentShoot, pendingDeleteShoot, nil}
return []*gardener_api.Shoot{currentShoot, currentShoot, currentShoot, currentShoot, pendingDeleteShoot, nil}
}

func fixSeedsSequence() []*gardener_api.Seed {
Expand Down
48 changes: 21 additions & 27 deletions internal/controller/runtime/test_client_obj_tracker.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func NewCustomTracker(tracker clienttesting.ObjectTracker, shoots []*gardener_ap
}

func (t *CustomTracker) IsSequenceFullyUsed() bool {
return (t.shootCallCnt == len(t.shootSequence) && len(t.shootSequence) > 0) && (t.seedCallCnt == len(t.seedSequence) && len(t.seedSequence) > 0)
return (t.shootCallCnt == len(t.shootSequence) && len(t.shootSequence) > 0) && t.seedCallCnt == len(t.seedSequence)
}

func (t *CustomTracker) Get(gvr schema.GroupVersionResource, ns, name string) (runtime.Object, error) {
Expand All @@ -47,32 +47,6 @@ func (t *CustomTracker) Get(gvr schema.GroupVersionResource, ns, name string) (r
return t.ObjectTracker.Get(gvr, ns, name)
}

//func getNextSeed(seed []*gardener_api.Seed, t *CustomTracker) (runtime.Object, error) {
// if t.seedCallCnt < len(seed) {
// obj := seed[t.seedCallCnt]
// t.seedCallCnt++
//
// if obj == nil {
// return nil, k8serrors.NewNotFound(schema.GroupResource{}, "")
// }
// return obj, nil
// }
// return nil, fmt.Errorf("no more seeds in sequence")
//}
//
//func getNextShoot(shoot []*gardener_api.Shoot, t *CustomTracker) (runtime.Object, error) {
// if t.shootCallCnt < len(shoot) {
// obj := shoot[t.shootCallCnt]
// t.shootCallCnt++
//
// if obj == nil {
// return nil, k8serrors.NewNotFound(schema.GroupResource{}, "")
// }
// return obj, nil
// }
// return nil, fmt.Errorf("no more shoots in sequence")
//}

func getNextObject[T any](sequence []*T, counter *int) (*T, error) {
if *counter < len(sequence) {
obj := sequence[*counter]
Expand All @@ -86,6 +60,26 @@ func getNextObject[T any](sequence []*T, counter *int) (*T, error) {
return nil, fmt.Errorf("no more objects in sequence")
}

func (t *CustomTracker) Update(gvr schema.GroupVersionResource, obj runtime.Object, ns string) error {
t.mu.Lock()
defer t.mu.Unlock()

if gvr.Resource == "shoots" {
shoot, ok := obj.(*gardener_api.Shoot)
if !ok {
return fmt.Errorf("object is not of type Gardener Shoot")
}
for index, existingShoot := range t.shootSequence {
if existingShoot != nil && existingShoot.Name == shoot.Name {
t.shootSequence[index] = shoot
return nil
}
}
return k8serrors.NewNotFound(schema.GroupResource{}, shoot.Name)
}
return t.ObjectTracker.Update(gvr, obj, ns)
}

func (t *CustomTracker) Delete(gvr schema.GroupVersionResource, ns, name string) error {
t.mu.Lock()
defer t.mu.Unlock()
Expand Down

0 comments on commit d0c21b7

Please sign in to comment.