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 "sigs.k8s.io/cli-utils/pkg/apply"
14 "sigs.k8s.io/cli-utils/pkg/apply/event"
15 "sigs.k8s.io/cli-utils/pkg/inventory"
16 "sigs.k8s.io/cli-utils/pkg/testutil"
17 "sigs.k8s.io/cli-utils/test/e2e/e2eutil"
18 "sigs.k8s.io/cli-utils/test/e2e/invconfig"
19 "sigs.k8s.io/controller-runtime/pkg/client"
20 )
21
22
23 func emptySetTest(ctx context.Context, c client.Client, invConfig invconfig.InventoryConfig, inventoryName, namespaceName string) {
24 By("Apply zero resources")
25 applier := invConfig.ApplierFactoryFunc()
26
27 inventoryID := fmt.Sprintf("%s-%s", inventoryName, namespaceName)
28 inventoryInfo := invConfig.InvWrapperFunc(invConfig.FactoryFunc(inventoryName, namespaceName, inventoryID))
29
30 resources := []*unstructured.Unstructured{}
31
32 applierEvents := e2eutil.RunCollect(applier.Run(ctx, inventoryInfo, resources, apply.ApplierOptions{
33 EmitStatusEvents: true,
34 }))
35
36 expEvents := []testutil.ExpEvent{
37 {
38
39 EventType: event.InitType,
40 InitEvent: &testutil.ExpInitEvent{},
41 },
42 {
43
44 EventType: event.ActionGroupType,
45 ActionGroupEvent: &testutil.ExpActionGroupEvent{
46 Action: event.InventoryAction,
47 GroupName: "inventory-add-0",
48 Type: event.Started,
49 },
50 },
51 {
52
53 EventType: event.ActionGroupType,
54 ActionGroupEvent: &testutil.ExpActionGroupEvent{
55 Action: event.InventoryAction,
56 GroupName: "inventory-add-0",
57 Type: event.Finished,
58 },
59 },
60 {
61
62 EventType: event.ActionGroupType,
63 ActionGroupEvent: &testutil.ExpActionGroupEvent{
64 Action: event.InventoryAction,
65 GroupName: "inventory-set-0",
66 Type: event.Started,
67 },
68 },
69 {
70
71 EventType: event.ActionGroupType,
72 ActionGroupEvent: &testutil.ExpActionGroupEvent{
73 Action: event.InventoryAction,
74 GroupName: "inventory-set-0",
75 Type: event.Finished,
76 },
77 },
78 }
79 Expect(testutil.EventsToExpEvents(applierEvents)).To(testutil.Equal(expEvents))
80
81 By("Verify inventory created")
82 invConfig.InvSizeVerifyFunc(ctx, c, inventoryName, namespaceName, inventoryID, 0, 0)
83
84 By("Destroy zero resources")
85 destroyer := invConfig.DestroyerFactoryFunc()
86
87 options := apply.DestroyerOptions{InventoryPolicy: inventory.PolicyAdoptIfNoInventory}
88 destroyerEvents := e2eutil.RunCollect(destroyer.Run(ctx, inventoryInfo, options))
89
90 expEvents = []testutil.ExpEvent{
91 {
92
93 EventType: event.InitType,
94 InitEvent: &testutil.ExpInitEvent{},
95 },
96 {
97
98 EventType: event.ActionGroupType,
99 ActionGroupEvent: &testutil.ExpActionGroupEvent{
100 Action: event.InventoryAction,
101 GroupName: "delete-inventory-0",
102 Type: event.Started,
103 },
104 },
105 {
106
107 EventType: event.ActionGroupType,
108 ActionGroupEvent: &testutil.ExpActionGroupEvent{
109 Action: event.InventoryAction,
110 GroupName: "delete-inventory-0",
111 Type: event.Finished,
112 },
113 },
114 }
115
116 Expect(testutil.EventsToExpEvents(destroyerEvents)).To(testutil.Equal(expEvents))
117
118 By("Verify inventory deleted")
119 invConfig.InvNotExistsFunc(ctx, c, inventoryName, namespaceName, inventoryID)
120 }
121
View as plain text