Skip to content

Commit 7a9241f

Browse files
author
Per Goncalves da Silva
committed
Make webhook support e2es more robust
Signed-off-by: Per Goncalves da Silva <[email protected]>
1 parent 5786e67 commit 7a9241f

File tree

1 file changed

+42
-29
lines changed

1 file changed

+42
-29
lines changed

test/experimental-e2e/experimental_e2e_test.go

Lines changed: 42 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package experimental_e2e
22

33
import (
44
"context"
5-
"fmt"
65
"os"
76
"testing"
87
"time"
@@ -119,7 +118,8 @@ func TestWebhookSupport(t *testing.T) {
119118
Source: ocv1.CatalogSource{
120119
Type: ocv1.SourceTypeImage,
121120
Image: &ocv1.ImageSource{
122-
Ref: fmt.Sprintf("%s/e2e/test-catalog:v1", os.Getenv("LOCAL_REGISTRY_HOST")),
121+
// Ref: fmt.Sprintf("%s/e2e/test-catalog:v1", os.Getenv("LOCAL_REGISTRY_HOST")),
122+
Ref: "quay.io/operator-framework/webhook-operator-index:0.0.3",
123123
PollIntervalMinutes: ptr.To(1),
124124
},
125125
},
@@ -132,11 +132,11 @@ func TestWebhookSupport(t *testing.T) {
132132

133133
t.Log("By waiting for the catalog to serve its metadata")
134134
require.EventuallyWithT(t, func(ct *assert.CollectT) {
135-
assert.NoError(t, c.Get(context.Background(), types.NamespacedName{Name: extensionCatalog.GetName()}, extensionCatalog))
135+
require.NoError(ct, c.Get(context.Background(), types.NamespacedName{Name: extensionCatalog.GetName()}, extensionCatalog))
136136
cond := apimeta.FindStatusCondition(extensionCatalog.Status.Conditions, ocv1.TypeServing)
137-
assert.NotNil(t, cond)
138-
assert.Equal(t, metav1.ConditionTrue, cond.Status)
139-
assert.Equal(t, ocv1.ReasonAvailable, cond.Reason)
137+
require.NotNil(ct, cond)
138+
require.Equal(ct, metav1.ConditionTrue, cond.Status)
139+
require.Equal(ct, ocv1.ReasonAvailable, cond.Reason)
140140
}, pollDuration, pollInterval)
141141

142142
t.Log("By installing the webhook-operator ClusterExtension")
@@ -167,27 +167,27 @@ func TestWebhookSupport(t *testing.T) {
167167

168168
t.Log("By waiting for webhook-operator extension to be installed successfully")
169169
require.EventuallyWithT(t, func(ct *assert.CollectT) {
170-
assert.NoError(ct, c.Get(t.Context(), types.NamespacedName{Name: clusterExtension.Name}, clusterExtension))
170+
require.NoError(ct, c.Get(t.Context(), types.NamespacedName{Name: clusterExtension.Name}, clusterExtension))
171171
cond := apimeta.FindStatusCondition(clusterExtension.Status.Conditions, ocv1.TypeInstalled)
172-
if assert.NotNil(ct, cond) {
173-
assert.Equal(ct, metav1.ConditionTrue, cond.Status)
174-
assert.Equal(ct, ocv1.ReasonSucceeded, cond.Reason)
175-
assert.Contains(ct, cond.Message, "Installed bundle")
176-
assert.NotEmpty(ct, clusterExtension.Status.Install.Bundle)
177-
}
172+
require.NotNil(ct, cond)
173+
require.Equal(ct, metav1.ConditionTrue, cond.Status)
174+
require.Equal(ct, ocv1.ReasonSucceeded, cond.Reason)
175+
require.Contains(ct, cond.Message, "Installed bundle")
176+
require.NotNil(ct, clusterExtension.Status.Install)
177+
require.NotEmpty(ct, clusterExtension.Status.Install.Bundle)
178178
}, pollDuration, pollInterval)
179179

180180
t.Log("By waiting for webhook-operator deployment to be available")
181181
require.EventuallyWithT(t, func(ct *assert.CollectT) {
182182
deployment := &appsv1.Deployment{}
183-
assert.NoError(ct, c.Get(t.Context(), types.NamespacedName{Namespace: namespace.GetName(), Name: "webhook-operator-webhook"}, deployment))
183+
require.NoError(ct, c.Get(t.Context(), types.NamespacedName{Namespace: namespace.GetName(), Name: "webhook-operator-webhook"}, deployment))
184184
available := false
185185
for _, cond := range deployment.Status.Conditions {
186186
if cond.Type == appsv1.DeploymentAvailable {
187187
available = cond.Status == corev1.ConditionTrue
188188
}
189189
}
190-
assert.True(ct, available)
190+
require.True(ct, available)
191191
}, pollDuration, pollInterval)
192192

193193
v1Gvr := schema.GroupVersionResource{
@@ -197,21 +197,29 @@ func TestWebhookSupport(t *testing.T) {
197197
}
198198
v1Client := dynamicClient.Resource(v1Gvr).Namespace(namespace.GetName())
199199

200-
t.Log("By checking an invalid CR is rejected by the validating webhook")
201-
obj := getWebhookOperatorResource("invalid-test-cr", namespace.GetName(), false)
202-
_, err := v1Client.Create(t.Context(), obj, metav1.CreateOptions{})
203-
require.Error(t, err)
204-
require.Contains(t, err.Error(), "Invalid value: false: Spec.Valid must be true")
200+
t.Log("By eventually seeing that invalid CR creation is rejected by the validating webhook")
201+
require.EventuallyWithT(t, func(ct *assert.CollectT) {
202+
obj := getWebhookOperatorResource("invalid-test-cr", namespace.GetName(), false)
203+
_, err := v1Client.Create(t.Context(), obj, metav1.CreateOptions{})
204+
require.Error(ct, err)
205+
require.Contains(ct, err.Error(), "Invalid value: false: Spec.Valid must be true")
206+
}, pollDuration, pollInterval)
205207

206-
t.Log("By checking a valid CR is mutated by the mutating webhook")
207-
obj = getWebhookOperatorResource("valid-test-cr", namespace.GetName(), true)
208-
_, err = v1Client.Create(t.Context(), obj, metav1.CreateOptions{})
209-
require.NoError(t, err)
208+
var (
209+
res *unstructured.Unstructured
210+
err error
211+
obj = getWebhookOperatorResource("valid-test-cr", namespace.GetName(), true)
212+
)
213+
214+
t.Log("By eventually creating a valid CR")
215+
require.EventuallyWithT(t, func(ct *assert.CollectT) {
216+
res, err = v1Client.Create(t.Context(), obj, metav1.CreateOptions{})
217+
require.NoError(ct, err)
218+
}, pollDuration, pollInterval)
210219
t.Cleanup(func() {
211-
require.NoError(t, dynamicClient.Resource(v1Gvr).Namespace(namespace.GetName()).Delete(context.Background(), obj.GetName(), metav1.DeleteOptions{}))
220+
require.NoError(t, v1Client.Delete(context.Background(), obj.GetName(), metav1.DeleteOptions{}))
212221
})
213-
res, err := v1Client.Get(t.Context(), obj.GetName(), metav1.GetOptions{})
214-
require.NoError(t, err)
222+
215223
require.Equal(t, map[string]interface{}{
216224
"valid": true,
217225
"mutate": true,
@@ -225,8 +233,13 @@ func TestWebhookSupport(t *testing.T) {
225233
}
226234
v2Client := dynamicClient.Resource(v2Gvr).Namespace(namespace.GetName())
227235

228-
res, err = v2Client.Get(t.Context(), obj.GetName(), metav1.GetOptions{})
229-
require.NoError(t, err)
236+
t.Log("By eventually getting the valid CR with a v2 client")
237+
require.EventuallyWithT(t, func(ct *assert.CollectT) {
238+
res, err = v2Client.Get(t.Context(), obj.GetName(), metav1.GetOptions{})
239+
require.NoError(ct, err)
240+
}, pollDuration, pollInterval)
241+
242+
t.Log("and verifying that the CR is correctly converted")
230243
require.Equal(t, map[string]interface{}{
231244
"conversion": map[string]interface{}{
232245
"valid": true,

0 commit comments

Comments
 (0)