k8s.io/kubernetes@v1.29.3/pkg/registry/admissionregistration/validatingwebhookconfiguration/storage/storage_test.go (about)

     1  /*
     2  Copyright 2021 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 storage
    18  
    19  import (
    20  	"testing"
    21  
    22  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    23  	"k8s.io/apimachinery/pkg/fields"
    24  	"k8s.io/apimachinery/pkg/labels"
    25  	"k8s.io/apimachinery/pkg/runtime"
    26  	"k8s.io/apiserver/pkg/registry/generic"
    27  	genericregistrytest "k8s.io/apiserver/pkg/registry/generic/testing"
    28  	etcd3testing "k8s.io/apiserver/pkg/storage/etcd3/testing"
    29  	"k8s.io/kubernetes/pkg/apis/admissionregistration"
    30  	"k8s.io/kubernetes/pkg/registry/registrytest"
    31  
    32  	// Ensure that admissionregistration package is initialized.
    33  	_ "k8s.io/kubernetes/pkg/apis/admissionregistration/install"
    34  )
    35  
    36  func TestCreate(t *testing.T) {
    37  	storage, server := newStorage(t)
    38  	defer server.Terminate(t)
    39  	defer storage.Store.DestroyFunc()
    40  	test := genericregistrytest.New(t, storage.Store).ClusterScope()
    41  	configuration := validValidatingWebhookConfiguration()
    42  	test.TestCreate(
    43  		// valid
    44  		configuration,
    45  		// invalid
    46  		newValidatingWebhookConfiguration(""),
    47  	)
    48  }
    49  
    50  func TestUpdate(t *testing.T) {
    51  	storage, server := newStorage(t)
    52  	defer server.Terminate(t)
    53  	defer storage.Store.DestroyFunc()
    54  	test := genericregistrytest.New(t, storage.Store).ClusterScope()
    55  
    56  	test.TestUpdate(
    57  		// valid
    58  		validValidatingWebhookConfiguration(),
    59  		// updateFunc
    60  		func(obj runtime.Object) runtime.Object {
    61  			object := obj.(*admissionregistration.ValidatingWebhookConfiguration)
    62  			object.Labels = map[string]string{"c": "d"}
    63  			return object
    64  		},
    65  		// invalid updateFunc
    66  		func(obj runtime.Object) runtime.Object {
    67  			object := obj.(*admissionregistration.ValidatingWebhookConfiguration)
    68  			object.Name = ""
    69  			return object
    70  		},
    71  	)
    72  }
    73  
    74  func TestGet(t *testing.T) {
    75  	storage, server := newStorage(t)
    76  	defer server.Terminate(t)
    77  	defer storage.Store.DestroyFunc()
    78  	test := genericregistrytest.New(t, storage.Store).ClusterScope()
    79  	test.TestGet(validValidatingWebhookConfiguration())
    80  }
    81  
    82  func TestList(t *testing.T) {
    83  	storage, server := newStorage(t)
    84  	defer server.Terminate(t)
    85  	defer storage.Store.DestroyFunc()
    86  	test := genericregistrytest.New(t, storage.Store).ClusterScope()
    87  	test.TestList(validValidatingWebhookConfiguration())
    88  }
    89  
    90  func TestDelete(t *testing.T) {
    91  	storage, server := newStorage(t)
    92  	defer server.Terminate(t)
    93  	defer storage.Store.DestroyFunc()
    94  	test := genericregistrytest.New(t, storage.Store).ClusterScope()
    95  	test.TestDelete(validValidatingWebhookConfiguration())
    96  }
    97  
    98  func TestWatch(t *testing.T) {
    99  	storage, server := newStorage(t)
   100  	defer server.Terminate(t)
   101  	defer storage.Store.DestroyFunc()
   102  	test := genericregistrytest.New(t, storage.Store).ClusterScope()
   103  	test.TestWatch(
   104  		validValidatingWebhookConfiguration(),
   105  		[]labels.Set{},
   106  		[]labels.Set{
   107  			{"hoo": "bar"},
   108  		},
   109  		[]fields.Set{
   110  			{"metadata.name": "foo"},
   111  		},
   112  		[]fields.Set{
   113  			{"metadata.name": "nomatch"},
   114  		},
   115  	)
   116  }
   117  
   118  func validValidatingWebhookConfiguration() *admissionregistration.ValidatingWebhookConfiguration {
   119  	ignore := admissionregistration.Ignore
   120  	exact := admissionregistration.Exact
   121  	thirty := int32(30)
   122  	none := admissionregistration.SideEffectClassNone
   123  	servicePath := "/"
   124  	return &admissionregistration.ValidatingWebhookConfiguration{
   125  		ObjectMeta: metav1.ObjectMeta{
   126  			Name: "foo",
   127  		},
   128  		Webhooks: []admissionregistration.ValidatingWebhook{{
   129  			Name: "foo.example.io",
   130  			ClientConfig: admissionregistration.WebhookClientConfig{
   131  				Service: &admissionregistration.ServiceReference{
   132  					Name:      "foo",
   133  					Namespace: "bar",
   134  					Path:      &servicePath,
   135  					Port:      443,
   136  				},
   137  			},
   138  			FailurePolicy:           &ignore,
   139  			MatchPolicy:             &exact,
   140  			TimeoutSeconds:          &thirty,
   141  			NamespaceSelector:       &metav1.LabelSelector{},
   142  			ObjectSelector:          &metav1.LabelSelector{},
   143  			SideEffects:             &none,
   144  			AdmissionReviewVersions: []string{"v1beta1"},
   145  		}},
   146  	}
   147  }
   148  
   149  func newValidatingWebhookConfiguration(name string) *admissionregistration.ValidatingWebhookConfiguration {
   150  	ignore := admissionregistration.Ignore
   151  	exact := admissionregistration.Exact
   152  	thirty := int32(30)
   153  	none := admissionregistration.SideEffectClassNone
   154  	servicePath := "/"
   155  	return &admissionregistration.ValidatingWebhookConfiguration{
   156  		ObjectMeta: metav1.ObjectMeta{
   157  			Name:   name,
   158  			Labels: map[string]string{"foo": "bar"},
   159  		},
   160  		Webhooks: []admissionregistration.ValidatingWebhook{{
   161  			Name: "foo.example.io",
   162  			ClientConfig: admissionregistration.WebhookClientConfig{
   163  				Service: &admissionregistration.ServiceReference{
   164  					Name:      "foo",
   165  					Namespace: "bar",
   166  					Path:      &servicePath,
   167  					Port:      443,
   168  				},
   169  			},
   170  			FailurePolicy:           &ignore,
   171  			MatchPolicy:             &exact,
   172  			TimeoutSeconds:          &thirty,
   173  			NamespaceSelector:       &metav1.LabelSelector{},
   174  			ObjectSelector:          &metav1.LabelSelector{},
   175  			SideEffects:             &none,
   176  			AdmissionReviewVersions: []string{"v1beta1"},
   177  		}},
   178  	}
   179  }
   180  
   181  func newStorage(t *testing.T) (*REST, *etcd3testing.EtcdTestServer) {
   182  	etcdStorage, server := registrytest.NewEtcdStorage(t, admissionregistration.GroupName)
   183  	restOptions := generic.RESTOptions{
   184  		StorageConfig:           etcdStorage,
   185  		Decorator:               generic.UndecoratedStorage,
   186  		DeleteCollectionWorkers: 1,
   187  		ResourcePrefix:          "validatingwebhookconfigurations"}
   188  	storage, err := NewREST(restOptions)
   189  	if err != nil {
   190  		t.Fatalf("unexpected error from REST storage: %v", err)
   191  	}
   192  	return storage, server
   193  }
   194  
   195  func TestCategories(t *testing.T) {
   196  	storage, server := newStorage(t)
   197  	defer server.Terminate(t)
   198  	defer storage.Store.DestroyFunc()
   199  	expected := []string{"api-extensions"}
   200  	registrytest.AssertCategories(t, storage, expected)
   201  }