|
| 1 | +package cel |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "testing" |
| 6 | + |
| 7 | + . "github.com/onsi/gomega" |
| 8 | + controllerruntime "sigs.k8s.io/controller-runtime" |
| 9 | + "sigs.k8s.io/controller-runtime/pkg/client" |
| 10 | + gatewayv1alpha2 "sigs.k8s.io/gateway-api/apis/v1alpha2" |
| 11 | + |
| 12 | + ngfAPIv1alpha1 "github.com/nginx/nginx-gateway-fabric/v2/apis/v1alpha1" |
| 13 | + "github.com/nginx/nginx-gateway-fabric/v2/tests/framework" |
| 14 | +) |
| 15 | + |
| 16 | +func TestClientSettingsPoliciesTargetRefKind(t *testing.T) { |
| 17 | + t.Parallel() |
| 18 | + g := NewWithT(t) |
| 19 | + k8sClient, err := getKubernetesClient(t) |
| 20 | + g.Expect(err).ToNot(HaveOccurred()) |
| 21 | + tests := []struct { |
| 22 | + policySpec ngfAPIv1alpha1.ClientSettingsPolicySpec |
| 23 | + name string |
| 24 | + wantErrors []string |
| 25 | + }{ |
| 26 | + { |
| 27 | + name: "Validate TargetRef of kind Gateway is allowed", |
| 28 | + policySpec: ngfAPIv1alpha1.ClientSettingsPolicySpec{ |
| 29 | + TargetRef: gatewayv1alpha2.LocalPolicyTargetReference{ |
| 30 | + Kind: gatewayKind, |
| 31 | + Group: gatewayGroup, |
| 32 | + }, |
| 33 | + }, |
| 34 | + }, |
| 35 | + { |
| 36 | + name: "Validate TargetRef of kind HTTPRoute is allowed", |
| 37 | + policySpec: ngfAPIv1alpha1.ClientSettingsPolicySpec{ |
| 38 | + TargetRef: gatewayv1alpha2.LocalPolicyTargetReference{ |
| 39 | + Kind: httpRouteKind, |
| 40 | + Group: gatewayGroup, |
| 41 | + }, |
| 42 | + }, |
| 43 | + }, |
| 44 | + { |
| 45 | + name: "Validate TargetRef of kind GRPCRoute is allowed", |
| 46 | + policySpec: ngfAPIv1alpha1.ClientSettingsPolicySpec{ |
| 47 | + TargetRef: gatewayv1alpha2.LocalPolicyTargetReference{ |
| 48 | + Kind: grpcRouteKind, |
| 49 | + Group: gatewayGroup, |
| 50 | + }, |
| 51 | + }, |
| 52 | + }, |
| 53 | + { |
| 54 | + name: "Validate Invalid TargetRef Kind is not allowed", |
| 55 | + wantErrors: []string{expectedTargetRefKindError}, |
| 56 | + policySpec: ngfAPIv1alpha1.ClientSettingsPolicySpec{ |
| 57 | + TargetRef: gatewayv1alpha2.LocalPolicyTargetReference{ |
| 58 | + Kind: invalidKind, |
| 59 | + Group: gatewayGroup, |
| 60 | + }, |
| 61 | + }, |
| 62 | + }, |
| 63 | + { |
| 64 | + name: "Validate TCPRoute TargetRef Kind is not allowed", |
| 65 | + wantErrors: []string{expectedTargetRefKindError}, |
| 66 | + policySpec: ngfAPIv1alpha1.ClientSettingsPolicySpec{ |
| 67 | + TargetRef: gatewayv1alpha2.LocalPolicyTargetReference{ |
| 68 | + Kind: tcpRouteKind, |
| 69 | + Group: gatewayGroup, |
| 70 | + }, |
| 71 | + }, |
| 72 | + }, |
| 73 | + } |
| 74 | + |
| 75 | + for _, tt := range tests { |
| 76 | + t.Run(tt.name, func(t *testing.T) { |
| 77 | + t.Parallel() |
| 78 | + validateClientSettingsPolicy(t, tt, g, k8sClient) |
| 79 | + }) |
| 80 | + } |
| 81 | +} |
| 82 | + |
| 83 | +func TestClientSettingsPoliciesTargetRefGroup(t *testing.T) { |
| 84 | + t.Parallel() |
| 85 | + g := NewWithT(t) |
| 86 | + k8sClient, err := getKubernetesClient(t) |
| 87 | + g.Expect(err).ToNot(HaveOccurred()) |
| 88 | + tests := []struct { |
| 89 | + policySpec ngfAPIv1alpha1.ClientSettingsPolicySpec |
| 90 | + name string |
| 91 | + wantErrors []string |
| 92 | + }{ |
| 93 | + { |
| 94 | + name: "Validate gateway.networking.k8s.io TargetRef Group is allowed", |
| 95 | + policySpec: ngfAPIv1alpha1.ClientSettingsPolicySpec{ |
| 96 | + TargetRef: gatewayv1alpha2.LocalPolicyTargetReference{ |
| 97 | + Kind: gatewayKind, |
| 98 | + Group: gatewayGroup, |
| 99 | + }, |
| 100 | + }, |
| 101 | + }, |
| 102 | + { |
| 103 | + name: "Validate invalid.networking.k8s.io TargetRef Group is not allowed", |
| 104 | + wantErrors: []string{expectedTargetRefGroupError}, |
| 105 | + policySpec: ngfAPIv1alpha1.ClientSettingsPolicySpec{ |
| 106 | + TargetRef: gatewayv1alpha2.LocalPolicyTargetReference{ |
| 107 | + Kind: gatewayKind, |
| 108 | + Group: invalidGroup, |
| 109 | + }, |
| 110 | + }, |
| 111 | + }, |
| 112 | + { |
| 113 | + name: "Validate discovery.k8s.io/v1 TargetRef Group is not allowed", |
| 114 | + wantErrors: []string{expectedTargetRefGroupError}, |
| 115 | + policySpec: ngfAPIv1alpha1.ClientSettingsPolicySpec{ |
| 116 | + TargetRef: gatewayv1alpha2.LocalPolicyTargetReference{ |
| 117 | + Kind: gatewayKind, |
| 118 | + Group: discoveryGroup, |
| 119 | + }, |
| 120 | + }, |
| 121 | + }, |
| 122 | + } |
| 123 | + |
| 124 | + for _, tt := range tests { |
| 125 | + t.Run(tt.name, func(t *testing.T) { |
| 126 | + t.Parallel() |
| 127 | + validateClientSettingsPolicy(t, tt, g, k8sClient) |
| 128 | + }) |
| 129 | + } |
| 130 | +} |
| 131 | + |
| 132 | +func validateClientSettingsPolicy(t *testing.T, tt struct { |
| 133 | + policySpec ngfAPIv1alpha1.ClientSettingsPolicySpec |
| 134 | + name string |
| 135 | + wantErrors []string |
| 136 | +}, g *WithT, k8sClient client.Client, |
| 137 | +) { |
| 138 | + t.Helper() |
| 139 | + |
| 140 | + policySpec := tt.policySpec |
| 141 | + policySpec.TargetRef.Name = gatewayv1alpha2.ObjectName(uniqueResourceName(testTargetRefName)) |
| 142 | + policyName := uniqueResourceName(testPolicyName) |
| 143 | + |
| 144 | + clientSettingsPolicy := &ngfAPIv1alpha1.ClientSettingsPolicy{ |
| 145 | + ObjectMeta: controllerruntime.ObjectMeta{ |
| 146 | + Name: policyName, |
| 147 | + Namespace: defaultNamespace, |
| 148 | + }, |
| 149 | + Spec: policySpec, |
| 150 | + } |
| 151 | + timeoutConfig := framework.DefaultTimeoutConfig() |
| 152 | + ctx, cancel := context.WithTimeout(context.Background(), timeoutConfig.KubernetesClientTimeout) |
| 153 | + err := k8sClient.Create(ctx, clientSettingsPolicy) |
| 154 | + defer cancel() |
| 155 | + |
| 156 | + // Clean up after test |
| 157 | + defer func() { |
| 158 | + _ = k8sClient.Delete(context.Background(), clientSettingsPolicy) |
| 159 | + }() |
| 160 | + |
| 161 | + if len(tt.wantErrors) == 0 { |
| 162 | + g.Expect(err).ToNot(HaveOccurred()) |
| 163 | + } else { |
| 164 | + g.Expect(err).To(HaveOccurred()) |
| 165 | + for _, wantError := range tt.wantErrors { |
| 166 | + g.Expect(err.Error()).To(ContainSubstring(wantError), "Expected error '%s' not found in: %s", wantError, err.Error()) |
| 167 | + } |
| 168 | + } |
| 169 | +} |
0 commit comments