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

     1  /*
     2  Copyright 2014 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  	api "k8s.io/kubernetes/pkg/apis/core"
    30  	"k8s.io/kubernetes/pkg/registry/registrytest"
    31  )
    32  
    33  func newStorage(t *testing.T) (*REST, *etcd3testing.EtcdTestServer) {
    34  	etcdStorage, server := registrytest.NewEtcdStorage(t, "")
    35  	restOptions := generic.RESTOptions{
    36  		StorageConfig:           etcdStorage,
    37  		Decorator:               generic.UndecoratedStorage,
    38  		DeleteCollectionWorkers: 1,
    39  		ResourcePrefix:          "podtemplates",
    40  	}
    41  	rest, err := NewREST(restOptions)
    42  	if err != nil {
    43  		t.Fatalf("unexpected error from REST storage: %v", err)
    44  	}
    45  	return rest, server
    46  }
    47  
    48  func validNewPodTemplate(name string) *api.PodTemplate {
    49  	return &api.PodTemplate{
    50  		ObjectMeta: metav1.ObjectMeta{
    51  			Name:      name,
    52  			Namespace: metav1.NamespaceDefault,
    53  		},
    54  		Template: api.PodTemplateSpec{
    55  			ObjectMeta: metav1.ObjectMeta{
    56  				Labels: map[string]string{"test": "foo"},
    57  			},
    58  			Spec: api.PodSpec{
    59  				RestartPolicy: api.RestartPolicyAlways,
    60  				DNSPolicy:     api.DNSClusterFirst,
    61  				Containers: []api.Container{
    62  					{
    63  						Name:            "foo",
    64  						Image:           "test",
    65  						ImagePullPolicy: api.PullAlways,
    66  
    67  						TerminationMessagePath:   api.TerminationMessagePathDefault,
    68  						TerminationMessagePolicy: api.TerminationMessageReadFile,
    69  					},
    70  				},
    71  			},
    72  		},
    73  	}
    74  }
    75  
    76  func TestCreate(t *testing.T) {
    77  	storage, server := newStorage(t)
    78  	defer server.Terminate(t)
    79  	defer storage.Store.DestroyFunc()
    80  	test := genericregistrytest.New(t, storage.Store)
    81  	pod := validNewPodTemplate("foo")
    82  	pod.ObjectMeta = metav1.ObjectMeta{}
    83  	test.TestCreate(
    84  		// valid
    85  		pod,
    86  		// invalid
    87  		&api.PodTemplate{
    88  			Template: api.PodTemplateSpec{},
    89  		},
    90  	)
    91  }
    92  
    93  func TestUpdate(t *testing.T) {
    94  	storage, server := newStorage(t)
    95  	defer server.Terminate(t)
    96  	defer storage.Store.DestroyFunc()
    97  	test := genericregistrytest.New(t, storage.Store)
    98  	test.TestUpdate(
    99  		//valid
   100  		validNewPodTemplate("foo"),
   101  		// updateFunc
   102  		func(obj runtime.Object) runtime.Object {
   103  			object := obj.(*api.PodTemplate)
   104  			object.Template.Spec.NodeSelector = map[string]string{"a": "b"}
   105  			return object
   106  		},
   107  	)
   108  }
   109  
   110  func TestDelete(t *testing.T) {
   111  	storage, server := newStorage(t)
   112  	defer server.Terminate(t)
   113  	defer storage.Store.DestroyFunc()
   114  	test := genericregistrytest.New(t, storage.Store).ReturnDeletedObject()
   115  	test.TestDelete(validNewPodTemplate("foo"))
   116  }
   117  
   118  func TestGet(t *testing.T) {
   119  	storage, server := newStorage(t)
   120  	defer server.Terminate(t)
   121  	defer storage.Store.DestroyFunc()
   122  	test := genericregistrytest.New(t, storage.Store)
   123  	test.TestGet(validNewPodTemplate("foo"))
   124  }
   125  
   126  func TestList(t *testing.T) {
   127  	storage, server := newStorage(t)
   128  	defer server.Terminate(t)
   129  	defer storage.Store.DestroyFunc()
   130  	test := genericregistrytest.New(t, storage.Store)
   131  	test.TestList(validNewPodTemplate("foo"))
   132  }
   133  
   134  func TestWatch(t *testing.T) {
   135  	storage, server := newStorage(t)
   136  	defer server.Terminate(t)
   137  	defer storage.Store.DestroyFunc()
   138  	test := genericregistrytest.New(t, storage.Store)
   139  	test.TestWatch(
   140  		validNewPodTemplate("foo"),
   141  		// matching labels
   142  		[]labels.Set{},
   143  		// not matching labels
   144  		[]labels.Set{
   145  			{"foo": "bar"},
   146  		},
   147  		// matching fields
   148  		[]fields.Set{},
   149  		// not matching fields
   150  		[]fields.Set{
   151  			{"metadata.name": "bar"},
   152  			{"name": "foo"},
   153  		},
   154  	)
   155  }