storage_test.go 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. /*
  2. Copyright 2015 The Kubernetes Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package storage
  14. import (
  15. "testing"
  16. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  17. "k8s.io/apimachinery/pkg/fields"
  18. "k8s.io/apimachinery/pkg/labels"
  19. "k8s.io/apimachinery/pkg/runtime"
  20. "k8s.io/apiserver/pkg/registry/generic"
  21. genericregistrytest "k8s.io/apiserver/pkg/registry/generic/testing"
  22. etcdtesting "k8s.io/apiserver/pkg/storage/etcd/testing"
  23. "k8s.io/kubernetes/pkg/apis/apps"
  24. api "k8s.io/kubernetes/pkg/apis/core"
  25. "k8s.io/kubernetes/pkg/registry/registrytest"
  26. )
  27. func newStorage(t *testing.T) (*REST, *StatusREST, *etcdtesting.EtcdTestServer) {
  28. etcdStorage, server := registrytest.NewEtcdStorage(t, apps.GroupName)
  29. restOptions := generic.RESTOptions{
  30. StorageConfig: etcdStorage,
  31. Decorator: generic.UndecoratedStorage,
  32. DeleteCollectionWorkers: 1,
  33. ResourcePrefix: "daemonsets",
  34. }
  35. daemonSetStorage, statusStorage := NewREST(restOptions)
  36. return daemonSetStorage, statusStorage, server
  37. }
  38. func newValidDaemonSet() *apps.DaemonSet {
  39. return &apps.DaemonSet{
  40. ObjectMeta: metav1.ObjectMeta{
  41. Name: "foo",
  42. Namespace: metav1.NamespaceDefault,
  43. Labels: map[string]string{"a": "b"},
  44. },
  45. Spec: apps.DaemonSetSpec{
  46. UpdateStrategy: apps.DaemonSetUpdateStrategy{
  47. Type: apps.OnDeleteDaemonSetStrategyType,
  48. },
  49. Selector: &metav1.LabelSelector{MatchLabels: map[string]string{"a": "b"}},
  50. Template: api.PodTemplateSpec{
  51. ObjectMeta: metav1.ObjectMeta{
  52. Labels: map[string]string{"a": "b"},
  53. },
  54. Spec: api.PodSpec{
  55. Containers: []api.Container{
  56. {
  57. Name: "test",
  58. Image: "test_image",
  59. ImagePullPolicy: api.PullIfNotPresent,
  60. TerminationMessagePolicy: api.TerminationMessageReadFile,
  61. },
  62. },
  63. RestartPolicy: api.RestartPolicyAlways,
  64. DNSPolicy: api.DNSClusterFirst,
  65. },
  66. },
  67. },
  68. }
  69. }
  70. var validDaemonSet = newValidDaemonSet()
  71. func TestCreate(t *testing.T) {
  72. storage, _, server := newStorage(t)
  73. defer server.Terminate(t)
  74. defer storage.Store.DestroyFunc()
  75. test := genericregistrytest.New(t, storage.Store)
  76. ds := newValidDaemonSet()
  77. ds.ObjectMeta = metav1.ObjectMeta{}
  78. test.TestCreate(
  79. // valid
  80. ds,
  81. // invalid (invalid selector)
  82. &apps.DaemonSet{
  83. Spec: apps.DaemonSetSpec{
  84. Selector: &metav1.LabelSelector{MatchLabels: map[string]string{}},
  85. Template: validDaemonSet.Spec.Template,
  86. },
  87. },
  88. // invalid update strategy
  89. &apps.DaemonSet{
  90. Spec: apps.DaemonSetSpec{
  91. Selector: validDaemonSet.Spec.Selector,
  92. Template: validDaemonSet.Spec.Template,
  93. },
  94. },
  95. )
  96. }
  97. func TestUpdate(t *testing.T) {
  98. storage, _, server := newStorage(t)
  99. defer server.Terminate(t)
  100. defer storage.Store.DestroyFunc()
  101. test := genericregistrytest.New(t, storage.Store)
  102. test.TestUpdate(
  103. // valid
  104. newValidDaemonSet(),
  105. // updateFunc
  106. func(obj runtime.Object) runtime.Object {
  107. object := obj.(*apps.DaemonSet)
  108. object.Spec.Template.Spec.NodeSelector = map[string]string{"c": "d"}
  109. object.Spec.Template.Spec.DNSPolicy = api.DNSDefault
  110. return object
  111. },
  112. // invalid updateFunc
  113. func(obj runtime.Object) runtime.Object {
  114. object := obj.(*apps.DaemonSet)
  115. object.Name = ""
  116. return object
  117. },
  118. func(obj runtime.Object) runtime.Object {
  119. object := obj.(*apps.DaemonSet)
  120. object.Spec.Template.Spec.RestartPolicy = api.RestartPolicyOnFailure
  121. return object
  122. },
  123. )
  124. }
  125. func TestDelete(t *testing.T) {
  126. storage, _, server := newStorage(t)
  127. defer server.Terminate(t)
  128. defer storage.Store.DestroyFunc()
  129. test := genericregistrytest.New(t, storage.Store)
  130. test.TestDelete(newValidDaemonSet())
  131. }
  132. func TestGet(t *testing.T) {
  133. storage, _, server := newStorage(t)
  134. defer server.Terminate(t)
  135. defer storage.Store.DestroyFunc()
  136. test := genericregistrytest.New(t, storage.Store)
  137. test.TestGet(newValidDaemonSet())
  138. }
  139. func TestList(t *testing.T) {
  140. storage, _, server := newStorage(t)
  141. defer server.Terminate(t)
  142. defer storage.Store.DestroyFunc()
  143. test := genericregistrytest.New(t, storage.Store)
  144. test.TestList(newValidDaemonSet())
  145. }
  146. func TestWatch(t *testing.T) {
  147. storage, _, server := newStorage(t)
  148. defer server.Terminate(t)
  149. defer storage.Store.DestroyFunc()
  150. test := genericregistrytest.New(t, storage.Store)
  151. test.TestWatch(
  152. validDaemonSet,
  153. // matching labels
  154. []labels.Set{
  155. {"a": "b"},
  156. },
  157. // not matching labels
  158. []labels.Set{
  159. {"a": "c"},
  160. {"foo": "bar"},
  161. },
  162. // matching fields
  163. []fields.Set{
  164. {"metadata.name": "foo"},
  165. },
  166. // notmatching fields
  167. []fields.Set{
  168. {"metadata.name": "bar"},
  169. {"name": "foo"},
  170. },
  171. )
  172. }
  173. func TestShortNames(t *testing.T) {
  174. storage, _, server := newStorage(t)
  175. defer server.Terminate(t)
  176. defer storage.Store.DestroyFunc()
  177. expected := []string{"ds"}
  178. registrytest.AssertShortNames(t, storage, expected)
  179. }
  180. // TODO TestUpdateStatus