This repository has been archived by the owner on Dec 16, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e6f428c
commit a225537
Showing
4 changed files
with
361 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,331 @@ | ||
package utils | ||
|
||
import ( | ||
"reflect" | ||
"testing" | ||
|
||
"sigs.k8s.io/gateway-api/apis/v1beta1" | ||
|
||
testutil "github.com/Kuadrant/multicluster-gateway-controller/test/util" | ||
) | ||
|
||
func TestAddressTypeToMultiCluster(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
addressType v1beta1.AddressType | ||
wantAddressType v1beta1.AddressType | ||
converted bool | ||
}{ | ||
{ | ||
name: "supported IP address", | ||
addressType: v1beta1.IPAddressType, | ||
wantAddressType: MultiClusterIPAddressType, | ||
converted: true, | ||
}, | ||
{ | ||
name: "supported host address", | ||
addressType: v1beta1.HostnameAddressType, | ||
wantAddressType: MultiClusterHostnameAddressType, | ||
converted: true, | ||
}, | ||
{ | ||
name: "not supported address", | ||
addressType: v1beta1.NamedAddressType, | ||
wantAddressType: v1beta1.NamedAddressType, | ||
converted: false, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
gotAddressType, converted := AddressTypeToMultiCluster(v1beta1.GatewayAddress{Type: &tt.addressType}) | ||
if gotAddressType != tt.wantAddressType { | ||
t.Errorf("AddressTypeToMultiCluster() got = %v, want %v", gotAddressType, tt.wantAddressType) | ||
} | ||
if converted != tt.converted { | ||
t.Errorf("AddressTypeToMultiCluster() got1 = %v, want %v", converted, tt.converted) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func TestAddressTypeToSingleCluster(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
addressType v1beta1.AddressType | ||
wantAddressType v1beta1.AddressType | ||
converted bool | ||
}{ | ||
{ | ||
name: "supported IP address", | ||
addressType: MultiClusterIPAddressType, | ||
wantAddressType: v1beta1.IPAddressType, | ||
converted: true, | ||
}, | ||
{ | ||
name: "supported host address", | ||
addressType: MultiClusterHostnameAddressType, | ||
wantAddressType: v1beta1.HostnameAddressType, | ||
converted: true, | ||
}, | ||
{ | ||
name: "not supported address", | ||
addressType: v1beta1.NamedAddressType, | ||
wantAddressType: v1beta1.NamedAddressType, | ||
converted: false, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
gotAddressType, converted := AddressTypeToSingleCluster(v1beta1.GatewayAddress{Type: &tt.addressType}) | ||
if gotAddressType != tt.wantAddressType { | ||
t.Errorf("AddressTypeToMultiCluster() got = %v, want %v", gotAddressType, tt.wantAddressType) | ||
} | ||
if converted != tt.converted { | ||
t.Errorf("AddressTypeToMultiCluster() got1 = %v, want %v", converted, tt.converted) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func TestGatewayWrapper_GetClusterGatewayAddresses(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
Gateway *v1beta1.Gateway | ||
want map[string][]v1beta1.GatewayAddress | ||
}{ | ||
{ | ||
name: "single cluster Gateway", | ||
Gateway: &v1beta1.Gateway{ | ||
Status: v1beta1.GatewayStatus{ | ||
Addresses: []v1beta1.GatewayAddress{ | ||
{ | ||
Type: testutil.Pointer(v1beta1.IPAddressType), | ||
Value: "1.1.1.1", | ||
}, | ||
}, | ||
}, | ||
}, | ||
want: map[string][]v1beta1.GatewayAddress{ | ||
SingleClusterAddressValue: { | ||
{ | ||
Type: testutil.Pointer(v1beta1.IPAddressType), | ||
Value: "1.1.1.1", | ||
}, | ||
}, | ||
}, | ||
}, | ||
{ | ||
name: "multi cluster Gateway", | ||
Gateway: &v1beta1.Gateway{ | ||
Status: v1beta1.GatewayStatus{ | ||
Addresses: []v1beta1.GatewayAddress{ | ||
{ | ||
Type: testutil.Pointer(MultiClusterIPAddressType), | ||
Value: "kind-mgc-control-plane/1.1.1.1", | ||
}, | ||
{ | ||
Type: testutil.Pointer(MultiClusterIPAddressType), | ||
Value: "kind-mgc-workload-1/2.2.2.2", | ||
}, | ||
{ | ||
Type: testutil.Pointer(MultiClusterHostnameAddressType), | ||
Value: "kind-mgc-workload-1/boop.com", | ||
}, | ||
}, | ||
}, | ||
}, | ||
want: map[string][]v1beta1.GatewayAddress{ | ||
"kind-mgc-control-plane": { | ||
{ | ||
Type: testutil.Pointer(MultiClusterIPAddressType), | ||
Value: "1.1.1.1", | ||
}, | ||
}, | ||
"kind-mgc-workload-1": { | ||
{ | ||
Type: testutil.Pointer(MultiClusterIPAddressType), | ||
Value: "2.2.2.2", | ||
}, | ||
{ | ||
Type: testutil.Pointer(MultiClusterHostnameAddressType), | ||
Value: "boop.com", | ||
}, | ||
}, | ||
}, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
g := NewGatewayWrapper(tt.Gateway) | ||
if got := g.GetClusterGatewayAddresses(); !reflect.DeepEqual(got, tt.want) { | ||
t.Errorf("GetClusterGatewayAddresses() = %v, want %v", got, tt.want) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func TestGatewayWrapper_ListenerTotalAttachedRoutes(t *testing.T) { | ||
|
||
tests := []struct { | ||
name string | ||
Gateway *v1beta1.Gateway | ||
downstreamClusterName string | ||
want int | ||
}{ | ||
{ | ||
name: "single cluster gateway", | ||
Gateway: &v1beta1.Gateway{ | ||
Spec: v1beta1.GatewaySpec{ | ||
Listeners: []v1beta1.Listener{ | ||
{ | ||
Name: "api", | ||
}, | ||
}, | ||
}, | ||
Status: v1beta1.GatewayStatus{ | ||
Listeners: []v1beta1.ListenerStatus{ | ||
{ | ||
Name: "api", | ||
AttachedRoutes: 1, | ||
}, | ||
}, | ||
}, | ||
}, | ||
downstreamClusterName: SingleClusterAddressValue, | ||
want: 1, | ||
}, | ||
{ | ||
name: "multi cluster gateway", | ||
Gateway: &v1beta1.Gateway{ | ||
Spec: v1beta1.GatewaySpec{ | ||
Listeners: []v1beta1.Listener{ | ||
{ | ||
Name: "api", | ||
}, | ||
}, | ||
}, | ||
Status: v1beta1.GatewayStatus{ | ||
Addresses: []v1beta1.GatewayAddress{ | ||
{ | ||
Type: testutil.Pointer(MultiClusterIPAddressType), | ||
Value: "kind-mgc-control-plane/1.1.1.1", | ||
}, | ||
}, | ||
Listeners: []v1beta1.ListenerStatus{ | ||
{ | ||
Name: "kind-mgc-control-plane.api", | ||
AttachedRoutes: 1, | ||
}, | ||
}, | ||
}, | ||
}, | ||
downstreamClusterName: "kind-mgc-control-plane", | ||
want: 1, | ||
}, | ||
{ | ||
name: "invalid status listener name", | ||
Gateway: &v1beta1.Gateway{ | ||
Spec: v1beta1.GatewaySpec{ | ||
Listeners: []v1beta1.Listener{ | ||
{ | ||
Name: "api", | ||
}, | ||
}, | ||
}, | ||
Status: v1beta1.GatewayStatus{ | ||
Addresses: []v1beta1.GatewayAddress{ | ||
{ | ||
Type: testutil.Pointer(MultiClusterIPAddressType), | ||
Value: "kind-mgc-control-plane/1.1.1.1", | ||
}, | ||
}, | ||
Listeners: []v1beta1.ListenerStatus{ | ||
{ | ||
Name: "kind-mgc-control-plane-api", | ||
AttachedRoutes: 1, | ||
}, | ||
}, | ||
}, | ||
}, | ||
want: 0, | ||
}, | ||
{ | ||
name: "no status", | ||
Gateway: &v1beta1.Gateway{ | ||
Spec: v1beta1.GatewaySpec{ | ||
Listeners: []v1beta1.Listener{ | ||
{ | ||
Name: "api", | ||
}, | ||
}, | ||
}, | ||
}, | ||
want: 0, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
g := NewGatewayWrapper(tt.Gateway) | ||
if got := g.ListenerTotalAttachedRoutes(tt.downstreamClusterName, tt.Gateway.Spec.Listeners[0]); got != tt.want { | ||
t.Errorf("ListenerTotalAttachedRoutes() = %v, want %v", got, tt.want) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func TestGatewayWrapper_Validate(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
Gateway *v1beta1.Gateway | ||
wantErr bool | ||
}{ | ||
{ | ||
name: "Valid Gateway", | ||
Gateway: &v1beta1.Gateway{ | ||
Status: v1beta1.GatewayStatus{ | ||
Addresses: []v1beta1.GatewayAddress{ | ||
{ | ||
Type: testutil.Pointer(MultiClusterIPAddressType), | ||
Value: "kind-mgc-control-plane/1.1.1.1", | ||
}, | ||
{ | ||
Type: testutil.Pointer(MultiClusterIPAddressType), | ||
Value: "kind-mgc-workload-1/2.2.2.2", | ||
}, | ||
{ | ||
Type: testutil.Pointer(MultiClusterHostnameAddressType), | ||
Value: "kind-mgc-workload-1/boop.com", | ||
}, | ||
}, | ||
}, | ||
}, | ||
wantErr: false, | ||
}, | ||
{ | ||
name: "invalid Gateway: inconsistent addresses", | ||
Gateway: &v1beta1.Gateway{ | ||
Status: v1beta1.GatewayStatus{ | ||
Addresses: []v1beta1.GatewayAddress{ | ||
{ | ||
Type: testutil.Pointer(MultiClusterIPAddressType), | ||
Value: "kind-mgc-control-plane/1.1.1.1", | ||
}, | ||
{ | ||
Type: testutil.Pointer(v1beta1.NamedAddressType), | ||
Value: "kind-mgc-workload-1/boop.com", | ||
}, | ||
}, | ||
}, | ||
}, | ||
wantErr: true, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
g := NewGatewayWrapper(tt.Gateway) | ||
if err := g.Validate(); (err != nil) != tt.wantErr { | ||
t.Errorf("Validate() error = %v, wantErr %v", err, tt.wantErr) | ||
} | ||
}) | ||
} | ||
} |
Oops, something went wrong.