|
| 1 | +// Copyright Amazon.com Inc. or its affiliates. All Rights Reserved. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"). You may |
| 4 | +// not use this file except in compliance with the License. A copy of the |
| 5 | +// License is located at |
| 6 | +// |
| 7 | +// http://aws.amazon.com/apache2.0/ |
| 8 | +// |
| 9 | +// or in the "license" file accompanying this file. This file is distributed |
| 10 | +// on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either |
| 11 | +// express or implied. See the License for the specific language governing |
| 12 | +// permissions and limitations under the License. |
| 13 | + |
| 14 | +package ack_test |
| 15 | + |
| 16 | +import ( |
| 17 | + "strings" |
| 18 | + "testing" |
| 19 | + |
| 20 | + "github.com/stretchr/testify/assert" |
| 21 | + "github.com/stretchr/testify/require" |
| 22 | + |
| 23 | + ackgenerate "github.com/aws-controllers-k8s/code-generator/pkg/generate/ack" |
| 24 | + "github.com/aws-controllers-k8s/code-generator/pkg/testutil" |
| 25 | +) |
| 26 | + |
| 27 | +// TestCELOnTypeDef verifies that custom_cel_rules configured on a |
| 28 | +// nested field are rendered as +kubebuilder:validation:XValidation markers in |
| 29 | +// the generated types.go (via the type_def template include). |
| 30 | +func TestCELOnTypeDef(t *testing.T) { |
| 31 | + assert := assert.New(t) |
| 32 | + require := require.New(t) |
| 33 | + |
| 34 | + g := testutil.NewModelForServiceWithOptions(t, "apigatewayv2", &testutil.TestingModelOptions{ |
| 35 | + GeneratorConfigFile: "generator-with-cel-rules.yaml", |
| 36 | + }) |
| 37 | + |
| 38 | + ts, err := ackgenerate.APIs(g, []string{testutil.TemplatesBasePath(t)}) |
| 39 | + require.NoError(err) |
| 40 | + require.NoError(ts.Execute()) |
| 41 | + |
| 42 | + typesGo, ok := ts.Executed()["types.go"] |
| 43 | + require.True(ok, "types.go not found in executed templates") |
| 44 | + |
| 45 | + output := typesGo.String() |
| 46 | + assert.True( |
| 47 | + strings.Contains(output, `// +kubebuilder:validation:XValidation:rule="self.startsWith('https://')",message="Issuer must be an HTTPS URL"`), |
| 48 | + "expected XValidation marker for Issuer CEL rule in types.go", |
| 49 | + ) |
| 50 | +} |
| 51 | + |
| 52 | +// TestCELOnSpec verifies that custom_cel_rules configured at the |
| 53 | +// resource level are rendered as +kubebuilder:validation:XValidation markers |
| 54 | +// on the generated CRD Spec struct (via the crd.go template). |
| 55 | +func TestCELOnSpec(t *testing.T) { |
| 56 | + assert := assert.New(t) |
| 57 | + require := require.New(t) |
| 58 | + |
| 59 | + g := testutil.NewModelForService(t, "route53") |
| 60 | + |
| 61 | + ts, err := ackgenerate.APIs(g, []string{testutil.TemplatesBasePath(t)}) |
| 62 | + require.NoError(err) |
| 63 | + require.NoError(ts.Execute()) |
| 64 | + |
| 65 | + hostedZoneGo, ok := ts.Executed()["hosted_zone.go"] |
| 66 | + require.True(ok, "hosted_zone.go not found in executed templates") |
| 67 | + |
| 68 | + output := hostedZoneGo.String() |
| 69 | + assert.True( |
| 70 | + strings.Contains(output, `// +kubebuilder:validation:XValidation:rule="!has(self.hostedZoneConfig) || !self.hostedZoneConfig.privateZone || has(self.vpc)",message="spec.vpc is required for private hosted zones"`), |
| 71 | + "expected XValidation marker for first HostedZone CEL rule", |
| 72 | + ) |
| 73 | + assert.True( |
| 74 | + strings.Contains(output, `// +kubebuilder:validation:XValidation:rule="size(self.name) > 0"`), |
| 75 | + "expected XValidation marker for second HostedZone CEL rule (no message)", |
| 76 | + ) |
| 77 | +} |
| 78 | + |
| 79 | +// TestCELOnField verifies that custom_cel_rules configured on a |
| 80 | +// top-level spec field are rendered as +kubebuilder:validation:XValidation |
| 81 | +// markers on the individual field in the generated CRD Spec struct. |
| 82 | +func TestCELOnField(t *testing.T) { |
| 83 | + assert := assert.New(t) |
| 84 | + require := require.New(t) |
| 85 | + |
| 86 | + g := testutil.NewModelForService(t, "route53") |
| 87 | + |
| 88 | + ts, err := ackgenerate.APIs(g, []string{testutil.TemplatesBasePath(t)}) |
| 89 | + require.NoError(err) |
| 90 | + require.NoError(ts.Execute()) |
| 91 | + |
| 92 | + recordSetGo, ok := ts.Executed()["record_set.go"] |
| 93 | + require.True(ok, "record_set.go not found in executed templates") |
| 94 | + |
| 95 | + output := recordSetGo.String() |
| 96 | + assert.True( |
| 97 | + strings.Contains(output, `// +kubebuilder:validation:XValidation:rule="self.endsWith('.')",message="DNS name must end with a dot"`), |
| 98 | + "expected XValidation marker for Name field CEL rule in record_set.go", |
| 99 | + ) |
| 100 | +} |
0 commit comments