|
| 1 | +/* |
| 2 | +Copyright 2024 The Kubernetes Authors. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package cache |
| 18 | + |
| 19 | +import ( |
| 20 | + "context" |
| 21 | + "fmt" |
| 22 | + "testing" |
| 23 | + "time" |
| 24 | + |
| 25 | + v1 "k8s.io/api/core/v1" |
| 26 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 27 | + "k8s.io/apimachinery/pkg/runtime" |
| 28 | + "k8s.io/apimachinery/pkg/util/wait" |
| 29 | + "k8s.io/apimachinery/pkg/watch" |
| 30 | + clientfeatures "k8s.io/client-go/features" |
| 31 | + clientfeaturestesting "k8s.io/client-go/features/testing" |
| 32 | + "k8s.io/client-go/util/consistencydetector" |
| 33 | + "k8s.io/klog/v2/ktesting" |
| 34 | +) |
| 35 | + |
| 36 | +func TestReflectorDataConsistencyDetector(t *testing.T) { |
| 37 | + clientfeaturestesting.SetFeatureDuringTest(t, clientfeatures.WatchListClient, true) |
| 38 | + restore := consistencydetector.SetDataConsistencyDetectionForWatchListEnabledForTest(true) |
| 39 | + defer restore() |
| 40 | + |
| 41 | + markTransformed := func(obj interface{}) (interface{}, error) { |
| 42 | + pod, ok := obj.(*v1.Pod) |
| 43 | + if !ok { |
| 44 | + return obj, nil |
| 45 | + } |
| 46 | + newPod := pod.DeepCopy() |
| 47 | + if newPod.Labels == nil { |
| 48 | + newPod.Labels = make(map[string]string) |
| 49 | + } |
| 50 | + newPod.Labels["transformed"] = "true" |
| 51 | + return newPod, nil |
| 52 | + } |
| 53 | + |
| 54 | + for _, inOrder := range []bool{false, true} { |
| 55 | + t.Run(fmt.Sprintf("InOrder=%v", inOrder), func(t *testing.T) { |
| 56 | + clientfeaturestesting.SetFeatureDuringTest(t, clientfeatures.InOrderInformers, inOrder) |
| 57 | + for _, transformerEnabled := range []bool{false, true} { |
| 58 | + var transformer TransformFunc |
| 59 | + if transformerEnabled { |
| 60 | + transformer = markTransformed |
| 61 | + } |
| 62 | + t.Run(fmt.Sprintf("Transformer=%v", transformerEnabled), func(t *testing.T) { |
| 63 | + runTestReflectorDataConsistencyDetector(t, transformer) |
| 64 | + }) |
| 65 | + } |
| 66 | + }) |
| 67 | + } |
| 68 | +} |
| 69 | + |
| 70 | +func runTestReflectorDataConsistencyDetector(t *testing.T, transformer TransformFunc) { |
| 71 | + _, ctx := ktesting.NewTestContext(t) |
| 72 | + ctx, cancel := context.WithCancel(ctx) |
| 73 | + defer cancel() |
| 74 | + |
| 75 | + store := NewStore(MetaNamespaceKeyFunc) |
| 76 | + fifo := newQueueFIFO(store, transformer) |
| 77 | + |
| 78 | + lw := &ListWatch{ |
| 79 | + ListFunc: func(options metav1.ListOptions) (runtime.Object, error) { |
| 80 | + return &v1.PodList{ |
| 81 | + ListMeta: metav1.ListMeta{ResourceVersion: "1"}, |
| 82 | + Items: []v1.Pod{ |
| 83 | + {ObjectMeta: metav1.ObjectMeta{Name: "pod-1", ResourceVersion: "1"}}, |
| 84 | + }, |
| 85 | + }, nil |
| 86 | + }, |
| 87 | + WatchFunc: func(options metav1.ListOptions) (watch.Interface, error) { |
| 88 | + w := watch.NewFake() |
| 89 | + go func() { |
| 90 | + w.Add(&v1.Pod{ObjectMeta: metav1.ObjectMeta{Name: "pod-1", ResourceVersion: "1"}}) |
| 91 | + w.Action(watch.Bookmark, &v1.Pod{ObjectMeta: metav1.ObjectMeta{ |
| 92 | + Name: "pod-1", |
| 93 | + ResourceVersion: "1", |
| 94 | + Annotations: map[string]string{metav1.InitialEventsAnnotationKey: "true"}, |
| 95 | + }}) |
| 96 | + }() |
| 97 | + return w, nil |
| 98 | + }, |
| 99 | + } |
| 100 | + |
| 101 | + r := NewReflector(lw, &v1.Pod{}, fifo, 0) |
| 102 | + |
| 103 | + go func() { |
| 104 | + _ = wait.PollUntilContextTimeout(ctx, 10*time.Millisecond, 5*time.Second, true, func(ctx context.Context) (bool, error) { |
| 105 | + return r.LastSyncResourceVersion() != "", nil |
| 106 | + }) |
| 107 | + cancel() |
| 108 | + }() |
| 109 | + |
| 110 | + err := r.ListAndWatchWithContext(ctx) |
| 111 | + if err != nil { |
| 112 | + t.Errorf("ListAndWatchWithContext returned error: %v", err) |
| 113 | + } |
| 114 | +} |
0 commit comments