storage_test.go 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. /*
  2. Copyright 2016 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/util/intstr"
  20. genericapirequest "k8s.io/apiserver/pkg/endpoints/request"
  21. "k8s.io/apiserver/pkg/registry/generic"
  22. genericregistrytest "k8s.io/apiserver/pkg/registry/generic/testing"
  23. "k8s.io/apiserver/pkg/registry/rest"
  24. etcd3testing "k8s.io/apiserver/pkg/storage/etcd3/testing"
  25. "k8s.io/kubernetes/pkg/apis/policy"
  26. "k8s.io/kubernetes/pkg/registry/registrytest"
  27. )
  28. func newStorage(t *testing.T) (*REST, *StatusREST, *etcd3testing.EtcdTestServer) {
  29. etcdStorage, server := registrytest.NewEtcdStorage(t, policy.GroupName)
  30. restOptions := generic.RESTOptions{StorageConfig: etcdStorage, Decorator: generic.UndecoratedStorage, DeleteCollectionWorkers: 1, ResourcePrefix: "poddisruptionbudgets"}
  31. podDisruptionBudgetStorage, statusStorage, err := NewREST(restOptions)
  32. if err != nil {
  33. t.Fatalf("unexpected error from REST storage: %v", err)
  34. }
  35. return podDisruptionBudgetStorage, statusStorage, server
  36. }
  37. func validNewPodDisruptionBudget() *policy.PodDisruptionBudget {
  38. minAvailable := intstr.FromInt(7)
  39. return &policy.PodDisruptionBudget{
  40. ObjectMeta: metav1.ObjectMeta{
  41. Name: "foo",
  42. Namespace: metav1.NamespaceDefault,
  43. Labels: map[string]string{"a": "b"},
  44. },
  45. Spec: policy.PodDisruptionBudgetSpec{
  46. Selector: &metav1.LabelSelector{MatchLabels: map[string]string{"a": "b"}},
  47. MinAvailable: &minAvailable,
  48. },
  49. Status: policy.PodDisruptionBudgetStatus{},
  50. }
  51. }
  52. func TestCreate(t *testing.T) {
  53. storage, _, server := newStorage(t)
  54. defer server.Terminate(t)
  55. defer storage.Store.DestroyFunc()
  56. test := genericregistrytest.New(t, storage.Store)
  57. pdb := validNewPodDisruptionBudget()
  58. pdb.ObjectMeta = metav1.ObjectMeta{}
  59. test.TestCreate(
  60. // valid
  61. pdb,
  62. // TODO: Add an invalid case when we have validation.
  63. )
  64. }
  65. // TODO: Test updates to spec when we allow them.
  66. func TestStatusUpdate(t *testing.T) {
  67. storage, statusStorage, server := newStorage(t)
  68. defer server.Terminate(t)
  69. defer storage.Store.DestroyFunc()
  70. ctx := genericapirequest.WithNamespace(genericapirequest.NewContext(), metav1.NamespaceDefault)
  71. key := "/poddisruptionbudgets/" + metav1.NamespaceDefault + "/foo"
  72. validPodDisruptionBudget := validNewPodDisruptionBudget()
  73. if err := storage.Storage.Create(ctx, key, validPodDisruptionBudget, nil, 0, false); err != nil {
  74. t.Fatalf("unexpected error: %v", err)
  75. }
  76. obj, err := storage.Get(ctx, "foo", &metav1.GetOptions{})
  77. if err != nil {
  78. t.Fatalf("failed to get pdb: %v", err)
  79. }
  80. obtainedPdb := obj.(*policy.PodDisruptionBudget)
  81. minAvailable := intstr.FromInt(8)
  82. update := policy.PodDisruptionBudget{
  83. ObjectMeta: obtainedPdb.ObjectMeta,
  84. Spec: policy.PodDisruptionBudgetSpec{
  85. MinAvailable: &minAvailable,
  86. },
  87. Status: policy.PodDisruptionBudgetStatus{
  88. ExpectedPods: 8,
  89. },
  90. }
  91. if _, _, err := statusStorage.Update(ctx, update.Name, rest.DefaultUpdatedObjectInfo(&update), rest.ValidateAllObjectFunc, rest.ValidateAllObjectUpdateFunc, false, &metav1.UpdateOptions{}); err != nil {
  92. t.Fatalf("unexpected error: %v", err)
  93. }
  94. obj, err = storage.Get(ctx, "foo", &metav1.GetOptions{})
  95. if err != nil {
  96. t.Fatalf("unexpected error: %v", err)
  97. }
  98. pdb := obj.(*policy.PodDisruptionBudget)
  99. if pdb.Spec.MinAvailable.IntValue() != 7 {
  100. t.Errorf("we expected .spec.replicas to not be updated but it was updated to %v", pdb.Spec.MinAvailable)
  101. }
  102. if pdb.Status.ExpectedPods != 8 {
  103. t.Errorf("we expected .status.replicas to be updated to %d but it was %v", 7, pdb.Status.ExpectedPods)
  104. }
  105. }
  106. func TestGet(t *testing.T) {
  107. storage, _, server := newStorage(t)
  108. defer server.Terminate(t)
  109. defer storage.Store.DestroyFunc()
  110. test := genericregistrytest.New(t, storage.Store)
  111. test.TestGet(validNewPodDisruptionBudget())
  112. }
  113. func TestList(t *testing.T) {
  114. storage, _, server := newStorage(t)
  115. defer server.Terminate(t)
  116. defer storage.Store.DestroyFunc()
  117. test := genericregistrytest.New(t, storage.Store)
  118. test.TestList(validNewPodDisruptionBudget())
  119. }
  120. func TestDelete(t *testing.T) {
  121. storage, _, server := newStorage(t)
  122. defer server.Terminate(t)
  123. defer storage.Store.DestroyFunc()
  124. test := genericregistrytest.New(t, storage.Store)
  125. test.TestDelete(validNewPodDisruptionBudget())
  126. }
  127. func TestWatch(t *testing.T) {
  128. storage, _, server := newStorage(t)
  129. defer server.Terminate(t)
  130. defer storage.Store.DestroyFunc()
  131. test := genericregistrytest.New(t, storage.Store)
  132. test.TestWatch(
  133. validNewPodDisruptionBudget(),
  134. // matching labels
  135. []labels.Set{
  136. {"a": "b"},
  137. },
  138. // not matching labels
  139. []labels.Set{
  140. {"a": "c"},
  141. {"foo": "bar"},
  142. },
  143. // matching fields
  144. []fields.Set{
  145. {"metadata.name": "foo"},
  146. },
  147. // not matching fields
  148. []fields.Set{
  149. {"metadata.name": "bar"},
  150. },
  151. )
  152. }
  153. // TODO: Test generation number.