namespaces_test.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. /*
  2. Copyright 2014 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 unversioned_test
  14. import (
  15. "net/url"
  16. "testing"
  17. "k8s.io/kubernetes/pkg/api"
  18. "k8s.io/kubernetes/pkg/api/testapi"
  19. "k8s.io/kubernetes/pkg/client/unversioned/testclient/simple"
  20. )
  21. func TestNamespaceCreate(t *testing.T) {
  22. // we create a namespace relative to another namespace
  23. namespace := &api.Namespace{
  24. ObjectMeta: api.ObjectMeta{Name: "foo"},
  25. }
  26. c := &simple.Client{
  27. Request: simple.Request{
  28. Method: "POST",
  29. Path: testapi.Default.ResourcePath("namespaces", "", ""),
  30. Body: namespace,
  31. },
  32. Response: simple.Response{StatusCode: 200, Body: namespace},
  33. }
  34. // from the source ns, provision a new global namespace "foo"
  35. response, err := c.Setup(t).Namespaces().Create(namespace)
  36. defer c.Close()
  37. if err != nil {
  38. t.Errorf("%#v should be nil.", err)
  39. }
  40. if e, a := response.Name, namespace.Name; e != a {
  41. t.Errorf("%#v != %#v.", e, a)
  42. }
  43. }
  44. func TestNamespaceGet(t *testing.T) {
  45. namespace := &api.Namespace{
  46. ObjectMeta: api.ObjectMeta{Name: "foo"},
  47. }
  48. c := &simple.Client{
  49. Request: simple.Request{
  50. Method: "GET",
  51. Path: testapi.Default.ResourcePath("namespaces", "", "foo"),
  52. Body: nil,
  53. },
  54. Response: simple.Response{StatusCode: 200, Body: namespace},
  55. }
  56. response, err := c.Setup(t).Namespaces().Get("foo")
  57. defer c.Close()
  58. if err != nil {
  59. t.Errorf("%#v should be nil.", err)
  60. }
  61. if e, r := response.Name, namespace.Name; e != r {
  62. t.Errorf("%#v != %#v.", e, r)
  63. }
  64. }
  65. func TestNamespaceList(t *testing.T) {
  66. namespaceList := &api.NamespaceList{
  67. Items: []api.Namespace{
  68. {
  69. ObjectMeta: api.ObjectMeta{Name: "foo"},
  70. },
  71. },
  72. }
  73. c := &simple.Client{
  74. Request: simple.Request{
  75. Method: "GET",
  76. Path: testapi.Default.ResourcePath("namespaces", "", ""),
  77. Body: nil,
  78. },
  79. Response: simple.Response{StatusCode: 200, Body: namespaceList},
  80. }
  81. response, err := c.Setup(t).Namespaces().List(api.ListOptions{})
  82. defer c.Close()
  83. if err != nil {
  84. t.Errorf("%#v should be nil.", err)
  85. }
  86. if len(response.Items) != 1 {
  87. t.Errorf("%#v response.Items should have len 1.", response.Items)
  88. }
  89. responseNamespace := response.Items[0]
  90. if e, r := responseNamespace.Name, "foo"; e != r {
  91. t.Errorf("%#v != %#v.", e, r)
  92. }
  93. }
  94. func TestNamespaceUpdate(t *testing.T) {
  95. requestNamespace := &api.Namespace{
  96. ObjectMeta: api.ObjectMeta{
  97. Name: "foo",
  98. ResourceVersion: "1",
  99. Labels: map[string]string{
  100. "foo": "bar",
  101. "name": "baz",
  102. },
  103. },
  104. Spec: api.NamespaceSpec{
  105. Finalizers: []api.FinalizerName{api.FinalizerKubernetes},
  106. },
  107. }
  108. c := &simple.Client{
  109. Request: simple.Request{
  110. Method: "PUT",
  111. Path: testapi.Default.ResourcePath("namespaces", "", "foo")},
  112. Response: simple.Response{StatusCode: 200, Body: requestNamespace},
  113. }
  114. receivedNamespace, err := c.Setup(t).Namespaces().Update(requestNamespace)
  115. defer c.Close()
  116. c.Validate(t, receivedNamespace, err)
  117. }
  118. func TestNamespaceFinalize(t *testing.T) {
  119. requestNamespace := &api.Namespace{
  120. ObjectMeta: api.ObjectMeta{
  121. Name: "foo",
  122. ResourceVersion: "1",
  123. Labels: map[string]string{
  124. "foo": "bar",
  125. "name": "baz",
  126. },
  127. },
  128. Spec: api.NamespaceSpec{
  129. Finalizers: []api.FinalizerName{api.FinalizerKubernetes},
  130. },
  131. }
  132. c := &simple.Client{
  133. Request: simple.Request{
  134. Method: "PUT",
  135. Path: testapi.Default.ResourcePath("namespaces", "", "foo") + "/finalize",
  136. },
  137. Response: simple.Response{StatusCode: 200, Body: requestNamespace},
  138. }
  139. receivedNamespace, err := c.Setup(t).Namespaces().Finalize(requestNamespace)
  140. defer c.Close()
  141. c.Validate(t, receivedNamespace, err)
  142. }
  143. func TestNamespaceDelete(t *testing.T) {
  144. c := &simple.Client{
  145. Request: simple.Request{Method: "DELETE", Path: testapi.Default.ResourcePath("namespaces", "", "foo")},
  146. Response: simple.Response{StatusCode: 200},
  147. }
  148. err := c.Setup(t).Namespaces().Delete("foo")
  149. defer c.Close()
  150. c.Validate(t, nil, err)
  151. }
  152. func TestNamespaceWatch(t *testing.T) {
  153. c := &simple.Client{
  154. Request: simple.Request{
  155. Method: "GET",
  156. Path: testapi.Default.ResourcePathWithPrefix("watch", "namespaces", "", ""),
  157. Query: url.Values{"resourceVersion": []string{}}},
  158. Response: simple.Response{StatusCode: 200},
  159. }
  160. _, err := c.Setup(t).Namespaces().Watch(api.ListOptions{})
  161. defer c.Close()
  162. c.Validate(t, nil, err)
  163. }