|
| 1 | +// Licensed to the Apache Software Foundation (ASF) under one or more |
| 2 | +// contributor license agreements. See the NOTICE file distributed with |
| 3 | +// this work for additional information regarding copyright ownership. |
| 4 | +// The ASF licenses this file to You under the Apache License, Version 2.0 |
| 5 | +// (the "License"); you may not use this file except in compliance with |
| 6 | +// the License. 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 | +package v2_test |
| 17 | + |
| 18 | +import ( |
| 19 | + "path/filepath" |
| 20 | + "runtime" |
| 21 | + "testing" |
| 22 | + |
| 23 | + "github.com/stretchr/testify/assert" |
| 24 | + "github.com/stretchr/testify/require" |
| 25 | + "k8s.io/apimachinery/pkg/util/intstr" |
| 26 | + |
| 27 | + apisixv2 "github.com/apache/apisix-ingress-controller/api/v2" |
| 28 | +) |
| 29 | + |
| 30 | +func loadApisixRouteSchema(t *testing.T) *crdSchemaValidator { |
| 31 | + t.Helper() |
| 32 | + _, thisFile, _, _ := runtime.Caller(0) |
| 33 | + crdPath := filepath.Join(filepath.Dir(thisFile), "..", "..", |
| 34 | + "config", "crd", "bases", "apisix.apache.org_apisixroutes.yaml") |
| 35 | + return loadCRDSchema(t, crdPath) |
| 36 | +} |
| 37 | + |
| 38 | +func strPtr(s string) *string { return &s } |
| 39 | +func boolPtr(b bool) *bool { return &b } |
| 40 | +func intPtr(i int) *int { return &i } |
| 41 | + |
| 42 | +func newRouteWithBodyExpr(ingressClass, fieldName, value string) *apisixv2.ApisixRoute { |
| 43 | + return &apisixv2.ApisixRoute{ |
| 44 | + Spec: apisixv2.ApisixRouteSpec{ |
| 45 | + IngressClassName: ingressClass, |
| 46 | + HTTP: []apisixv2.ApisixRouteHTTP{ |
| 47 | + { |
| 48 | + Name: "rule0", |
| 49 | + Websocket: boolPtr(false), |
| 50 | + Match: apisixv2.ApisixRouteHTTPMatch{ |
| 51 | + Paths: []string{"/*"}, |
| 52 | + NginxVars: apisixv2.ApisixRouteHTTPMatchExprs{ |
| 53 | + { |
| 54 | + Subject: apisixv2.ApisixRouteHTTPMatchExprSubject{ |
| 55 | + Scope: apisixv2.ScopeBody, |
| 56 | + Name: fieldName, |
| 57 | + }, |
| 58 | + Op: apisixv2.OpEqual, |
| 59 | + Set: []string{}, |
| 60 | + Value: strPtr(value), |
| 61 | + }, |
| 62 | + }, |
| 63 | + }, |
| 64 | + Backends: []apisixv2.ApisixRouteHTTPBackend{ |
| 65 | + {ServiceName: "my-svc", ServicePort: intstr.FromInt(80), Weight: intPtr(100)}, |
| 66 | + }, |
| 67 | + }, |
| 68 | + }, |
| 69 | + }, |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +// TestApisixRoute_BodyScope_SimpleField verifies that a Body scope expr with a |
| 74 | +// simple field name passes CRD schema validation. |
| 75 | +func TestApisixRoute_BodyScope_SimpleField(t *testing.T) { |
| 76 | + v := loadApisixRouteSchema(t) |
| 77 | + assert.NoError(t, v.Validate(t, newRouteWithBodyExpr("apisix", "action", "login"))) |
| 78 | +} |
| 79 | + |
| 80 | +// TestApisixRoute_BodyScope_NestedJSONPath verifies that a Body scope expr with |
| 81 | +// a dot-notation JSON path passes CRD schema validation. |
| 82 | +func TestApisixRoute_BodyScope_NestedJSONPath(t *testing.T) { |
| 83 | + v := loadApisixRouteSchema(t) |
| 84 | + assert.NoError(t, v.Validate(t, newRouteWithBodyExpr("apisix", "model.version", "gpt-4"))) |
| 85 | +} |
| 86 | + |
| 87 | +// TestApisixRoute_BodyScope_EmptyName verifies that a Body scope expr with an |
| 88 | +// empty name is rejected by the CEL XValidation rule. |
| 89 | +func TestApisixRoute_BodyScope_EmptyName(t *testing.T) { |
| 90 | + v := loadApisixRouteSchema(t) |
| 91 | + err := v.Validate(t, newRouteWithBodyExpr("apisix", "", "login")) |
| 92 | + require.Error(t, err) |
| 93 | + assert.Contains(t, err.Error(), "name is required when scope is not Path") |
| 94 | +} |
| 95 | + |
| 96 | +// TestApisixRoute_PathScope_EmptyName verifies that Path scope without a name |
| 97 | +// passes CRD schema validation (name is optional for Path). |
| 98 | +func TestApisixRoute_PathScope_EmptyName(t *testing.T) { |
| 99 | + v := loadApisixRouteSchema(t) |
| 100 | + ar := &apisixv2.ApisixRoute{ |
| 101 | + Spec: apisixv2.ApisixRouteSpec{ |
| 102 | + HTTP: []apisixv2.ApisixRouteHTTP{ |
| 103 | + { |
| 104 | + Name: "rule0", |
| 105 | + Websocket: boolPtr(false), |
| 106 | + Match: apisixv2.ApisixRouteHTTPMatch{ |
| 107 | + Paths: []string{"/*"}, |
| 108 | + NginxVars: apisixv2.ApisixRouteHTTPMatchExprs{ |
| 109 | + { |
| 110 | + Subject: apisixv2.ApisixRouteHTTPMatchExprSubject{ |
| 111 | + Scope: apisixv2.ScopePath, |
| 112 | + }, |
| 113 | + Op: apisixv2.OpEqual, |
| 114 | + Set: []string{}, |
| 115 | + Value: strPtr("/api"), |
| 116 | + }, |
| 117 | + }, |
| 118 | + }, |
| 119 | + Backends: []apisixv2.ApisixRouteHTTPBackend{ |
| 120 | + {ServiceName: "my-svc", ServicePort: intstr.FromInt(80), Weight: intPtr(100)}, |
| 121 | + }, |
| 122 | + }, |
| 123 | + }, |
| 124 | + }, |
| 125 | + } |
| 126 | + assert.NoError(t, v.Validate(t, ar)) |
| 127 | +} |
0 commit comments