Skip to content

Commit 1239557

Browse files
committed
Add unit test for metadata validation
1 parent cf43bf7 commit 1239557

File tree

10 files changed

+389
-6
lines changed

10 files changed

+389
-6
lines changed

api/v1beta1/common_validate.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
Copyright 2021 The Kubernetes Authors.
2+
Copyright 2023 The Kubernetes Authors.
33
44
Licensed under the Apache License, Version 2.0 (the "License");
55
you may not use this file except in compliance with the License.
@@ -17,7 +17,7 @@ limitations under the License.
1717
package v1beta1
1818

1919
import (
20-
metavalidation "k8s.io/apimachinery/pkg/api/validation"
20+
apivalidation "k8s.io/apimachinery/pkg/api/validation"
2121
metav1validation "k8s.io/apimachinery/pkg/apis/meta/v1/validation"
2222
"k8s.io/apimachinery/pkg/util/validation/field"
2323
)
@@ -28,7 +28,7 @@ func (metadata *ObjectMeta) Validate(parent *field.Path) field.ErrorList {
2828
metadata.Labels,
2929
parent.Child("labels"),
3030
)
31-
allErrs = append(allErrs, metavalidation.ValidateAnnotations(
31+
allErrs = append(allErrs, apivalidation.ValidateAnnotations(
3232
metadata.Annotations,
3333
parent.Child("annotations"),
3434
)...)

api/v1beta1/machinedeployment_webhook_test.go

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package v1beta1
1818

1919
import (
2020
"context"
21+
"strings"
2122
"testing"
2223

2324
. "github.com/onsi/gomega"
@@ -569,3 +570,56 @@ func defaultValidateTestCustomDefaulter(object admission.Validator, customDefaul
569570
})
570571
}
571572
}
573+
574+
func TestMachineDeploymentTemplateMetadataValidation(t *testing.T) {
575+
tests := []struct {
576+
name string
577+
labels map[string]string
578+
annotations map[string]string
579+
expectErr bool
580+
}{
581+
{
582+
name: "should return error for invalid labels and annotations",
583+
labels: map[string]string{
584+
"foo": "$invalid-key",
585+
"bar": strings.Repeat("a", 64) + "too-long-value",
586+
"/invalid-key": "foo",
587+
},
588+
annotations: map[string]string{
589+
"/invalid-key": "foo",
590+
},
591+
expectErr: true,
592+
},
593+
}
594+
595+
for _, tt := range tests {
596+
t.Run(tt.name, func(t *testing.T) {
597+
g := NewWithT(t)
598+
md := &MachineDeployment{
599+
Spec: MachineDeploymentSpec{
600+
Template: MachineTemplateSpec{
601+
ObjectMeta: ObjectMeta{
602+
Labels: tt.labels,
603+
Annotations: tt.annotations,
604+
},
605+
},
606+
},
607+
}
608+
if tt.expectErr {
609+
warnings, err := md.ValidateCreate()
610+
g.Expect(err).To(HaveOccurred())
611+
g.Expect(warnings).To(BeEmpty())
612+
warnings, err = md.ValidateUpdate(md)
613+
g.Expect(err).To(HaveOccurred())
614+
g.Expect(warnings).To(BeEmpty())
615+
} else {
616+
warnings, err := md.ValidateCreate()
617+
g.Expect(err).ToNot(HaveOccurred())
618+
g.Expect(warnings).To(BeEmpty())
619+
warnings, err = md.ValidateUpdate(md)
620+
g.Expect(err).ToNot(HaveOccurred())
621+
g.Expect(warnings).To(BeEmpty())
622+
}
623+
})
624+
}
625+
}

api/v1beta1/machineset_webhook_test.go

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ limitations under the License.
1717
package v1beta1
1818

1919
import (
20+
"strings"
2021
"testing"
2122

2223
. "github.com/onsi/gomega"
@@ -307,3 +308,56 @@ func TestValidateSkippedMachineSetPreflightChecks(t *testing.T) {
307308
})
308309
}
309310
}
311+
312+
func TestMachineSetTemplateMetadataValidation(t *testing.T) {
313+
tests := []struct {
314+
name string
315+
labels map[string]string
316+
annotations map[string]string
317+
expectErr bool
318+
}{
319+
{
320+
name: "should return error for invalid labels and annotations",
321+
labels: map[string]string{
322+
"foo": "$invalid-key",
323+
"bar": strings.Repeat("a", 64) + "too-long-value",
324+
"/invalid-key": "foo",
325+
},
326+
annotations: map[string]string{
327+
"/invalid-key": "foo",
328+
},
329+
expectErr: true,
330+
},
331+
}
332+
333+
for _, tt := range tests {
334+
t.Run(tt.name, func(t *testing.T) {
335+
g := NewWithT(t)
336+
ms := &MachineSet{
337+
Spec: MachineSetSpec{
338+
Template: MachineTemplateSpec{
339+
ObjectMeta: ObjectMeta{
340+
Labels: tt.labels,
341+
Annotations: tt.annotations,
342+
},
343+
},
344+
},
345+
}
346+
if tt.expectErr {
347+
warnings, err := ms.ValidateCreate()
348+
g.Expect(err).To(HaveOccurred())
349+
g.Expect(warnings).To(BeEmpty())
350+
warnings, err = ms.ValidateUpdate(ms)
351+
g.Expect(err).To(HaveOccurred())
352+
g.Expect(warnings).To(BeEmpty())
353+
} else {
354+
warnings, err := ms.ValidateCreate()
355+
g.Expect(err).ToNot(HaveOccurred())
356+
g.Expect(warnings).To(BeEmpty())
357+
warnings, err = ms.ValidateUpdate(ms)
358+
g.Expect(err).ToNot(HaveOccurred())
359+
g.Expect(warnings).To(BeEmpty())
360+
}
361+
})
362+
}
363+
}

bootstrap/kubeadm/api/v1beta1/kubeadmconfigtemplate_webhook_test.go

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,14 @@ limitations under the License.
1717
package v1beta1_test
1818

1919
import (
20+
"strings"
2021
"testing"
2122

2223
. "github.com/onsi/gomega"
2324
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2425
"k8s.io/utils/pointer"
2526

27+
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
2628
bootstrapv1 "sigs.k8s.io/cluster-api/bootstrap/kubeadm/api/v1beta1"
2729
utildefaulting "sigs.k8s.io/cluster-api/util/defaulting"
2830
)
@@ -46,7 +48,8 @@ func TestKubeadmConfigTemplateDefault(t *testing.T) {
4648

4749
func TestKubeadmConfigTemplateValidation(t *testing.T) {
4850
cases := map[string]struct {
49-
in *bootstrapv1.KubeadmConfigTemplate
51+
in *bootstrapv1.KubeadmConfigTemplate
52+
expectErr bool
5053
}{
5154
"valid configuration": {
5255
in: &bootstrapv1.KubeadmConfigTemplate{
@@ -61,6 +64,21 @@ func TestKubeadmConfigTemplateValidation(t *testing.T) {
6164
},
6265
},
6366
},
67+
"should return error for invalid labels and annotations": {
68+
in: &bootstrapv1.KubeadmConfigTemplate{Spec: bootstrapv1.KubeadmConfigTemplateSpec{
69+
Template: bootstrapv1.KubeadmConfigTemplateResource{ObjectMeta: clusterv1.ObjectMeta{
70+
Labels: map[string]string{
71+
"foo": "$invalid-key",
72+
"bar": strings.Repeat("a", 64) + "too-long-value",
73+
"/invalid-key": "foo",
74+
},
75+
Annotations: map[string]string{
76+
"/invalid-key": "foo",
77+
},
78+
}},
79+
}},
80+
expectErr: true,
81+
},
6482
}
6583

6684
for name, tt := range cases {
@@ -69,10 +87,18 @@ func TestKubeadmConfigTemplateValidation(t *testing.T) {
6987
t.Run(name, func(t *testing.T) {
7088
g := NewWithT(t)
7189
warnings, err := tt.in.ValidateCreate()
72-
g.Expect(err).ToNot(HaveOccurred())
90+
if tt.expectErr {
91+
g.Expect(err).To(HaveOccurred())
92+
} else {
93+
g.Expect(err).ToNot(HaveOccurred())
94+
}
7395
g.Expect(warnings).To(BeEmpty())
7496
warnings, err = tt.in.ValidateUpdate(nil)
75-
g.Expect(err).ToNot(HaveOccurred())
97+
if tt.expectErr {
98+
g.Expect(err).To(HaveOccurred())
99+
} else {
100+
g.Expect(err).ToNot(HaveOccurred())
101+
}
76102
g.Expect(warnings).To(BeEmpty())
77103
})
78104
}

controlplane/kubeadm/api/v1beta1/kubeadm_control_plane_webhook_test.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ limitations under the License.
1717
package v1beta1
1818

1919
import (
20+
"strings"
2021
"testing"
2122
"time"
2223

@@ -152,6 +153,16 @@ func TestKubeadmControlPlaneValidateCreate(t *testing.T) {
152153
validIgnitionConfiguration.Spec.KubeadmConfigSpec.Format = bootstrapv1.Ignition
153154
validIgnitionConfiguration.Spec.KubeadmConfigSpec.Ignition = &bootstrapv1.IgnitionSpec{}
154155

156+
invalidMetadata := valid.DeepCopy()
157+
invalidMetadata.Spec.MachineTemplate.ObjectMeta.Labels = map[string]string{
158+
"foo": "$invalid-key",
159+
"bar": strings.Repeat("a", 64) + "too-long-value",
160+
"/invalid-key": "foo",
161+
}
162+
invalidMetadata.Spec.MachineTemplate.ObjectMeta.Annotations = map[string]string{
163+
"/invalid-key": "foo",
164+
}
165+
155166
tests := []struct {
156167
name string
157168
enableIgnitionFeature bool
@@ -236,6 +247,12 @@ func TestKubeadmControlPlaneValidateCreate(t *testing.T) {
236247
expectErr: false,
237248
kcp: validIgnitionConfiguration,
238249
},
250+
{
251+
name: "should return error for invalid metadata",
252+
enableIgnitionFeature: true,
253+
expectErr: true,
254+
kcp: invalidMetadata,
255+
},
239256
}
240257

241258
for _, tt := range tests {
@@ -671,6 +688,16 @@ func TestKubeadmControlPlaneValidateUpdate(t *testing.T) {
671688
{"/var/lib/testdir", "/var/lib/etcd/data"},
672689
}
673690

691+
invalidMetadata := before.DeepCopy()
692+
invalidMetadata.Spec.MachineTemplate.ObjectMeta.Labels = map[string]string{
693+
"foo": "$invalid-key",
694+
"bar": strings.Repeat("a", 64) + "too-long-value",
695+
"/invalid-key": "foo",
696+
}
697+
invalidMetadata.Spec.MachineTemplate.ObjectMeta.Annotations = map[string]string{
698+
"/invalid-key": "foo",
699+
}
700+
674701
tests := []struct {
675702
name string
676703
enableIgnitionFeature bool
@@ -1016,6 +1043,13 @@ func TestKubeadmControlPlaneValidateUpdate(t *testing.T) {
10161043
before: before,
10171044
kcp: switchFromCloudInitToIgnition,
10181045
},
1046+
{
1047+
name: "should return error for invalid metadata",
1048+
enableIgnitionFeature: true,
1049+
expectErr: true,
1050+
before: before,
1051+
kcp: invalidMetadata,
1052+
},
10191053
}
10201054

10211055
for _, tt := range tests {

controlplane/kubeadm/api/v1beta1/kubeadmcontrolplanetemplate_webhook_test.go

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,15 @@ limitations under the License.
1717
package v1beta1
1818

1919
import (
20+
"strings"
2021
"testing"
2122
"time"
2223

2324
. "github.com/onsi/gomega"
2425
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2526
utilfeature "k8s.io/component-base/featuregate/testing"
2627

28+
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
2729
bootstrapv1 "sigs.k8s.io/cluster-api/bootstrap/kubeadm/api/v1beta1"
2830
"sigs.k8s.io/cluster-api/feature"
2931
utildefaulting "sigs.k8s.io/cluster-api/util/defaulting"
@@ -110,3 +112,42 @@ func TestKubeadmControlPlaneTemplateValidationFeatureGateDisabled(t *testing.T)
110112
g.Expect(warnings).To(BeEmpty())
111113
})
112114
}
115+
116+
func TestKubeadmControlPlaneTemplateValidationMetadata(t *testing.T) {
117+
t.Run("create kubeadmcontrolplanetemplate should not pass if metadata is invalid", func(t *testing.T) {
118+
g := NewWithT(t)
119+
kcpTemplate := &KubeadmControlPlaneTemplate{
120+
Spec: KubeadmControlPlaneTemplateSpec{
121+
Template: KubeadmControlPlaneTemplateResource{
122+
ObjectMeta: clusterv1.ObjectMeta{
123+
Labels: map[string]string{
124+
"foo": "$invalid-key",
125+
"bar": strings.Repeat("a", 64) + "too-long-value",
126+
"/invalid-key": "foo",
127+
},
128+
Annotations: map[string]string{
129+
"/invalid-key": "foo",
130+
},
131+
},
132+
Spec: KubeadmControlPlaneTemplateResourceSpec{
133+
MachineTemplate: &KubeadmControlPlaneTemplateMachineTemplate{
134+
ObjectMeta: clusterv1.ObjectMeta{
135+
Labels: map[string]string{
136+
"foo": "$invalid-key",
137+
"bar": strings.Repeat("a", 64) + "too-long-value",
138+
"/invalid-key": "foo",
139+
},
140+
Annotations: map[string]string{
141+
"/invalid-key": "foo",
142+
},
143+
},
144+
},
145+
},
146+
},
147+
},
148+
}
149+
warnings, err := kcpTemplate.ValidateCreate()
150+
g.Expect(err).To(HaveOccurred())
151+
g.Expect(warnings).To(BeEmpty())
152+
})
153+
}

0 commit comments

Comments
 (0)