storage_test.go 5.2 KB

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