Skip to content

Commit

Permalink
Add unit test for metadata validation
Browse files Browse the repository at this point in the history
  • Loading branch information
LuBingtan committed Jul 10, 2023
1 parent cf43bf7 commit 445bbf8
Show file tree
Hide file tree
Showing 10 changed files with 360 additions and 6 deletions.
6 changes: 3 additions & 3 deletions api/v1beta1/common_validate.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright 2021 The Kubernetes Authors.
Copyright 2023 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand All @@ -17,7 +17,7 @@ limitations under the License.
package v1beta1

import (
metavalidation "k8s.io/apimachinery/pkg/api/validation"
apivalidation "k8s.io/apimachinery/pkg/api/validation"
metav1validation "k8s.io/apimachinery/pkg/apis/meta/v1/validation"
"k8s.io/apimachinery/pkg/util/validation/field"
)
Expand All @@ -28,7 +28,7 @@ func (metadata *ObjectMeta) Validate(parent *field.Path) field.ErrorList {
metadata.Labels,
parent.Child("labels"),
)
allErrs = append(allErrs, metavalidation.ValidateAnnotations(
allErrs = append(allErrs, apivalidation.ValidateAnnotations(
metadata.Annotations,
parent.Child("annotations"),
)...)
Expand Down
46 changes: 46 additions & 0 deletions api/v1beta1/machinedeployment_webhook_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package v1beta1

import (
"context"
"strings"
"testing"

. "github.com/onsi/gomega"
Expand Down Expand Up @@ -569,3 +570,48 @@ func defaultValidateTestCustomDefaulter(object admission.Validator, customDefaul
})
}
}

func TestMachineDeploymentTemplateMetadataValidation(t *testing.T) {
tests := []struct {
name string
labels map[string]string
annotations map[string]string
expectErr bool
}{
{
name: "should return error for invalid labels and annotations",
labels: map[string]string{
"foo": "$invalid-key",
"bar": strings.Repeat("a", 64) + "too-long-value",
"/invalid-key": "foo",
},
annotations: map[string]string{
"/invalid-key": "foo",
},
expectErr: true,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
g := NewWithT(t)
md := &MachineDeployment{
Spec: MachineDeploymentSpec{
Template: MachineTemplateSpec{
ObjectMeta: ObjectMeta{
Labels: tt.labels,
Annotations: tt.annotations,
},
},
},
}
if tt.expectErr {
g.Expect(md.ValidateCreate()).NotTo(Succeed())
g.Expect(md.ValidateUpdate(md)).NotTo(Succeed())
} else {
g.Expect(md.ValidateCreate()).To(Succeed())
g.Expect(md.ValidateUpdate(md)).To(Succeed())
}
})
}
}
46 changes: 46 additions & 0 deletions api/v1beta1/machineset_webhook_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ limitations under the License.
package v1beta1

import (
"strings"
"testing"

. "github.com/onsi/gomega"
Expand Down Expand Up @@ -307,3 +308,48 @@ func TestValidateSkippedMachineSetPreflightChecks(t *testing.T) {
})
}
}

func TestMachineSetTemplateMetadataValidation(t *testing.T) {
tests := []struct {
name string
labels map[string]string
annotations map[string]string
expectErr bool
}{
{
name: "should return error for invalid labels and annotations",
labels: map[string]string{
"foo": "$invalid-key",
"bar": strings.Repeat("a", 64) + "too-long-value",
"/invalid-key": "foo",
},
annotations: map[string]string{
"/invalid-key": "foo",
},
expectErr: true,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
g := NewWithT(t)
ms := &MachineSet{
Spec: MachineSetSpec{
Template: MachineTemplateSpec{
ObjectMeta: ObjectMeta{
Labels: tt.labels,
Annotations: tt.annotations,
},
},
},
}
if tt.expectErr {
g.Expect(ms.ValidateCreate()).NotTo(Succeed())
g.Expect(ms.ValidateUpdate(ms)).NotTo(Succeed())
} else {
g.Expect(ms.ValidateCreate()).To(Succeed())
g.Expect(ms.ValidateUpdate(ms)).To(Succeed())
}
})
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,14 @@ limitations under the License.
package v1beta1_test

import (
"strings"
"testing"

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

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

func TestKubeadmConfigTemplateValidation(t *testing.T) {
cases := map[string]struct {
in *bootstrapv1.KubeadmConfigTemplate
in *bootstrapv1.KubeadmConfigTemplate
expectErr bool
}{
"valid configuration": {
in: &bootstrapv1.KubeadmConfigTemplate{
Expand All @@ -61,6 +64,21 @@ func TestKubeadmConfigTemplateValidation(t *testing.T) {
},
},
},
"should return error for invalid labels and annotations": {
in: &bootstrapv1.KubeadmConfigTemplate{Spec: bootstrapv1.KubeadmConfigTemplateSpec{
Template: bootstrapv1.KubeadmConfigTemplateResource{ObjectMeta: clusterv1.ObjectMeta{
Labels: map[string]string{
"foo": "$invalid-key",
"bar": strings.Repeat("a", 64) + "too-long-value",
"/invalid-key": "foo",
},
Annotations: map[string]string{
"/invalid-key": "foo",
},
}},
}},
expectErr: true,
},
}

for name, tt := range cases {
Expand All @@ -69,10 +87,18 @@ func TestKubeadmConfigTemplateValidation(t *testing.T) {
t.Run(name, func(t *testing.T) {
g := NewWithT(t)
warnings, err := tt.in.ValidateCreate()
g.Expect(err).ToNot(HaveOccurred())
if tt.expectErr {
g.Expect(err).To(HaveOccurred())
} else {
g.Expect(err).ToNot(HaveOccurred())
}
g.Expect(warnings).To(BeEmpty())
warnings, err = tt.in.ValidateUpdate(nil)
g.Expect(err).ToNot(HaveOccurred())
if tt.expectErr {
g.Expect(err).To(HaveOccurred())
} else {
g.Expect(err).ToNot(HaveOccurred())
}
g.Expect(warnings).To(BeEmpty())
})
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ limitations under the License.
package v1beta1

import (
"strings"
"testing"
"time"

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

invalidMetadata := valid.DeepCopy()
invalidMetadata.Spec.MachineTemplate.ObjectMeta.Labels = map[string]string{
"foo": "$invalid-key",
"bar": strings.Repeat("a", 64) + "too-long-value",
"/invalid-key": "foo",
}
invalidMetadata.Spec.MachineTemplate.ObjectMeta.Annotations = map[string]string{
"/invalid-key": "foo",
}

tests := []struct {
name string
enableIgnitionFeature bool
Expand Down Expand Up @@ -236,6 +247,12 @@ func TestKubeadmControlPlaneValidateCreate(t *testing.T) {
expectErr: false,
kcp: validIgnitionConfiguration,
},
{
name: "should return error for invalid metadata",
enableIgnitionFeature: true,
expectErr: true,
kcp: invalidMetadata,
},
}

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

invalidMetadata := before.DeepCopy()
invalidMetadata.Spec.MachineTemplate.ObjectMeta.Labels = map[string]string{
"foo": "$invalid-key",
"bar": strings.Repeat("a", 64) + "too-long-value",
"/invalid-key": "foo",
}
invalidMetadata.Spec.MachineTemplate.ObjectMeta.Annotations = map[string]string{
"/invalid-key": "foo",
}

tests := []struct {
name string
enableIgnitionFeature bool
Expand Down Expand Up @@ -1016,6 +1043,13 @@ func TestKubeadmControlPlaneValidateUpdate(t *testing.T) {
before: before,
kcp: switchFromCloudInitToIgnition,
},
{
name: "should return error for invalid metadata",
enableIgnitionFeature: true,
expectErr: true,
before: before,
kcp: invalidMetadata,
},
}

for _, tt := range tests {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,15 @@ limitations under the License.
package v1beta1

import (
"strings"
"testing"
"time"

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

clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
bootstrapv1 "sigs.k8s.io/cluster-api/bootstrap/kubeadm/api/v1beta1"
"sigs.k8s.io/cluster-api/feature"
utildefaulting "sigs.k8s.io/cluster-api/util/defaulting"
Expand Down Expand Up @@ -110,3 +112,40 @@ func TestKubeadmControlPlaneTemplateValidationFeatureGateDisabled(t *testing.T)
g.Expect(warnings).To(BeEmpty())
})
}

func TestKubeadmControlPlaneTemplateValidationMetadata(t *testing.T) {
t.Run("create kubeadmcontrolplanetemplate should not pass if metadata is invalid", func(t *testing.T) {
g := NewWithT(t)
kcpTemplate := &KubeadmControlPlaneTemplate{
Spec: KubeadmControlPlaneTemplateSpec{
Template: KubeadmControlPlaneTemplateResource{
ObjectMeta: clusterv1.ObjectMeta{
Labels: map[string]string{
"foo": "$invalid-key",
"bar": strings.Repeat("a", 64) + "too-long-value",
"/invalid-key": "foo",
},
Annotations: map[string]string{
"/invalid-key": "foo",
},
},
Spec: KubeadmControlPlaneTemplateResourceSpec{
MachineTemplate: &KubeadmControlPlaneTemplateMachineTemplate{
ObjectMeta: clusterv1.ObjectMeta{
Labels: map[string]string{
"foo": "$invalid-key",
"bar": strings.Repeat("a", 64) + "too-long-value",
"/invalid-key": "foo",
},
Annotations: map[string]string{
"/invalid-key": "foo",
},
},
},
},
},
},
}
g.Expect(kcpTemplate.ValidateCreate()).ShouldNot(Succeed())
})
}
46 changes: 46 additions & 0 deletions exp/api/v1beta1/machinepool_webhook_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ limitations under the License.
package v1beta1

import (
"strings"
"testing"

. "github.com/onsi/gomega"
Expand Down Expand Up @@ -320,3 +321,48 @@ func TestMachinePoolVersionValidation(t *testing.T) {
})
}
}

func TestMachinePoolMetadataValidation(t *testing.T) {
tests := []struct {
name string
labels map[string]string
annotations map[string]string
expectErr bool
}{
{
name: "should return error for invalid labels and annotations",
labels: map[string]string{
"foo": "$invalid-key",
"bar": strings.Repeat("a", 64) + "too-long-value",
"/invalid-key": "foo",
},
annotations: map[string]string{
"/invalid-key": "foo",
},
expectErr: true,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
g := NewWithT(t)
mp := &MachinePool{
Spec: MachinePoolSpec{
Template: clusterv1.MachineTemplateSpec{
ObjectMeta: clusterv1.ObjectMeta{
Labels: tt.labels,
Annotations: tt.annotations,
},
},
},
}
if tt.expectErr {
g.Expect(mp.ValidateCreate()).NotTo(Succeed())
g.Expect(mp.ValidateUpdate(mp)).NotTo(Succeed())
} else {
g.Expect(mp.ValidateCreate()).To(Succeed())
g.Expect(mp.ValidateUpdate(mp)).To(Succeed())
}
})
}
}
Loading

0 comments on commit 445bbf8

Please sign in to comment.