Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: make CreateInventoryInfo return an error #662

Merged
merged 1 commit into from
Mar 27, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions test/e2e/apply_and_destroy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,10 @@ import (
func applyAndDestroyTest(ctx context.Context, c client.Client, invConfig invconfig.InventoryConfig, inventoryName, namespaceName string) {
By("Apply resources")
applier := invConfig.ApplierFactoryFunc()
inventoryID := fmt.Sprintf("%s-%s", inventoryName, namespaceName)

inventoryInfo := invconfig.CreateInventoryInfo(invConfig, inventoryName, namespaceName, inventoryID)
inventoryID := fmt.Sprintf("%s-%s", inventoryName, namespaceName)
inventoryInfo, err := invconfig.CreateInventoryInfo(invConfig, inventoryName, namespaceName, inventoryID)
Expect(err).ToNot(HaveOccurred())

deployment1Obj := e2eutil.WithNamespace(e2eutil.ManifestToUnstructured(deployment1), namespaceName)
resources := []*unstructured.Unstructured{
Expand Down
5 changes: 4 additions & 1 deletion test/e2e/continue_on_error_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package e2e

import (
"context"
"fmt"

. "github.com/onsi/ginkgo/v2" //nolint:revive
. "github.com/onsi/gomega" //nolint:revive
Expand All @@ -28,7 +29,9 @@ func continueOnErrorTest(ctx context.Context, c client.Client, invConfig invconf
By("apply an invalid CRD")
applier := invConfig.ApplierFactoryFunc()

inv := invConfig.InvWrapperFunc(invConfig.FactoryFunc(inventoryName, namespaceName, "test"))
inventoryID := fmt.Sprintf("%s-%s", inventoryName, namespaceName)
inv, err := invconfig.CreateInventoryInfo(invConfig, inventoryName, namespaceName, inventoryID)
Expect(err).ToNot(HaveOccurred())

invalidCrdObj := e2eutil.ManifestToUnstructured(invalidCrd)
pod1Obj := e2eutil.WithNamespace(e2eutil.ManifestToUnstructured(pod1), namespaceName)
Expand Down
5 changes: 4 additions & 1 deletion test/e2e/crd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package e2e

import (
"context"
"fmt"
"strings"
"time"

Expand All @@ -26,7 +27,9 @@ func crdTest(ctx context.Context, _ client.Client, invConfig invconfig.Inventory
By("apply a set of resources that includes both a crd and a cr")
applier := invConfig.ApplierFactoryFunc()

inv := invConfig.InvWrapperFunc(invConfig.FactoryFunc(inventoryName, namespaceName, "test"))
inventoryID := fmt.Sprintf("%s-%s", inventoryName, namespaceName)
inv, err := invconfig.CreateInventoryInfo(invConfig, inventoryName, namespaceName, inventoryID)
Expect(err).ToNot(HaveOccurred())

crdObj := e2eutil.ManifestToUnstructured(crd)
crObj := e2eutil.ManifestToUnstructured(cr)
Expand Down
6 changes: 4 additions & 2 deletions test/e2e/current_uid_filter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,10 @@ type: Warning
// - 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)
inventoryInfo, err := invconfig.CreateInventoryInfo(invConfig, inventoryName, namespaceName, inventoryID)
Expect(err).ToNot(HaveOccurred())

templateFields := struct{ Namespace string }{Namespace: namespaceName}
v1Event := e2eutil.TemplateToUnstructured(v1EventTemplate, templateFields)
Expand All @@ -70,7 +72,7 @@ func currentUIDFilterTest(ctx context.Context, c client.Client, invConfig invcon
resources := []*unstructured.Unstructured{
v1Event,
}
err := e2eutil.Run(applier.Run(ctx, inventoryInfo, resources, apply.ApplierOptions{}))
err = e2eutil.Run(applier.Run(ctx, inventoryInfo, resources, apply.ApplierOptions{}))
Expect(err).ToNot(HaveOccurred())

By("Verify resource available in both apiGroups")
Expand Down
4 changes: 2 additions & 2 deletions test/e2e/customprovider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,6 @@ func fromUnstructured(obj *unstructured.Unstructured) (*inventory.UnstructuredIn
return inv, nil
}

func WrapInventoryInfoObj(obj *unstructured.Unstructured) inventory.Info {
return inventory.NewUnstructuredInventory(obj).Info()
func WrapInventoryInfoObj(obj *unstructured.Unstructured) (inventory.Info, error) {
return inventory.NewUnstructuredInventory(obj).Info(), nil
}
5 changes: 3 additions & 2 deletions test/e2e/deletion_prevention_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,10 @@ import (
func deletionPreventionTest(ctx context.Context, c client.Client, invConfig invconfig.InventoryConfig, inventoryName, namespaceName string) {
By("Apply resources")
applier := invConfig.ApplierFactoryFunc()
inventoryID := fmt.Sprintf("%s-%s", inventoryName, namespaceName)

inventoryInfo := invconfig.CreateInventoryInfo(invConfig, inventoryName, namespaceName, inventoryID)
inventoryID := fmt.Sprintf("%s-%s", inventoryName, namespaceName)
inventoryInfo, err := invconfig.CreateInventoryInfo(invConfig, inventoryName, namespaceName, inventoryID)
Expect(err).ToNot(HaveOccurred())

resources := []*unstructured.Unstructured{
e2eutil.WithNamespace(e2eutil.ManifestToUnstructured(deployment1), namespaceName),
Expand Down
8 changes: 5 additions & 3 deletions test/e2e/dependency_filter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@ func dependencyFilterTest(ctx context.Context, c client.Client, invConfig invcon
By("apply resources in order based on depends-on annotation")
applier := invConfig.ApplierFactoryFunc()

inv := invConfig.InvWrapperFunc(invConfig.FactoryFunc(inventoryName, namespaceName, "test"))
inventoryID := fmt.Sprintf("%s-%s", inventoryName, namespaceName)
inventoryInfo, err := invconfig.CreateInventoryInfo(invConfig, inventoryName, namespaceName, inventoryID)
Expect(err).ToNot(HaveOccurred())

pod1Obj := e2eutil.WithDependsOn(e2eutil.WithNamespace(e2eutil.ManifestToUnstructured(pod1), namespaceName), fmt.Sprintf("/namespaces/%s/Pod/pod2", namespaceName))
pod2Obj := e2eutil.WithNamespace(e2eutil.ManifestToUnstructured(pod2), namespaceName)
Expand All @@ -45,7 +47,7 @@ func dependencyFilterTest(ctx context.Context, c client.Client, invConfig invcon
e2eutil.DeleteUnstructuredIfExists(ctx, c, pod2Obj)
}(ctx, c)

applierEvents := e2eutil.RunCollect(applier.Run(ctx, inv, resources, apply.ApplierOptions{
applierEvents := e2eutil.RunCollect(applier.Run(ctx, inventoryInfo, resources, apply.ApplierOptions{
EmitStatusEvents: false,
}))

Expand Down Expand Up @@ -245,7 +247,7 @@ func dependencyFilterTest(ctx context.Context, c client.Client, invConfig invcon
pod1Obj,
}

applierEvents = e2eutil.RunCollect(applier.Run(ctx, inv, resources, apply.ApplierOptions{
applierEvents = e2eutil.RunCollect(applier.Run(ctx, inventoryInfo, resources, apply.ApplierOptions{
EmitStatusEvents: false,
ValidationPolicy: validation.SkipInvalid,
}))
Expand Down
8 changes: 5 additions & 3 deletions test/e2e/depends_on_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ func dependsOnTest(ctx context.Context, c client.Client, invConfig invconfig.Inv
By("apply resources in order based on depends-on annotation")
applier := invConfig.ApplierFactoryFunc()

inv := invConfig.InvWrapperFunc(invConfig.FactoryFunc(inventoryName, namespaceName, "test"))
inventoryID := fmt.Sprintf("%s-%s", inventoryName, namespaceName)
inventoryInfo, err := invconfig.CreateInventoryInfo(invConfig, inventoryName, namespaceName, inventoryID)
Expect(err).ToNot(HaveOccurred())

namespace1Name := fmt.Sprintf("%s-ns1", namespaceName)
namespace1Obj := e2eutil.UnstructuredNamespace(namespace1Name)
Expand Down Expand Up @@ -53,7 +55,7 @@ func dependsOnTest(ctx context.Context, c client.Client, invConfig invconfig.Inv
pod3Obj,
}

applierEvents := e2eutil.RunCollect(applier.Run(ctx, inv, resources, apply.ApplierOptions{
applierEvents := e2eutil.RunCollect(applier.Run(ctx, inventoryInfo, resources, apply.ApplierOptions{
EmitStatusEvents: false,
}))

Expand Down Expand Up @@ -421,7 +423,7 @@ func dependsOnTest(ctx context.Context, c client.Client, invConfig invconfig.Inv
By("destroy resources in opposite order")
destroyer := invConfig.DestroyerFactoryFunc()
options := apply.DestroyerOptions{InventoryPolicy: inventory.PolicyAdoptIfNoInventory}
destroyerEvents := e2eutil.RunCollect(destroyer.Run(ctx, inv, options))
destroyerEvents := e2eutil.RunCollect(destroyer.Run(ctx, inventoryInfo, options))

expEvents = []testutil.ExpEvent{
{
Expand Down
10 changes: 5 additions & 5 deletions test/e2e/destroy_reconciliation_failure_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ func destroyReconciliationFailureTest(ctx context.Context, c client.Client, invC
applier := invConfig.ApplierFactoryFunc()

inventoryID := fmt.Sprintf("%s-%s", inventoryName, namespaceName)

inv := invconfig.CreateInventoryInfo(invConfig, inventoryName, namespaceName, inventoryID)
inventoryInfo, err := invconfig.CreateInventoryInfo(invConfig, inventoryName, namespaceName, inventoryID)
Expect(err).ToNot(HaveOccurred())

podObject := e2eutil.WithNamespace(e2eutil.ManifestToUnstructured(pod1), namespaceName)
podWithFinalizerObject := e2eutil.WithNamespace(e2eutil.ManifestToUnstructured(pod2), namespaceName)
Expand All @@ -37,7 +37,7 @@ func destroyReconciliationFailureTest(ctx context.Context, c client.Client, invC
podWithFinalizerObject,
}

_ = e2eutil.RunCollect(applier.Run(ctx, inv, resource1, apply.ApplierOptions{
_ = e2eutil.RunCollect(applier.Run(ctx, inventoryInfo, resource1, apply.ApplierOptions{
EmitStatusEvents: false,
}))

Expand Down Expand Up @@ -83,7 +83,7 @@ func destroyReconciliationFailureTest(ctx context.Context, c client.Client, invC
},
}
for _, ec := range expectedCounts {
_ = e2eutil.RunCollect(destroyer.Run(ctx, inv, options))
_ = e2eutil.RunCollect(destroyer.Run(ctx, inventoryInfo, options))

By("Verify pod1 is deleted")
e2eutil.AssertUnstructuredDoesNotExist(ctx, c, podObject)
Expand All @@ -99,7 +99,7 @@ func destroyReconciliationFailureTest(ctx context.Context, c client.Client, invC
podWithFinalizerObject = e2eutil.WithoutFinalizers(podWithFinalizerObject)
e2eutil.ApplyUnstructured(ctx, c, podWithFinalizerObject)
// re-run the destroyer and verify the object is removed from the inventory
_ = e2eutil.RunCollect(destroyer.Run(ctx, inv, options))
_ = e2eutil.RunCollect(destroyer.Run(ctx, inventoryInfo, options))

By("Verify pod1 is deleted")
e2eutil.AssertUnstructuredDoesNotExist(ctx, c, podObject)
Expand Down
5 changes: 3 additions & 2 deletions test/e2e/dry_run_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,10 @@ import (
func dryRunTest(ctx context.Context, c client.Client, invConfig invconfig.InventoryConfig, inventoryName, namespaceName string) {
By("Apply with DryRun")
applier := invConfig.ApplierFactoryFunc()
inventoryID := fmt.Sprintf("%s-%s", inventoryName, namespaceName)

inventoryInfo := invconfig.CreateInventoryInfo(invConfig, inventoryName, namespaceName, inventoryID)
inventoryID := fmt.Sprintf("%s-%s", inventoryName, namespaceName)
inventoryInfo, err := invconfig.CreateInventoryInfo(invConfig, inventoryName, namespaceName, inventoryID)
Expect(err).ToNot(HaveOccurred())

namespace1Name := fmt.Sprintf("%s-ns1", namespaceName)

Expand Down
3 changes: 2 additions & 1 deletion test/e2e/empty_set_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ func emptySetTest(ctx context.Context, c client.Client, invConfig invconfig.Inve
applier := invConfig.ApplierFactoryFunc()

inventoryID := fmt.Sprintf("%s-%s", inventoryName, namespaceName)
inventoryInfo := invConfig.InvWrapperFunc(invConfig.FactoryFunc(inventoryName, namespaceName, inventoryID))
inventoryInfo, err := invconfig.CreateInventoryInfo(invConfig, inventoryName, namespaceName, inventoryID)
Expect(err).ToNot(HaveOccurred())

resources := []*unstructured.Unstructured{}

Expand Down
6 changes: 4 additions & 2 deletions test/e2e/exit_early_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ func exitEarlyTest(ctx context.Context, c client.Client, invConfig invconfig.Inv
By("exit early on invalid object")
applier := invConfig.ApplierFactoryFunc()

inv := invConfig.InvWrapperFunc(invConfig.FactoryFunc(inventoryName, namespaceName, "test"))
inventoryID := fmt.Sprintf("%s-%s", inventoryName, namespaceName)
inventoryInfo, err := invconfig.CreateInventoryInfo(invConfig, inventoryName, namespaceName, inventoryID)
Expect(err).ToNot(HaveOccurred())

fields := struct{ Namespace string }{Namespace: namespaceName}
// valid pod
Expand All @@ -42,7 +44,7 @@ func exitEarlyTest(ctx context.Context, c client.Client, invConfig invconfig.Inv
invalidPodObj,
}

applierEvents := e2eutil.RunCollect(applier.Run(ctx, inv, resources, apply.ApplierOptions{
applierEvents := e2eutil.RunCollect(applier.Run(ctx, inventoryInfo, resources, apply.ApplierOptions{
EmitStatusEvents: false,
ValidationPolicy: validation.ExitEarly,
}))
Expand Down
11 changes: 3 additions & 8 deletions test/e2e/invconfig/configmap.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (

"github.com/onsi/gomega"
v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/client-go/rest"
"sigs.k8s.io/cli-utils/pkg/apply"
"sigs.k8s.io/cli-utils/pkg/common"
Expand All @@ -18,13 +17,9 @@ import (

func NewConfigMapTypeInvConfig(cfg *rest.Config) InventoryConfig {
return InventoryConfig{
ClientConfig: cfg,
FactoryFunc: cmInventoryManifest,
InvWrapperFunc: func(obj *unstructured.Unstructured) inventory.Info {
info, err := inventory.ConfigMapToInventoryInfo(obj)
gomega.Expect(err).ToNot(gomega.HaveOccurred())
return info
},
ClientConfig: cfg,
FactoryFunc: cmInventoryManifest,
InvWrapperFunc: inventory.ConfigMapToInventoryInfo,
ApplierFactoryFunc: newDefaultInvApplierFactory(cfg),
DestroyerFactoryFunc: newDefaultInvDestroyerFactory(cfg),
InvSizeVerifyFunc: defaultInvSizeVerifyFunc,
Expand Down
4 changes: 2 additions & 2 deletions test/e2e/invconfig/invconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import (
)

type inventoryFactoryFunc func(name, namespace, id string) *unstructured.Unstructured
type invWrapperFunc func(*unstructured.Unstructured) inventory.Info
type invWrapperFunc func(*unstructured.Unstructured) (inventory.Info, error)
type applierFactoryFunc func() *apply.Applier
type destroyerFactoryFunc func() *apply.Destroyer
type invSizeVerifyFunc func(ctx context.Context, c client.Client, name, namespace, id string, specCount, statusCount int)
Expand All @@ -35,7 +35,7 @@ type InventoryConfig struct {
InvNotExistsFunc invNotExistsFunc
}

func CreateInventoryInfo(invConfig InventoryConfig, inventoryName, namespaceName, inventoryID string) inventory.Info {
func CreateInventoryInfo(invConfig InventoryConfig, inventoryName, namespaceName, inventoryID string) (inventory.Info, error) {
return invConfig.InvWrapperFunc(invConfig.FactoryFunc(inventoryName, namespaceName, inventoryID))
}

Expand Down
Loading