cmd/kube-controller-manager/app/options: Improving test coverage

Add a snippet of the expected error string related to the aspect being tested

Signed-off-by: TommyStarK <thomasmilox@gmail.com>
This commit is contained in:
TommyStarK 2022-12-12 19:59:22 +01:00
parent 270141aa0b
commit 454290c583

View File

@ -19,13 +19,16 @@ package options
import ( import (
"reflect" "reflect"
"sort" "sort"
"strings"
"testing" "testing"
"time" "time"
"github.com/spf13/pflag" "github.com/spf13/pflag"
eventv1 "k8s.io/api/events/v1" eventv1 "k8s.io/api/events/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/diff" "k8s.io/apimachinery/pkg/util/diff"
utilerrors "k8s.io/apimachinery/pkg/util/errors"
apiserveroptions "k8s.io/apiserver/pkg/server/options" apiserveroptions "k8s.io/apiserver/pkg/server/options"
cpconfig "k8s.io/cloud-provider/config" cpconfig "k8s.io/cloud-provider/config"
serviceconfig "k8s.io/cloud-provider/controllers/service/config" serviceconfig "k8s.io/cloud-provider/controllers/service/config"
@ -648,13 +651,15 @@ func TestApplyTo(t *testing.T) {
func TestValidateControllersOptions(t *testing.T) { func TestValidateControllersOptions(t *testing.T) {
testCases := []struct { testCases := []struct {
name string name string
expectErrors bool expectErrors bool
validate func() []error expectedErrorSubString string
validate func() []error
}{ }{
{ {
name: "AttachDetachControllerOptions reconciler sync loop period less than one second", name: "AttachDetachControllerOptions reconciler sync loop period less than one second",
expectErrors: true, expectErrors: true,
expectedErrorSubString: "duration time must be greater than one second",
validate: (&AttachDetachControllerOptions{ validate: (&AttachDetachControllerOptions{
&attachdetachconfig.AttachDetachControllerConfiguration{ &attachdetachconfig.AttachDetachControllerConfiguration{
ReconcilerSyncLoopPeriod: metav1.Duration{Duration: time.Second / 2}, ReconcilerSyncLoopPeriod: metav1.Duration{Duration: time.Second / 2},
@ -663,8 +668,9 @@ func TestValidateControllersOptions(t *testing.T) {
}).Validate, }).Validate,
}, },
{ {
name: "CSRSigningControllerOptions KubeletServingSignerConfiguration no cert file", name: "CSRSigningControllerOptions KubeletServingSignerConfiguration no cert file",
expectErrors: true, expectErrors: true,
expectedErrorSubString: "cannot specify key without cert",
validate: (&CSRSigningControllerOptions{ validate: (&CSRSigningControllerOptions{
&csrsigningconfig.CSRSigningControllerConfiguration{ &csrsigningconfig.CSRSigningControllerConfiguration{
ClusterSigningCertFile: "", ClusterSigningCertFile: "",
@ -690,8 +696,9 @@ func TestValidateControllersOptions(t *testing.T) {
}).Validate, }).Validate,
}, },
{ {
name: "CSRSigningControllerOptions KubeletServingSignerConfiguration no key file", name: "CSRSigningControllerOptions KubeletServingSignerConfiguration no key file",
expectErrors: true, expectErrors: true,
expectedErrorSubString: "cannot specify cert without key",
validate: (&CSRSigningControllerOptions{ validate: (&CSRSigningControllerOptions{
&csrsigningconfig.CSRSigningControllerConfiguration{ &csrsigningconfig.CSRSigningControllerConfiguration{
ClusterSigningCertFile: "", ClusterSigningCertFile: "",
@ -717,8 +724,9 @@ func TestValidateControllersOptions(t *testing.T) {
}).Validate, }).Validate,
}, },
{ {
name: "CSRSigningControllerOptions KubeletClientSignerConfiguration no cert file", name: "CSRSigningControllerOptions KubeletClientSignerConfiguration no cert file",
expectErrors: true, expectErrors: true,
expectedErrorSubString: "cannot specify key without cert",
validate: (&CSRSigningControllerOptions{ validate: (&CSRSigningControllerOptions{
&csrsigningconfig.CSRSigningControllerConfiguration{ &csrsigningconfig.CSRSigningControllerConfiguration{
ClusterSigningCertFile: "", ClusterSigningCertFile: "",
@ -744,8 +752,9 @@ func TestValidateControllersOptions(t *testing.T) {
}).Validate, }).Validate,
}, },
{ {
name: "CSRSigningControllerOptions KubeletClientSignerConfiguration no key file", name: "CSRSigningControllerOptions KubeletClientSignerConfiguration no key file",
expectErrors: true, expectErrors: true,
expectedErrorSubString: "cannot specify cert without key",
validate: (&CSRSigningControllerOptions{ validate: (&CSRSigningControllerOptions{
&csrsigningconfig.CSRSigningControllerConfiguration{ &csrsigningconfig.CSRSigningControllerConfiguration{
ClusterSigningCertFile: "", ClusterSigningCertFile: "",
@ -771,8 +780,9 @@ func TestValidateControllersOptions(t *testing.T) {
}).Validate, }).Validate,
}, },
{ {
name: "CSRSigningControllerOptions KubeAPIServerClientSignerConfiguration no cert file", name: "CSRSigningControllerOptions KubeAPIServerClientSignerConfiguration no cert file",
expectErrors: true, expectErrors: true,
expectedErrorSubString: "cannot specify key without cert",
validate: (&CSRSigningControllerOptions{ validate: (&CSRSigningControllerOptions{
&csrsigningconfig.CSRSigningControllerConfiguration{ &csrsigningconfig.CSRSigningControllerConfiguration{
ClusterSigningCertFile: "", ClusterSigningCertFile: "",
@ -798,8 +808,9 @@ func TestValidateControllersOptions(t *testing.T) {
}).Validate, }).Validate,
}, },
{ {
name: "CSRSigningControllerOptions KubeAPIServerClientSignerConfiguration no key file", name: "CSRSigningControllerOptions KubeAPIServerClientSignerConfiguration no key file",
expectErrors: true, expectErrors: true,
expectedErrorSubString: "cannot specify cert without key",
validate: (&CSRSigningControllerOptions{ validate: (&CSRSigningControllerOptions{
&csrsigningconfig.CSRSigningControllerConfiguration{ &csrsigningconfig.CSRSigningControllerConfiguration{
ClusterSigningCertFile: "", ClusterSigningCertFile: "",
@ -825,8 +836,9 @@ func TestValidateControllersOptions(t *testing.T) {
}).Validate, }).Validate,
}, },
{ {
name: "CSRSigningControllerOptions LegacyUnknownSignerConfiguration no cert file", name: "CSRSigningControllerOptions LegacyUnknownSignerConfiguration no cert file",
expectErrors: true, expectErrors: true,
expectedErrorSubString: "cannot specify key without cert",
validate: (&CSRSigningControllerOptions{ validate: (&CSRSigningControllerOptions{
&csrsigningconfig.CSRSigningControllerConfiguration{ &csrsigningconfig.CSRSigningControllerConfiguration{
ClusterSigningCertFile: "", ClusterSigningCertFile: "",
@ -852,8 +864,9 @@ func TestValidateControllersOptions(t *testing.T) {
}).Validate, }).Validate,
}, },
{ {
name: "CSRSigningControllerOptions LegacyUnknownSignerConfiguration no key file", name: "CSRSigningControllerOptions LegacyUnknownSignerConfiguration no key file",
expectErrors: true, expectErrors: true,
expectedErrorSubString: "cannot specify cert without key",
validate: (&CSRSigningControllerOptions{ validate: (&CSRSigningControllerOptions{
&csrsigningconfig.CSRSigningControllerConfiguration{ &csrsigningconfig.CSRSigningControllerConfiguration{
ClusterSigningCertFile: "", ClusterSigningCertFile: "",
@ -879,8 +892,9 @@ func TestValidateControllersOptions(t *testing.T) {
}).Validate, }).Validate,
}, },
{ {
name: "CSRSigningControllerOptions specific file set along with cluster single signing file", name: "CSRSigningControllerOptions specific file set along with cluster single signing file",
expectErrors: true, expectErrors: true,
expectedErrorSubString: "cannot specify --cluster-signing-{cert,key}-file and other --cluster-signing-*-file flags at the same time",
validate: (&CSRSigningControllerOptions{ validate: (&CSRSigningControllerOptions{
&csrsigningconfig.CSRSigningControllerConfiguration{ &csrsigningconfig.CSRSigningControllerConfiguration{
ClusterSigningCertFile: "/cluster-signing-cert-file", ClusterSigningCertFile: "/cluster-signing-cert-file",
@ -906,8 +920,9 @@ func TestValidateControllersOptions(t *testing.T) {
}).Validate, }).Validate,
}, },
{ {
name: "EndpointSliceControllerOptions ConcurrentServiceEndpointSyncs lower than minConcurrentServiceEndpointSyncs (1)", name: "EndpointSliceControllerOptions ConcurrentServiceEndpointSyncs lower than minConcurrentServiceEndpointSyncs (1)",
expectErrors: true, expectErrors: true,
expectedErrorSubString: "concurrent-service-endpoint-syncs must not be less than 1",
validate: (&EndpointSliceControllerOptions{ validate: (&EndpointSliceControllerOptions{
&endpointsliceconfig.EndpointSliceControllerConfiguration{ &endpointsliceconfig.EndpointSliceControllerConfiguration{
ConcurrentServiceEndpointSyncs: 0, ConcurrentServiceEndpointSyncs: 0,
@ -916,8 +931,9 @@ func TestValidateControllersOptions(t *testing.T) {
}).Validate, }).Validate,
}, },
{ {
name: "EndpointSliceControllerOptions ConcurrentServiceEndpointSyncs greater than maxConcurrentServiceEndpointSyncs (50)", name: "EndpointSliceControllerOptions ConcurrentServiceEndpointSyncs greater than maxConcurrentServiceEndpointSyncs (50)",
expectErrors: true, expectErrors: true,
expectedErrorSubString: "concurrent-service-endpoint-syncs must not be more than 50",
validate: (&EndpointSliceControllerOptions{ validate: (&EndpointSliceControllerOptions{
&endpointsliceconfig.EndpointSliceControllerConfiguration{ &endpointsliceconfig.EndpointSliceControllerConfiguration{
ConcurrentServiceEndpointSyncs: 51, ConcurrentServiceEndpointSyncs: 51,
@ -926,8 +942,9 @@ func TestValidateControllersOptions(t *testing.T) {
}).Validate, }).Validate,
}, },
{ {
name: "EndpointSliceControllerOptions MaxEndpointsPerSlice lower than minMaxEndpointsPerSlice (1)", name: "EndpointSliceControllerOptions MaxEndpointsPerSlice lower than minMaxEndpointsPerSlice (1)",
expectErrors: true, expectErrors: true,
expectedErrorSubString: "max-endpoints-per-slice must not be less than 1",
validate: (&EndpointSliceControllerOptions{ validate: (&EndpointSliceControllerOptions{
&endpointsliceconfig.EndpointSliceControllerConfiguration{ &endpointsliceconfig.EndpointSliceControllerConfiguration{
ConcurrentServiceEndpointSyncs: 10, ConcurrentServiceEndpointSyncs: 10,
@ -936,8 +953,9 @@ func TestValidateControllersOptions(t *testing.T) {
}).Validate, }).Validate,
}, },
{ {
name: "EndpointSliceControllerOptions MaxEndpointsPerSlice greater than maxMaxEndpointsPerSlice (1000)", name: "EndpointSliceControllerOptions MaxEndpointsPerSlice greater than maxMaxEndpointsPerSlice (1000)",
expectErrors: true, expectErrors: true,
expectedErrorSubString: "max-endpoints-per-slice must not be more than 1000",
validate: (&EndpointSliceControllerOptions{ validate: (&EndpointSliceControllerOptions{
&endpointsliceconfig.EndpointSliceControllerConfiguration{ &endpointsliceconfig.EndpointSliceControllerConfiguration{
ConcurrentServiceEndpointSyncs: 10, ConcurrentServiceEndpointSyncs: 10,
@ -946,8 +964,9 @@ func TestValidateControllersOptions(t *testing.T) {
}).Validate, }).Validate,
}, },
{ {
name: "EndpointSliceMirroringControllerOptions MirroringConcurrentServiceEndpointSyncs lower than mirroringMinConcurrentServiceEndpointSyncs (1)", name: "EndpointSliceMirroringControllerOptions MirroringConcurrentServiceEndpointSyncs lower than mirroringMinConcurrentServiceEndpointSyncs (1)",
expectErrors: true, expectErrors: true,
expectedErrorSubString: "mirroring-concurrent-service-endpoint-syncs must not be less than 1",
validate: (&EndpointSliceMirroringControllerOptions{ validate: (&EndpointSliceMirroringControllerOptions{
&endpointslicemirroringconfig.EndpointSliceMirroringControllerConfiguration{ &endpointslicemirroringconfig.EndpointSliceMirroringControllerConfiguration{
MirroringConcurrentServiceEndpointSyncs: 0, MirroringConcurrentServiceEndpointSyncs: 0,
@ -956,8 +975,9 @@ func TestValidateControllersOptions(t *testing.T) {
}).Validate, }).Validate,
}, },
{ {
name: "EndpointSliceMirroringControllerOptions MirroringConcurrentServiceEndpointSyncs greater than mirroringMaxConcurrentServiceEndpointSyncs (50)", name: "EndpointSliceMirroringControllerOptions MirroringConcurrentServiceEndpointSyncs greater than mirroringMaxConcurrentServiceEndpointSyncs (50)",
expectErrors: true, expectErrors: true,
expectedErrorSubString: "mirroring-concurrent-service-endpoint-syncs must not be more than 50",
validate: (&EndpointSliceMirroringControllerOptions{ validate: (&EndpointSliceMirroringControllerOptions{
&endpointslicemirroringconfig.EndpointSliceMirroringControllerConfiguration{ &endpointslicemirroringconfig.EndpointSliceMirroringControllerConfiguration{
MirroringConcurrentServiceEndpointSyncs: 51, MirroringConcurrentServiceEndpointSyncs: 51,
@ -966,8 +986,9 @@ func TestValidateControllersOptions(t *testing.T) {
}).Validate, }).Validate,
}, },
{ {
name: "EndpointSliceMirroringControllerOptions MirroringMaxEndpointsPerSubset lower than mirroringMinMaxEndpointsPerSubset (1)", name: "EndpointSliceMirroringControllerOptions MirroringMaxEndpointsPerSubset lower than mirroringMinMaxEndpointsPerSubset (1)",
expectErrors: true, expectErrors: true,
expectedErrorSubString: "mirroring-max-endpoints-per-subset must not be less than 1",
validate: (&EndpointSliceMirroringControllerOptions{ validate: (&EndpointSliceMirroringControllerOptions{
&endpointslicemirroringconfig.EndpointSliceMirroringControllerConfiguration{ &endpointslicemirroringconfig.EndpointSliceMirroringControllerConfiguration{
MirroringConcurrentServiceEndpointSyncs: 10, MirroringConcurrentServiceEndpointSyncs: 10,
@ -976,8 +997,9 @@ func TestValidateControllersOptions(t *testing.T) {
}).Validate, }).Validate,
}, },
{ {
name: "EndpointSliceMirroringControllerOptions MirroringMaxEndpointsPerSubset greater than mirroringMaxMaxEndpointsPerSubset (1000)", name: "EndpointSliceMirroringControllerOptions MirroringMaxEndpointsPerSubset greater than mirroringMaxMaxEndpointsPerSubset (1000)",
expectErrors: true, expectErrors: true,
expectedErrorSubString: "mirroring-max-endpoints-per-subset must not be more than 1000",
validate: (&EndpointSliceMirroringControllerOptions{ validate: (&EndpointSliceMirroringControllerOptions{
&endpointslicemirroringconfig.EndpointSliceMirroringControllerConfiguration{ &endpointslicemirroringconfig.EndpointSliceMirroringControllerConfiguration{
MirroringConcurrentServiceEndpointSyncs: 10, MirroringConcurrentServiceEndpointSyncs: 10,
@ -986,8 +1008,9 @@ func TestValidateControllersOptions(t *testing.T) {
}).Validate, }).Validate,
}, },
{ {
name: "EphemeralVolumeControllerOptions ConcurrentEphemeralVolumeSyncs equal 0", name: "EphemeralVolumeControllerOptions ConcurrentEphemeralVolumeSyncs equal 0",
expectErrors: true, expectErrors: true,
expectedErrorSubString: "concurrent-ephemeralvolume-syncs must be greater than 0",
validate: (&EphemeralVolumeControllerOptions{ validate: (&EphemeralVolumeControllerOptions{
&ephemeralvolumeconfig.EphemeralVolumeControllerConfiguration{ &ephemeralvolumeconfig.EphemeralVolumeControllerConfiguration{
ConcurrentEphemeralVolumeSyncs: 0, ConcurrentEphemeralVolumeSyncs: 0,
@ -995,8 +1018,9 @@ func TestValidateControllersOptions(t *testing.T) {
}).Validate, }).Validate,
}, },
{ {
name: "HPAControllerOptions ConcurrentHorizontalPodAutoscalerSyncs equal 0", name: "HPAControllerOptions ConcurrentHorizontalPodAutoscalerSyncs equal 0",
expectErrors: true, expectErrors: true,
expectedErrorSubString: "concurrent-horizontal-pod-autoscaler-syncs must be greater than 0",
validate: (&HPAControllerOptions{ validate: (&HPAControllerOptions{
&poautosclerconfig.HPAControllerConfiguration{ &poautosclerconfig.HPAControllerConfiguration{
ConcurrentHorizontalPodAutoscalerSyncs: 0, ConcurrentHorizontalPodAutoscalerSyncs: 0,
@ -1011,8 +1035,9 @@ func TestValidateControllersOptions(t *testing.T) {
}).Validate, }).Validate,
}, },
{ {
name: "NodeIPAMControllerOptions service cluster ip range more than two entries", name: "NodeIPAMControllerOptions service cluster ip range more than two entries",
expectErrors: true, expectErrors: true,
expectedErrorSubString: "--service-cluster-ip-range can not contain more than two entries",
validate: (&NodeIPAMControllerOptions{ validate: (&NodeIPAMControllerOptions{
&nodeipamconfig.NodeIPAMControllerConfiguration{ &nodeipamconfig.NodeIPAMControllerConfiguration{
ServiceCIDR: "10.0.0.0/16,244.0.0.0/16,3000::/108", ServiceCIDR: "10.0.0.0/16,244.0.0.0/16,3000::/108",
@ -1023,8 +1048,9 @@ func TestValidateControllersOptions(t *testing.T) {
}).Validate, }).Validate,
}, },
{ {
name: "PersistentVolumeBinderControllerOptions bad cidr deny list", name: "PersistentVolumeBinderControllerOptions bad cidr deny list",
expectErrors: true, expectErrors: true,
expectedErrorSubString: "bad --volume-host-ip-denylist/--volume-host-allow-local-loopback failed to parse cidr",
validate: (&PersistentVolumeBinderControllerOptions{ validate: (&PersistentVolumeBinderControllerOptions{
&persistentvolumeconfig.PersistentVolumeBinderControllerConfiguration{ &persistentvolumeconfig.PersistentVolumeBinderControllerConfiguration{
PVClaimBinderSyncPeriod: metav1.Duration{Duration: 30 * time.Second}, PVClaimBinderSyncPeriod: metav1.Duration{Duration: 30 * time.Second},
@ -1046,8 +1072,9 @@ func TestValidateControllersOptions(t *testing.T) {
}).Validate, }).Validate,
}, },
{ {
name: "StatefulSetControllerOptions ConcurrentStatefulSetSyncs equal 0", name: "StatefulSetControllerOptions ConcurrentStatefulSetSyncs equal 0",
expectErrors: true, expectErrors: true,
expectedErrorSubString: "concurrent-statefulset-syncs must be greater than 0",
validate: (&StatefulSetControllerOptions{ validate: (&StatefulSetControllerOptions{
&statefulsetconfig.StatefulSetControllerConfiguration{ &statefulsetconfig.StatefulSetControllerConfiguration{
ConcurrentStatefulSetSyncs: 0, ConcurrentStatefulSetSyncs: 0,
@ -1215,6 +1242,12 @@ func TestValidateControllersOptions(t *testing.T) {
if len(errs) == 0 && tc.expectErrors { if len(errs) == 0 && tc.expectErrors {
t.Errorf("expected errors, no errors found") t.Errorf("expected errors, no errors found")
} }
if len(errs) > 0 && tc.expectErrors {
if !strings.Contains(utilerrors.NewAggregate(errs).Error(), tc.expectedErrorSubString) {
t.Errorf("expected error: %s, but no error found", tc.expectedErrorSubString)
}
}
}) })
} }
} }