|
| 1 | +/* |
| 2 | +Copyright 2022 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 tests |
| 18 | + |
| 19 | +import ( |
| 20 | + "testing" |
| 21 | + |
| 22 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 23 | + "k8s.io/apimachinery/pkg/types" |
| 24 | + |
| 25 | + "sigs.k8s.io/gateway-api/apis/v1alpha2" |
| 26 | + "sigs.k8s.io/gateway-api/conformance/utils/http" |
| 27 | + "sigs.k8s.io/gateway-api/conformance/utils/kubernetes" |
| 28 | + "sigs.k8s.io/gateway-api/conformance/utils/suite" |
| 29 | +) |
| 30 | + |
| 31 | +func init() { |
| 32 | + ConformanceTests = append(ConformanceTests, HTTPRouteInvalidReferencePolicy) |
| 33 | +} |
| 34 | + |
| 35 | +var HTTPRouteInvalidReferencePolicy = suite.ConformanceTest{ |
| 36 | + ShortName: "HTTPRouteInvalidReferencePolicy", |
| 37 | + Description: "A single HTTPRoute in the gateway-conformance-infra namespace should fail to attach to a Gateway in the same namespace if the route has a backendRef Service in the gateway-conformance-app-backend namespace and a ReferencePolicy exists but does not grant permission to route to that specific Service", |
| 38 | + Features: []suite.SupportedFeature{ |
| 39 | + suite.SupportReferencePolicy, |
| 40 | + }, |
| 41 | + Manifests: []string{"tests/httproute-invalid-reference-policy.yaml"}, |
| 42 | + Test: func(t *testing.T, s *suite.ConformanceTestSuite) { |
| 43 | + routeNN := types.NamespacedName{Name: "invalid-reference-policy", Namespace: "gateway-conformance-infra"} |
| 44 | + gwNN := types.NamespacedName{Name: "same-namespace", Namespace: "gateway-conformance-infra"} |
| 45 | + |
| 46 | + ns := v1alpha2.Namespace(gwNN.Namespace) |
| 47 | + gwKind := v1alpha2.Kind("Gateway") |
| 48 | + |
| 49 | + // TODO(mikemorris): Add check for Accepted condition once |
| 50 | + // https://github.com/kubernetes-sigs/gateway-api/issues/1112 |
| 51 | + // has been resolved |
| 52 | + t.Run("Route status should have a route parent status with a ResolvedRefs condition with status False and reason RefNotPermitted", func(t *testing.T) { |
| 53 | + parents := []v1alpha2.RouteParentStatus{{ |
| 54 | + ParentRef: v1alpha2.ParentReference{ |
| 55 | + Group: (*v1alpha2.Group)(&v1alpha2.GroupVersion.Group), |
| 56 | + Kind: &gwKind, |
| 57 | + Name: v1alpha2.ObjectName(gwNN.Name), |
| 58 | + Namespace: &ns, |
| 59 | + }, |
| 60 | + ControllerName: v1alpha2.GatewayController(s.ControllerName), |
| 61 | + Conditions: []metav1.Condition{{ |
| 62 | + Type: string(v1alpha2.RouteConditionResolvedRefs), |
| 63 | + Status: metav1.ConditionFalse, |
| 64 | + Reason: string(v1alpha2.RouteReasonRefNotPermitted), |
| 65 | + }}, |
| 66 | + }} |
| 67 | + |
| 68 | + kubernetes.HTTPRouteMustHaveParents(t, s.Client, routeNN, parents, false, 60) |
| 69 | + }) |
| 70 | + |
| 71 | + // TODO(mikemorris): Un-skip check for Listener ResolvedRefs |
| 72 | + // RefNotPermitted condition and/or add check for attached |
| 73 | + // routes and any expected Listener conditions once |
| 74 | + // https://github.com/kubernetes-sigs/gateway-api/issues/1112 |
| 75 | + // has been resolved |
| 76 | + t.Skip("Gateway listener should have a ResolvedRefs condition with status False and reason RefNotPermitted", func(t *testing.T) { |
| 77 | + listeners := []v1alpha2.ListenerStatus{{ |
| 78 | + Name: v1alpha2.SectionName("http"), |
| 79 | + SupportedKinds: []v1alpha2.RouteGroupKind{{ |
| 80 | + Group: (*v1alpha2.Group)(&v1alpha2.GroupVersion.Group), |
| 81 | + Kind: v1alpha2.Kind("HTTPRoute"), |
| 82 | + }}, |
| 83 | + Conditions: []metav1.Condition{{ |
| 84 | + Type: string(v1alpha2.RouteConditionResolvedRefs), |
| 85 | + Status: metav1.ConditionFalse, |
| 86 | + Reason: string(v1alpha2.RouteReasonRefNotPermitted), |
| 87 | + }}, |
| 88 | + }} |
| 89 | + |
| 90 | + kubernetes.GatewayStatusMustHaveListeners(t, s.Client, gwNN, listeners, 60) |
| 91 | + }) |
| 92 | + |
| 93 | + gwAddr := kubernetes.GatewayAndHTTPRoutesMustBeReady(t, s.Client, s.ControllerName, gwNN, routeNN) |
| 94 | + |
| 95 | + // TODO(mikemorris): Add check for HTTP requests successfully reaching |
| 96 | + // app-backend-v1 at path "/" if it is determined that a Route with at |
| 97 | + // at least one allowed BackendRef should be accepted by a Gateway |
| 98 | + // and partially configured. |
| 99 | + |
| 100 | + t.Run("Simple HTTP request should not reach app-backend-v2", func(t *testing.T) { |
| 101 | + http.MakeRequestAndExpectEventuallyConsistentResponse(t, s.RoundTripper, gwAddr, http.ExpectedResponse{ |
| 102 | + Request: http.ExpectedRequest{ |
| 103 | + Method: "GET", |
| 104 | + Path: "/v2", |
| 105 | + }, |
| 106 | + StatusCode: 503, |
| 107 | + }) |
| 108 | + }) |
| 109 | + }, |
| 110 | +} |
0 commit comments