k8s.io/kubernetes@v1.29.3/pkg/kubeapiserver/admission/initializer_test.go (about)

     1  /*
     2  Copyright 2016 The Kubernetes Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package admission
    18  
    19  import (
    20  	"context"
    21  	"testing"
    22  
    23  	"k8s.io/apimachinery/pkg/api/meta"
    24  	"k8s.io/apimachinery/pkg/runtime/schema"
    25  	"k8s.io/apiserver/pkg/admission"
    26  	quota "k8s.io/apiserver/pkg/quota/v1"
    27  )
    28  
    29  type doNothingAdmission struct{}
    30  
    31  func (doNothingAdmission) Admit(ctx context.Context, a admission.Attributes, o admission.ObjectInterfaces) error {
    32  	return nil
    33  }
    34  func (doNothingAdmission) Handles(o admission.Operation) bool { return false }
    35  func (doNothingAdmission) Validate() error                    { return nil }
    36  
    37  type doNothingPluginInitialization struct{}
    38  
    39  func (doNothingPluginInitialization) ValidateInitialization() error { return nil }
    40  
    41  type WantsCloudConfigAdmissionPlugin struct {
    42  	doNothingAdmission
    43  	cloudConfig []byte
    44  }
    45  
    46  func (p *WantsCloudConfigAdmissionPlugin) SetCloudConfig(cloudConfig []byte) {
    47  	p.cloudConfig = cloudConfig
    48  }
    49  
    50  func TestCloudConfigAdmissionPlugin(t *testing.T) {
    51  	cloudConfig := []byte("cloud-configuration")
    52  	initializer := NewPluginInitializer(cloudConfig, nil, nil)
    53  	wantsCloudConfigAdmission := &WantsCloudConfigAdmissionPlugin{}
    54  	initializer.Initialize(wantsCloudConfigAdmission)
    55  
    56  	if wantsCloudConfigAdmission.cloudConfig == nil {
    57  		t.Errorf("Expected cloud config to be initialized but found nil")
    58  	}
    59  }
    60  
    61  type doNothingRESTMapper struct{}
    62  
    63  func (doNothingRESTMapper) KindFor(resource schema.GroupVersionResource) (schema.GroupVersionKind, error) {
    64  	return schema.GroupVersionKind{}, nil
    65  }
    66  func (doNothingRESTMapper) KindsFor(resource schema.GroupVersionResource) ([]schema.GroupVersionKind, error) {
    67  	return nil, nil
    68  }
    69  func (doNothingRESTMapper) ResourceFor(input schema.GroupVersionResource) (schema.GroupVersionResource, error) {
    70  	return schema.GroupVersionResource{}, nil
    71  }
    72  func (doNothingRESTMapper) ResourcesFor(input schema.GroupVersionResource) ([]schema.GroupVersionResource, error) {
    73  	return nil, nil
    74  }
    75  func (doNothingRESTMapper) RESTMapping(gk schema.GroupKind, versions ...string) (*meta.RESTMapping, error) {
    76  	return nil, nil
    77  }
    78  func (doNothingRESTMapper) RESTMappings(gk schema.GroupKind, versions ...string) ([]*meta.RESTMapping, error) {
    79  	return nil, nil
    80  }
    81  func (doNothingRESTMapper) ResourceSingularizer(resource string) (singular string, err error) {
    82  	return "", nil
    83  }
    84  
    85  type WantsRESTMapperAdmissionPlugin struct {
    86  	doNothingAdmission
    87  	doNothingPluginInitialization
    88  	mapper meta.RESTMapper
    89  }
    90  
    91  func (p *WantsRESTMapperAdmissionPlugin) SetRESTMapper(mapper meta.RESTMapper) {
    92  	p.mapper = mapper
    93  }
    94  
    95  func TestRESTMapperAdmissionPlugin(t *testing.T) {
    96  	mapper := doNothingRESTMapper{}
    97  	initializer := NewPluginInitializer(nil, mapper, nil)
    98  	wantsRESTMapperAdmission := &WantsRESTMapperAdmissionPlugin{}
    99  	initializer.Initialize(wantsRESTMapperAdmission)
   100  
   101  	if wantsRESTMapperAdmission.mapper == nil {
   102  		t.Errorf("Expected REST mapper to be initialized but found nil")
   103  	}
   104  }
   105  
   106  type doNothingQuotaConfiguration struct{}
   107  
   108  func (doNothingQuotaConfiguration) IgnoredResources() map[schema.GroupResource]struct{} { return nil }
   109  
   110  func (doNothingQuotaConfiguration) Evaluators() []quota.Evaluator { return nil }
   111  
   112  type WantsQuotaConfigurationAdmissionPlugin struct {
   113  	doNothingAdmission
   114  	doNothingPluginInitialization
   115  	config quota.Configuration
   116  }
   117  
   118  func (p *WantsQuotaConfigurationAdmissionPlugin) SetQuotaConfiguration(config quota.Configuration) {
   119  	p.config = config
   120  }
   121  
   122  func TestQuotaConfigurationAdmissionPlugin(t *testing.T) {
   123  	config := doNothingQuotaConfiguration{}
   124  	initializer := NewPluginInitializer(nil, nil, config)
   125  	wantsQuotaConfigurationAdmission := &WantsQuotaConfigurationAdmissionPlugin{}
   126  	initializer.Initialize(wantsQuotaConfigurationAdmission)
   127  
   128  	if wantsQuotaConfigurationAdmission.config == nil {
   129  		t.Errorf("Expected quota configuration to be initialized but found nil")
   130  	}
   131  }