From 7547c3172f1a7078c1cd212504d194487cc2cc6b Mon Sep 17 00:00:00 2001 From: Philip Laine Date: Tue, 18 Jun 2024 10:59:46 +0200 Subject: [PATCH] feat: add immediate watcher and test wait ready --- 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()