|
| 1 | +// Copyright 2025 Google LLC |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package translate |
| 16 | + |
| 17 | +import ( |
| 18 | + "fmt" |
| 19 | + "testing" |
| 20 | + |
| 21 | + "github.com/stretchr/testify/require" |
| 22 | + apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1" |
| 23 | +) |
| 24 | + |
| 25 | +func TestSelectVersion(t *testing.T) { |
| 26 | + // Define some sample versions to be reused in the tests |
| 27 | + v1 := apiextensionsv1.CustomResourceDefinitionVersion{Name: "v1", Served: true} |
| 28 | + v2 := apiextensionsv1.CustomResourceDefinitionVersion{Name: "v2", Served: true} |
| 29 | + v1beta1 := apiextensionsv1.CustomResourceDefinitionVersion{Name: "v1beta1", Served: true} |
| 30 | + |
| 31 | + testCases := []struct { |
| 32 | + name string |
| 33 | + spec *apiextensionsv1.CustomResourceDefinitionSpec |
| 34 | + version string |
| 35 | + want *apiextensionsv1.CustomResourceDefinitionVersion |
| 36 | + }{ |
| 37 | + { |
| 38 | + name: "should return nil when spec has no versions", |
| 39 | + spec: &apiextensionsv1.CustomResourceDefinitionSpec{ |
| 40 | + Versions: []apiextensionsv1.CustomResourceDefinitionVersion{}, |
| 41 | + }, |
| 42 | + version: "v1", |
| 43 | + want: nil, |
| 44 | + }, |
| 45 | + { |
| 46 | + name: "should return the first version when version string is empty", |
| 47 | + spec: &apiextensionsv1.CustomResourceDefinitionSpec{ |
| 48 | + Versions: []apiextensionsv1.CustomResourceDefinitionVersion{v1beta1, v1}, |
| 49 | + }, |
| 50 | + version: "", |
| 51 | + want: &v1beta1, |
| 52 | + }, |
| 53 | + { |
| 54 | + name: "should return the matching version when it exists", |
| 55 | + spec: &apiextensionsv1.CustomResourceDefinitionSpec{ |
| 56 | + Versions: []apiextensionsv1.CustomResourceDefinitionVersion{v1, v2}, |
| 57 | + }, |
| 58 | + version: "v2", |
| 59 | + want: &v2, |
| 60 | + }, |
| 61 | + { |
| 62 | + name: "should return nil when the requested version does not exist", |
| 63 | + spec: &apiextensionsv1.CustomResourceDefinitionSpec{ |
| 64 | + Versions: []apiextensionsv1.CustomResourceDefinitionVersion{v1, v2}, |
| 65 | + }, |
| 66 | + version: "v3", |
| 67 | + want: nil, |
| 68 | + }, |
| 69 | + { |
| 70 | + name: "should handle a single version in the spec", |
| 71 | + spec: &apiextensionsv1.CustomResourceDefinitionSpec{ |
| 72 | + Versions: []apiextensionsv1.CustomResourceDefinitionVersion{v1}, |
| 73 | + }, |
| 74 | + version: "v1", |
| 75 | + want: &v1, |
| 76 | + }, |
| 77 | + } |
| 78 | + |
| 79 | + for _, tc := range testCases { |
| 80 | + t.Run(tc.name, func(t *testing.T) { |
| 81 | + got := selectVersion(tc.spec, tc.version) |
| 82 | + require.Equal(t, tc.want, got) |
| 83 | + }) |
| 84 | + } |
| 85 | +} |
| 86 | + |
| 87 | +func TestGetOpenAPIProperties(t *testing.T) { |
| 88 | + const kind = "MyTestCRD" |
| 89 | + |
| 90 | + happyPathVersion := &apiextensionsv1.CustomResourceDefinitionVersion{ |
| 91 | + Name: "v1", |
| 92 | + Schema: &apiextensionsv1.CustomResourceValidation{ |
| 93 | + OpenAPIV3Schema: &apiextensionsv1.JSONSchemaProps{ |
| 94 | + Properties: map[string]apiextensionsv1.JSONSchemaProps{ |
| 95 | + "spec": { |
| 96 | + Type: "object", |
| 97 | + Properties: map[string]apiextensionsv1.JSONSchemaProps{ |
| 98 | + "fieldA": {Type: "string"}, |
| 99 | + }, |
| 100 | + }, |
| 101 | + }, |
| 102 | + }, |
| 103 | + }, |
| 104 | + } |
| 105 | + |
| 106 | + testCases := []struct { |
| 107 | + name string |
| 108 | + version *apiextensionsv1.CustomResourceDefinitionVersion |
| 109 | + wantProperties map[string]apiextensionsv1.JSONSchemaProps |
| 110 | + wantErrMsg string |
| 111 | + }{ |
| 112 | + { |
| 113 | + name: "should return properties", |
| 114 | + version: happyPathVersion, |
| 115 | + wantProperties: happyPathVersion.Schema.OpenAPIV3Schema.Properties, |
| 116 | + wantErrMsg: "", |
| 117 | + }, |
| 118 | + { |
| 119 | + name: "should return error if version is nil", |
| 120 | + version: nil, |
| 121 | + wantProperties: nil, |
| 122 | + wantErrMsg: fmt.Sprintf("missing version (nil) from %v spec", kind), |
| 123 | + }, |
| 124 | + { |
| 125 | + name: "should return error if schema is nil", |
| 126 | + version: &apiextensionsv1.CustomResourceDefinitionVersion{ |
| 127 | + Name: "v1", |
| 128 | + Schema: nil, // The point of failure |
| 129 | + }, |
| 130 | + wantProperties: nil, |
| 131 | + wantErrMsg: fmt.Sprintf("missing version schema from %v spec", kind), |
| 132 | + }, |
| 133 | + { |
| 134 | + name: "error - should return error if OpenAPIV3Schema is nil", |
| 135 | + version: &apiextensionsv1.CustomResourceDefinitionVersion{ |
| 136 | + Name: "v1", |
| 137 | + Schema: &apiextensionsv1.CustomResourceValidation{ |
| 138 | + OpenAPIV3Schema: nil, // The point of failure |
| 139 | + }, |
| 140 | + }, |
| 141 | + wantProperties: nil, |
| 142 | + wantErrMsg: fmt.Sprintf("missing version OpenAPI Schema from %v spec", kind), |
| 143 | + }, |
| 144 | + { |
| 145 | + name: "should return error if Properties map is nil", |
| 146 | + version: &apiextensionsv1.CustomResourceDefinitionVersion{ |
| 147 | + Name: "v1", |
| 148 | + Schema: &apiextensionsv1.CustomResourceValidation{ |
| 149 | + OpenAPIV3Schema: &apiextensionsv1.JSONSchemaProps{ |
| 150 | + Properties: nil, // The point of failure |
| 151 | + }, |
| 152 | + }, |
| 153 | + }, |
| 154 | + wantProperties: nil, |
| 155 | + wantErrMsg: fmt.Sprintf("missing version OpenAPI Properties from %v spec", kind), |
| 156 | + }, |
| 157 | + } |
| 158 | + |
| 159 | + for _, tc := range testCases { |
| 160 | + t.Run(tc.name, func(t *testing.T) { |
| 161 | + gotProperties, err := getOpenAPIProperties(kind, tc.version) |
| 162 | + if tc.wantErrMsg != "" { |
| 163 | + require.Error(t, err) |
| 164 | + require.EqualError(t, err, tc.wantErrMsg) |
| 165 | + require.Nil(t, gotProperties) |
| 166 | + } else { |
| 167 | + require.NoError(t, err) |
| 168 | + require.Equal(t, tc.wantProperties, gotProperties) |
| 169 | + } |
| 170 | + }) |
| 171 | + } |
| 172 | +} |
| 173 | + |
| 174 | +func TestGetSpecPropertiesFor(t *testing.T) { |
| 175 | + const kind = "MyTestCRD" |
| 176 | + |
| 177 | + // Reusable properties for the test cases |
| 178 | + nestedProperties := map[string]apiextensionsv1.JSONSchemaProps{ |
| 179 | + "replicas": {Type: "integer"}, |
| 180 | + "image": {Type: "string"}, |
| 181 | + } |
| 182 | + |
| 183 | + testCases := []struct { |
| 184 | + name string |
| 185 | + props map[string]apiextensionsv1.JSONSchemaProps |
| 186 | + field string |
| 187 | + wantProperties map[string]apiextensionsv1.JSONSchemaProps |
| 188 | + wantErrMsg string |
| 189 | + }{ |
| 190 | + { |
| 191 | + name: "should return nested properties", |
| 192 | + props: map[string]apiextensionsv1.JSONSchemaProps{ |
| 193 | + "spec": { |
| 194 | + Type: "object", |
| 195 | + Properties: nestedProperties, |
| 196 | + }, |
| 197 | + "status": { |
| 198 | + Type: "object", |
| 199 | + }, |
| 200 | + }, |
| 201 | + field: "spec", |
| 202 | + wantProperties: nestedProperties, |
| 203 | + wantErrMsg: "", // Expect no error |
| 204 | + }, |
| 205 | + { |
| 206 | + name: "field is missing from properties map", |
| 207 | + props: map[string]apiextensionsv1.JSONSchemaProps{ |
| 208 | + "status": {Type: "object"}, |
| 209 | + }, |
| 210 | + field: "spec", // This field does not exist |
| 211 | + wantProperties: nil, |
| 212 | + wantErrMsg: fmt.Sprintf("kind %q spec is missing field %q on", kind, "spec"), |
| 213 | + }, |
| 214 | + { |
| 215 | + name: "field is not of type object", |
| 216 | + props: map[string]apiextensionsv1.JSONSchemaProps{ |
| 217 | + "spec": {Type: "string"}, // The point of failure |
| 218 | + }, |
| 219 | + field: "spec", |
| 220 | + wantProperties: nil, |
| 221 | + wantErrMsg: fmt.Sprintf("kind %q field %q expected to be object but is %v", kind, "spec", "string"), |
| 222 | + }, |
| 223 | + { |
| 224 | + name: "field is an object but has no nested properties", |
| 225 | + props: map[string]apiextensionsv1.JSONSchemaProps{ |
| 226 | + "spec": { |
| 227 | + Type: "object", |
| 228 | + Properties: nil, // The nested map is nil |
| 229 | + }, |
| 230 | + }, |
| 231 | + field: "spec", |
| 232 | + wantProperties: nil, // Expecting a nil map is correct here |
| 233 | + wantErrMsg: "", |
| 234 | + }, |
| 235 | + } |
| 236 | + |
| 237 | + for _, tc := range testCases { |
| 238 | + t.Run(tc.name, func(t *testing.T) { |
| 239 | + // Act |
| 240 | + gotProperties, err := getSpecPropertiesFor(kind, tc.props, tc.field) |
| 241 | + |
| 242 | + // Assert |
| 243 | + if tc.wantErrMsg != "" { |
| 244 | + // We expect an error |
| 245 | + require.Error(t, err) |
| 246 | + require.EqualError(t, err, tc.wantErrMsg) |
| 247 | + require.Nil(t, gotProperties) |
| 248 | + } else { |
| 249 | + // We expect success |
| 250 | + require.NoError(t, err) |
| 251 | + require.Equal(t, tc.wantProperties, gotProperties) |
| 252 | + } |
| 253 | + }) |
| 254 | + } |
| 255 | +} |
0 commit comments