mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-24 12:15:52 +00:00
kubeapiserver/admission: Improving test coverage
Signed-off-by: TommyStarK <thomasmilox@gmail.com>
This commit is contained in:
parent
e2d407c1d2
commit
bd6a86471b
@ -20,7 +20,10 @@ import (
|
||||
"context"
|
||||
"testing"
|
||||
|
||||
"k8s.io/apimachinery/pkg/api/meta"
|
||||
"k8s.io/apimachinery/pkg/runtime/schema"
|
||||
"k8s.io/apiserver/pkg/admission"
|
||||
quota "k8s.io/apiserver/pkg/quota/v1"
|
||||
)
|
||||
|
||||
type doNothingAdmission struct{}
|
||||
@ -31,6 +34,10 @@ func (doNothingAdmission) Admit(ctx context.Context, a admission.Attributes, o a
|
||||
func (doNothingAdmission) Handles(o admission.Operation) bool { return false }
|
||||
func (doNothingAdmission) Validate() error { return nil }
|
||||
|
||||
type doNothingPluginInitialization struct{}
|
||||
|
||||
func (doNothingPluginInitialization) ValidateInitialization() error { return nil }
|
||||
|
||||
type WantsCloudConfigAdmissionPlugin struct {
|
||||
doNothingAdmission
|
||||
cloudConfig []byte
|
||||
@ -50,3 +57,75 @@ func TestCloudConfigAdmissionPlugin(t *testing.T) {
|
||||
t.Errorf("Expected cloud config to be initialized but found nil")
|
||||
}
|
||||
}
|
||||
|
||||
type doNothingRESTMapper struct{}
|
||||
|
||||
func (doNothingRESTMapper) KindFor(resource schema.GroupVersionResource) (schema.GroupVersionKind, error) {
|
||||
return schema.GroupVersionKind{}, nil
|
||||
}
|
||||
func (doNothingRESTMapper) KindsFor(resource schema.GroupVersionResource) ([]schema.GroupVersionKind, error) {
|
||||
return nil, nil
|
||||
}
|
||||
func (doNothingRESTMapper) ResourceFor(input schema.GroupVersionResource) (schema.GroupVersionResource, error) {
|
||||
return schema.GroupVersionResource{}, nil
|
||||
}
|
||||
func (doNothingRESTMapper) ResourcesFor(input schema.GroupVersionResource) ([]schema.GroupVersionResource, error) {
|
||||
return nil, nil
|
||||
}
|
||||
func (doNothingRESTMapper) RESTMapping(gk schema.GroupKind, versions ...string) (*meta.RESTMapping, error) {
|
||||
return nil, nil
|
||||
}
|
||||
func (doNothingRESTMapper) RESTMappings(gk schema.GroupKind, versions ...string) ([]*meta.RESTMapping, error) {
|
||||
return nil, nil
|
||||
}
|
||||
func (doNothingRESTMapper) ResourceSingularizer(resource string) (singular string, err error) {
|
||||
return "", nil
|
||||
}
|
||||
|
||||
type WantsRESTMapperAdmissionPlugin struct {
|
||||
doNothingAdmission
|
||||
doNothingPluginInitialization
|
||||
mapper meta.RESTMapper
|
||||
}
|
||||
|
||||
func (p *WantsRESTMapperAdmissionPlugin) SetRESTMapper(mapper meta.RESTMapper) {
|
||||
p.mapper = mapper
|
||||
}
|
||||
|
||||
func TestRESTMapperAdmissionPlugin(t *testing.T) {
|
||||
mapper := doNothingRESTMapper{}
|
||||
initializer := NewPluginInitializer(nil, mapper, nil)
|
||||
wantsRESTMapperAdmission := &WantsRESTMapperAdmissionPlugin{}
|
||||
initializer.Initialize(wantsRESTMapperAdmission)
|
||||
|
||||
if wantsRESTMapperAdmission.mapper == nil {
|
||||
t.Errorf("Expected REST mapper to be initialized but found nil")
|
||||
}
|
||||
}
|
||||
|
||||
type doNothingQuotaConfiguration struct{}
|
||||
|
||||
func (doNothingQuotaConfiguration) IgnoredResources() map[schema.GroupResource]struct{} { return nil }
|
||||
|
||||
func (doNothingQuotaConfiguration) Evaluators() []quota.Evaluator { return nil }
|
||||
|
||||
type WantsQuotaConfigurationAdmissionPlugin struct {
|
||||
doNothingAdmission
|
||||
doNothingPluginInitialization
|
||||
config quota.Configuration
|
||||
}
|
||||
|
||||
func (p *WantsQuotaConfigurationAdmissionPlugin) SetQuotaConfiguration(config quota.Configuration) {
|
||||
p.config = config
|
||||
}
|
||||
|
||||
func TestQuotaConfigurationAdmissionPlugin(t *testing.T) {
|
||||
config := doNothingQuotaConfiguration{}
|
||||
initializer := NewPluginInitializer(nil, nil, config)
|
||||
wantsQuotaConfigurationAdmission := &WantsQuotaConfigurationAdmissionPlugin{}
|
||||
initializer.Initialize(wantsQuotaConfigurationAdmission)
|
||||
|
||||
if wantsQuotaConfigurationAdmission.config == nil {
|
||||
t.Errorf("Expected quota configuration to be initialized but found nil")
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user