Skip to content

Commit

Permalink
add failing currentUIDFilter e2e test demonstrating #613
Browse files Browse the repository at this point in the history
  • Loading branch information
manfredlift committed Jan 16, 2023
1 parent 7f1d7db commit 982fab3
Show file tree
Hide file tree
Showing 2 changed files with 111 additions and 0 deletions.
107 changes: 107 additions & 0 deletions test/e2e/current_uid_filter_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
// Copyright 2021 The Kubernetes Authors.
// SPDX-License-Identifier: Apache-2.0

package e2e

import (
"context"
"fmt"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"sigs.k8s.io/cli-utils/pkg/apply"
"sigs.k8s.io/cli-utils/test/e2e/e2eutil"
"sigs.k8s.io/cli-utils/test/e2e/invconfig"
"sigs.k8s.io/controller-runtime/pkg/client"
)

const v1EventTemplate = `
apiVersion: v1
involvedObject:
apiVersion: v1
kind: Pod
name: pod
namespace: {{.Namespace}}
kind: Event
message: Back-off restarting failed container
metadata:
name: test
namespace: {{.Namespace}}
reason: BackOff
type: Warning
`

const v1EventsEventTemplate = `
apiVersion: events.k8s.io/v1
eventTime: null
kind: Event
metadata:
name: test
namespace: {{.Namespace}}
note: Back-off restarting failed container
reason: BackOff
regarding:
apiVersion: v1
kind: Pod
name: pod
namespace: {{.Namespace}}
type: Warning
`

// Note this tests the scenario of "cohabitating" k8s objects (an object available via multiple apiGroups), but having the same UID.
// As of k8s 1.25 an example of such "cohabitating" kinds is Event which is available via both "v1" and "events.k8s.io/v1".
// When the user upgrades from one apiGroup to the other:
// - should not result in object being pruned
// - object pruning should be skipped due to CurrentUIDFilter (even though a diff is found)
// - 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
func currentUIDFilterTest(ctx context.Context, c client.Client, invConfig invconfig.InventoryConfig, inventoryName, namespaceName string) {
applier := invConfig.ApplierFactoryFunc()
inventoryID := fmt.Sprintf("%s-%s", inventoryName, namespaceName)
inventoryInfo := invconfig.CreateInventoryInfo(invConfig, inventoryName, namespaceName, inventoryID)

templateFields := struct{ Namespace string }{Namespace: namespaceName}
v1Event := e2eutil.TemplateToUnstructured(v1EventTemplate, templateFields)
v1EventsEvent := e2eutil.TemplateToUnstructured(v1EventsEventTemplate, templateFields)

By("Apply resource with deprecated groupKind")
resources := []*unstructured.Unstructured{
v1Event,
}
err := e2eutil.Run(applier.Run(ctx, inventoryInfo, resources, apply.ApplierOptions{}))
Expect(err).ToNot(HaveOccurred())

By("Verify resource available in both apiGroups")
objDeprecated := e2eutil.AssertUnstructuredExists(ctx, c, v1Event)
objNew := e2eutil.AssertUnstructuredExists(ctx, c, v1EventsEvent)

By("Verify UID matches for cohabitating resources")
uid := objDeprecated.GetUID()
Expect(uid).ToNot(BeEmpty())
Expect(objDeprecated.GetUID()).To(Equal(objNew.GetUID()))

By("Verify only 1 item in inventory")
invConfig.InvSizeVerifyFunc(ctx, c, inventoryName, namespaceName, inventoryID, 1, 1)

By("Apply resource with new groupKind")
resources = []*unstructured.Unstructured{
v1EventsEvent,
}
err = e2eutil.Run(applier.Run(ctx, inventoryInfo, resources, apply.ApplierOptions{}))
Expect(err).ToNot(HaveOccurred())

By("Verify resource still available in both apiGroups")
objDeprecated = e2eutil.AssertUnstructuredExists(ctx, c, v1Event)
objNew = e2eutil.AssertUnstructuredExists(ctx, c, v1EventsEvent)

By("Verify UID matches for cohabitating resources")
Expect(objDeprecated.GetUID()).To(Equal(objNew.GetUID()))

By("Verify UID matches the UID from previous apply")
Expect(objDeprecated.GetUID()).To(Equal(uid))

By("Verify still only 1 item in inventory")
// Expecting statusCount=2:
// one object applied and one prune skipped
invConfig.InvSizeVerifyFunc(ctx, c, inventoryName, namespaceName, inventoryID, 1, 2)
}
4 changes: 4 additions & 0 deletions test/e2e/e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,10 @@ var _ = Describe("E2E", func() {
namespaceFilterTest(ctx, c, invConfig, inventoryName, namespace.GetName())
})

It("CurrentUIDFilter", func() {
currentUIDFilterTest(ctx, c, invConfig, inventoryName, namespace.GetName())
})

It("PruneRetrievalError", func() {
pruneRetrieveErrorTest(ctx, c, invConfig, inventoryName, namespace.GetName())
})
Expand Down

0 comments on commit 982fab3

Please sign in to comment.