Skip to content

Commit 9fb0561

Browse files
committed
Drop WatchSequenceEventVerifier from configmap lifecycle test
Collecting events in a certain order and then verifying the order in which they were collected feels redundant.
1 parent 71c352d commit 9fb0561

File tree

1 file changed

+78
-101
lines changed

1 file changed

+78
-101
lines changed

test/e2e/common/configmap.go

Lines changed: 78 additions & 101 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,11 @@ import (
2424

2525
v1 "k8s.io/api/core/v1"
2626
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
27-
"k8s.io/apimachinery/pkg/runtime/schema"
2827
"k8s.io/apimachinery/pkg/types"
2928
"k8s.io/apimachinery/pkg/util/uuid"
3029
watch "k8s.io/apimachinery/pkg/watch"
3130
"k8s.io/client-go/dynamic"
31+
"k8s.io/client-go/tools/cache"
3232
watchtools "k8s.io/client-go/tools/watch"
3333
"k8s.io/kubernetes/test/e2e/framework"
3434
imageutils "k8s.io/kubernetes/test/utils/image"
@@ -172,12 +172,6 @@ var _ = ginkgo.Describe("[sig-node] ConfigMap", func() {
172172
testNamespaceName := f.Namespace.Name
173173
testConfigMapName := "test-configmap" + string(uuid.NewUUID())
174174

175-
configMapResource := schema.GroupVersionResource{Group: "", Version: "v1", Resource: "configmaps"}
176-
expectedWatchEvents := []watch.Event{
177-
{Type: watch.Added},
178-
{Type: watch.Modified},
179-
{Type: watch.Deleted},
180-
}
181175
testConfigMap := v1.ConfigMap{
182176
ObjectMeta: metav1.ObjectMeta{
183177
Name: testConfigMapName,
@@ -190,103 +184,86 @@ var _ = ginkgo.Describe("[sig-node] ConfigMap", func() {
190184
},
191185
}
192186

193-
framework.WatchEventSequenceVerifier(context.TODO(), dc, configMapResource, testNamespaceName, testConfigMapName, metav1.ListOptions{LabelSelector: "test-configmap-static=true"}, expectedWatchEvents, func(retryWatcher *watchtools.RetryWatcher) (actualWatchEvents []watch.Event) {
194-
ginkgo.By("creating a ConfigMap")
195-
_, err := f.ClientSet.CoreV1().ConfigMaps(testNamespaceName).Create(context.TODO(), &testConfigMap, metav1.CreateOptions{})
196-
framework.ExpectNoError(err, "failed to create ConfigMap")
197-
eventFound := false
198-
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
199-
defer cancel()
200-
_, err = framework.WatchUntilWithoutRetry(ctx, retryWatcher, func(watchEvent watch.Event) (bool, error) {
201-
if watchEvent.Type != watch.Added {
202-
return false, nil
203-
}
204-
actualWatchEvents = append(actualWatchEvents, watchEvent)
205-
eventFound = true
206-
return true, nil
207-
})
208-
framework.ExpectNoError(err, "Wait until condition with watch events should not return an error")
209-
framework.ExpectEqual(eventFound, true, "failed to find ConfigMap %v event", watch.Added)
210-
211-
configMapPatchPayload, err := json.Marshal(v1.ConfigMap{
212-
ObjectMeta: metav1.ObjectMeta{
213-
Labels: map[string]string{
214-
"test-configmap": "patched",
215-
},
216-
},
217-
Data: map[string]string{
218-
"valueName": "value1",
187+
w := &cache.ListWatch{
188+
WatchFunc: func(options metav1.ListOptions) (watch.Interface, error) {
189+
options.LabelSelector = "test-configmap-static=true"
190+
return f.ClientSet.CoreV1().ConfigMaps(testNamespaceName).Watch(context.TODO(), options)
191+
},
192+
}
193+
cml, err := f.ClientSet.CoreV1().ConfigMaps(testNamespaceName).List(context.TODO(), metav1.ListOptions{LabelSelector: "test-configmap-static=true"})
194+
framework.ExpectNoError(err)
195+
retryWatcher, err := watchtools.NewRetryWatcher(cml.ResourceVersion, w)
196+
197+
ginkgo.By("creating a ConfigMap")
198+
_, err = f.ClientSet.CoreV1().ConfigMaps(testNamespaceName).Create(context.TODO(), &testConfigMap, metav1.CreateOptions{})
199+
framework.ExpectNoError(err, "failed to create ConfigMap")
200+
201+
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
202+
defer cancel()
203+
_, err = framework.WatchUntilWithoutRetry(ctx, retryWatcher, func(watchEvent watch.Event) (bool, error) {
204+
return watchEvent.Type == watch.Added, nil
205+
})
206+
framework.ExpectNoError(err, "failed to see a watch.Added event for the configmap we created")
207+
208+
configMapPatchPayload, err := json.Marshal(v1.ConfigMap{
209+
ObjectMeta: metav1.ObjectMeta{
210+
Labels: map[string]string{
211+
"test-configmap": "patched",
219212
},
220-
})
221-
framework.ExpectNoError(err, "failed to marshal patch data")
222-
223-
ginkgo.By("patching the ConfigMap")
224-
_, err = f.ClientSet.CoreV1().ConfigMaps(testNamespaceName).Patch(context.TODO(), testConfigMapName, types.StrategicMergePatchType, []byte(configMapPatchPayload), metav1.PatchOptions{})
225-
framework.ExpectNoError(err, "failed to patch ConfigMap")
226-
ginkgo.By("waiting for the ConfigMap to be modified")
227-
eventFound = false
228-
ctx, cancel = context.WithTimeout(context.Background(), 30*time.Second)
229-
defer cancel()
230-
_, err = framework.WatchUntilWithoutRetry(ctx, retryWatcher, func(watchEvent watch.Event) (bool, error) {
231-
if watchEvent.Type != watch.Modified {
232-
return false, nil
233-
}
234-
actualWatchEvents = append(actualWatchEvents, watchEvent)
235-
eventFound = true
236-
return true, nil
237-
})
238-
framework.ExpectNoError(err, "Wait until condition with watch events should not return an error")
239-
framework.ExpectEqual(eventFound, true, "failed to find ConfigMap %v event", watch.Modified)
240-
241-
ginkgo.By("fetching the ConfigMap")
242-
configMap, err := f.ClientSet.CoreV1().ConfigMaps(testNamespaceName).Get(context.TODO(), testConfigMapName, metav1.GetOptions{})
243-
framework.ExpectNoError(err, "failed to get ConfigMap")
244-
framework.ExpectEqual(configMap.Data["valueName"], "value1", "failed to patch ConfigMap")
245-
framework.ExpectEqual(configMap.Labels["test-configmap"], "patched", "failed to patch ConfigMap")
246-
247-
ginkgo.By("listing all ConfigMaps in all namespaces")
248-
configMapList, err := f.ClientSet.CoreV1().ConfigMaps("").List(context.TODO(), metav1.ListOptions{
249-
LabelSelector: "test-configmap-static=true",
250-
})
251-
framework.ExpectNoError(err, "failed to list ConfigMaps with LabelSelector")
252-
framework.ExpectNotEqual(len(configMapList.Items), 0, "no ConfigMaps found in ConfigMap list")
253-
testConfigMapFound := false
254-
for _, cm := range configMapList.Items {
255-
if cm.ObjectMeta.Name == testConfigMapName &&
256-
cm.ObjectMeta.Namespace == testNamespaceName &&
257-
cm.ObjectMeta.Labels["test-configmap-static"] == "true" &&
258-
cm.Data["valueName"] == "value1" {
259-
testConfigMapFound = true
260-
break
261-
}
213+
},
214+
Data: map[string]string{
215+
"valueName": "value1",
216+
},
217+
})
218+
framework.ExpectNoError(err, "failed to marshal patch data")
219+
220+
ginkgo.By("patching the ConfigMap")
221+
_, err = f.ClientSet.CoreV1().ConfigMaps(testNamespaceName).Patch(context.TODO(), testConfigMapName, types.StrategicMergePatchType, []byte(configMapPatchPayload), metav1.PatchOptions{})
222+
framework.ExpectNoError(err, "failed to patch ConfigMap")
223+
ginkgo.By("waiting for the ConfigMap to be modified")
224+
ctx, cancel = context.WithTimeout(context.Background(), 30*time.Second)
225+
defer cancel()
226+
_, err = framework.WatchUntilWithoutRetry(ctx, retryWatcher, func(watchEvent watch.Event) (bool, error) {
227+
return watchEvent.Type == watch.Modified, nil
228+
})
229+
framework.ExpectNoError(err, "failed to see a watch.Modified event for the configmap we patched")
230+
231+
ginkgo.By("fetching the ConfigMap")
232+
configMap, err := f.ClientSet.CoreV1().ConfigMaps(testNamespaceName).Get(context.TODO(), testConfigMapName, metav1.GetOptions{})
233+
framework.ExpectNoError(err, "failed to get ConfigMap")
234+
framework.ExpectEqual(configMap.Data["valueName"], "value1", "failed to patch ConfigMap")
235+
framework.ExpectEqual(configMap.Labels["test-configmap"], "patched", "failed to patch ConfigMap")
236+
237+
ginkgo.By("listing all ConfigMaps in all namespaces")
238+
configMapList, err := f.ClientSet.CoreV1().ConfigMaps("").List(context.TODO(), metav1.ListOptions{
239+
LabelSelector: "test-configmap-static=true",
240+
})
241+
framework.ExpectNoError(err, "failed to list ConfigMaps with LabelSelector")
242+
framework.ExpectNotEqual(len(configMapList.Items), 0, "no ConfigMaps found in ConfigMap list")
243+
testConfigMapFound := false
244+
for _, cm := range configMapList.Items {
245+
if cm.ObjectMeta.Name == testConfigMapName &&
246+
cm.ObjectMeta.Namespace == testNamespaceName &&
247+
cm.ObjectMeta.Labels["test-configmap-static"] == "true" &&
248+
cm.Data["valueName"] == "value1" {
249+
testConfigMapFound = true
250+
break
262251
}
263-
framework.ExpectEqual(testConfigMapFound, true, "failed to find ConfigMap in list")
264-
265-
ginkgo.By("deleting the ConfigMap by a collection")
266-
err = f.ClientSet.CoreV1().ConfigMaps(testNamespaceName).DeleteCollection(context.TODO(), metav1.DeleteOptions{}, metav1.ListOptions{
267-
LabelSelector: "test-configmap-static=true",
268-
})
269-
framework.ExpectNoError(err, "failed to delete ConfigMap collection with LabelSelector")
270-
ginkgo.By("waiting for the ConfigMap to be deleted")
271-
eventFound = false
272-
ctx, cancel = context.WithTimeout(context.Background(), 30*time.Second)
273-
defer cancel()
274-
_, err = framework.WatchUntilWithoutRetry(ctx, retryWatcher, func(watchEvent watch.Event) (bool, error) {
275-
if watchEvent.Type != watch.Deleted {
276-
return false, nil
277-
}
278-
actualWatchEvents = append(actualWatchEvents, watchEvent)
279-
eventFound = true
280-
return true, nil
281-
})
282-
framework.ExpectNoError(err, "Wait until condition with watch events should not return an error")
283-
framework.ExpectEqual(eventFound, true, "failed to find ConfigMap %v event", watch.Deleted)
284-
285-
return actualWatchEvents
286-
}, func() (err error) {
287-
_ = f.ClientSet.CoreV1().ConfigMaps(testNamespaceName).DeleteCollection(context.TODO(), metav1.DeleteOptions{}, metav1.ListOptions{LabelSelector: "test-configmap-static=true"})
288-
return err
252+
}
253+
framework.ExpectEqual(testConfigMapFound, true, "failed to find ConfigMap in list")
254+
255+
ginkgo.By("deleting the ConfigMap by a collection")
256+
err = f.ClientSet.CoreV1().ConfigMaps(testNamespaceName).DeleteCollection(context.TODO(), metav1.DeleteOptions{}, metav1.ListOptions{
257+
LabelSelector: "test-configmap-static=true",
258+
})
259+
framework.ExpectNoError(err, "failed to delete ConfigMap collection with LabelSelector")
260+
ginkgo.By("waiting for the ConfigMap to be deleted")
261+
ctx, cancel = context.WithTimeout(context.Background(), 30*time.Second)
262+
defer cancel()
263+
_, err = framework.WatchUntilWithoutRetry(ctx, retryWatcher, func(watchEvent watch.Event) (bool, error) {
264+
return watchEvent.Type == watch.Deleted, nil
289265
})
266+
framework.ExpectNoError(err, "fasiled to observe a watch.Deleted event for the ConfigMap we deleted")
290267
})
291268
})
292269

0 commit comments

Comments
 (0)