Skip to content

Commit 80e121a

Browse files
committed
Add tests for autoscaling validation
1 parent b1da498 commit 80e121a

File tree

2 files changed

+83
-11
lines changed

2 files changed

+83
-11
lines changed

tests/cel/common.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ const (
3636
expectedHeaderWithoutServerError = `header can only be specified if server is specified`
3737
expectedOneOfDeploymentOrDaemonSetError = `only one of deployment or daemonSet can be set`
3838
expectedIfModeSetTrustedAddressesError = `if mode is set, trustedAddresses is a required field`
39+
expectedMinReplicasLessThanOrEqualError = `minReplicas must be less than or equal to maxReplicas`
3940
)
4041

4142
const (

tests/cel/nginxproxy_test.go

Lines changed: 82 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,14 @@ func TestNginxProxyKubernetes(t *testing.T) {
1818
g.Expect(err).ToNot(HaveOccurred())
1919

2020
tests := []struct {
21-
policySpec ngfAPIv1alpha2.NginxProxySpec
21+
spec ngfAPIv1alpha2.NginxProxySpec
2222
name string
2323
wantErrors []string
2424
}{
2525
{
2626
name: "Validate NginxProxy with both Deployment and DaemonSet is invalid",
2727
wantErrors: []string{expectedOneOfDeploymentOrDaemonSetError},
28-
policySpec: ngfAPIv1alpha2.NginxProxySpec{
28+
spec: ngfAPIv1alpha2.NginxProxySpec{
2929
Kubernetes: &ngfAPIv1alpha2.KubernetesSpec{
3030
Deployment: &ngfAPIv1alpha2.DeploymentSpec{},
3131
DaemonSet: &ngfAPIv1alpha2.DaemonSetSpec{},
@@ -34,15 +34,15 @@ func TestNginxProxyKubernetes(t *testing.T) {
3434
},
3535
{
3636
name: "Validate NginxProxy with Deployment only is valid",
37-
policySpec: ngfAPIv1alpha2.NginxProxySpec{
37+
spec: ngfAPIv1alpha2.NginxProxySpec{
3838
Kubernetes: &ngfAPIv1alpha2.KubernetesSpec{
3939
Deployment: &ngfAPIv1alpha2.DeploymentSpec{},
4040
},
4141
},
4242
},
4343
{
4444
name: "Validate NginxProxy with DaemonSet only is valid",
45-
policySpec: ngfAPIv1alpha2.NginxProxySpec{
45+
spec: ngfAPIv1alpha2.NginxProxySpec{
4646
Kubernetes: &ngfAPIv1alpha2.KubernetesSpec{
4747
DaemonSet: &ngfAPIv1alpha2.DaemonSetSpec{},
4848
},
@@ -53,15 +53,15 @@ func TestNginxProxyKubernetes(t *testing.T) {
5353
for _, tt := range tests {
5454
t.Run(tt.name, func(t *testing.T) {
5555
t.Parallel()
56-
policySpec := tt.policySpec
56+
spec := tt.spec
5757
policyName := uniqueResourceName(testPolicyName)
5858

5959
nginxProxy := &ngfAPIv1alpha2.NginxProxy{
6060
ObjectMeta: controllerruntime.ObjectMeta{
6161
Name: policyName,
6262
Namespace: defaultNamespace,
6363
},
64-
Spec: policySpec,
64+
Spec: spec,
6565
}
6666
validateCrd(t, tt.wantErrors, g, nginxProxy, k8sClient)
6767
})
@@ -76,22 +76,22 @@ func TestNginxProxyRewriteClientIP(t *testing.T) {
7676
g.Expect(err).ToNot(HaveOccurred())
7777

7878
tests := []struct {
79-
policySpec ngfAPIv1alpha2.NginxProxySpec
79+
spec ngfAPIv1alpha2.NginxProxySpec
8080
name string
8181
wantErrors []string
8282
}{
8383
{
8484
name: "Validate NginxProxy is invalid when trustedAddresses is not set and mode is set",
8585
wantErrors: []string{expectedIfModeSetTrustedAddressesError},
86-
policySpec: ngfAPIv1alpha2.NginxProxySpec{
86+
spec: ngfAPIv1alpha2.NginxProxySpec{
8787
RewriteClientIP: &ngfAPIv1alpha2.RewriteClientIP{
8888
Mode: helpers.GetPointer[ngfAPIv1alpha2.RewriteClientIPModeType]("XForwardedFor"),
8989
},
9090
},
9191
},
9292
{
9393
name: "Validate NginxProxy is valid when both mode and trustedAddresses are set",
94-
policySpec: ngfAPIv1alpha2.NginxProxySpec{
94+
spec: ngfAPIv1alpha2.NginxProxySpec{
9595
RewriteClientIP: &ngfAPIv1alpha2.RewriteClientIP{
9696
Mode: helpers.GetPointer[ngfAPIv1alpha2.RewriteClientIPModeType]("XForwardedFor"),
9797
TrustedAddresses: []ngfAPIv1alpha2.RewriteClientIPAddress{
@@ -108,15 +108,86 @@ func TestNginxProxyRewriteClientIP(t *testing.T) {
108108
for _, tt := range tests {
109109
t.Run(tt.name, func(t *testing.T) {
110110
t.Parallel()
111-
policySpec := tt.policySpec
111+
spec := tt.spec
112112
policyName := uniqueResourceName(testPolicyName)
113113

114114
nginxProxy := &ngfAPIv1alpha2.NginxProxy{
115115
ObjectMeta: controllerruntime.ObjectMeta{
116116
Name: policyName,
117117
Namespace: defaultNamespace,
118118
},
119-
Spec: policySpec,
119+
Spec: spec,
120+
}
121+
validateCrd(t, tt.wantErrors, g, nginxProxy, k8sClient)
122+
})
123+
}
124+
}
125+
126+
func TestNginxProxyAutoscaling(t *testing.T) {
127+
t.Parallel()
128+
g := NewWithT(t)
129+
130+
k8sClient, err := getKubernetesClient(t)
131+
g.Expect(err).ToNot(HaveOccurred())
132+
133+
tests := []struct {
134+
spec ngfAPIv1alpha2.NginxProxySpec
135+
name string
136+
wantErrors []string
137+
}{
138+
{
139+
name: "Validate NginxProxy is invalid when MinReplicas not less than, or equal to MaxReplicas",
140+
wantErrors: []string{expectedMinReplicasLessThanOrEqualError},
141+
spec: ngfAPIv1alpha2.NginxProxySpec{
142+
Kubernetes: &ngfAPIv1alpha2.KubernetesSpec{
143+
Deployment: &ngfAPIv1alpha2.DeploymentSpec{
144+
Autoscaling: &ngfAPIv1alpha2.AutoscalingSpec{
145+
MinReplicas: helpers.GetPointer[int32](10),
146+
MaxReplicas: 5,
147+
},
148+
},
149+
},
150+
},
151+
},
152+
{
153+
name: "Validate NginxProxy is valid when MinReplicas is less than MaxReplicas",
154+
spec: ngfAPIv1alpha2.NginxProxySpec{
155+
Kubernetes: &ngfAPIv1alpha2.KubernetesSpec{
156+
Deployment: &ngfAPIv1alpha2.DeploymentSpec{
157+
Autoscaling: &ngfAPIv1alpha2.AutoscalingSpec{
158+
MinReplicas: helpers.GetPointer[int32](1),
159+
MaxReplicas: 5,
160+
},
161+
},
162+
},
163+
},
164+
},
165+
{
166+
name: "Validate NginxProxy is valid when MinReplicas is equal to MaxReplicas",
167+
spec: ngfAPIv1alpha2.NginxProxySpec{
168+
Kubernetes: &ngfAPIv1alpha2.KubernetesSpec{
169+
Deployment: &ngfAPIv1alpha2.DeploymentSpec{
170+
Autoscaling: &ngfAPIv1alpha2.AutoscalingSpec{
171+
MinReplicas: helpers.GetPointer[int32](5),
172+
MaxReplicas: 5,
173+
},
174+
},
175+
},
176+
},
177+
},
178+
}
179+
for _, tt := range tests {
180+
t.Run(tt.name, func(t *testing.T) {
181+
t.Parallel()
182+
spec := tt.spec
183+
policyName := uniqueResourceName(testPolicyName)
184+
185+
nginxProxy := &ngfAPIv1alpha2.NginxProxy{
186+
ObjectMeta: controllerruntime.ObjectMeta{
187+
Name: policyName,
188+
Namespace: defaultNamespace,
189+
},
190+
Spec: spec,
120191
}
121192
validateCrd(t, tt.wantErrors, g, nginxProxy, k8sClient)
122193
})

0 commit comments

Comments
 (0)