From 089902911ff3df6502784e75b800f27c60e1b4d7 Mon Sep 17 00:00:00 2001 From: Philip Laine Date: Thu, 20 Jun 2024 18:05:26 +0200 Subject: [PATCH] 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 --- kubernetes/wait.go | 28 ++++++++++++++++++++++++++++ kubernetes/wait_test.go | 17 +++++++++++++++++ 2 files changed, 45 insertions(+) diff --git a/kubernetes/wait.go b/kubernetes/wait.go index 15a0fed..04d1477 100644 --- a/kubernetes/wait.go +++ b/kubernetes/wait.go @@ -70,3 +70,31 @@ func WaitForReady(ctx context.Context, sw watcher.StatusWatcher, objs []object.O } return nil } + +// ImmediateWatcher should only be used for testing and returns the set status immediatly. +type ImmediateWatcher struct { + status status.Status +} + +// NewImmediateWatcher returns a ImmediateWatcher. +func NewImmediateWatcher(status status.Status) *ImmediateWatcher { + return &ImmediateWatcher{ + status: status, + } +} + +// Watch watches the given objects and immediatly returns the configured status. +func (w *ImmediateWatcher) Watch(_ context.Context, objs object.ObjMetadataSet, _ watcher.Options) <-chan event.Event { + eventCh := make(chan event.Event, len(objs)) + for _, obj := range objs { + eventCh <- event.Event{ + Type: event.ResourceUpdateEvent, + Resource: &event.ResourceStatus{ + Identifier: obj, + Status: w.status, + }, + } + } + close(eventCh) + return eventCh +} diff --git a/kubernetes/wait_test.go b/kubernetes/wait_test.go index 9646531..dece27b 100644 --- a/kubernetes/wait_test.go +++ b/kubernetes/wait_test.go @@ -9,10 +9,27 @@ import ( "github.com/stretchr/testify/require" "k8s.io/apimachinery/pkg/runtime/schema" + "sigs.k8s.io/cli-utils/pkg/kstatus/status" "sigs.k8s.io/cli-utils/pkg/kstatus/watcher" "sigs.k8s.io/cli-utils/pkg/object" ) +func TestWaitForReady(t *testing.T) { + sw := NewImmediateWatcher(status.CurrentStatus) + objs := []object.ObjMetadata{ + { + GroupKind: schema.GroupKind{ + Group: "apps", + Kind: "Deployment", + }, + Namespace: "foo", + Name: "bar", + }, + } + err := WaitForReady(context.Background(), sw, objs) + require.NoError(t, err) +} + func TestWaitForReadyCanceled(t *testing.T) { ctx, cancel := context.WithCancel(context.Background()) cancel()