storage_test.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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/batch"
  24. api "k8s.io/kubernetes/pkg/apis/core"
  25. "k8s.io/kubernetes/pkg/registry/registrytest"
  26. )
  27. func newStorage(t *testing.T) (*JobStorage, *etcdtesting.EtcdTestServer) {
  28. etcdStorage, server := registrytest.NewEtcdStorage(t, batch.GroupName)
  29. restOptions := generic.RESTOptions{
  30. StorageConfig: etcdStorage,
  31. Decorator: generic.UndecoratedStorage,
  32. DeleteCollectionWorkers: 1,
  33. ResourcePrefix: "jobs",
  34. }
  35. jobStorage := NewStorage(restOptions)
  36. return &jobStorage, server
  37. }
  38. func validNewJob() *batch.Job {
  39. completions := int32(1)
  40. parallelism := int32(1)
  41. return &batch.Job{
  42. ObjectMeta: metav1.ObjectMeta{
  43. Name: "foo",
  44. Namespace: "default",
  45. },
  46. Spec: batch.JobSpec{
  47. Completions: &completions,
  48. Parallelism: &parallelism,
  49. Selector: &metav1.LabelSelector{
  50. MatchLabels: map[string]string{"a": "b"},
  51. },
  52. ManualSelector: newBool(true),
  53. Template: api.PodTemplateSpec{
  54. ObjectMeta: metav1.ObjectMeta{
  55. Labels: map[string]string{"a": "b"},
  56. },
  57. Spec: api.PodSpec{
  58. Containers: []api.Container{
  59. {
  60. Name: "test",
  61. Image: "test_image",
  62. ImagePullPolicy: api.PullIfNotPresent,
  63. TerminationMessagePolicy: api.TerminationMessageReadFile,
  64. },
  65. },
  66. RestartPolicy: api.RestartPolicyOnFailure,
  67. DNSPolicy: api.DNSClusterFirst,
  68. },
  69. },
  70. },
  71. }
  72. }
  73. func TestCreate(t *testing.T) {
  74. storage, server := newStorage(t)
  75. defer server.Terminate(t)
  76. defer storage.Job.Store.DestroyFunc()
  77. test := genericregistrytest.New(t, storage.Job.Store)
  78. validJob := validNewJob()
  79. validJob.ObjectMeta = metav1.ObjectMeta{}
  80. test.TestCreate(
  81. // valid
  82. validJob,
  83. // invalid (empty selector)
  84. &batch.Job{
  85. Spec: batch.JobSpec{
  86. Completions: validJob.Spec.Completions,
  87. Selector: &metav1.LabelSelector{},
  88. Template: validJob.Spec.Template,
  89. },
  90. },
  91. )
  92. }
  93. func TestUpdate(t *testing.T) {
  94. storage, server := newStorage(t)
  95. defer server.Terminate(t)
  96. defer storage.Job.Store.DestroyFunc()
  97. test := genericregistrytest.New(t, storage.Job.Store)
  98. two := int32(2)
  99. test.TestUpdate(
  100. // valid
  101. validNewJob(),
  102. // updateFunc
  103. func(obj runtime.Object) runtime.Object {
  104. object := obj.(*batch.Job)
  105. object.Spec.Parallelism = &two
  106. return object
  107. },
  108. // invalid updateFunc
  109. func(obj runtime.Object) runtime.Object {
  110. object := obj.(*batch.Job)
  111. object.Spec.Selector = &metav1.LabelSelector{}
  112. return object
  113. },
  114. func(obj runtime.Object) runtime.Object {
  115. object := obj.(*batch.Job)
  116. object.Spec.Completions = &two
  117. return object
  118. },
  119. )
  120. }
  121. func TestDelete(t *testing.T) {
  122. storage, server := newStorage(t)
  123. defer server.Terminate(t)
  124. defer storage.Job.Store.DestroyFunc()
  125. test := genericregistrytest.New(t, storage.Job.Store)
  126. test.TestDelete(validNewJob())
  127. }
  128. func TestGet(t *testing.T) {
  129. storage, server := newStorage(t)
  130. defer server.Terminate(t)
  131. defer storage.Job.Store.DestroyFunc()
  132. test := genericregistrytest.New(t, storage.Job.Store)
  133. test.TestGet(validNewJob())
  134. }
  135. func TestList(t *testing.T) {
  136. storage, server := newStorage(t)
  137. defer server.Terminate(t)
  138. defer storage.Job.Store.DestroyFunc()
  139. test := genericregistrytest.New(t, storage.Job.Store)
  140. test.TestList(validNewJob())
  141. }
  142. func TestWatch(t *testing.T) {
  143. storage, server := newStorage(t)
  144. defer server.Terminate(t)
  145. defer storage.Job.Store.DestroyFunc()
  146. test := genericregistrytest.New(t, storage.Job.Store)
  147. test.TestWatch(
  148. validNewJob(),
  149. // matching labels
  150. []labels.Set{},
  151. // not matching labels
  152. []labels.Set{
  153. {"x": "y"},
  154. },
  155. // matching fields
  156. []fields.Set{},
  157. // not matching fields
  158. []fields.Set{
  159. {"metadata.name": "xyz"},
  160. {"name": "foo"},
  161. },
  162. )
  163. }
  164. // TODO: test update /status
  165. func newBool(val bool) *bool {
  166. p := new(bool)
  167. *p = val
  168. return p
  169. }
  170. func TestCategories(t *testing.T) {
  171. storage, server := newStorage(t)
  172. defer server.Terminate(t)
  173. defer storage.Job.Store.DestroyFunc()
  174. expected := []string{"all"}
  175. registrytest.AssertCategories(t, storage.Job, expected)
  176. }