|
| 1 | +/*--------------------------------------------------------------------------------------------- |
| 2 | + * Copyright (c) Microsoft Corporation. All rights reserved. |
| 3 | + * Licensed under the MIT License. See LICENSE in the project root for license information. |
| 4 | + *--------------------------------------------------------------------------------------------*/ |
| 5 | + |
| 6 | +package v1 |
| 7 | + |
| 8 | +import ( |
| 9 | + "context" |
| 10 | + "errors" |
| 11 | + "io" |
| 12 | + "testing" |
| 13 | + |
| 14 | + "github.com/go-logr/logr" |
| 15 | + "github.com/stretchr/testify/require" |
| 16 | + apiserver_resource "github.com/tilt-dev/tilt-apiserver/pkg/server/builder/resource" |
| 17 | + metainternalversion "k8s.io/apimachinery/pkg/apis/meta/internalversion" |
| 18 | + "k8s.io/apimachinery/pkg/watch" |
| 19 | + registry_rest "k8s.io/apiserver/pkg/registry/rest" |
| 20 | + |
| 21 | + "github.com/microsoft/dcp/pkg/testutil" |
| 22 | +) |
| 23 | + |
| 24 | +// Verifies that LogStorage.watchResource does not allocate a resource watcher when parent |
| 25 | +// watch setup fails and can establish the watch on a subsequent request. |
| 26 | +func TestLogStorageWatchSetupFailureCanBeRetried(t *testing.T) { |
| 27 | + ctx, cancel := testutil.GetTestContext(t, 0) |
| 28 | + defer cancel() |
| 29 | + setupErr := errors.New("test watch setup failure") |
| 30 | + parentWatcher := watch.NewRaceFreeFake() |
| 31 | + parentStorage := &logStorageWatchSource{watcher: parentWatcher, watchErr: setupErr} |
| 32 | + storage, storageErr := NewLogStorage(parentStorage, logStorageTestStreamer{}) |
| 33 | + require.NoError(t, storageErr) |
| 34 | + t.Cleanup(storage.Destroy) |
| 35 | + |
| 36 | + resourceWatcher, watchErr := storage.watchResource("resource", logr.Discard()) |
| 37 | + require.ErrorIs(t, watchErr, setupErr) |
| 38 | + require.Nil(t, resourceWatcher) |
| 39 | + require.Nil(t, storage.watcher) |
| 40 | + require.True(t, storage.resourceWatchers.Empty()) |
| 41 | + |
| 42 | + parentStorage.watchErr = nil |
| 43 | + retryWatcher, retryErr := storage.watchResource("resource", logr.Discard()) |
| 44 | + require.NoError(t, retryErr) |
| 45 | + retryWatcher.Stop() |
| 46 | + select { |
| 47 | + case _, open := <-retryWatcher.ResultChan(): |
| 48 | + require.False(t, open) |
| 49 | + case <-ctx.Done(): |
| 50 | + t.Fatal(ctx.Err()) |
| 51 | + } |
| 52 | + |
| 53 | + storage.Destroy() |
| 54 | + require.True(t, parentWatcher.IsStopped()) |
| 55 | +} |
| 56 | + |
| 57 | +// Verifies that LogStorage.Destroy closes its disposal signal without a parent watcher, |
| 58 | +// remains idempotent, and rejects subsequent resource-watch requests. |
| 59 | +func TestLogStorageDestroyWithoutParentWatcher(t *testing.T) { |
| 60 | + storage, storageErr := NewLogStorage(&logStorageWatchSource{}, logStorageTestStreamer{}) |
| 61 | + require.NoError(t, storageErr) |
| 62 | + t.Cleanup(storage.Destroy) |
| 63 | + |
| 64 | + storage.Destroy() |
| 65 | + storage.Destroy() |
| 66 | + select { |
| 67 | + case <-storage.disposeCh: |
| 68 | + default: |
| 69 | + t.Fatal("log storage disposal signal was not closed") |
| 70 | + } |
| 71 | + resourceWatcher, watchErr := storage.watchResource("resource", logr.Discard()) |
| 72 | + require.ErrorContains(t, watchErr, "disposed") |
| 73 | + require.Nil(t, resourceWatcher) |
| 74 | + require.True(t, storage.resourceWatchers.Empty()) |
| 75 | +} |
| 76 | + |
| 77 | +type logStorageWatchSource struct { |
| 78 | + registry_rest.StandardStorage |
| 79 | + watcher watch.Interface |
| 80 | + watchErr error |
| 81 | +} |
| 82 | + |
| 83 | +func (source *logStorageWatchSource) Watch(context.Context, *metainternalversion.ListOptions) (watch.Interface, error) { |
| 84 | + if source.watchErr != nil { |
| 85 | + return nil, source.watchErr |
| 86 | + } |
| 87 | + return source.watcher, nil |
| 88 | +} |
| 89 | + |
| 90 | +type logStorageTestStreamer struct{} |
| 91 | + |
| 92 | +func (logStorageTestStreamer) Preflight(apiserver_resource.Object, *LogOptions) error { |
| 93 | + return nil |
| 94 | +} |
| 95 | + |
| 96 | +func (logStorageTestStreamer) StreamLogs(context.Context, io.Writer, apiserver_resource.Object, *LogOptions, logr.Logger) (ResourceStreamStatus, <-chan struct{}, error) { |
| 97 | + return ResourceStreamStatusDone, nil, nil |
| 98 | +} |
| 99 | + |
| 100 | +func (logStorageTestStreamer) OnResourceUpdated(ResourceWatcherEvent, logr.Logger) {} |
| 101 | + |
| 102 | +func (logStorageTestStreamer) Dispose() error { |
| 103 | + return nil |
| 104 | +} |
0 commit comments