|
| 1 | +package clientsettingspolicies |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + |
| 6 | + gatewayv1alpha2 "sigs.k8s.io/gateway-api/apis/v1alpha2" |
| 7 | +) |
| 8 | + |
| 9 | +func TestClientSettingsPoliciesTargetRef(t *testing.T) { |
| 10 | + |
| 11 | + allowedKinds := map[string]bool{ |
| 12 | + "Gateway": true, |
| 13 | + "HTTPRoute": true, |
| 14 | + "GRPCRoute": true, |
| 15 | + } |
| 16 | + |
| 17 | + testInvalidTargetRefs(t, allowedKinds) |
| 18 | + testValidTargetRefs(t, allowedKinds) |
| 19 | +} |
| 20 | + |
| 21 | +func testInvalidTargetRefs(t *testing.T, allowedKinds map[string]bool) { |
| 22 | + t.Helper() |
| 23 | + |
| 24 | + tests := []struct { |
| 25 | + name string |
| 26 | + wantErrors string |
| 27 | + targetRef gatewayv1alpha2.LocalPolicyTargetReference |
| 28 | + }{ |
| 29 | + { |
| 30 | + name: "Validate TargetRef is of an allowed kind", |
| 31 | + wantErrors: "TargetRef Kind must be one of: Gateway, HTTPRoute, or GRPCRoute'", |
| 32 | + targetRef: gatewayv1alpha2.LocalPolicyTargetReference{ |
| 33 | + Kind: "InvalidKind", |
| 34 | + Group: "gateway.networking.k8s.io", |
| 35 | + }, |
| 36 | + }, |
| 37 | + { |
| 38 | + name: "Validate TargetRef is of an allowed kind", |
| 39 | + wantErrors: "TargetRef Kind must be one of: Gateway, HTTPRoute, or GRPCRoute'", |
| 40 | + targetRef: gatewayv1alpha2.LocalPolicyTargetReference{ |
| 41 | + Kind: "TCPRoute", |
| 42 | + Group: "gateway.networking.k8s.io", |
| 43 | + }, |
| 44 | + }, |
| 45 | + } |
| 46 | + |
| 47 | + for _, tt := range tests { |
| 48 | + t.Run(tt.name, func(t *testing.T) { |
| 49 | + |
| 50 | + if _, ok := allowedKinds[string(tt.targetRef.Kind)]; !ok { |
| 51 | + gotError := "TargetRef Kind must be one of: Gateway, HTTPRoute, or GRPCRoute'" |
| 52 | + |
| 53 | + if tt.wantErrors != gotError { |
| 54 | + t.Errorf("Test %s failed: got error %q, want %q", tt.name, gotError, tt.wantErrors) |
| 55 | + } |
| 56 | + } |
| 57 | + }) |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | +func testValidTargetRefs(t *testing.T, allowedKinds map[string]bool) { |
| 62 | + t.Helper() |
| 63 | + |
| 64 | + tests := []struct { |
| 65 | + name string |
| 66 | + wantErrors string |
| 67 | + targetRef gatewayv1alpha2.LocalPolicyTargetReference |
| 68 | + }{ |
| 69 | + { |
| 70 | + name: "Validate TargetRef is of an allowed kind", |
| 71 | + wantErrors: "TargetRef Kind must be one of: Gateway, HTTPRoute, or GRPCRoute'", |
| 72 | + targetRef: gatewayv1alpha2.LocalPolicyTargetReference{ |
| 73 | + Kind: "Gateway", |
| 74 | + Group: "gateway.networking.k8s.io", |
| 75 | + }, |
| 76 | + }, |
| 77 | + { |
| 78 | + name: "Validate TargetRef is of an allowed kind", |
| 79 | + wantErrors: "TargetRef Kind must be one of: Gateway, HTTPRoute, or GRPCRoute'", |
| 80 | + targetRef: gatewayv1alpha2.LocalPolicyTargetReference{ |
| 81 | + Kind: "HTTPRoute", |
| 82 | + Group: "gateway.networking.k8s.io", |
| 83 | + }, |
| 84 | + }, |
| 85 | + { |
| 86 | + name: "Validate TargetRef is of an allowed kind", |
| 87 | + wantErrors: "TargetRef Kind must be one of: Gateway, HTTPRoute, or GRPCRoute'", |
| 88 | + targetRef: gatewayv1alpha2.LocalPolicyTargetReference{ |
| 89 | + Kind: "GRPCRoute", |
| 90 | + Group: "gateway.networking.k8s.io", |
| 91 | + }, |
| 92 | + }, |
| 93 | + } |
| 94 | + |
| 95 | + for _, tt := range tests { |
| 96 | + t.Run(tt.name, func(t *testing.T) { |
| 97 | + |
| 98 | + if _, ok := allowedKinds[string(tt.targetRef.Kind)]; !ok { |
| 99 | + gotError := "TargetRef Kind must be one of: Gateway, HTTPRoute, or GRPCRoute'" |
| 100 | + |
| 101 | + if tt.wantErrors == gotError { |
| 102 | + t.Errorf("Test %s failed: got error %q, want %q", tt.name, gotError, tt.wantErrors) |
| 103 | + } |
| 104 | + } |
| 105 | + }) |
| 106 | + } |
| 107 | +} |
0 commit comments