Skip to content

Commit

Permalink
Simplify predicate tests and move to test package
Browse files Browse the repository at this point in the history
  • Loading branch information
timebertt committed Jul 22, 2022
1 parent e55398e commit 358e47f
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 69 deletions.
13 changes: 7 additions & 6 deletions pkg/controllermanager/controller/bastion/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func (r *Reconciler) AddToManager(mgr manager.Manager) error {
c, err := builder.
ControllerManagedBy(mgr).
Named(ControllerName).
For(&operationsv1alpha1.Bastion{}, builder.WithPredicates(&predicate.GenerationChangedPredicate{})).
For(&operationsv1alpha1.Bastion{}, builder.WithPredicates(predicate.GenerationChangedPredicate{})).
WithOptions(controller.Options{
MaxConcurrentReconciles: pointer.IntDeref(r.Config.ConcurrentSyncs, 0),
RecoverPanic: true,
Expand All @@ -61,14 +61,14 @@ func (r *Reconciler) AddToManager(mgr manager.Manager) error {

return c.Watch(
&source.Kind{Type: &gardencorev1beta1.Shoot{}},
mapper.EnqueueRequestsFrom(mapper.MapFunc(mapShootToBastions), mapper.UpdateWithNew, c.GetLogger()),
shootPredicate,
mapper.EnqueueRequestsFrom(mapper.MapFunc(MapShootToBastions), mapper.UpdateWithNew, c.GetLogger()),
ShootPredicate,
)
}

var (
// react on Shoot events that indicate that we need to garbage collect the Bastion
shootPredicate = predicate.Or(predicateutils.IsDeleting(), shootSeedNameChanged)
// ShootPredicate reacts on Shoot events that indicate that we need to garbage collect the Bastion
ShootPredicate = predicate.Or(predicateutils.IsDeleting(), shootSeedNameChanged)

// detect update events where shoot.spec.seedName is changed, as we need to delete Bastions, if the corresponding
// Shoot is migrated to a different Seed
Expand All @@ -95,7 +95,8 @@ var (
}
)

func mapShootToBastions(ctx context.Context, log logr.Logger, reader client.Reader, obj client.Object) []reconcile.Request {
// MapShootToBastions is a mapper.MapFunc for mapping shoots to referencing Bastions.
func MapShootToBastions(ctx context.Context, log logr.Logger, reader client.Reader, obj client.Object) []reconcile.Request {
shoot, ok := obj.(*gardencorev1beta1.Shoot)
if !ok {
return nil
Expand Down
96 changes: 33 additions & 63 deletions pkg/controllermanager/controller/bastion/add_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,15 @@
// See the License for the specific language governing permissions and
// limitations under the License.

package bastion
package bastion_test

import (
"context"

gardencorev1beta1 "github.com/gardener/gardener/pkg/apis/core/v1beta1"
operationsv1alpha1 "github.com/gardener/gardener/pkg/apis/operations/v1alpha1"
"github.com/gardener/gardener/pkg/client/kubernetes"
. "github.com/gardener/gardener/pkg/controllermanager/controller/bastion"

"github.com/go-logr/logr"
. "github.com/onsi/ginkgo/v2"
Expand All @@ -35,7 +36,7 @@ import (
)

var _ = Describe("Add", func() {
Describe("shootPredicate", func() {
Describe("ShootPredicate", func() {
var obj *gardencorev1beta1.Shoot

BeforeEach(func() {
Expand All @@ -45,66 +46,51 @@ var _ = Describe("Add", func() {
Describe("#Create", func() {
var e event.CreateEvent

JustBeforeEach(func() {
BeforeEach(func() {
e = event.CreateEvent{Object: obj}
})

It("should return false if the object is not deleting", func() {
Expect(shootPredicate.Create(e)).To(BeFalse())
Expect(ShootPredicate.Create(e)).To(BeFalse())
})

Context("when object is deleting", func() {
BeforeEach(func() {
obj.DeletionTimestamp = &metav1.Time{}
})

It("should return true", func() {
Expect(shootPredicate.Create(e)).To(BeTrue())
})
It("should return true if object is deleting", func() {
obj.DeletionTimestamp = &metav1.Time{}
Expect(ShootPredicate.Create(e)).To(BeTrue())
})
})

Describe("#Delete", func() {
var e event.DeleteEvent

JustBeforeEach(func() {
BeforeEach(func() {
e = event.DeleteEvent{Object: obj}
})

It("should return false if the object is not deleting", func() {
Expect(shootPredicate.Delete(e)).To(BeFalse())
Expect(ShootPredicate.Delete(e)).To(BeFalse())
})

Context("when object is deleting", func() {
BeforeEach(func() {
obj.DeletionTimestamp = &metav1.Time{}
})

It("should return true", func() {
Expect(shootPredicate.Delete(e)).To(BeTrue())
})
It("should return true if object is deleting", func() {
obj.DeletionTimestamp = &metav1.Time{}
Expect(ShootPredicate.Delete(e)).To(BeTrue())
})
})

Describe("#Generic", func() {
var e event.GenericEvent

JustBeforeEach(func() {
BeforeEach(func() {
e = event.GenericEvent{Object: obj}
})

It("should return false if the object is not deleting", func() {
Expect(shootPredicate.Generic(e)).To(BeFalse())
Expect(ShootPredicate.Generic(e)).To(BeFalse())
})

Context("when object is deleting", func() {
BeforeEach(func() {
obj.DeletionTimestamp = &metav1.Time{}
})

It("should return true", func() {
Expect(shootPredicate.Generic(e)).To(BeTrue())
})
It("should return true if object is deleting", func() {
obj.DeletionTimestamp = &metav1.Time{}
Expect(ShootPredicate.Generic(e)).To(BeTrue())
})
})

Expand All @@ -116,51 +102,35 @@ var _ = Describe("Add", func() {

BeforeEach(func() {
objNew = obj.DeepCopy()
})

JustBeforeEach(func() {
e = event.UpdateEvent{ObjectOld: obj, ObjectNew: objNew}
})

It("should return false if the object is not deleting and seed name did not change", func() {
Expect(shootPredicate.Update(e)).To(BeFalse())
Expect(ShootPredicate.Update(e)).To(BeFalse())
})

Context("when shoot is scheduled for the first time", func() {
BeforeEach(func() {
obj.Spec.SeedName = nil
objNew.Spec.SeedName = pointer.String("some-seed-name")
})
It("should return false when shoot is scheduled for the first time", func() {
obj.Spec.SeedName = nil
objNew.Spec.SeedName = pointer.String("some-seed-name")

It("should return false", func() {
Expect(shootPredicate.Update(e)).To(BeFalse())
})
Expect(ShootPredicate.Update(e)).To(BeFalse())
})

Context("when seed name changed", func() {
BeforeEach(func() {
obj.Spec.SeedName = pointer.String("old-seed")
objNew.Spec.SeedName = pointer.String("new-seed")
})
It("should return true when seed name changed", func() {
obj.Spec.SeedName = pointer.String("old-seed")
objNew.Spec.SeedName = pointer.String("new-seed")

It("should return true", func() {
Expect(shootPredicate.Update(e)).To(BeTrue())
})
Expect(ShootPredicate.Update(e)).To(BeTrue())
})

Context("when object is deleting", func() {
BeforeEach(func() {
objNew.DeletionTimestamp = &metav1.Time{}
})

It("should return true", func() {
Expect(shootPredicate.Update(e)).To(BeTrue())
})
It("should return true if object is deleting", func() {
objNew.DeletionTimestamp = &metav1.Time{}
Expect(ShootPredicate.Update(e)).To(BeTrue())
})
})
})

Describe("mapShootToBastions", func() {
Describe("MapShootToBastions", func() {
var (
ctx = context.TODO()
log logr.Logger
Expand All @@ -173,7 +143,7 @@ var _ = Describe("Add", func() {
})

It("should do nothing if the object is no shoot", func() {
Expect(mapShootToBastions(ctx, log, fakeClient, &corev1.Secret{})).To(BeEmpty())
Expect(MapShootToBastions(ctx, log, fakeClient, &corev1.Secret{})).To(BeEmpty())
})

It("should map the shoot to bastions", func() {
Expand Down Expand Up @@ -212,7 +182,7 @@ var _ = Describe("Add", func() {
Expect(fakeClient.Create(ctx, bastion1)).To(Succeed())
Expect(fakeClient.Create(ctx, bastion2)).To(Succeed())

Expect(mapShootToBastions(ctx, log, fakeClient, shoot)).To(ConsistOf(
Expect(MapShootToBastions(ctx, log, fakeClient, shoot)).To(ConsistOf(
reconcile.Request{NamespacedName: types.NamespacedName{Name: bastion1.Name, Namespace: bastion1.Namespace}},
reconcile.Request{NamespacedName: types.NamespacedName{Name: bastion2.Name, Namespace: bastion2.Namespace}},
))
Expand Down

0 comments on commit 358e47f

Please sign in to comment.