Skip to content

Commit 9631d54

Browse files
committed
Add tests for kubernetes validation
1 parent 206b84e commit 9631d54

File tree

1 file changed

+104
-0
lines changed

1 file changed

+104
-0
lines changed

tests/cel/nginxproxy_test.go

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
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+
11+
ngfAPIv1alpha2 "github.com/nginx/nginx-gateway-fabric/v2/apis/v1alpha2"
12+
"github.com/nginx/nginx-gateway-fabric/v2/internal/framework/helpers"
13+
"github.com/nginx/nginx-gateway-fabric/v2/tests/framework"
14+
)
15+
16+
func TestNginxProxyKubernetes(t *testing.T) {
17+
t.Parallel()
18+
g := NewWithT(t)
19+
20+
k8sClient, err := getKubernetesClient(t)
21+
g.Expect(err).ToNot(HaveOccurred())
22+
23+
tests := []struct {
24+
policySpec ngfAPIv1alpha2.NginxProxySpec
25+
name string
26+
wantErrors []string
27+
}{
28+
{
29+
name: "Validate NginxProxy with both Deployment and DaemonSet is invalid",
30+
wantErrors: []string{"only one of deployment or daemonSet can be set"},
31+
policySpec: ngfAPIv1alpha2.NginxProxySpec{
32+
Kubernetes: &ngfAPIv1alpha2.KubernetesSpec{
33+
Deployment: &ngfAPIv1alpha2.DeploymentSpec{
34+
Replicas: helpers.GetPointer[int32](3),
35+
},
36+
DaemonSet: &ngfAPIv1alpha2.DaemonSetSpec{},
37+
},
38+
},
39+
},
40+
{
41+
name: "Validate NginxProxy with Deployment only is valid",
42+
policySpec: ngfAPIv1alpha2.NginxProxySpec{
43+
Kubernetes: &ngfAPIv1alpha2.KubernetesSpec{
44+
Deployment: &ngfAPIv1alpha2.DeploymentSpec{
45+
Replicas: helpers.GetPointer[int32](3),
46+
},
47+
},
48+
},
49+
},
50+
{
51+
name: "Validate NginxProxy with DaemonSet only is valid",
52+
policySpec: ngfAPIv1alpha2.NginxProxySpec{
53+
Kubernetes: &ngfAPIv1alpha2.KubernetesSpec{
54+
DaemonSet: &ngfAPIv1alpha2.DaemonSetSpec{},
55+
},
56+
},
57+
},
58+
}
59+
60+
for _, tt := range tests {
61+
t.Run(tt.name, func(t *testing.T) {
62+
t.Parallel()
63+
validateNginxProxy(t, tt, g, k8sClient)
64+
})
65+
}
66+
}
67+
68+
func validateNginxProxy(t *testing.T, tt struct {
69+
policySpec ngfAPIv1alpha2.NginxProxySpec
70+
name string
71+
wantErrors []string
72+
}, g *WithT, k8sClient client.Client,
73+
) {
74+
t.Helper()
75+
76+
policySpec := tt.policySpec
77+
policyName := uniqueResourceName(testPolicyName)
78+
79+
nginxProxy := &ngfAPIv1alpha2.NginxProxy{
80+
ObjectMeta: controllerruntime.ObjectMeta{
81+
Name: policyName,
82+
Namespace: defaultNamespace,
83+
},
84+
Spec: policySpec,
85+
}
86+
timeoutConfig := framework.DefaultTimeoutConfig()
87+
ctx, cancel := context.WithTimeout(context.Background(), timeoutConfig.KubernetesClientTimeout)
88+
err := k8sClient.Create(ctx, nginxProxy)
89+
defer cancel()
90+
91+
// Clean up after test
92+
defer func() {
93+
_ = k8sClient.Delete(context.Background(), nginxProxy)
94+
}()
95+
96+
if len(tt.wantErrors) == 0 {
97+
g.Expect(err).ToNot(HaveOccurred())
98+
} else {
99+
g.Expect(err).To(HaveOccurred())
100+
for _, wantError := range tt.wantErrors {
101+
g.Expect(err.Error()).To(ContainSubstring(wantError), "Expected error '%s' not found in: %s", wantError, err.Error())
102+
}
103+
}
104+
}

0 commit comments

Comments
 (0)