Files
higress/pkg/ingress/kube/gateway/istio/conditions_test.go
EndlessSeeker efaef2e3d0 Support configurable GatewayClass isolation (#3981)
Signed-off-by: EndlessSeeker <1766508902@qq.com>
2026-06-17 20:27:45 +08:00

152 lines
4.3 KiB
Go

// Copyright Istio Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package istio
import (
"reflect"
"testing"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
k8s "sigs.k8s.io/gateway-api/apis/v1beta1"
higressconstants "github.com/alibaba/higress/v2/pkg/config/constants"
"istio.io/istio/pkg/config"
"istio.io/istio/pkg/config/schema/gvk"
)
func TestCreateRouteStatus(t *testing.T) {
setGatewayClassNameForTest(t, "")
lastTransitionTime := metav1.Now()
parentRef := httpRouteSpec.ParentRefs[0]
parentStatus := []k8s.RouteParentStatus{
{
ParentRef: parentRef,
ControllerName: k8s.GatewayController("another-gateway-controller"),
Conditions: []metav1.Condition{
{Type: "foo", Status: "bar"},
},
},
{
ParentRef: parentRef,
ControllerName: k8s.GatewayController(higressconstants.ManagedGatewayController),
Conditions: []metav1.Condition{
{
Type: string(k8s.RouteReasonAccepted),
Status: metav1.ConditionTrue,
ObservedGeneration: 1,
LastTransitionTime: lastTransitionTime,
Message: "Route was valid",
},
{
Type: string(k8s.RouteConditionResolvedRefs),
Status: metav1.ConditionTrue,
ObservedGeneration: 1,
LastTransitionTime: lastTransitionTime,
Message: "All references resolved",
},
{
Type: string(RouteConditionResolvedWaypoints),
Status: metav1.ConditionTrue,
ObservedGeneration: 1,
LastTransitionTime: lastTransitionTime,
Message: "All waypoints resolved",
},
},
},
}
httpRoute := config.Config{
Meta: config.Meta{
GroupVersionKind: gvk.HTTPRoute,
Namespace: "foo",
Name: "bar",
Generation: 1,
},
Spec: &httpRouteSpec,
Status: &k8s.HTTPRouteStatus{
RouteStatus: k8s.RouteStatus{
Parents: parentStatus,
},
},
}
type args struct {
gateways []RouteParentResult
obj config.Config
current []k8s.RouteParentStatus
}
tests := []struct {
name string
args args
wantEqual bool
}{
{
name: "no error",
args: args{
gateways: []RouteParentResult{{OriginalReference: parentRef}},
obj: httpRoute,
current: parentStatus,
},
wantEqual: true,
},
{
name: "route status error",
args: args{
gateways: []RouteParentResult{{OriginalReference: parentRef, RouteError: &ConfigError{
Reason: ConfigErrorReason(k8s.RouteReasonRefNotPermitted),
}}},
obj: httpRoute,
current: parentStatus,
},
wantEqual: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := createRouteStatus(tt.args.gateways, "default", tt.args.obj.Generation, tt.args.current)
equal := reflect.DeepEqual(got, tt.args.current)
if equal != tt.wantEqual {
t.Errorf("route status: old: %+v, new: %+v", tt.args.current, got)
}
})
}
}
func TestCreateRouteStatusWithCustomController(t *testing.T) {
if runInGatewayClassSubprocess(t) {
return
}
setGatewayClassNameForTest(t, "higress-internal")
parentRef := httpRouteSpec.ParentRefs[0]
customController := k8s.GatewayController(managedGatewayController)
current := []k8s.RouteParentStatus{
{
ParentRef: parentRef,
ControllerName: k8s.GatewayController(higressconstants.ManagedGatewayController),
},
}
got := createRouteStatus([]RouteParentResult{{OriginalReference: parentRef}}, "default", 1, current)
if len(got) != 2 {
t.Fatalf("expected default and custom controller status entries, got %+v", got)
}
if got[0].ControllerName != k8s.GatewayController(higressconstants.ManagedGatewayController) {
t.Fatalf("expected existing default controller status to be preserved, got %+v", got)
}
if got[1].ControllerName != customController {
t.Fatalf("expected custom controller status %q, got %+v", customController, got)
}
}