|
| 1 | +// Licensed to the Apache Software Foundation (ASF) under one |
| 2 | +// or more contributor license agreements. See the NOTICE file |
| 3 | +// distributed with this work for additional information |
| 4 | +// regarding copyright ownership. The ASF licenses this file |
| 5 | +// to you under the Apache License, Version 2.0 (the |
| 6 | +// "License"); you may not use this file except in compliance |
| 7 | +// with the License. You may obtain a copy of the License at |
| 8 | +// |
| 9 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +// |
| 11 | +// Unless required by applicable law or agreed to in writing, |
| 12 | +// software distributed under the License is distributed on an |
| 13 | +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +// KIND, either express or implied. See the License for the |
| 15 | +// specific language governing permissions and limitations |
| 16 | +// under the License. |
| 17 | + |
| 18 | +package controller |
| 19 | + |
| 20 | +import ( |
| 21 | + "context" |
| 22 | + "testing" |
| 23 | + |
| 24 | + "github.com/stretchr/testify/assert" |
| 25 | + "github.com/stretchr/testify/require" |
| 26 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 27 | + "sigs.k8s.io/controller-runtime/pkg/client" |
| 28 | + "sigs.k8s.io/controller-runtime/pkg/client/fake" |
| 29 | + gatewayv1 "sigs.k8s.io/gateway-api/apis/v1" |
| 30 | + |
| 31 | + "github.com/apache/apisix-ingress-controller/internal/controller/indexer" |
| 32 | +) |
| 33 | + |
| 34 | +// A listener whose protocol the implementation does not serve must say so with |
| 35 | +// UnsupportedProtocol and advertise no route kinds, rather than be accepted and |
| 36 | +// then quietly serve nothing. |
| 37 | +func TestGetListenerStatus_UnsupportedProtocol(t *testing.T) { |
| 38 | + scheme := parentRefTestScheme(t) |
| 39 | + |
| 40 | + for _, tc := range []struct { |
| 41 | + name string |
| 42 | + listener gatewayv1.Listener |
| 43 | + wantStatus metav1.ConditionStatus |
| 44 | + wantReason gatewayv1.ListenerConditionReason |
| 45 | + wantKinds int |
| 46 | + }{ |
| 47 | + { |
| 48 | + name: "unknown protocol", |
| 49 | + listener: gatewayv1.Listener{ |
| 50 | + Name: "invalid", Port: 1111, Protocol: gatewayv1.ProtocolType("INVALID"), |
| 51 | + }, |
| 52 | + wantStatus: metav1.ConditionFalse, |
| 53 | + wantReason: gatewayv1.ListenerReasonUnsupportedProtocol, |
| 54 | + wantKinds: 0, |
| 55 | + }, |
| 56 | + { |
| 57 | + name: "known protocol", |
| 58 | + listener: gatewayv1.Listener{ |
| 59 | + Name: "http", Port: 80, Protocol: gatewayv1.HTTPProtocolType, |
| 60 | + }, |
| 61 | + wantStatus: metav1.ConditionTrue, |
| 62 | + wantReason: gatewayv1.ListenerReasonAccepted, |
| 63 | + wantKinds: 2, |
| 64 | + }, |
| 65 | + } { |
| 66 | + t.Run(tc.name, func(t *testing.T) { |
| 67 | + gw := &gatewayv1.Gateway{ |
| 68 | + ObjectMeta: metav1.ObjectMeta{Namespace: "default", Name: "gw"}, |
| 69 | + Spec: gatewayv1.GatewaySpec{ |
| 70 | + GatewayClassName: "apisix", |
| 71 | + Listeners: []gatewayv1.Listener{tc.listener}, |
| 72 | + }, |
| 73 | + } |
| 74 | + cli := fake.NewClientBuilder().WithScheme(scheme). |
| 75 | + WithObjects(newParentRefGatewayClass(), gw). |
| 76 | + // An HTTP listener counts both route kinds it can serve, so both |
| 77 | + // indexes have to exist even though no route does. |
| 78 | + WithIndex(&gatewayv1.HTTPRoute{}, indexer.ParentRefs, |
| 79 | + func(client.Object) []string { return nil }). |
| 80 | + WithIndex(&gatewayv1.GRPCRoute{}, indexer.ParentRefs, |
| 81 | + func(client.Object) []string { return nil }). |
| 82 | + Build() |
| 83 | + |
| 84 | + statuses, err := getListenerStatus(context.Background(), cli, gw) |
| 85 | + require.NoError(t, err) |
| 86 | + require.Len(t, statuses, 1) |
| 87 | + |
| 88 | + var accepted *metav1.Condition |
| 89 | + for i := range statuses[0].Conditions { |
| 90 | + if statuses[0].Conditions[i].Type == string(gatewayv1.ListenerConditionAccepted) { |
| 91 | + accepted = &statuses[0].Conditions[i] |
| 92 | + } |
| 93 | + } |
| 94 | + require.NotNil(t, accepted, "listener must report an Accepted condition") |
| 95 | + assert.Equal(t, tc.wantStatus, accepted.Status) |
| 96 | + assert.Equal(t, string(tc.wantReason), accepted.Reason) |
| 97 | + assert.Len(t, statuses[0].SupportedKinds, tc.wantKinds) |
| 98 | + assert.Zero(t, statuses[0].AttachedRoutes) |
| 99 | + }) |
| 100 | + } |
| 101 | +} |
| 102 | + |
| 103 | +// The Gateway's own Accepted condition follows its listeners: one bad listener |
| 104 | +// is enough for ListenersNotValid, and the status separates "some listeners |
| 105 | +// work" from "none do". |
| 106 | +func TestGatewayAcceptanceFromListeners(t *testing.T) { |
| 107 | + listener := func(name string, accepted bool) gatewayv1.ListenerStatus { |
| 108 | + status := metav1.ConditionTrue |
| 109 | + if !accepted { |
| 110 | + status = metav1.ConditionFalse |
| 111 | + } |
| 112 | + return gatewayv1.ListenerStatus{ |
| 113 | + Name: gatewayv1.SectionName(name), |
| 114 | + Conditions: []metav1.Condition{{ |
| 115 | + Type: string(gatewayv1.ListenerConditionAccepted), |
| 116 | + Status: status, |
| 117 | + }}, |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + for _, tc := range []struct { |
| 122 | + name string |
| 123 | + listeners []gatewayv1.ListenerStatus |
| 124 | + wantInvalid bool |
| 125 | + wantStatus bool |
| 126 | + }{ |
| 127 | + { |
| 128 | + name: "all listeners accepted", |
| 129 | + listeners: []gatewayv1.ListenerStatus{listener("http", true), listener("https", true)}, |
| 130 | + wantInvalid: false, |
| 131 | + }, |
| 132 | + { |
| 133 | + name: "one of two listeners rejected", |
| 134 | + listeners: []gatewayv1.ListenerStatus{listener("http", true), listener("invalid", false)}, |
| 135 | + wantInvalid: true, |
| 136 | + wantStatus: true, |
| 137 | + }, |
| 138 | + { |
| 139 | + name: "every listener rejected", |
| 140 | + listeners: []gatewayv1.ListenerStatus{listener("invalid", false)}, |
| 141 | + wantInvalid: true, |
| 142 | + wantStatus: false, |
| 143 | + }, |
| 144 | + { |
| 145 | + name: "no listeners", |
| 146 | + listeners: nil, |
| 147 | + wantInvalid: false, |
| 148 | + }, |
| 149 | + } { |
| 150 | + t.Run(tc.name, func(t *testing.T) { |
| 151 | + status, invalid := gatewayAcceptanceFromListeners(tc.listeners) |
| 152 | + assert.Equal(t, tc.wantInvalid, invalid) |
| 153 | + if invalid { |
| 154 | + assert.Equal(t, tc.wantStatus, status) |
| 155 | + } |
| 156 | + }) |
| 157 | + } |
| 158 | +} |
| 159 | + |
| 160 | +// Accepted=False used to report Reason=Accepted, which says nothing about what |
| 161 | +// went wrong. |
| 162 | +func TestSetGatewayConditionAccepted_Reason(t *testing.T) { |
| 163 | + gw := &gatewayv1.Gateway{ObjectMeta: metav1.ObjectMeta{Namespace: "default", Name: "gw"}} |
| 164 | + |
| 165 | + SetGatewayConditionAccepted(gw, false, gatewayv1.GatewayReasonInvalidParameters, "parametersRef is not resolvable") |
| 166 | + |
| 167 | + require.Len(t, gw.Status.Conditions, 1) |
| 168 | + assert.Equal(t, string(gatewayv1.GatewayConditionAccepted), gw.Status.Conditions[0].Type) |
| 169 | + assert.Equal(t, metav1.ConditionFalse, gw.Status.Conditions[0].Status) |
| 170 | + assert.Equal(t, string(gatewayv1.GatewayReasonInvalidParameters), gw.Status.Conditions[0].Reason) |
| 171 | +} |
0 commit comments