|
| 1 | +package observe |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "errors" |
| 6 | + "strings" |
| 7 | + "testing" |
| 8 | + "time" |
| 9 | + |
| 10 | + apierrors "k8s.io/apimachinery/pkg/api/errors" |
| 11 | + "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 12 | + "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" |
| 13 | + "k8s.io/apimachinery/pkg/runtime/schema" |
| 14 | + "k8s.io/apimachinery/pkg/types" |
| 15 | + "k8s.io/apimachinery/pkg/watch" |
| 16 | + "k8s.io/client-go/dynamic" |
| 17 | + "k8s.io/klog/v2" |
| 18 | +) |
| 19 | + |
| 20 | +type fakeNamespaceableResource struct { |
| 21 | + listFn func(ctx context.Context, opts v1.ListOptions) (*unstructured.UnstructuredList, error) |
| 22 | + watchFn func(ctx context.Context, opts v1.ListOptions) (watch.Interface, error) |
| 23 | +} |
| 24 | + |
| 25 | +func (f *fakeNamespaceableResource) Namespace(string) dynamic.ResourceInterface { |
| 26 | + return f |
| 27 | +} |
| 28 | + |
| 29 | +func (f *fakeNamespaceableResource) Create(context.Context, *unstructured.Unstructured, v1.CreateOptions, ...string) (*unstructured.Unstructured, error) { |
| 30 | + panic("not implemented") |
| 31 | +} |
| 32 | + |
| 33 | +func (f *fakeNamespaceableResource) Update(context.Context, *unstructured.Unstructured, v1.UpdateOptions, ...string) (*unstructured.Unstructured, error) { |
| 34 | + panic("not implemented") |
| 35 | +} |
| 36 | + |
| 37 | +func (f *fakeNamespaceableResource) UpdateStatus(context.Context, *unstructured.Unstructured, v1.UpdateOptions) (*unstructured.Unstructured, error) { |
| 38 | + panic("not implemented") |
| 39 | +} |
| 40 | + |
| 41 | +func (f *fakeNamespaceableResource) Delete(context.Context, string, v1.DeleteOptions, ...string) error { |
| 42 | + panic("not implemented") |
| 43 | +} |
| 44 | + |
| 45 | +func (f *fakeNamespaceableResource) DeleteCollection(context.Context, v1.DeleteOptions, v1.ListOptions) error { |
| 46 | + panic("not implemented") |
| 47 | +} |
| 48 | + |
| 49 | +func (f *fakeNamespaceableResource) Get(context.Context, string, v1.GetOptions, ...string) (*unstructured.Unstructured, error) { |
| 50 | + panic("not implemented") |
| 51 | +} |
| 52 | + |
| 53 | +func (f *fakeNamespaceableResource) List(ctx context.Context, opts v1.ListOptions) (*unstructured.UnstructuredList, error) { |
| 54 | + return f.listFn(ctx, opts) |
| 55 | +} |
| 56 | + |
| 57 | +func (f *fakeNamespaceableResource) Watch(ctx context.Context, opts v1.ListOptions) (watch.Interface, error) { |
| 58 | + return f.watchFn(ctx, opts) |
| 59 | +} |
| 60 | + |
| 61 | +func (f *fakeNamespaceableResource) Patch(context.Context, string, types.PatchType, []byte, v1.PatchOptions, ...string) (*unstructured.Unstructured, error) { |
| 62 | + panic("not implemented") |
| 63 | +} |
| 64 | + |
| 65 | +func (f *fakeNamespaceableResource) Apply(context.Context, string, *unstructured.Unstructured, v1.ApplyOptions, ...string) (*unstructured.Unstructured, error) { |
| 66 | + panic("not implemented") |
| 67 | +} |
| 68 | + |
| 69 | +func (f *fakeNamespaceableResource) ApplyStatus(context.Context, string, *unstructured.Unstructured, v1.ApplyOptions) (*unstructured.Unstructured, error) { |
| 70 | + panic("not implemented") |
| 71 | +} |
| 72 | + |
| 73 | +type trackingWatch struct { |
| 74 | + resultC chan watch.Event |
| 75 | + stopped bool |
| 76 | +} |
| 77 | + |
| 78 | +func newTrackingWatch() *trackingWatch { |
| 79 | + return &trackingWatch{ |
| 80 | + resultC: make(chan watch.Event, 4), |
| 81 | + } |
| 82 | +} |
| 83 | + |
| 84 | +func (w *trackingWatch) Stop() { |
| 85 | + if w.stopped { |
| 86 | + return |
| 87 | + } |
| 88 | + w.stopped = true |
| 89 | + close(w.resultC) |
| 90 | +} |
| 91 | + |
| 92 | +func (w *trackingWatch) ResultChan() <-chan watch.Event { |
| 93 | + return w.resultC |
| 94 | +} |
| 95 | + |
| 96 | +func TestListAndWatchResource_StopsWatchAndHandlesErrorEvent(t *testing.T) { |
| 97 | + t.Parallel() |
| 98 | + |
| 99 | + resourceWatch := newTrackingWatch() |
| 100 | + resourceWatch.resultC <- watch.Event{ |
| 101 | + Type: watch.Error, |
| 102 | + Object: &v1.Status{ |
| 103 | + Reason: v1.StatusReasonExpired, |
| 104 | + Message: "resource version too old", |
| 105 | + }, |
| 106 | + } |
| 107 | + |
| 108 | + client := &fakeNamespaceableResource{ |
| 109 | + listFn: func(context.Context, v1.ListOptions) (*unstructured.UnstructuredList, error) { |
| 110 | + return &unstructured.UnstructuredList{ |
| 111 | + Object: map[string]interface{}{ |
| 112 | + "metadata": map[string]interface{}{ |
| 113 | + "resourceVersion": "123", |
| 114 | + }, |
| 115 | + }, |
| 116 | + }, nil |
| 117 | + }, |
| 118 | + watchFn: func(context.Context, v1.ListOptions) (watch.Interface, error) { |
| 119 | + return resourceWatch, nil |
| 120 | + }, |
| 121 | + } |
| 122 | + |
| 123 | + err := listAndWatchResource(context.Background(), klog.NewKlogr(), client, schema.GroupVersionResource{Resource: "pods"}, map[types.UID]*resourceMeta{}, make(chan *ResourceObservation, 8)) |
| 124 | + if !errors.Is(err, errWatchErrorEvent) { |
| 125 | + t.Fatalf("expected watch error event, got: %v", err) |
| 126 | + } |
| 127 | + if !strings.Contains(err.Error(), "resource version too old") { |
| 128 | + t.Fatalf("expected status message in error, got: %v", err) |
| 129 | + } |
| 130 | + if !resourceWatch.stopped { |
| 131 | + t.Fatalf("expected watch.Stop() to be called") |
| 132 | + } |
| 133 | +} |
| 134 | + |
| 135 | +func TestNextRetryDelay_BackoffAndNotFound(t *testing.T) { |
| 136 | + t.Parallel() |
| 137 | + |
| 138 | + notFoundWrapped := apierrors.NewNotFound(schema.GroupResource{Group: "apps", Resource: "deployments"}, "example") |
| 139 | + if got := nextRetryDelay(notFoundWrapped, 5); got != notFoundRetryDelay { |
| 140 | + t.Fatalf("expected not found retry delay %v, got %v", notFoundRetryDelay, got) |
| 141 | + } |
| 142 | + |
| 143 | + first := nextRetryDelay(errors.New("watch failed"), 0) |
| 144 | + second := nextRetryDelay(errors.New("watch failed"), 1) |
| 145 | + if second <= first { |
| 146 | + t.Fatalf("expected retry delay to increase, got first=%v second=%v", first, second) |
| 147 | + } |
| 148 | + |
| 149 | + maxed := nextRetryDelay(errors.New("watch failed"), 50) |
| 150 | + if maxed > maxRetryDelay { |
| 151 | + t.Fatalf("expected retry delay to cap at <= %v, got %v", maxRetryDelay, maxed) |
| 152 | + } |
| 153 | +} |
| 154 | + |
| 155 | +func TestWaitForRetry_CancelledContext(t *testing.T) { |
| 156 | + t.Parallel() |
| 157 | + |
| 158 | + ctx, cancel := context.WithCancel(context.Background()) |
| 159 | + cancel() |
| 160 | + |
| 161 | + start := time.Now() |
| 162 | + ok := waitForRetry(ctx, 2*time.Second) |
| 163 | + if ok { |
| 164 | + t.Fatalf("expected waitForRetry to abort on cancelled context") |
| 165 | + } |
| 166 | + if elapsed := time.Since(start); elapsed > 250*time.Millisecond { |
| 167 | + t.Fatalf("expected prompt cancellation, elapsed=%v", elapsed) |
| 168 | + } |
| 169 | +} |
0 commit comments