mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-22 11:21:47 +00:00
Merge pull request #113823 from TommyStarK/unit-tests/cmd-kube-apiserver-app-options
cmd/kube-apiserver/app/options: Improving test coverage
This commit is contained in:
commit
6d823a3815
@ -22,7 +22,10 @@ import (
|
|||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
utilnet "k8s.io/apimachinery/pkg/util/net"
|
utilnet "k8s.io/apimachinery/pkg/util/net"
|
||||||
|
kubeapiserveradmission "k8s.io/apiserver/pkg/admission"
|
||||||
genericoptions "k8s.io/apiserver/pkg/server/options"
|
genericoptions "k8s.io/apiserver/pkg/server/options"
|
||||||
|
basemetrics "k8s.io/component-base/metrics"
|
||||||
|
kubeoptions "k8s.io/kubernetes/pkg/kubeapiserver/options"
|
||||||
netutils "k8s.io/utils/net"
|
netutils "k8s.io/utils/net"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -89,7 +92,11 @@ func TestClusterServiceIPRange(t *testing.T) {
|
|||||||
expectErrors: true,
|
expectErrors: true,
|
||||||
options: makeOptionsWithCIDRs("10.0.0.0/16", "3000::/64"),
|
options: makeOptionsWithCIDRs("10.0.0.0/16", "3000::/64"),
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
name: "more than two entries",
|
||||||
|
expectErrors: true,
|
||||||
|
options: makeOptionsWithCIDRs("10.0.0.0/16,244.0.0.0/16", "3000::/108"),
|
||||||
|
},
|
||||||
/* success cases */
|
/* success cases */
|
||||||
{
|
{
|
||||||
name: "valid primary",
|
name: "valid primary",
|
||||||
@ -218,7 +225,7 @@ func TestValidateMaxCIDRRange(t *testing.T) {
|
|||||||
expectErrors: false,
|
expectErrors: false,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "ipv4 cidr to big",
|
name: "ipv4 cidr too big",
|
||||||
cidr: *getIPnetFromCIDR("10.92.0.0/8"),
|
cidr: *getIPnetFromCIDR("10.92.0.0/8"),
|
||||||
maxCIDRBits: 20,
|
maxCIDRBits: 20,
|
||||||
cidrFlag: "--service-cluster-ip-range",
|
cidrFlag: "--service-cluster-ip-range",
|
||||||
@ -226,7 +233,7 @@ func TestValidateMaxCIDRRange(t *testing.T) {
|
|||||||
expectErrors: true,
|
expectErrors: true,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "ipv6 cidr to big",
|
name: "ipv6 cidr too big",
|
||||||
cidr: *getIPnetFromCIDR("3000::/64"),
|
cidr: *getIPnetFromCIDR("3000::/64"),
|
||||||
maxCIDRBits: 20,
|
maxCIDRBits: 20,
|
||||||
cidrFlag: "--service-cluster-ip-range",
|
cidrFlag: "--service-cluster-ip-range",
|
||||||
@ -279,6 +286,10 @@ func TestValidateAPIPriorityAndFairness(t *testing.T) {
|
|||||||
runtimeConfig: "flowcontrol.apiserver.k8s.io/v1beta3=false",
|
runtimeConfig: "flowcontrol.apiserver.k8s.io/v1beta3=false",
|
||||||
errShouldContain: conflict,
|
errShouldContain: conflict,
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
runtimeConfig: "flowcontrol.apiserver.k8s.io/v1beta3=true",
|
||||||
|
errShouldContain: "",
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, test := range tests {
|
for _, test := range tests {
|
||||||
@ -301,3 +312,91 @@ func TestValidateAPIPriorityAndFairness(t *testing.T) {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestValidateServerRunOptions(t *testing.T) {
|
||||||
|
cidrOpts := makeOptionsWithCIDRs("10.0.0.0/16", "3000::/64")
|
||||||
|
nodePortOpts := makeOptionsWithPort(-1, 30065, 1)
|
||||||
|
|
||||||
|
testCases := []struct {
|
||||||
|
name string
|
||||||
|
options *ServerRunOptions
|
||||||
|
expectErrors bool
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
name: "validate master count equal 0",
|
||||||
|
expectErrors: true,
|
||||||
|
options: &ServerRunOptions{
|
||||||
|
MasterCount: 0,
|
||||||
|
GenericServerRunOptions: &genericoptions.ServerRunOptions{},
|
||||||
|
Etcd: &genericoptions.EtcdOptions{},
|
||||||
|
SecureServing: &genericoptions.SecureServingOptionsWithLoopback{},
|
||||||
|
Audit: &genericoptions.AuditOptions{},
|
||||||
|
Admission: &kubeoptions.AdmissionOptions{
|
||||||
|
GenericAdmission: &genericoptions.AdmissionOptions{
|
||||||
|
EnablePlugins: []string{"foo"},
|
||||||
|
Plugins: kubeapiserveradmission.NewPlugins(),
|
||||||
|
},
|
||||||
|
PluginNames: []string{"foo"},
|
||||||
|
},
|
||||||
|
Authentication: &kubeoptions.BuiltInAuthenticationOptions{
|
||||||
|
APIAudiences: []string{"bar"},
|
||||||
|
ServiceAccounts: &kubeoptions.ServiceAccountAuthenticationOptions{
|
||||||
|
Issuers: []string{"baz"},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
Authorization: &kubeoptions.BuiltInAuthorizationOptions{},
|
||||||
|
APIEnablement: genericoptions.NewAPIEnablementOptions(),
|
||||||
|
Metrics: &basemetrics.Options{},
|
||||||
|
ServiceClusterIPRanges: cidrOpts.ServiceClusterIPRanges,
|
||||||
|
PrimaryServiceClusterIPRange: cidrOpts.PrimaryServiceClusterIPRange,
|
||||||
|
SecondaryServiceClusterIPRange: cidrOpts.SecondaryServiceClusterIPRange,
|
||||||
|
ServiceNodePortRange: nodePortOpts.ServiceNodePortRange,
|
||||||
|
KubernetesServiceNodePort: nodePortOpts.KubernetesServiceNodePort,
|
||||||
|
ServiceAccountSigningKeyFile: "",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "validate token request enable not attempted",
|
||||||
|
expectErrors: true,
|
||||||
|
options: &ServerRunOptions{
|
||||||
|
MasterCount: 1,
|
||||||
|
GenericServerRunOptions: &genericoptions.ServerRunOptions{},
|
||||||
|
Etcd: &genericoptions.EtcdOptions{},
|
||||||
|
SecureServing: &genericoptions.SecureServingOptionsWithLoopback{},
|
||||||
|
Audit: &genericoptions.AuditOptions{},
|
||||||
|
Admission: &kubeoptions.AdmissionOptions{
|
||||||
|
GenericAdmission: &genericoptions.AdmissionOptions{
|
||||||
|
EnablePlugins: []string{""},
|
||||||
|
Plugins: kubeapiserveradmission.NewPlugins(),
|
||||||
|
},
|
||||||
|
PluginNames: []string{""},
|
||||||
|
},
|
||||||
|
Authentication: &kubeoptions.BuiltInAuthenticationOptions{
|
||||||
|
ServiceAccounts: &kubeoptions.ServiceAccountAuthenticationOptions{},
|
||||||
|
},
|
||||||
|
Authorization: &kubeoptions.BuiltInAuthorizationOptions{},
|
||||||
|
APIEnablement: genericoptions.NewAPIEnablementOptions(),
|
||||||
|
Metrics: &basemetrics.Options{},
|
||||||
|
ServiceClusterIPRanges: cidrOpts.ServiceClusterIPRanges,
|
||||||
|
PrimaryServiceClusterIPRange: cidrOpts.PrimaryServiceClusterIPRange,
|
||||||
|
SecondaryServiceClusterIPRange: cidrOpts.SecondaryServiceClusterIPRange,
|
||||||
|
ServiceNodePortRange: nodePortOpts.ServiceNodePortRange,
|
||||||
|
KubernetesServiceNodePort: nodePortOpts.KubernetesServiceNodePort,
|
||||||
|
ServiceAccountSigningKeyFile: "",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, tc := range testCases {
|
||||||
|
t.Run(tc.name, func(t *testing.T) {
|
||||||
|
errs := tc.options.Validate()
|
||||||
|
if len(errs) > 0 && !tc.expectErrors {
|
||||||
|
t.Errorf("expected no errors, errors found %+v", errs)
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(errs) == 0 && tc.expectErrors {
|
||||||
|
t.Errorf("expected errors, no errors found")
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user