etcd_test.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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 etcd
  14. import (
  15. "testing"
  16. "k8s.io/kubernetes/pkg/api"
  17. "k8s.io/kubernetes/pkg/api/unversioned"
  18. "k8s.io/kubernetes/pkg/fields"
  19. "k8s.io/kubernetes/pkg/labels"
  20. "k8s.io/kubernetes/pkg/registry/generic"
  21. "k8s.io/kubernetes/pkg/registry/registrytest"
  22. "k8s.io/kubernetes/pkg/storage/etcd/etcdtest"
  23. etcdtesting "k8s.io/kubernetes/pkg/storage/etcd/testing"
  24. )
  25. func newStorage(t *testing.T) (*REST, *etcdtesting.EtcdTestServer) {
  26. etcdStorage, server := registrytest.NewEtcdStorage(t, "")
  27. restOptions := generic.RESTOptions{StorageConfig: etcdStorage, Decorator: generic.UndecoratedStorage, DeleteCollectionWorkers: 1, ResourcePrefix: "namespaces"}
  28. namespaceStorage, _, _ := NewREST(restOptions)
  29. return namespaceStorage, server
  30. }
  31. func validNewNamespace() *api.Namespace {
  32. return &api.Namespace{
  33. ObjectMeta: api.ObjectMeta{
  34. Name: "foo",
  35. },
  36. }
  37. }
  38. func TestCreate(t *testing.T) {
  39. storage, server := newStorage(t)
  40. defer server.Terminate(t)
  41. test := registrytest.New(t, storage.Store).ClusterScope()
  42. namespace := validNewNamespace()
  43. namespace.ObjectMeta = api.ObjectMeta{GenerateName: "foo"}
  44. test.TestCreate(
  45. // valid
  46. namespace,
  47. // invalid
  48. &api.Namespace{
  49. ObjectMeta: api.ObjectMeta{Name: "bad value"},
  50. },
  51. )
  52. }
  53. func TestCreateSetsFields(t *testing.T) {
  54. storage, server := newStorage(t)
  55. defer server.Terminate(t)
  56. namespace := validNewNamespace()
  57. ctx := api.NewContext()
  58. _, err := storage.Create(ctx, namespace)
  59. if err != nil {
  60. t.Fatalf("unexpected error: %v", err)
  61. }
  62. object, err := storage.Get(ctx, "foo")
  63. if err != nil {
  64. t.Errorf("unexpected error: %v", err)
  65. }
  66. actual := object.(*api.Namespace)
  67. if actual.Name != namespace.Name {
  68. t.Errorf("unexpected namespace: %#v", actual)
  69. }
  70. if len(actual.UID) == 0 {
  71. t.Errorf("expected namespace UID to be set: %#v", actual)
  72. }
  73. if actual.Status.Phase != api.NamespaceActive {
  74. t.Errorf("expected namespace phase to be set to active, but %v", actual.Status.Phase)
  75. }
  76. }
  77. func TestDelete(t *testing.T) {
  78. storage, server := newStorage(t)
  79. defer server.Terminate(t)
  80. test := registrytest.New(t, storage.Store).ClusterScope().ReturnDeletedObject()
  81. test.TestDelete(validNewNamespace())
  82. }
  83. func TestGet(t *testing.T) {
  84. storage, server := newStorage(t)
  85. defer server.Terminate(t)
  86. test := registrytest.New(t, storage.Store).ClusterScope()
  87. test.TestGet(validNewNamespace())
  88. }
  89. func TestList(t *testing.T) {
  90. storage, server := newStorage(t)
  91. defer server.Terminate(t)
  92. test := registrytest.New(t, storage.Store).ClusterScope()
  93. test.TestList(validNewNamespace())
  94. }
  95. func TestWatch(t *testing.T) {
  96. storage, server := newStorage(t)
  97. defer server.Terminate(t)
  98. test := registrytest.New(t, storage.Store).ClusterScope()
  99. test.TestWatch(
  100. validNewNamespace(),
  101. // matching labels
  102. []labels.Set{},
  103. // not matching labels
  104. []labels.Set{
  105. {"foo": "bar"},
  106. },
  107. // matching fields
  108. []fields.Set{
  109. {"metadata.name": "foo"},
  110. {"name": "foo"},
  111. },
  112. // not matching fields
  113. []fields.Set{
  114. {"metadata.name": "bar"},
  115. },
  116. )
  117. }
  118. func TestDeleteNamespaceWithIncompleteFinalizers(t *testing.T) {
  119. storage, server := newStorage(t)
  120. defer server.Terminate(t)
  121. key := etcdtest.AddPrefix("namespaces/foo")
  122. ctx := api.NewContext()
  123. now := unversioned.Now()
  124. namespace := &api.Namespace{
  125. ObjectMeta: api.ObjectMeta{
  126. Name: "foo",
  127. DeletionTimestamp: &now,
  128. },
  129. Spec: api.NamespaceSpec{
  130. Finalizers: []api.FinalizerName{api.FinalizerKubernetes},
  131. },
  132. Status: api.NamespaceStatus{Phase: api.NamespaceActive},
  133. }
  134. if err := storage.Storage.Create(ctx, key, namespace, nil, 0); err != nil {
  135. t.Fatalf("unexpected error: %v", err)
  136. }
  137. if _, err := storage.Delete(ctx, "foo", nil); err == nil {
  138. t.Errorf("unexpected error: %v", err)
  139. }
  140. }
  141. func TestDeleteNamespaceWithCompleteFinalizers(t *testing.T) {
  142. storage, server := newStorage(t)
  143. defer server.Terminate(t)
  144. key := etcdtest.AddPrefix("namespaces/foo")
  145. ctx := api.NewContext()
  146. now := unversioned.Now()
  147. namespace := &api.Namespace{
  148. ObjectMeta: api.ObjectMeta{
  149. Name: "foo",
  150. DeletionTimestamp: &now,
  151. },
  152. Spec: api.NamespaceSpec{
  153. Finalizers: []api.FinalizerName{},
  154. },
  155. Status: api.NamespaceStatus{Phase: api.NamespaceActive},
  156. }
  157. if err := storage.Storage.Create(ctx, key, namespace, nil, 0); err != nil {
  158. t.Fatalf("unexpected error: %v", err)
  159. }
  160. if _, err := storage.Delete(ctx, "foo", nil); err != nil {
  161. t.Errorf("unexpected error: %v", err)
  162. }
  163. }