|
| 1 | +/* |
| 2 | +Copyright 2026 The Kubernetes Authors. |
| 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 genall |
| 18 | + |
| 19 | +import ( |
| 20 | + . "github.com/onsi/ginkgo" |
| 21 | + g "github.com/onsi/gomega" |
| 22 | +) |
| 23 | + |
| 24 | +var _ = Describe("yamlMarshal", func() { |
| 25 | + It("should preserve integer types without adding decimal points", func() { |
| 26 | + type schemaLike struct { |
| 27 | + Minimum *float64 `json:"minimum,omitempty"` |
| 28 | + Maximum *float64 `json:"maximum,omitempty"` |
| 29 | + Port *int32 `json:"port,omitempty"` |
| 30 | + Count *int64 `json:"count,omitempty"` |
| 31 | + } |
| 32 | + |
| 33 | + minVal := -0.5 |
| 34 | + maxVal := 1.5 |
| 35 | + port := int32(8080) |
| 36 | + count := int64(2) |
| 37 | + |
| 38 | + out, err := yamlMarshal(schemaLike{Minimum: &minVal, Maximum: &maxVal, Port: &port, Count: &count}) |
| 39 | + g.Expect(err).NotTo(g.HaveOccurred()) |
| 40 | + |
| 41 | + y := string(out) |
| 42 | + g.Expect(y).To(g.ContainSubstring("minimum: -0.5")) |
| 43 | + g.Expect(y).To(g.ContainSubstring("maximum: 1.5")) |
| 44 | + g.Expect(y).To(g.ContainSubstring("port: 8080")) |
| 45 | + g.Expect(y).To(g.ContainSubstring("count: 2")) |
| 46 | + g.Expect(y).NotTo(g.ContainSubstring("2.0")) |
| 47 | + g.Expect(y).NotTo(g.ContainSubstring("8080.0")) |
| 48 | + }) |
| 49 | + |
| 50 | + It("should marshal nil as null", func() { |
| 51 | + out, err := yamlMarshal(nil) |
| 52 | + g.Expect(err).NotTo(g.HaveOccurred()) |
| 53 | + g.Expect(string(out)).To(g.ContainSubstring("null")) |
| 54 | + }) |
| 55 | + |
| 56 | + It("should marshal string values correctly", func() { |
| 57 | + obj := map[string]any{ |
| 58 | + "name": "hello-world", |
| 59 | + "special": "value: with colon", |
| 60 | + "empty": "", |
| 61 | + } |
| 62 | + out, err := yamlMarshal(obj) |
| 63 | + g.Expect(err).NotTo(g.HaveOccurred()) |
| 64 | + |
| 65 | + y := string(out) |
| 66 | + g.Expect(y).To(g.ContainSubstring("name: hello-world")) |
| 67 | + g.Expect(y).To(g.Or( |
| 68 | + g.ContainSubstring("value: with colon"), |
| 69 | + g.ContainSubstring("'value: with colon'"), |
| 70 | + g.ContainSubstring(`"value: with colon"`), |
| 71 | + )) |
| 72 | + }) |
| 73 | + |
| 74 | + It("should marshal arrays correctly", func() { |
| 75 | + obj := map[string]any{ |
| 76 | + "items": []any{"a", "b", "c"}, |
| 77 | + } |
| 78 | + out, err := yamlMarshal(obj) |
| 79 | + g.Expect(err).NotTo(g.HaveOccurred()) |
| 80 | + |
| 81 | + y := string(out) |
| 82 | + g.Expect(y).To(g.ContainSubstring("- a")) |
| 83 | + g.Expect(y).To(g.ContainSubstring("- b")) |
| 84 | + g.Expect(y).To(g.ContainSubstring("- c")) |
| 85 | + }) |
| 86 | + |
| 87 | + It("should marshal deeply nested maps correctly", func() { |
| 88 | + obj := map[string]any{ |
| 89 | + "spec": map[string]any{ |
| 90 | + "template": map[string]any{ |
| 91 | + "metadata": map[string]any{ |
| 92 | + "labels": map[string]any{ |
| 93 | + "app": "myapp", |
| 94 | + }, |
| 95 | + }, |
| 96 | + }, |
| 97 | + }, |
| 98 | + } |
| 99 | + out, err := yamlMarshal(obj) |
| 100 | + g.Expect(err).NotTo(g.HaveOccurred()) |
| 101 | + g.Expect(string(out)).To(g.ContainSubstring("app: myapp")) |
| 102 | + }) |
| 103 | + |
| 104 | + It("should marshal booleans correctly", func() { |
| 105 | + obj := map[string]any{"enabled": true, "disabled": false} |
| 106 | + out, err := yamlMarshal(obj) |
| 107 | + g.Expect(err).NotTo(g.HaveOccurred()) |
| 108 | + |
| 109 | + y := string(out) |
| 110 | + g.Expect(y).To(g.ContainSubstring("enabled: true")) |
| 111 | + g.Expect(y).To(g.ContainSubstring("disabled: false")) |
| 112 | + }) |
| 113 | + |
| 114 | + It("should apply multiple transforms in order", func() { |
| 115 | + obj := map[string]any{ |
| 116 | + "metadata": map[string]any{ |
| 117 | + "name": "example", |
| 118 | + "creationTimestamp": "2024-01-01T00:00:00Z", |
| 119 | + }, |
| 120 | + "extra": "keep", |
| 121 | + } |
| 122 | + |
| 123 | + removeExtra := func(o map[string]any) error { |
| 124 | + delete(o, "extra") |
| 125 | + return nil |
| 126 | + } |
| 127 | + |
| 128 | + out, err := yamlMarshal(obj, |
| 129 | + WithTransform(TransformRemoveCreationTimestamp), |
| 130 | + WithTransform(removeExtra), |
| 131 | + ) |
| 132 | + g.Expect(err).NotTo(g.HaveOccurred()) |
| 133 | + |
| 134 | + y := string(out) |
| 135 | + g.Expect(y).NotTo(g.ContainSubstring("creationTimestamp")) |
| 136 | + g.Expect(y).NotTo(g.ContainSubstring("extra")) |
| 137 | + g.Expect(y).To(g.ContainSubstring("name: example")) |
| 138 | + }) |
| 139 | + |
| 140 | + It("should apply transforms and produce valid YAML for a CRD-like object", func() { |
| 141 | + obj := map[string]any{ |
| 142 | + "apiVersion": "apiextensions.k8s.io/v1", |
| 143 | + "kind": "CustomResourceDefinition", |
| 144 | + "metadata": map[string]any{ |
| 145 | + "name": "foos.example.com", |
| 146 | + "creationTimestamp": "2024-01-01T00:00:00Z", |
| 147 | + }, |
| 148 | + "spec": map[string]any{ |
| 149 | + "group": "example.com", |
| 150 | + "versions": []any{ |
| 151 | + map[string]any{"name": "v1", "served": true, "storage": true}, |
| 152 | + }, |
| 153 | + }, |
| 154 | + } |
| 155 | + |
| 156 | + out, err := yamlMarshal(obj, WithTransform(TransformRemoveCreationTimestamp)) |
| 157 | + g.Expect(err).NotTo(g.HaveOccurred()) |
| 158 | + |
| 159 | + y := string(out) |
| 160 | + g.Expect(y).NotTo(g.ContainSubstring("creationTimestamp")) |
| 161 | + g.Expect(y).To(g.ContainSubstring("name: foos.example.com")) |
| 162 | + g.Expect(y).To(g.ContainSubstring("group: example.com")) |
| 163 | + }) |
| 164 | +}) |
| 165 | + |
| 166 | +var _ = Describe("TransformRemoveCreationTimestamp", func() { |
| 167 | + It("should remove creationTimestamp when present", func() { |
| 168 | + obj := map[string]any{ |
| 169 | + "metadata": map[string]any{ |
| 170 | + "name": "test", |
| 171 | + "creationTimestamp": "2024-01-01T00:00:00Z", |
| 172 | + }, |
| 173 | + } |
| 174 | + g.Expect(TransformRemoveCreationTimestamp(obj)).To(g.Succeed()) |
| 175 | + |
| 176 | + meta := obj["metadata"].(map[string]any) |
| 177 | + g.Expect(meta).NotTo(g.HaveKey("creationTimestamp")) |
| 178 | + g.Expect(meta).To(g.HaveKey("name")) |
| 179 | + }) |
| 180 | + |
| 181 | + It("should be a no-op when metadata is absent", func() { |
| 182 | + obj := map[string]any{"kind": "CronJob"} |
| 183 | + g.Expect(TransformRemoveCreationTimestamp(obj)).To(g.Succeed()) |
| 184 | + }) |
| 185 | + |
| 186 | + It("should be a no-op when creationTimestamp is already absent", func() { |
| 187 | + obj := map[string]any{ |
| 188 | + "metadata": map[string]any{"name": "test"}, |
| 189 | + } |
| 190 | + g.Expect(TransformRemoveCreationTimestamp(obj)).To(g.Succeed()) |
| 191 | + |
| 192 | + meta := obj["metadata"].(map[string]any) |
| 193 | + g.Expect(meta).To(g.HaveKey("name")) |
| 194 | + }) |
| 195 | + |
| 196 | + It("should be a no-op when metadata is a non-map type", func() { |
| 197 | + obj := map[string]any{"metadata": "not-a-map"} |
| 198 | + g.Expect(TransformRemoveCreationTimestamp(obj)).To(g.Succeed()) |
| 199 | + }) |
| 200 | + |
| 201 | + It("should be a no-op when metadata is nil", func() { |
| 202 | + obj := map[string]any{"metadata": nil} |
| 203 | + g.Expect(TransformRemoveCreationTimestamp(obj)).To(g.Succeed()) |
| 204 | + }) |
| 205 | +}) |
0 commit comments