|
| 1 | +/* |
| 2 | +Copyright 2025 Flant JSC |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package drop_helm_labels_from_generic_vmclass |
| 18 | + |
| 19 | +import ( |
| 20 | + "context" |
| 21 | + "fmt" |
| 22 | + "testing" |
| 23 | + |
| 24 | + "github.com/deckhouse/deckhouse/pkg/log" |
| 25 | + "github.com/deckhouse/module-sdk/pkg" |
| 26 | + "github.com/deckhouse/module-sdk/testing/mock" |
| 27 | + . "github.com/onsi/ginkgo/v2" |
| 28 | + . "github.com/onsi/gomega" |
| 29 | +) |
| 30 | + |
| 31 | +func TestDropHelmLabelsFromGenericVMClass(t *testing.T) { |
| 32 | + RegisterFailHandler(Fail) |
| 33 | + RunSpecs(t, "Drop Helm labels from generic VMClass Suite") |
| 34 | +} |
| 35 | + |
| 36 | +var _ = Describe("Drop Helm labels from generic VMClass", func() { |
| 37 | + var ( |
| 38 | + snapshots *mock.SnapshotsMock |
| 39 | + patchCollector *mock.PatchCollectorMock |
| 40 | + ) |
| 41 | + |
| 42 | + newInput := func() *pkg.HookInput { |
| 43 | + return &pkg.HookInput{ |
| 44 | + Snapshots: snapshots, |
| 45 | + PatchCollector: patchCollector, |
| 46 | + Logger: log.NewNop(), |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + newSnapshot := func(withManagedBy, withHeritage bool) pkg.Snapshot { |
| 51 | + return mock.NewSnapshotMock(GinkgoT()).UnmarshalToMock.Set(func(v any) (err error) { |
| 52 | + obj, ok := v.(*VMClassMetadata) |
| 53 | + Expect(ok).To(BeTrue()) |
| 54 | + obj.Name = genericVMClassName |
| 55 | + obj.Labels = make(map[string]string) |
| 56 | + |
| 57 | + // Required labels for VMClass to be found by the hook |
| 58 | + obj.Labels["app"] = "virtualization-controller" |
| 59 | + obj.Labels["module"] = "virtualization" |
| 60 | + |
| 61 | + if withManagedBy { |
| 62 | + obj.Labels[helmManagedByLabel] = "Helm" |
| 63 | + } |
| 64 | + if withHeritage { |
| 65 | + obj.Labels[helmHeritageLabel] = "deckhouse" |
| 66 | + } |
| 67 | + |
| 68 | + return nil |
| 69 | + }) |
| 70 | + } |
| 71 | + |
| 72 | + setSnapshots := func(snaps ...pkg.Snapshot) { |
| 73 | + snapshots.GetMock.When(vmClassSnapshot).Then(snaps) |
| 74 | + } |
| 75 | + |
| 76 | + BeforeEach(func() { |
| 77 | + snapshots = mock.NewSnapshotsMock(GinkgoT()) |
| 78 | + patchCollector = mock.NewPatchCollectorMock(GinkgoT()) |
| 79 | + }) |
| 80 | + |
| 81 | + It("Should drop both Helm labels from generic VMClass with all required labels", func() { |
| 82 | + setSnapshots(newSnapshot(true, true)) |
| 83 | + patchCollector.PatchWithJSONMock.Set(func(patch any, apiVersion, kind, namespace, name string, opts ...pkg.PatchCollectorOption) { |
| 84 | + Expect(apiVersion).To(Equal("deckhouse.io/v1alpha2")) |
| 85 | + Expect(kind).To(Equal("VirtualMachineClass")) |
| 86 | + Expect(namespace).To(Equal("")) |
| 87 | + Expect(name).To(Equal(genericVMClassName)) |
| 88 | + Expect(opts).To(HaveLen(0)) |
| 89 | + |
| 90 | + jsonPatch, ok := patch.([]map[string]interface{}) |
| 91 | + Expect(ok).To(BeTrue()) |
| 92 | + Expect(jsonPatch).To(HaveLen(2)) |
| 93 | + |
| 94 | + // Check first patch (managed-by label) |
| 95 | + Expect(jsonPatch[0]["op"]).To(Equal("remove")) |
| 96 | + Expect(jsonPatch[0]["path"]).To(Equal(fmt.Sprintf("/metadata/labels/%s", jsonPatchEscape(helmManagedByLabel)))) |
| 97 | + Expect(jsonPatch[0]["value"]).To(BeNil()) |
| 98 | + |
| 99 | + // Check second patch (heritage label) |
| 100 | + Expect(jsonPatch[1]["op"]).To(Equal("remove")) |
| 101 | + Expect(jsonPatch[1]["path"]).To(Equal(fmt.Sprintf("/metadata/labels/%s", jsonPatchEscape(helmHeritageLabel)))) |
| 102 | + Expect(jsonPatch[1]["value"]).To(BeNil()) |
| 103 | + }) |
| 104 | + |
| 105 | + Expect(handlerDropHelmLabels(context.Background(), newInput())).To(Succeed()) |
| 106 | + }) |
| 107 | + |
| 108 | + It("Should do nothing when VMClass doesn't have all required labels", func() { |
| 109 | + // Create a snapshot with VMClass that has only some required labels |
| 110 | + partialLabelSnapshot := mock.NewSnapshotMock(GinkgoT()).UnmarshalToMock.Set(func(v any) (err error) { |
| 111 | + obj, ok := v.(*VMClassMetadata) |
| 112 | + Expect(ok).To(BeTrue()) |
| 113 | + obj.Name = genericVMClassName |
| 114 | + obj.Labels = make(map[string]string) |
| 115 | + |
| 116 | + // Only some required labels - VMClass won't be processed |
| 117 | + obj.Labels["app"] = "virtualization-controller" |
| 118 | + obj.Labels["module"] = "virtualization" |
| 119 | + // Missing helmManagedByLabel and helmHeritageLabel |
| 120 | + |
| 121 | + return nil |
| 122 | + }) |
| 123 | + |
| 124 | + setSnapshots(partialLabelSnapshot) |
| 125 | + Expect(handlerDropHelmLabels(context.Background(), newInput())).To(Succeed()) |
| 126 | + }) |
| 127 | + |
| 128 | + It("Should do nothing when VMClass not found", func() { |
| 129 | + setSnapshots() |
| 130 | + Expect(handlerDropHelmLabels(context.Background(), newInput())).To(Succeed()) |
| 131 | + }) |
| 132 | + |
| 133 | + It("Should do nothing when VMClass exists but doesn't match label selector", func() { |
| 134 | + // Create a snapshot with VMClass that has wrong labels |
| 135 | + wrongLabelSnapshot := mock.NewSnapshotMock(GinkgoT()).UnmarshalToMock.Set(func(v any) (err error) { |
| 136 | + obj, ok := v.(*VMClassMetadata) |
| 137 | + Expect(ok).To(BeTrue()) |
| 138 | + obj.Name = genericVMClassName |
| 139 | + obj.Labels = make(map[string]string) |
| 140 | + |
| 141 | + // Wrong labels - VMClass won't be found by the hook |
| 142 | + obj.Labels["app"] = "wrong-app" |
| 143 | + obj.Labels["module"] = "wrong-module" |
| 144 | + obj.Labels[helmManagedByLabel] = "Helm" |
| 145 | + obj.Labels[helmHeritageLabel] = "deckhouse" |
| 146 | + |
| 147 | + return nil |
| 148 | + }) |
| 149 | + |
| 150 | + setSnapshots(wrongLabelSnapshot) |
| 151 | + Expect(handlerDropHelmLabels(context.Background(), newInput())).To(Succeed()) |
| 152 | + }) |
| 153 | +}) |
0 commit comments