...
1
16
17 package validation
18
19 import (
20 "testing"
21
22 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
23
24 gatewayv1 "sigs.k8s.io/gateway-api/apis/v1"
25 gatewayv1a2 "sigs.k8s.io/gateway-api/apis/v1alpha2"
26 )
27
28 func TestValidateGateway(t *testing.T) {
29 listeners := []gatewayv1a2.Listener{
30 {
31 Hostname: nil,
32 },
33 }
34 addresses := []gatewayv1a2.GatewayAddress{
35 {
36 Type: nil,
37 },
38 }
39 baseGateway := gatewayv1a2.Gateway{
40 ObjectMeta: metav1.ObjectMeta{
41 Name: "foo",
42 Namespace: metav1.NamespaceDefault,
43 },
44 Spec: gatewayv1a2.GatewaySpec{
45 GatewayClassName: "foo",
46 Listeners: listeners,
47 Addresses: addresses,
48 },
49 }
50
51 testCases := map[string]struct {
52 mutate func(gw *gatewayv1a2.Gateway)
53 expectErrsOnFields []string
54 }{
55 "hostname present with tcp protocol": {
56 mutate: func(gw *gatewayv1a2.Gateway) {
57 hostname := gatewayv1a2.Hostname("foo.bar.com")
58 gw.Spec.Listeners[0].Hostname = &hostname
59 gw.Spec.Listeners[0].Protocol = gatewayv1.TCPProtocolType
60 },
61 expectErrsOnFields: []string{"spec.listeners[0].hostname"},
62 },
63 "hostname present with udp protocol": {
64 mutate: func(gw *gatewayv1a2.Gateway) {
65 hostname := gatewayv1a2.Hostname("foo.bar.com")
66 gw.Spec.Listeners[0].Hostname = &hostname
67 gw.Spec.Listeners[0].Protocol = gatewayv1.UDPProtocolType
68 },
69 expectErrsOnFields: []string{"spec.listeners[0].hostname"},
70 },
71 }
72
73 for name, tc := range testCases {
74 tc := tc
75 t.Run(name, func(t *testing.T) {
76 gw := baseGateway.DeepCopy()
77 tc.mutate(gw)
78 errs := ValidateGateway(gw)
79 if len(tc.expectErrsOnFields) != len(errs) {
80 t.Fatalf("Expected %d errors, got %d errors: %v", len(tc.expectErrsOnFields), len(errs), errs)
81 }
82 for i, err := range errs {
83 if err.Field != tc.expectErrsOnFields[i] {
84 t.Errorf("Expected error on field: %s, got: %s", tc.expectErrsOnFields[i], err.Error())
85 }
86 }
87 })
88 }
89 }
90
View as plain text