|
| 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 main |
| 18 | + |
| 19 | +import ( |
| 20 | + "bytes" |
| 21 | + "context" |
| 22 | + "fmt" |
| 23 | + "testing" |
| 24 | + "time" |
| 25 | + |
| 26 | + "github.com/deckhouse/deckhouse/pkg/log" |
| 27 | + "github.com/deckhouse/module-sdk/pkg" |
| 28 | + "github.com/deckhouse/module-sdk/testing/mock" |
| 29 | + . "github.com/onsi/ginkgo/v2" |
| 30 | + . "github.com/onsi/gomega" |
| 31 | +) |
| 32 | + |
| 33 | +func createSnapshotMock(nodeInfo NodeInfo) pkg.Snapshot { |
| 34 | + m := mock.NewSnapshotMock(GinkgoT()) |
| 35 | + m.UnmarshalToMock.Set(func(v any) error { |
| 36 | + target, ok := v.(*NodeInfo) |
| 37 | + if !ok { |
| 38 | + return fmt.Errorf("expected *NodeInfo, got %T", v) |
| 39 | + } |
| 40 | + *target = nodeInfo |
| 41 | + return nil |
| 42 | + }) |
| 43 | + return m |
| 44 | +} |
| 45 | + |
| 46 | +func TestCleanUpVirtHandlerNodeLabels(t *testing.T) { |
| 47 | + RegisterFailHandler(Fail) |
| 48 | + RunSpecs(t, "Cleanup virtHandler node labels Suite") |
| 49 | +} |
| 50 | + |
| 51 | +var _ = Describe("Cleanup virtHandler node labels", func() { |
| 52 | + var ( |
| 53 | + snapshots *mock.SnapshotsMock |
| 54 | + values *mock.PatchableValuesCollectorMock |
| 55 | + patchCollector *mock.PatchCollectorMock |
| 56 | + input *pkg.HookInput |
| 57 | + buf *bytes.Buffer |
| 58 | + ) |
| 59 | + |
| 60 | + BeforeEach(func() { |
| 61 | + snapshots = mock.NewSnapshotsMock(GinkgoT()) |
| 62 | + values = mock.NewPatchableValuesCollectorMock(GinkgoT()) |
| 63 | + patchCollector = mock.NewPatchCollectorMock(GinkgoT()) |
| 64 | + |
| 65 | + buf = bytes.NewBuffer([]byte{}) |
| 66 | + |
| 67 | + input = &pkg.HookInput{ |
| 68 | + Values: values, |
| 69 | + Snapshots: snapshots, |
| 70 | + Logger: log.NewLogger(log.Options{ |
| 71 | + Level: log.LevelDebug.Level(), |
| 72 | + Output: buf, |
| 73 | + TimeFunc: func(_ time.Time) time.Time { |
| 74 | + parsedTime, err := time.Parse(time.DateTime, "2006-01-02 15:04:05") |
| 75 | + Expect(err).ShouldNot(HaveOccurred()) |
| 76 | + return parsedTime |
| 77 | + }, |
| 78 | + }), |
| 79 | + PatchCollector: patchCollector, |
| 80 | + } |
| 81 | + }) |
| 82 | + |
| 83 | + Context("Empty cluster", func() { |
| 84 | + It("Hook must execute successfully", func() { |
| 85 | + snapshots.GetMock.When(nodesSnapshot).Then( |
| 86 | + []pkg.Snapshot{}, |
| 87 | + ) |
| 88 | + err := handleCleanUpNodeLabels(context.Background(), input) |
| 89 | + Expect(err).ShouldNot(HaveOccurred()) |
| 90 | + }) |
| 91 | + }) |
| 92 | + |
| 93 | + Context("Nodes should be patched.", func() { |
| 94 | + It("Hook must execute successfully", func() { |
| 95 | + expectedNodes := map[string]interface{}{ |
| 96 | + "node1": []map[string]string{ |
| 97 | + map[string]string{ |
| 98 | + "op": "remove", |
| 99 | + "path": "/metadata/labels/kubevirt.internal.virtualization.deckhouse.io~1schedulable", |
| 100 | + }, |
| 101 | + }, |
| 102 | + "node2": []map[string]string{ |
| 103 | + { |
| 104 | + "op": "remove", |
| 105 | + "path": "/metadata/labels/kubevirt.internal.virtualization.deckhouse.io~1schedulable", |
| 106 | + }, |
| 107 | + map[string]string{ |
| 108 | + "op": "remove", |
| 109 | + "path": "/metadata/labels/virtualization.deckhouse.io~1kvm-enabled", |
| 110 | + }, |
| 111 | + }, |
| 112 | + } |
| 113 | + |
| 114 | + snapshots.GetMock.When(nodesSnapshot).Then([]pkg.Snapshot{ |
| 115 | + // should be patched |
| 116 | + createSnapshotMock(NodeInfo{ |
| 117 | + Name: "node1", |
| 118 | + Labels: map[string]string{ |
| 119 | + "kubevirt.internal.virtualization.deckhouse.io/schedulable": "true", |
| 120 | + }, |
| 121 | + }), |
| 122 | + // should be patched |
| 123 | + createSnapshotMock(NodeInfo{ |
| 124 | + Name: "node2", |
| 125 | + Labels: map[string]string{ |
| 126 | + "kubevirt.internal.virtualization.deckhouse.io/schedulable": "true", |
| 127 | + "virtualization.deckhouse.io/kvm-enabled": "true", |
| 128 | + }, |
| 129 | + }), |
| 130 | + }) |
| 131 | + |
| 132 | + patchCollector.PatchWithJSONMock.Set(func(patch any, apiVersion, kind, namespace, name string, opts ...pkg.PatchCollectorOption) { |
| 133 | + p, ok := patch.([]map[string]string) |
| 134 | + Expect(ok).To(BeTrue()) |
| 135 | + Expect(expectedNodes).To(HaveKey(name)) |
| 136 | + Expect(p).To(Equal(expectedNodes[name])) |
| 137 | + delete(expectedNodes, name) |
| 138 | + }) |
| 139 | + |
| 140 | + err := handleCleanUpNodeLabels(context.Background(), input) |
| 141 | + Expect(err).ShouldNot(HaveOccurred()) |
| 142 | + |
| 143 | + Expect(buf.String()).To(ContainSubstring(fmt.Sprintf(logMessageTemplate, 1, labelPattern, "node1"))) |
| 144 | + Expect(buf.String()).To(ContainSubstring(fmt.Sprintf(logMessageTemplate, 2, labelPattern, "node2"))) |
| 145 | + |
| 146 | + Expect(expectedNodes).To(HaveLen(0)) |
| 147 | + }) |
| 148 | + }) |
| 149 | +}) |
0 commit comments