Skip to content

Commit 0899029

Browse files
authored
feat: add immediate watcher and test wait ready (#104)
## Description Adds and immediate watcher for unit testing. This change also expands the wait for ready test using the immediate watcher. ## Related Issue N/A
1 parent b2d5cd4 commit 0899029

File tree

2 files changed

+45
-0
lines changed

2 files changed

+45
-0
lines changed

kubernetes/wait.go

+28
Original file line numberDiff line numberDiff line change
@@ -70,3 +70,31 @@ func WaitForReady(ctx context.Context, sw watcher.StatusWatcher, objs []object.O
7070
}
7171
return nil
7272
}
73+
74+
// ImmediateWatcher should only be used for testing and returns the set status immediatly.
75+
type ImmediateWatcher struct {
76+
status status.Status
77+
}
78+
79+
// NewImmediateWatcher returns a ImmediateWatcher.
80+
func NewImmediateWatcher(status status.Status) *ImmediateWatcher {
81+
return &ImmediateWatcher{
82+
status: status,
83+
}
84+
}
85+
86+
// Watch watches the given objects and immediatly returns the configured status.
87+
func (w *ImmediateWatcher) Watch(_ context.Context, objs object.ObjMetadataSet, _ watcher.Options) <-chan event.Event {
88+
eventCh := make(chan event.Event, len(objs))
89+
for _, obj := range objs {
90+
eventCh <- event.Event{
91+
Type: event.ResourceUpdateEvent,
92+
Resource: &event.ResourceStatus{
93+
Identifier: obj,
94+
Status: w.status,
95+
},
96+
}
97+
}
98+
close(eventCh)
99+
return eventCh
100+
}

kubernetes/wait_test.go

+17
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,27 @@ import (
99

1010
"github.com/stretchr/testify/require"
1111
"k8s.io/apimachinery/pkg/runtime/schema"
12+
"sigs.k8s.io/cli-utils/pkg/kstatus/status"
1213
"sigs.k8s.io/cli-utils/pkg/kstatus/watcher"
1314
"sigs.k8s.io/cli-utils/pkg/object"
1415
)
1516

17+
func TestWaitForReady(t *testing.T) {
18+
sw := NewImmediateWatcher(status.CurrentStatus)
19+
objs := []object.ObjMetadata{
20+
{
21+
GroupKind: schema.GroupKind{
22+
Group: "apps",
23+
Kind: "Deployment",
24+
},
25+
Namespace: "foo",
26+
Name: "bar",
27+
},
28+
}
29+
err := WaitForReady(context.Background(), sw, objs)
30+
require.NoError(t, err)
31+
}
32+
1633
func TestWaitForReadyCanceled(t *testing.T) {
1734
ctx, cancel := context.WithCancel(context.Background())
1835
cancel()

0 commit comments

Comments
 (0)