Skip to content

Commit 982fab3

Browse files
committed
add failing currentUIDFilter e2e test demonstrating #613
1 parent 7f1d7db commit 982fab3

File tree

2 files changed

+111
-0
lines changed

2 files changed

+111
-0
lines changed

test/e2e/current_uid_filter_test.go

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
// Copyright 2021 The Kubernetes Authors.
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
package e2e
5+
6+
import (
7+
"context"
8+
"fmt"
9+
10+
. "github.com/onsi/ginkgo/v2"
11+
. "github.com/onsi/gomega"
12+
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
13+
"sigs.k8s.io/cli-utils/pkg/apply"
14+
"sigs.k8s.io/cli-utils/test/e2e/e2eutil"
15+
"sigs.k8s.io/cli-utils/test/e2e/invconfig"
16+
"sigs.k8s.io/controller-runtime/pkg/client"
17+
)
18+
19+
const v1EventTemplate = `
20+
apiVersion: v1
21+
involvedObject:
22+
apiVersion: v1
23+
kind: Pod
24+
name: pod
25+
namespace: {{.Namespace}}
26+
kind: Event
27+
message: Back-off restarting failed container
28+
metadata:
29+
name: test
30+
namespace: {{.Namespace}}
31+
reason: BackOff
32+
type: Warning
33+
`
34+
35+
const v1EventsEventTemplate = `
36+
apiVersion: events.k8s.io/v1
37+
eventTime: null
38+
kind: Event
39+
metadata:
40+
name: test
41+
namespace: {{.Namespace}}
42+
note: Back-off restarting failed container
43+
reason: BackOff
44+
regarding:
45+
apiVersion: v1
46+
kind: Pod
47+
name: pod
48+
namespace: {{.Namespace}}
49+
type: Warning
50+
`
51+
52+
// Note this tests the scenario of "cohabitating" k8s objects (an object available via multiple apiGroups), but having the same UID.
53+
// As of k8s 1.25 an example of such "cohabitating" kinds is Event which is available via both "v1" and "events.k8s.io/v1".
54+
// When the user upgrades from one apiGroup to the other:
55+
// - should not result in object being pruned
56+
// - object pruning should be skipped due to CurrentUIDFilter (even though a diff is found)
57+
// - inventory should not double-track the object i.e. we should hold reference only to the object with the groupKind that was most recently applied
58+
func currentUIDFilterTest(ctx context.Context, c client.Client, invConfig invconfig.InventoryConfig, inventoryName, namespaceName string) {
59+
applier := invConfig.ApplierFactoryFunc()
60+
inventoryID := fmt.Sprintf("%s-%s", inventoryName, namespaceName)
61+
inventoryInfo := invconfig.CreateInventoryInfo(invConfig, inventoryName, namespaceName, inventoryID)
62+
63+
templateFields := struct{ Namespace string }{Namespace: namespaceName}
64+
v1Event := e2eutil.TemplateToUnstructured(v1EventTemplate, templateFields)
65+
v1EventsEvent := e2eutil.TemplateToUnstructured(v1EventsEventTemplate, templateFields)
66+
67+
By("Apply resource with deprecated groupKind")
68+
resources := []*unstructured.Unstructured{
69+
v1Event,
70+
}
71+
err := e2eutil.Run(applier.Run(ctx, inventoryInfo, resources, apply.ApplierOptions{}))
72+
Expect(err).ToNot(HaveOccurred())
73+
74+
By("Verify resource available in both apiGroups")
75+
objDeprecated := e2eutil.AssertUnstructuredExists(ctx, c, v1Event)
76+
objNew := e2eutil.AssertUnstructuredExists(ctx, c, v1EventsEvent)
77+
78+
By("Verify UID matches for cohabitating resources")
79+
uid := objDeprecated.GetUID()
80+
Expect(uid).ToNot(BeEmpty())
81+
Expect(objDeprecated.GetUID()).To(Equal(objNew.GetUID()))
82+
83+
By("Verify only 1 item in inventory")
84+
invConfig.InvSizeVerifyFunc(ctx, c, inventoryName, namespaceName, inventoryID, 1, 1)
85+
86+
By("Apply resource with new groupKind")
87+
resources = []*unstructured.Unstructured{
88+
v1EventsEvent,
89+
}
90+
err = e2eutil.Run(applier.Run(ctx, inventoryInfo, resources, apply.ApplierOptions{}))
91+
Expect(err).ToNot(HaveOccurred())
92+
93+
By("Verify resource still available in both apiGroups")
94+
objDeprecated = e2eutil.AssertUnstructuredExists(ctx, c, v1Event)
95+
objNew = e2eutil.AssertUnstructuredExists(ctx, c, v1EventsEvent)
96+
97+
By("Verify UID matches for cohabitating resources")
98+
Expect(objDeprecated.GetUID()).To(Equal(objNew.GetUID()))
99+
100+
By("Verify UID matches the UID from previous apply")
101+
Expect(objDeprecated.GetUID()).To(Equal(uid))
102+
103+
By("Verify still only 1 item in inventory")
104+
// Expecting statusCount=2:
105+
// one object applied and one prune skipped
106+
invConfig.InvSizeVerifyFunc(ctx, c, inventoryName, namespaceName, inventoryID, 1, 2)
107+
}

test/e2e/e2e_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,10 @@ var _ = Describe("E2E", func() {
183183
namespaceFilterTest(ctx, c, invConfig, inventoryName, namespace.GetName())
184184
})
185185

186+
It("CurrentUIDFilter", func() {
187+
currentUIDFilterTest(ctx, c, invConfig, inventoryName, namespace.GetName())
188+
})
189+
186190
It("PruneRetrievalError", func() {
187191
pruneRetrieveErrorTest(ctx, c, invConfig, inventoryName, namespace.GetName())
188192
})

0 commit comments

Comments
 (0)