Skip to content

Commit 50b5016

Browse files
authored
Return watch errors from helpers (#11)
Signed-off-by: Jakub Scholz <www@scholzj.com>
1 parent 157aef6 commit 50b5016

File tree

2 files changed

+38
-2
lines changed

2 files changed

+38
-2
lines changed

cmd/utils.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ func waitUntilReady(client strimzi.Interface, name string, namespace string, tim
130130

131131
watcher, err := client.KafkaV1().Kafkas(namespace).Watch(watchContext, metav1.ListOptions{FieldSelector: fields.OneTermEqualSelector(metav1.ObjectNameField, name).String()})
132132
if err != nil {
133-
panic(err)
133+
return false, fmt.Errorf("failed to watch Kafka cluster %s in namespace %s: %w", name, namespace, err)
134134
}
135135

136136
defer watcher.Stop()
@@ -172,7 +172,7 @@ func waitUntilReconciliationPaused(client strimzi.Interface, name string, namesp
172172

173173
watcher, err := client.KafkaV1().Kafkas(namespace).Watch(watchContext, metav1.ListOptions{FieldSelector: fields.OneTermEqualSelector(metav1.ObjectNameField, name).String()})
174174
if err != nil {
175-
panic(err)
175+
return false, fmt.Errorf("failed to watch Kafka cluster %s in namespace %s: %w", name, namespace, err)
176176
}
177177

178178
defer watcher.Stop()

cmd/utils_test.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,24 @@ func TestWaitUntilReady_ReturnsErrorOnTimeout(t *testing.T) {
229229
}
230230
}
231231

232+
func TestWaitUntilReady_ReturnsErrorWhenWatchFails(t *testing.T) {
233+
strimziClient := strimzifake.NewSimpleClientset()
234+
wantErr := errors.New("watch failed")
235+
236+
strimziClient.PrependWatchReactor("kafkas", func(action k8stesting.Action) (bool, watch.Interface, error) {
237+
return true, nil, wantErr
238+
})
239+
240+
_, err := waitUntilReady(strimziClient, "my-cluster", "ns", 10)
241+
if err == nil {
242+
t.Fatal("expected watch error, got nil")
243+
}
244+
245+
if !errors.Is(err, wantErr) {
246+
t.Fatalf("expected %v, got %v", wantErr, err)
247+
}
248+
}
249+
232250
func TestWaitUntilReconciliationPaused_ReturnsTrueWhenPausedEventArrives(t *testing.T) {
233251
strimziClient := strimzifake.NewSimpleClientset()
234252
fakeWatch := watch.NewFake()
@@ -301,6 +319,24 @@ func TestWaitUntilReconciliationPaused_ReturnsErrorOnTimeout(t *testing.T) {
301319
}
302320
}
303321

322+
func TestWaitUntilReconciliationPaused_ReturnsErrorWhenWatchFails(t *testing.T) {
323+
strimziClient := strimzifake.NewSimpleClientset()
324+
wantErr := errors.New("watch failed")
325+
326+
strimziClient.PrependWatchReactor("kafkas", func(action k8stesting.Action) (bool, watch.Interface, error) {
327+
return true, nil, wantErr
328+
})
329+
330+
_, err := waitUntilReconciliationPaused(strimziClient, "my-cluster", "ns", 10)
331+
if err == nil {
332+
t.Fatal("expected watch error, got nil")
333+
}
334+
335+
if !errors.Is(err, wantErr) {
336+
t.Fatalf("expected %v, got %v", wantErr, err)
337+
}
338+
}
339+
304340
func TestWaitForPodSetPodsDeletion_ReturnsNilWhenNoPodsExist(t *testing.T) {
305341
kube := k8sfake.NewSimpleClientset()
306342

0 commit comments

Comments
 (0)