...

Source file src/k8s.io/kubernetes/pkg/registry/apps/daemonset/storage/storage_test.go

Documentation: k8s.io/kubernetes/pkg/registry/apps/daemonset/storage

     1  /*
     2  Copyright 2015 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/apps"
    30  	api "k8s.io/kubernetes/pkg/apis/core"
    31  	"k8s.io/kubernetes/pkg/registry/registrytest"
    32  )
    33  
    34  func newStorage(t *testing.T) (*REST, *StatusREST, *etcd3testing.EtcdTestServer) {
    35  	etcdStorage, server := registrytest.NewEtcdStorage(t, apps.GroupName)
    36  	restOptions := generic.RESTOptions{
    37  		StorageConfig:           etcdStorage,
    38  		Decorator:               generic.UndecoratedStorage,
    39  		DeleteCollectionWorkers: 1,
    40  		ResourcePrefix:          "daemonsets",
    41  	}
    42  	daemonSetStorage, statusStorage, err := NewREST(restOptions)
    43  	if err != nil {
    44  		t.Fatalf("unexpected error from REST storage: %v", err)
    45  	}
    46  	return daemonSetStorage, statusStorage, server
    47  }
    48  
    49  func newValidDaemonSet() *apps.DaemonSet {
    50  	return &apps.DaemonSet{
    51  		ObjectMeta: metav1.ObjectMeta{
    52  			Name:      "foo",
    53  			Namespace: metav1.NamespaceDefault,
    54  			Labels:    map[string]string{"a": "b"},
    55  		},
    56  		Spec: apps.DaemonSetSpec{
    57  			UpdateStrategy: apps.DaemonSetUpdateStrategy{
    58  				Type: apps.OnDeleteDaemonSetStrategyType,
    59  			},
    60  			Selector: &metav1.LabelSelector{MatchLabels: map[string]string{"a": "b"}},
    61  			Template: api.PodTemplateSpec{
    62  				ObjectMeta: metav1.ObjectMeta{
    63  					Labels: map[string]string{"a": "b"},
    64  				},
    65  				Spec: api.PodSpec{
    66  					Containers: []api.Container{
    67  						{
    68  							Name:                     "test",
    69  							Image:                    "test_image",
    70  							ImagePullPolicy:          api.PullIfNotPresent,
    71  							TerminationMessagePolicy: api.TerminationMessageReadFile,
    72  						},
    73  					},
    74  					RestartPolicy: api.RestartPolicyAlways,
    75  					DNSPolicy:     api.DNSClusterFirst,
    76  				},
    77  			},
    78  		},
    79  	}
    80  }
    81  
    82  var validDaemonSet = newValidDaemonSet()
    83  
    84  func TestCreate(t *testing.T) {
    85  	storage, _, server := newStorage(t)
    86  	defer server.Terminate(t)
    87  	defer storage.Store.DestroyFunc()
    88  	test := genericregistrytest.New(t, storage.Store)
    89  	ds := newValidDaemonSet()
    90  	ds.ObjectMeta = metav1.ObjectMeta{}
    91  	test.TestCreate(
    92  		// valid
    93  		ds,
    94  		// invalid (invalid selector)
    95  		&apps.DaemonSet{
    96  			Spec: apps.DaemonSetSpec{
    97  				Selector: &metav1.LabelSelector{MatchLabels: map[string]string{}},
    98  				Template: validDaemonSet.Spec.Template,
    99  			},
   100  		},
   101  		// invalid update strategy
   102  		&apps.DaemonSet{
   103  			Spec: apps.DaemonSetSpec{
   104  				Selector: validDaemonSet.Spec.Selector,
   105  				Template: validDaemonSet.Spec.Template,
   106  			},
   107  		},
   108  	)
   109  }
   110  
   111  func TestUpdate(t *testing.T) {
   112  	storage, _, server := newStorage(t)
   113  	defer server.Terminate(t)
   114  	defer storage.Store.DestroyFunc()
   115  	test := genericregistrytest.New(t, storage.Store)
   116  	test.TestUpdate(
   117  		// valid
   118  		newValidDaemonSet(),
   119  		// updateFunc
   120  		func(obj runtime.Object) runtime.Object {
   121  			object := obj.(*apps.DaemonSet)
   122  			object.Spec.Template.Spec.NodeSelector = map[string]string{"c": "d"}
   123  			object.Spec.Template.Spec.DNSPolicy = api.DNSDefault
   124  			return object
   125  		},
   126  		// invalid updateFunc
   127  		func(obj runtime.Object) runtime.Object {
   128  			object := obj.(*apps.DaemonSet)
   129  			object.Name = ""
   130  			return object
   131  		},
   132  		func(obj runtime.Object) runtime.Object {
   133  			object := obj.(*apps.DaemonSet)
   134  			object.Spec.Template.Spec.RestartPolicy = api.RestartPolicyOnFailure
   135  			return object
   136  		},
   137  	)
   138  }
   139  
   140  func TestDelete(t *testing.T) {
   141  	storage, _, server := newStorage(t)
   142  	defer server.Terminate(t)
   143  	defer storage.Store.DestroyFunc()
   144  	test := genericregistrytest.New(t, storage.Store)
   145  	test.TestDelete(newValidDaemonSet())
   146  }
   147  
   148  func TestGet(t *testing.T) {
   149  	storage, _, server := newStorage(t)
   150  	defer server.Terminate(t)
   151  	defer storage.Store.DestroyFunc()
   152  	test := genericregistrytest.New(t, storage.Store)
   153  	test.TestGet(newValidDaemonSet())
   154  }
   155  
   156  func TestList(t *testing.T) {
   157  	storage, _, server := newStorage(t)
   158  	defer server.Terminate(t)
   159  	defer storage.Store.DestroyFunc()
   160  	test := genericregistrytest.New(t, storage.Store)
   161  	test.TestList(newValidDaemonSet())
   162  }
   163  
   164  func TestWatch(t *testing.T) {
   165  	storage, _, server := newStorage(t)
   166  	defer server.Terminate(t)
   167  	defer storage.Store.DestroyFunc()
   168  	test := genericregistrytest.New(t, storage.Store)
   169  	test.TestWatch(
   170  		validDaemonSet,
   171  		// matching labels
   172  		[]labels.Set{
   173  			{"a": "b"},
   174  		},
   175  		// not matching labels
   176  		[]labels.Set{
   177  			{"a": "c"},
   178  			{"foo": "bar"},
   179  		},
   180  		// matching fields
   181  		[]fields.Set{
   182  			{"metadata.name": "foo"},
   183  		},
   184  		// notmatching fields
   185  		[]fields.Set{
   186  			{"metadata.name": "bar"},
   187  			{"name": "foo"},
   188  		},
   189  	)
   190  }
   191  
   192  func TestShortNames(t *testing.T) {
   193  	storage, _, server := newStorage(t)
   194  	defer server.Terminate(t)
   195  	defer storage.Store.DestroyFunc()
   196  	expected := []string{"ds"}
   197  	registrytest.AssertShortNames(t, storage, expected)
   198  }
   199  
   200  // TODO TestUpdateStatus
   201  

View as plain text