Skip to content

Commit 8718031

Browse files
Improve UX of framework helpers
1 parent 5fdcfde commit 8718031

File tree

2 files changed

+79
-20
lines changed

2 files changed

+79
-20
lines changed

test/framework/all_type_helpers.go

Lines changed: 57 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -33,44 +33,81 @@ import (
3333
"sigs.k8s.io/controller-runtime/pkg/client"
3434
)
3535

36-
// ObjectGetterInput is the input for generic object getter methods.
37-
type ObjectGetterInput struct {
36+
type GetterInterface interface {
37+
GetReader() client.Reader
38+
GetObject() client.Object
39+
}
40+
41+
type Condition = func() bool
42+
43+
type ConditionalInterface interface {
44+
GetterInterface
45+
Satifies() bool
46+
}
47+
48+
type ConditionalInput struct {
3849
client.Reader
3950
client.Object
51+
Condition
52+
}
53+
54+
func For(object client.Object) *ConditionalInput {
55+
return &ConditionalInput{
56+
Object: object,
57+
}
58+
}
59+
60+
func (in *ConditionalInput) In(reader client.Reader) *ConditionalInput {
61+
in.Reader = reader
62+
return in
63+
}
64+
65+
func (in *ConditionalInput) ToSatisfy(condition Condition) *ConditionalInput {
66+
in.Condition = condition
67+
return in
68+
}
69+
70+
func (in ConditionalInput) Satifies() bool {
71+
if in.Condition == nil {
72+
return true
73+
}
74+
75+
By("Waiting for the object to satisfy condition...")
76+
return in.Condition()
77+
}
78+
79+
func (in ConditionalInput) GetReader() client.Reader {
80+
return in.Reader
81+
}
82+
83+
func (in ConditionalInput) GetObject() client.Object {
84+
return in.Object
4085
}
4186

4287
// WaitForDelete will wait for object removal
43-
func WaitForDelete(ctx context.Context, input ObjectGetterInput, intervals ...interface{}) {
44-
By("Waiting for the object to be removed...")
88+
func WaitForDelete(ctx context.Context, input GetterInterface, intervals ...interface{}) {
89+
By(fmt.Sprintf("Waiting for the %s object to be removed...", client.ObjectKeyFromObject(input.GetObject())))
4590
Eventually(func() bool {
46-
if err := input.Get(ctx, client.ObjectKeyFromObject(input.Object), input.Object); err != nil {
91+
if err := input.GetReader().Get(ctx, client.ObjectKeyFromObject(input.GetObject()), input.GetObject()); err != nil {
4792
if apierrors.IsNotFound(err) {
4893
return true
4994
}
5095
klog.Infof("Failed to get an object: %+v", err)
5196
}
5297
return false
53-
}, intervals...).Should(BeTrue(), "Failed to wait until object deletion %s", klog.KObj(input.Object))
54-
}
55-
56-
type Conditional = func() bool
57-
58-
type ObjectConditionalInput struct {
59-
client.Reader
60-
client.Object
61-
Conditional
98+
}, intervals...).Should(BeTrue(), "Failed to wait until object deletion %s", klog.KObj(input.GetObject()))
6299
}
63100

64-
// WaitForConditional will wait for conclusive result from specified callback
65-
func WaitForConditional(ctx context.Context, input ObjectConditionalInput, intervals ...interface{}) {
66-
By("Waiting for condition...")
101+
// WaitFor will wait for condition match on existing object
102+
func WaitFor(ctx context.Context, input ConditionalInterface, intervals ...interface{}) {
67103
Eventually(func() bool {
68-
if err := input.Get(ctx, client.ObjectKeyFromObject(input.Object), input.Object); err != nil {
104+
By(fmt.Sprintf("Waiting for %s...", client.ObjectKeyFromObject(input.GetObject())))
105+
if err := input.GetReader().Get(ctx, client.ObjectKeyFromObject(input.GetObject()), input.GetObject()); err != nil {
69106
klog.Infof("Failed to get an object: %+v", err)
70107
return false
71108
}
72-
return input.Conditional()
73-
}, intervals...).Should(BeTrue(), "Failed to wait until object condition match %s", klog.KObj(input.Object))
109+
return input.Satifies()
110+
}, intervals...).Should(BeTrue(), "Failed to wait until object condition match %s", klog.KObj(input.GetObject()))
74111
}
75112

76113
type HelmChart struct {

test/framework/conditions.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package framework
2+
3+
import (
4+
"fmt"
5+
6+
. "github.com/onsi/ginkgo/v2"
7+
8+
corev1 "k8s.io/api/core/v1"
9+
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
10+
)
11+
12+
func HaveStatusCondition(conditions *clusterv1.Conditions, condition clusterv1.ConditionType) Condition {
13+
return func() bool {
14+
By(fmt.Sprintf("Checking if %s condition is set...", condition))
15+
for _, c := range *conditions {
16+
if c.Type == condition && c.Status == corev1.ConditionTrue {
17+
return true
18+
}
19+
}
20+
return false
21+
}
22+
}

0 commit comments

Comments
 (0)