mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-21 10:51:29 +00:00
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:
parent
270141aa0b
commit
454290c583
@ -19,13 +19,16 @@ package options
|
||||
import (
|
||||
"reflect"
|
||||
"sort"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/spf13/pflag"
|
||||
|
||||
eventv1 "k8s.io/api/events/v1"
|
||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
"k8s.io/apimachinery/pkg/util/diff"
|
||||
utilerrors "k8s.io/apimachinery/pkg/util/errors"
|
||||
apiserveroptions "k8s.io/apiserver/pkg/server/options"
|
||||
cpconfig "k8s.io/cloud-provider/config"
|
||||
serviceconfig "k8s.io/cloud-provider/controllers/service/config"
|
||||
@ -650,11 +653,13 @@ func TestValidateControllersOptions(t *testing.T) {
|
||||
testCases := []struct {
|
||||
name string
|
||||
expectErrors bool
|
||||
expectedErrorSubString string
|
||||
validate func() []error
|
||||
}{
|
||||
{
|
||||
name: "AttachDetachControllerOptions reconciler sync loop period less than one second",
|
||||
expectErrors: true,
|
||||
expectedErrorSubString: "duration time must be greater than one second",
|
||||
validate: (&AttachDetachControllerOptions{
|
||||
&attachdetachconfig.AttachDetachControllerConfiguration{
|
||||
ReconcilerSyncLoopPeriod: metav1.Duration{Duration: time.Second / 2},
|
||||
@ -665,6 +670,7 @@ func TestValidateControllersOptions(t *testing.T) {
|
||||
{
|
||||
name: "CSRSigningControllerOptions KubeletServingSignerConfiguration no cert file",
|
||||
expectErrors: true,
|
||||
expectedErrorSubString: "cannot specify key without cert",
|
||||
validate: (&CSRSigningControllerOptions{
|
||||
&csrsigningconfig.CSRSigningControllerConfiguration{
|
||||
ClusterSigningCertFile: "",
|
||||
@ -692,6 +698,7 @@ func TestValidateControllersOptions(t *testing.T) {
|
||||
{
|
||||
name: "CSRSigningControllerOptions KubeletServingSignerConfiguration no key file",
|
||||
expectErrors: true,
|
||||
expectedErrorSubString: "cannot specify cert without key",
|
||||
validate: (&CSRSigningControllerOptions{
|
||||
&csrsigningconfig.CSRSigningControllerConfiguration{
|
||||
ClusterSigningCertFile: "",
|
||||
@ -719,6 +726,7 @@ func TestValidateControllersOptions(t *testing.T) {
|
||||
{
|
||||
name: "CSRSigningControllerOptions KubeletClientSignerConfiguration no cert file",
|
||||
expectErrors: true,
|
||||
expectedErrorSubString: "cannot specify key without cert",
|
||||
validate: (&CSRSigningControllerOptions{
|
||||
&csrsigningconfig.CSRSigningControllerConfiguration{
|
||||
ClusterSigningCertFile: "",
|
||||
@ -746,6 +754,7 @@ func TestValidateControllersOptions(t *testing.T) {
|
||||
{
|
||||
name: "CSRSigningControllerOptions KubeletClientSignerConfiguration no key file",
|
||||
expectErrors: true,
|
||||
expectedErrorSubString: "cannot specify cert without key",
|
||||
validate: (&CSRSigningControllerOptions{
|
||||
&csrsigningconfig.CSRSigningControllerConfiguration{
|
||||
ClusterSigningCertFile: "",
|
||||
@ -773,6 +782,7 @@ func TestValidateControllersOptions(t *testing.T) {
|
||||
{
|
||||
name: "CSRSigningControllerOptions KubeAPIServerClientSignerConfiguration no cert file",
|
||||
expectErrors: true,
|
||||
expectedErrorSubString: "cannot specify key without cert",
|
||||
validate: (&CSRSigningControllerOptions{
|
||||
&csrsigningconfig.CSRSigningControllerConfiguration{
|
||||
ClusterSigningCertFile: "",
|
||||
@ -800,6 +810,7 @@ func TestValidateControllersOptions(t *testing.T) {
|
||||
{
|
||||
name: "CSRSigningControllerOptions KubeAPIServerClientSignerConfiguration no key file",
|
||||
expectErrors: true,
|
||||
expectedErrorSubString: "cannot specify cert without key",
|
||||
validate: (&CSRSigningControllerOptions{
|
||||
&csrsigningconfig.CSRSigningControllerConfiguration{
|
||||
ClusterSigningCertFile: "",
|
||||
@ -827,6 +838,7 @@ func TestValidateControllersOptions(t *testing.T) {
|
||||
{
|
||||
name: "CSRSigningControllerOptions LegacyUnknownSignerConfiguration no cert file",
|
||||
expectErrors: true,
|
||||
expectedErrorSubString: "cannot specify key without cert",
|
||||
validate: (&CSRSigningControllerOptions{
|
||||
&csrsigningconfig.CSRSigningControllerConfiguration{
|
||||
ClusterSigningCertFile: "",
|
||||
@ -854,6 +866,7 @@ func TestValidateControllersOptions(t *testing.T) {
|
||||
{
|
||||
name: "CSRSigningControllerOptions LegacyUnknownSignerConfiguration no key file",
|
||||
expectErrors: true,
|
||||
expectedErrorSubString: "cannot specify cert without key",
|
||||
validate: (&CSRSigningControllerOptions{
|
||||
&csrsigningconfig.CSRSigningControllerConfiguration{
|
||||
ClusterSigningCertFile: "",
|
||||
@ -881,6 +894,7 @@ func TestValidateControllersOptions(t *testing.T) {
|
||||
{
|
||||
name: "CSRSigningControllerOptions specific file set along with cluster single signing file",
|
||||
expectErrors: true,
|
||||
expectedErrorSubString: "cannot specify --cluster-signing-{cert,key}-file and other --cluster-signing-*-file flags at the same time",
|
||||
validate: (&CSRSigningControllerOptions{
|
||||
&csrsigningconfig.CSRSigningControllerConfiguration{
|
||||
ClusterSigningCertFile: "/cluster-signing-cert-file",
|
||||
@ -908,6 +922,7 @@ func TestValidateControllersOptions(t *testing.T) {
|
||||
{
|
||||
name: "EndpointSliceControllerOptions ConcurrentServiceEndpointSyncs lower than minConcurrentServiceEndpointSyncs (1)",
|
||||
expectErrors: true,
|
||||
expectedErrorSubString: "concurrent-service-endpoint-syncs must not be less than 1",
|
||||
validate: (&EndpointSliceControllerOptions{
|
||||
&endpointsliceconfig.EndpointSliceControllerConfiguration{
|
||||
ConcurrentServiceEndpointSyncs: 0,
|
||||
@ -918,6 +933,7 @@ func TestValidateControllersOptions(t *testing.T) {
|
||||
{
|
||||
name: "EndpointSliceControllerOptions ConcurrentServiceEndpointSyncs greater than maxConcurrentServiceEndpointSyncs (50)",
|
||||
expectErrors: true,
|
||||
expectedErrorSubString: "concurrent-service-endpoint-syncs must not be more than 50",
|
||||
validate: (&EndpointSliceControllerOptions{
|
||||
&endpointsliceconfig.EndpointSliceControllerConfiguration{
|
||||
ConcurrentServiceEndpointSyncs: 51,
|
||||
@ -928,6 +944,7 @@ func TestValidateControllersOptions(t *testing.T) {
|
||||
{
|
||||
name: "EndpointSliceControllerOptions MaxEndpointsPerSlice lower than minMaxEndpointsPerSlice (1)",
|
||||
expectErrors: true,
|
||||
expectedErrorSubString: "max-endpoints-per-slice must not be less than 1",
|
||||
validate: (&EndpointSliceControllerOptions{
|
||||
&endpointsliceconfig.EndpointSliceControllerConfiguration{
|
||||
ConcurrentServiceEndpointSyncs: 10,
|
||||
@ -938,6 +955,7 @@ func TestValidateControllersOptions(t *testing.T) {
|
||||
{
|
||||
name: "EndpointSliceControllerOptions MaxEndpointsPerSlice greater than maxMaxEndpointsPerSlice (1000)",
|
||||
expectErrors: true,
|
||||
expectedErrorSubString: "max-endpoints-per-slice must not be more than 1000",
|
||||
validate: (&EndpointSliceControllerOptions{
|
||||
&endpointsliceconfig.EndpointSliceControllerConfiguration{
|
||||
ConcurrentServiceEndpointSyncs: 10,
|
||||
@ -948,6 +966,7 @@ func TestValidateControllersOptions(t *testing.T) {
|
||||
{
|
||||
name: "EndpointSliceMirroringControllerOptions MirroringConcurrentServiceEndpointSyncs lower than mirroringMinConcurrentServiceEndpointSyncs (1)",
|
||||
expectErrors: true,
|
||||
expectedErrorSubString: "mirroring-concurrent-service-endpoint-syncs must not be less than 1",
|
||||
validate: (&EndpointSliceMirroringControllerOptions{
|
||||
&endpointslicemirroringconfig.EndpointSliceMirroringControllerConfiguration{
|
||||
MirroringConcurrentServiceEndpointSyncs: 0,
|
||||
@ -958,6 +977,7 @@ func TestValidateControllersOptions(t *testing.T) {
|
||||
{
|
||||
name: "EndpointSliceMirroringControllerOptions MirroringConcurrentServiceEndpointSyncs greater than mirroringMaxConcurrentServiceEndpointSyncs (50)",
|
||||
expectErrors: true,
|
||||
expectedErrorSubString: "mirroring-concurrent-service-endpoint-syncs must not be more than 50",
|
||||
validate: (&EndpointSliceMirroringControllerOptions{
|
||||
&endpointslicemirroringconfig.EndpointSliceMirroringControllerConfiguration{
|
||||
MirroringConcurrentServiceEndpointSyncs: 51,
|
||||
@ -968,6 +988,7 @@ func TestValidateControllersOptions(t *testing.T) {
|
||||
{
|
||||
name: "EndpointSliceMirroringControllerOptions MirroringMaxEndpointsPerSubset lower than mirroringMinMaxEndpointsPerSubset (1)",
|
||||
expectErrors: true,
|
||||
expectedErrorSubString: "mirroring-max-endpoints-per-subset must not be less than 1",
|
||||
validate: (&EndpointSliceMirroringControllerOptions{
|
||||
&endpointslicemirroringconfig.EndpointSliceMirroringControllerConfiguration{
|
||||
MirroringConcurrentServiceEndpointSyncs: 10,
|
||||
@ -978,6 +999,7 @@ func TestValidateControllersOptions(t *testing.T) {
|
||||
{
|
||||
name: "EndpointSliceMirroringControllerOptions MirroringMaxEndpointsPerSubset greater than mirroringMaxMaxEndpointsPerSubset (1000)",
|
||||
expectErrors: true,
|
||||
expectedErrorSubString: "mirroring-max-endpoints-per-subset must not be more than 1000",
|
||||
validate: (&EndpointSliceMirroringControllerOptions{
|
||||
&endpointslicemirroringconfig.EndpointSliceMirroringControllerConfiguration{
|
||||
MirroringConcurrentServiceEndpointSyncs: 10,
|
||||
@ -988,6 +1010,7 @@ func TestValidateControllersOptions(t *testing.T) {
|
||||
{
|
||||
name: "EphemeralVolumeControllerOptions ConcurrentEphemeralVolumeSyncs equal 0",
|
||||
expectErrors: true,
|
||||
expectedErrorSubString: "concurrent-ephemeralvolume-syncs must be greater than 0",
|
||||
validate: (&EphemeralVolumeControllerOptions{
|
||||
&ephemeralvolumeconfig.EphemeralVolumeControllerConfiguration{
|
||||
ConcurrentEphemeralVolumeSyncs: 0,
|
||||
@ -997,6 +1020,7 @@ func TestValidateControllersOptions(t *testing.T) {
|
||||
{
|
||||
name: "HPAControllerOptions ConcurrentHorizontalPodAutoscalerSyncs equal 0",
|
||||
expectErrors: true,
|
||||
expectedErrorSubString: "concurrent-horizontal-pod-autoscaler-syncs must be greater than 0",
|
||||
validate: (&HPAControllerOptions{
|
||||
&poautosclerconfig.HPAControllerConfiguration{
|
||||
ConcurrentHorizontalPodAutoscalerSyncs: 0,
|
||||
@ -1013,6 +1037,7 @@ func TestValidateControllersOptions(t *testing.T) {
|
||||
{
|
||||
name: "NodeIPAMControllerOptions service cluster ip range more than two entries",
|
||||
expectErrors: true,
|
||||
expectedErrorSubString: "--service-cluster-ip-range can not contain more than two entries",
|
||||
validate: (&NodeIPAMControllerOptions{
|
||||
&nodeipamconfig.NodeIPAMControllerConfiguration{
|
||||
ServiceCIDR: "10.0.0.0/16,244.0.0.0/16,3000::/108",
|
||||
@ -1025,6 +1050,7 @@ func TestValidateControllersOptions(t *testing.T) {
|
||||
{
|
||||
name: "PersistentVolumeBinderControllerOptions bad cidr deny list",
|
||||
expectErrors: true,
|
||||
expectedErrorSubString: "bad --volume-host-ip-denylist/--volume-host-allow-local-loopback failed to parse cidr",
|
||||
validate: (&PersistentVolumeBinderControllerOptions{
|
||||
&persistentvolumeconfig.PersistentVolumeBinderControllerConfiguration{
|
||||
PVClaimBinderSyncPeriod: metav1.Duration{Duration: 30 * time.Second},
|
||||
@ -1048,6 +1074,7 @@ func TestValidateControllersOptions(t *testing.T) {
|
||||
{
|
||||
name: "StatefulSetControllerOptions ConcurrentStatefulSetSyncs equal 0",
|
||||
expectErrors: true,
|
||||
expectedErrorSubString: "concurrent-statefulset-syncs must be greater than 0",
|
||||
validate: (&StatefulSetControllerOptions{
|
||||
&statefulsetconfig.StatefulSetControllerConfiguration{
|
||||
ConcurrentStatefulSetSyncs: 0,
|
||||
@ -1215,6 +1242,12 @@ func TestValidateControllersOptions(t *testing.T) {
|
||||
if len(errs) == 0 && tc.expectErrors {
|
||||
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)
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user