Skip to content

Commit

Permalink
feat: add immediate watcher and test wait ready (#104)
Browse files Browse the repository at this point in the history
## 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
  • Loading branch information
phillebaba authored Jun 20, 2024
1 parent b2d5cd4 commit 0899029
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 0 deletions.
28 changes: 28 additions & 0 deletions kubernetes/wait.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
17 changes: 17 additions & 0 deletions kubernetes/wait_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit 0899029

Please sign in to comment.