Skip to content

Commit 8404b7d

Browse files
committed
Move validation function to common.go
1 parent 8455a14 commit 8404b7d

File tree

3 files changed

+78
-88
lines changed

3 files changed

+78
-88
lines changed

tests/cel/clientsettingspolicy_test.go

Lines changed: 30 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,14 @@
11
package cel
22

33
import (
4-
"context"
54
"testing"
65

76
. "github.com/onsi/gomega"
87
controllerruntime "sigs.k8s.io/controller-runtime"
9-
"sigs.k8s.io/controller-runtime/pkg/client"
108
gatewayv1alpha2 "sigs.k8s.io/gateway-api/apis/v1alpha2"
119

1210
ngfAPIv1alpha1 "github.com/nginx/nginx-gateway-fabric/v2/apis/v1alpha1"
1311
"github.com/nginx/nginx-gateway-fabric/v2/internal/framework/helpers"
14-
"github.com/nginx/nginx-gateway-fabric/v2/tests/framework"
1512
)
1613

1714
func TestClientSettingsPoliciesTargetRefKind(t *testing.T) {
@@ -78,7 +75,16 @@ func TestClientSettingsPoliciesTargetRefKind(t *testing.T) {
7875
for _, tt := range tests {
7976
t.Run(tt.name, func(t *testing.T) {
8077
t.Parallel()
81-
validateClientSettingsPolicy(t, tt, g, k8sClient)
78+
policySpec := tt.policySpec
79+
policySpec.TargetRef.Name = gatewayv1alpha2.ObjectName(uniqueResourceName(testTargetRefName))
80+
clientSettingsPolicy := &ngfAPIv1alpha1.ClientSettingsPolicy{
81+
ObjectMeta: controllerruntime.ObjectMeta{
82+
Name: uniqueResourceName(testPolicyName),
83+
Namespace: defaultNamespace,
84+
},
85+
Spec: policySpec,
86+
}
87+
validateCrd(t, tt.wantErrors, g, clientSettingsPolicy, k8sClient)
8288
})
8389
}
8490
}
@@ -129,7 +135,16 @@ func TestClientSettingsPoliciesTargetRefGroup(t *testing.T) {
129135
for _, tt := range tests {
130136
t.Run(tt.name, func(t *testing.T) {
131137
t.Parallel()
132-
validateClientSettingsPolicy(t, tt, g, k8sClient)
138+
policySpec := tt.policySpec
139+
policySpec.TargetRef.Name = gatewayv1alpha2.ObjectName(uniqueResourceName(testTargetRefName))
140+
clientSettingsPolicy := &ngfAPIv1alpha1.ClientSettingsPolicy{
141+
ObjectMeta: controllerruntime.ObjectMeta{
142+
Name: uniqueResourceName(testPolicyName),
143+
Namespace: defaultNamespace,
144+
},
145+
Spec: policySpec,
146+
}
147+
validateCrd(t, tt.wantErrors, g, clientSettingsPolicy, k8sClient)
133148
})
134149
}
135150
}
@@ -191,46 +206,16 @@ func TestClientSettingsPoliciesKeepAliveTimeout(t *testing.T) {
191206
for _, tt := range tests {
192207
t.Run(tt.name, func(t *testing.T) {
193208
t.Parallel()
194-
validateClientSettingsPolicy(t, tt, g, k8sClient)
209+
policySpec := tt.policySpec
210+
policySpec.TargetRef.Name = gatewayv1alpha2.ObjectName(uniqueResourceName(testTargetRefName))
211+
clientSettingsPolicy := &ngfAPIv1alpha1.ClientSettingsPolicy{
212+
ObjectMeta: controllerruntime.ObjectMeta{
213+
Name: uniqueResourceName(testPolicyName),
214+
Namespace: defaultNamespace,
215+
},
216+
Spec: policySpec,
217+
}
218+
validateCrd(t, tt.wantErrors, g, clientSettingsPolicy, k8sClient)
195219
})
196220
}
197221
}
198-
199-
func validateClientSettingsPolicy(t *testing.T, tt struct {
200-
policySpec ngfAPIv1alpha1.ClientSettingsPolicySpec
201-
name string
202-
wantErrors []string
203-
}, g *WithT, k8sClient client.Client,
204-
) {
205-
t.Helper()
206-
207-
policySpec := tt.policySpec
208-
policySpec.TargetRef.Name = gatewayv1alpha2.ObjectName(uniqueResourceName(testTargetRefName))
209-
policyName := uniqueResourceName(testPolicyName)
210-
211-
clientSettingsPolicy := &ngfAPIv1alpha1.ClientSettingsPolicy{
212-
ObjectMeta: controllerruntime.ObjectMeta{
213-
Name: policyName,
214-
Namespace: defaultNamespace,
215-
},
216-
Spec: policySpec,
217-
}
218-
timeoutConfig := framework.DefaultTimeoutConfig()
219-
ctx, cancel := context.WithTimeout(context.Background(), timeoutConfig.KubernetesClientTimeout)
220-
err := k8sClient.Create(ctx, clientSettingsPolicy)
221-
defer cancel()
222-
223-
// Clean up after test
224-
defer func() {
225-
_ = k8sClient.Delete(context.Background(), clientSettingsPolicy)
226-
}()
227-
228-
if len(tt.wantErrors) == 0 {
229-
g.Expect(err).ToNot(HaveOccurred())
230-
} else {
231-
g.Expect(err).To(HaveOccurred())
232-
for _, wantError := range tt.wantErrors {
233-
g.Expect(err.Error()).To(ContainSubstring(wantError), "Expected error '%s' not found in: %s", wantError, err.Error())
234-
}
235-
}
236-
}

tests/cel/common.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,19 @@
11
package cel
22

33
import (
4+
"context"
45
"crypto/rand"
56
"fmt"
67
"testing"
78

9+
. "github.com/onsi/gomega"
810
"k8s.io/apimachinery/pkg/runtime"
911
controllerruntime "sigs.k8s.io/controller-runtime"
1012
"sigs.k8s.io/controller-runtime/pkg/client"
1113

1214
ngfAPIv1alpha1 "github.com/nginx/nginx-gateway-fabric/v2/apis/v1alpha1"
1315
ngfAPIv1alpha2 "github.com/nginx/nginx-gateway-fabric/v2/apis/v1alpha2"
16+
"github.com/nginx/nginx-gateway-fabric/v2/tests/framework"
1417
)
1518

1619
const (
@@ -77,3 +80,26 @@ func randomPrimeNumber() int64 {
7780
func uniqueResourceName(name string) string {
7881
return fmt.Sprintf("%s-%d", name, randomPrimeNumber())
7982
}
83+
84+
func validateCrd(t *testing.T, wantErrors []string, g *WithT, crd client.Object, k8sClient client.Client) {
85+
t.Helper()
86+
87+
timeoutConfig := framework.DefaultTimeoutConfig()
88+
ctx, cancel := context.WithTimeout(context.Background(), timeoutConfig.KubernetesClientTimeout)
89+
err := k8sClient.Create(ctx, crd)
90+
defer cancel()
91+
92+
// Clean up after test
93+
defer func() {
94+
_ = k8sClient.Delete(context.Background(), crd)
95+
}()
96+
97+
if len(wantErrors) == 0 {
98+
g.Expect(err).ToNot(HaveOccurred())
99+
} else {
100+
g.Expect(err).To(HaveOccurred())
101+
for _, wantError := range wantErrors {
102+
g.Expect(err.Error()).To(ContainSubstring(wantError), "Expected error '%s' not found in: %s", wantError, err.Error())
103+
}
104+
}
105+
}

tests/cel/nginxproxy_test.go

Lines changed: 22 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,13 @@
11
package cel
22

33
import (
4-
"context"
54
"testing"
65

76
. "github.com/onsi/gomega"
87
controllerruntime "sigs.k8s.io/controller-runtime"
9-
"sigs.k8s.io/controller-runtime/pkg/client"
108

119
ngfAPIv1alpha2 "github.com/nginx/nginx-gateway-fabric/v2/apis/v1alpha2"
1210
"github.com/nginx/nginx-gateway-fabric/v2/internal/framework/helpers"
13-
"github.com/nginx/nginx-gateway-fabric/v2/tests/framework"
1411
)
1512

1613
func TestNginxProxyKubernetes(t *testing.T) {
@@ -60,7 +57,17 @@ func TestNginxProxyKubernetes(t *testing.T) {
6057
for _, tt := range tests {
6158
t.Run(tt.name, func(t *testing.T) {
6259
t.Parallel()
63-
validateNginxProxy(t, tt, g, k8sClient)
60+
policySpec := tt.policySpec
61+
policyName := uniqueResourceName(testPolicyName)
62+
63+
nginxProxy := &ngfAPIv1alpha2.NginxProxy{
64+
ObjectMeta: controllerruntime.ObjectMeta{
65+
Name: policyName,
66+
Namespace: defaultNamespace,
67+
},
68+
Spec: policySpec,
69+
}
70+
validateCrd(t, tt.wantErrors, g, nginxProxy, k8sClient)
6471
})
6572
}
6673
}
@@ -105,45 +112,17 @@ func TestNginxProxyRewriteClientIP(t *testing.T) {
105112
for _, tt := range tests {
106113
t.Run(tt.name, func(t *testing.T) {
107114
t.Parallel()
108-
validateNginxProxy(t, tt, g, k8sClient)
109-
})
110-
}
111-
}
112-
113-
func validateNginxProxy(t *testing.T, tt struct {
114-
policySpec ngfAPIv1alpha2.NginxProxySpec
115-
name string
116-
wantErrors []string
117-
}, g *WithT, k8sClient client.Client,
118-
) {
119-
t.Helper()
120-
121-
policySpec := tt.policySpec
122-
policyName := uniqueResourceName(testPolicyName)
115+
policySpec := tt.policySpec
116+
policyName := uniqueResourceName(testPolicyName)
123117

124-
nginxProxy := &ngfAPIv1alpha2.NginxProxy{
125-
ObjectMeta: controllerruntime.ObjectMeta{
126-
Name: policyName,
127-
Namespace: defaultNamespace,
128-
},
129-
Spec: policySpec,
130-
}
131-
timeoutConfig := framework.DefaultTimeoutConfig()
132-
ctx, cancel := context.WithTimeout(context.Background(), timeoutConfig.KubernetesClientTimeout)
133-
err := k8sClient.Create(ctx, nginxProxy)
134-
defer cancel()
135-
136-
// Clean up after test
137-
defer func() {
138-
_ = k8sClient.Delete(context.Background(), nginxProxy)
139-
}()
140-
141-
if len(tt.wantErrors) == 0 {
142-
g.Expect(err).ToNot(HaveOccurred())
143-
} else {
144-
g.Expect(err).To(HaveOccurred())
145-
for _, wantError := range tt.wantErrors {
146-
g.Expect(err.Error()).To(ContainSubstring(wantError), "Expected error '%s' not found in: %s", wantError, err.Error())
147-
}
118+
nginxProxy := &ngfAPIv1alpha2.NginxProxy{
119+
ObjectMeta: controllerruntime.ObjectMeta{
120+
Name: policyName,
121+
Namespace: defaultNamespace,
122+
},
123+
Spec: policySpec,
124+
}
125+
validateCrd(t, tt.wantErrors, g, nginxProxy, k8sClient)
126+
})
148127
}
149128
}

0 commit comments

Comments
 (0)