-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Description
While building a number of controllers last year, a colleague and I came up with an extension to Gomega that allowed us to reduce a lot of boilerplate within our test suites. Our assertions ended up looking something like
m.Eventually(workerNode1, timeout).Should(utils.WithField("Spec.Taints",
ContainElement(SatisfyAll(
utils.WithField("Effect", Equal(corev1.TaintEffect("NoSchedule"))),
utils.WithField("Key", Equal("node.kubernetes.io/unschedulable")),
)),
))
To check that the node object eventually had a taint with the desired Key and Effect. The benefit here being that we knew what we wanted to test in a particular test case, we wanted to see the controller adding this taint, we didn't care what the rest of the object looked like so comparing a whole object was out of the question. The act of fetching and creating the async assertion is all captured by the custom matcher we wrote and our transformation utils.WithField allowed us to extract raw fields from any object passed so that we could use Gomega's core matching library to make asserations.
For comparison, the code to check there's a taint for this object in raw Gomega would look something like
Eventually(func() (bool, err) {
node := &corev1.Node{}
key := client.ObjectKey{Namespace: workerNode1.Namespace, Name workerNode1.Name}
err := c.Get(ctx, key, node)
if err != nil {
return false, err
}
for _, taint := range node.Spec.Taints {
if taint.Effect == corev1.TaintEffect("NoSchedule") && taint.Key == "node.kubernetes.io/unschedulable" {
return true, nil
}
}
return false, nil
}, timeout).Should(BeTrue())
It has long been on my mind that contributing this Gomega extension upstream may be desirable so that others can make use of it, but I hadn't got around to posting yet.
So my question to the community is, does this look like something that could be useful? Is it something you want to know more about? Would a PR to add this kind of extension be accepted? I am happy to share more if there is interest in adding this kind of thing to the kubebuilder/controller-runtime project