|
| 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 | + "encoding/json" |
| 23 | + "os" |
| 24 | + "testing" |
| 25 | + "time" |
| 26 | + |
| 27 | + "github.com/deckhouse/deckhouse/pkg/log" |
| 28 | + "github.com/deckhouse/module-sdk/pkg" |
| 29 | + "github.com/deckhouse/module-sdk/pkg/jq" |
| 30 | + "github.com/deckhouse/module-sdk/testing/mock" |
| 31 | + . "github.com/onsi/ginkgo" |
| 32 | + . "github.com/onsi/gomega" |
| 33 | + v1 "k8s.io/api/core/v1" |
| 34 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 35 | + "sigs.k8s.io/yaml" |
| 36 | +) |
| 37 | + |
| 38 | +func TestMigrateVirthandlerKVMLabels(t *testing.T) { |
| 39 | + RegisterFailHandler(Fail) |
| 40 | + RunSpecs(t, "Migrate virthandler KVM labels Suite") |
| 41 | +} |
| 42 | + |
| 43 | +var _ = Describe("Migrate virthandler KVM labels", func() { |
| 44 | + err := os.Setenv("D8_IS_TESTS_ENVIRONMENT", "true") |
| 45 | + Expect(err).ShouldNot(HaveOccurred()) |
| 46 | + |
| 47 | + const ( |
| 48 | + node1YAML = ` |
| 49 | +--- |
| 50 | +apiVersion: v1 |
| 51 | +kind: Node |
| 52 | +metadata: |
| 53 | + labels: |
| 54 | + kubevirt.internal.virtualization.deckhouse.io/schedulable: "true" |
| 55 | + name: node1 |
| 56 | +` |
| 57 | + |
| 58 | + node2YAML = ` |
| 59 | +--- |
| 60 | +apiVersion: v1 |
| 61 | +kind: Node |
| 62 | +metadata: |
| 63 | + labels: |
| 64 | + kubevirt.internal.virtualization.deckhouse.io/schedulable: "true" |
| 65 | + virtualization.deckhouse.io/kvm-enabled: "true" |
| 66 | + name: node2 |
| 67 | +` |
| 68 | + |
| 69 | + node3YAML = ` |
| 70 | +--- |
| 71 | +apiVersion: v1 |
| 72 | +kind: Node |
| 73 | +metadata: |
| 74 | + labels: |
| 75 | + kubevirt.internal.virtualization.deckhouse.io/schedulable: "true" |
| 76 | + virtualization.deckhouse.io/kvm-enabled: "false" |
| 77 | + name: node3 |
| 78 | +` |
| 79 | + |
| 80 | + node4YAML = ` |
| 81 | +--- |
| 82 | +apiVersion: v1 |
| 83 | +kind: Node |
| 84 | +metadata: |
| 85 | + labels: |
| 86 | + kubevirt.internal.virtualization.deckhouse.io/schedulable: "true" |
| 87 | + name: node6 |
| 88 | +` |
| 89 | + ) |
| 90 | + |
| 91 | + var ( |
| 92 | + snapshots *mock.SnapshotsMock |
| 93 | + values *mock.PatchableValuesCollectorMock |
| 94 | + patchCollector *mock.PatchCollectorMock |
| 95 | + input *pkg.HookInput |
| 96 | + buf *bytes.Buffer |
| 97 | + ) |
| 98 | + |
| 99 | + filterResultNode1, err := nodeYamlToSnapshot(node1YAML) |
| 100 | + if err != nil { |
| 101 | + Expect(err).ShouldNot(HaveOccurred()) |
| 102 | + } |
| 103 | + |
| 104 | + filterResultNode2, err := nodeYamlToSnapshot(node2YAML) |
| 105 | + if err != nil { |
| 106 | + Expect(err).ShouldNot(HaveOccurred()) |
| 107 | + } |
| 108 | + |
| 109 | + filterResultNode3, err := nodeYamlToSnapshot(node3YAML) |
| 110 | + if err != nil { |
| 111 | + Expect(err).ShouldNot(HaveOccurred()) |
| 112 | + } |
| 113 | + |
| 114 | + filterResultNode4, err := nodeYamlToSnapshot(node4YAML) |
| 115 | + if err != nil { |
| 116 | + Expect(err).ShouldNot(HaveOccurred()) |
| 117 | + } |
| 118 | + |
| 119 | + BeforeEach(func() { |
| 120 | + snapshots = mock.NewSnapshotsMock(GinkgoT()) |
| 121 | + values = mock.NewPatchableValuesCollectorMock(GinkgoT()) |
| 122 | + patchCollector = mock.NewPatchCollectorMock(GinkgoT()) |
| 123 | + |
| 124 | + buf = bytes.NewBuffer([]byte{}) |
| 125 | + |
| 126 | + input = &pkg.HookInput{ |
| 127 | + Values: values, |
| 128 | + Snapshots: snapshots, |
| 129 | + Logger: log.NewLogger(log.Options{ |
| 130 | + Level: log.LevelDebug.Level(), |
| 131 | + Output: buf, |
| 132 | + TimeFunc: func(_ time.Time) time.Time { |
| 133 | + parsedTime, err := time.Parse(time.DateTime, "2006-01-02 15:04:05") |
| 134 | + Expect(err).ShouldNot(HaveOccurred()) |
| 135 | + return parsedTime |
| 136 | + }, |
| 137 | + }), |
| 138 | + PatchCollector: patchCollector, |
| 139 | + } |
| 140 | + }) |
| 141 | + |
| 142 | + Context("Empty cluster", func() { |
| 143 | + It("Hook must execute successfully", func() { |
| 144 | + snapshots.GetMock.When(nodesMetadataSnapshot).Then( |
| 145 | + []pkg.Snapshot{}, |
| 146 | + ) |
| 147 | + err := handleVirtHandlerNodes(context.Background(), input) |
| 148 | + Expect(err).ShouldNot(HaveOccurred()) |
| 149 | + }) |
| 150 | + }) |
| 151 | + |
| 152 | + Context("Four nodes but only two should be patched.", func() { |
| 153 | + It("Hook must execute successfully", func() { |
| 154 | + |
| 155 | + expectedVMLabels := map[string]map[string]string{ |
| 156 | + "node1": { |
| 157 | + virtHandlerLabel: virtHandlerNodeLabelValue, |
| 158 | + }, |
| 159 | + "node6": { |
| 160 | + virtHandlerLabel: virtHandlerNodeLabelValue, |
| 161 | + }, |
| 162 | + } |
| 163 | + |
| 164 | + snapshots.GetMock.When(nodesMetadataSnapshot).Then( |
| 165 | + []pkg.Snapshot{ |
| 166 | + mock.NewSnapshotMock(GinkgoT()).UnmarshalToMock.Set(getNodeSnapshot(filterResultNode1)), |
| 167 | + mock.NewSnapshotMock(GinkgoT()).UnmarshalToMock.Set(getNodeSnapshot(filterResultNode2)), |
| 168 | + mock.NewSnapshotMock(GinkgoT()).UnmarshalToMock.Set(getNodeSnapshot(filterResultNode3)), |
| 169 | + mock.NewSnapshotMock(GinkgoT()).UnmarshalToMock.Set(getNodeSnapshot(filterResultNode4)), |
| 170 | + }, |
| 171 | + ) |
| 172 | + |
| 173 | + patchCollector.PatchWithMergeMock.Set(func(patch any, apiVersion, kind, namespace, name string, opts ...pkg.PatchCollectorOption) { |
| 174 | + node, ok := patch.(v1.Node) |
| 175 | + Expect(ok).To(BeTrue()) |
| 176 | + Expect(expectedVMLabels).To(HaveKey(name)) |
| 177 | + for k, v := range expectedVMLabels[name] { |
| 178 | + Expect(node.Labels).Should(HaveKeyWithValue(k, v)) |
| 179 | + } |
| 180 | + delete(expectedVMLabels, name) |
| 181 | + }) |
| 182 | + err := handleVirtHandlerNodes(context.Background(), input) |
| 183 | + Expect(err).ShouldNot(HaveOccurred()) |
| 184 | + Expect(expectedVMLabels).To(HaveLen(0)) |
| 185 | + }) |
| 186 | + }) |
| 187 | + |
| 188 | +}) |
| 189 | + |
| 190 | +func nodeYamlToSnapshot(manifest string) (string, error) { |
| 191 | + node := new(v1.Node) |
| 192 | + err := yaml.Unmarshal([]byte(manifest), node) |
| 193 | + if err != nil { |
| 194 | + return "", err |
| 195 | + } |
| 196 | + |
| 197 | + query, err := jq.NewQuery(nodeJQFilter) |
| 198 | + if err != nil { |
| 199 | + return "", err |
| 200 | + } |
| 201 | + |
| 202 | + filterResult, err := query.FilterObject(context.Background(), node) |
| 203 | + if err != nil { |
| 204 | + return "", err |
| 205 | + } |
| 206 | + |
| 207 | + return filterResult.String(), nil |
| 208 | +} |
| 209 | + |
| 210 | +func getNodeSnapshot(nodeManifest string) func(v any) (err error) { |
| 211 | + return func(v any) (err error) { |
| 212 | + rt := v.(*metav1.ObjectMeta) |
| 213 | + if err := json.Unmarshal([]byte(nodeManifest), rt); err != nil { |
| 214 | + return err |
| 215 | + } |
| 216 | + |
| 217 | + return nil |
| 218 | + } |
| 219 | +} |
0 commit comments