...
1
2
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 "k8s.io/apimachinery/pkg/util/validation/field"
14 "sigs.k8s.io/cli-utils/pkg/apply"
15 "sigs.k8s.io/cli-utils/pkg/apply/event"
16 "sigs.k8s.io/cli-utils/pkg/object"
17 "sigs.k8s.io/cli-utils/pkg/object/validation"
18 "sigs.k8s.io/cli-utils/pkg/testutil"
19 "sigs.k8s.io/cli-utils/test/e2e/e2eutil"
20 "sigs.k8s.io/cli-utils/test/e2e/invconfig"
21 "sigs.k8s.io/controller-runtime/pkg/client"
22 )
23
24 func exitEarlyTest(ctx context.Context, c client.Client, invConfig invconfig.InventoryConfig, inventoryName, namespaceName string) {
25 By("exit early on invalid object")
26 applier := invConfig.ApplierFactoryFunc()
27
28 inv := invConfig.InvWrapperFunc(invConfig.FactoryFunc(inventoryName, namespaceName, "test"))
29
30 fields := struct{ Namespace string }{Namespace: namespaceName}
31
32 pod1Obj := e2eutil.WithNamespace(e2eutil.ManifestToUnstructured(pod1), namespaceName)
33
34 deployment1Obj := e2eutil.WithDependsOn(e2eutil.WithNamespace(e2eutil.ManifestToUnstructured(deployment1), namespaceName),
35 fmt.Sprintf("/namespaces/%s/Pod/%s", namespaceName, pod1Obj.GetName()))
36
37 invalidPodObj := e2eutil.TemplateToUnstructured(invalidPodTemplate, fields)
38
39 resources := []*unstructured.Unstructured{
40 pod1Obj,
41 deployment1Obj,
42 invalidPodObj,
43 }
44
45 applierEvents := e2eutil.RunCollect(applier.Run(ctx, inv, resources, apply.ApplierOptions{
46 EmitStatusEvents: false,
47 ValidationPolicy: validation.ExitEarly,
48 }))
49
50 expEvents := []testutil.ExpEvent{
51 {
52
53 EventType: event.ErrorType,
54 ErrorEvent: &testutil.ExpErrorEvent{
55 Err: testutil.EqualError(
56 validation.NewError(
57 field.Required(field.NewPath("metadata", "name"), "name is required"),
58 object.UnstructuredToObjMetadata(invalidPodObj),
59 ),
60 ),
61 },
62 },
63 }
64 Expect(testutil.EventsToExpEvents(applierEvents)).To(testutil.Equal(expEvents))
65
66 By("verify pod1 not found")
67 e2eutil.AssertUnstructuredDoesNotExist(ctx, c, pod1Obj)
68
69 By("verify deployment1 not found")
70 e2eutil.AssertUnstructuredDoesNotExist(ctx, c, deployment1Obj)
71 }
72
View as plain text