nodes_test.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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 unversioned_test
  14. import (
  15. "net/url"
  16. "testing"
  17. "k8s.io/kubernetes/pkg/api"
  18. "k8s.io/kubernetes/pkg/api/resource"
  19. "k8s.io/kubernetes/pkg/api/testapi"
  20. "k8s.io/kubernetes/pkg/api/unversioned"
  21. "k8s.io/kubernetes/pkg/client/unversioned/testclient/simple"
  22. "k8s.io/kubernetes/pkg/labels"
  23. )
  24. func getNodesResourceName() string {
  25. return "nodes"
  26. }
  27. func TestListNodes(t *testing.T) {
  28. c := &simple.Client{
  29. Request: simple.Request{
  30. Method: "GET",
  31. Path: testapi.Default.ResourcePath(getNodesResourceName(), "", ""),
  32. },
  33. Response: simple.Response{StatusCode: 200, Body: &api.NodeList{ListMeta: unversioned.ListMeta{ResourceVersion: "1"}}},
  34. }
  35. response, err := c.Setup(t).Nodes().List(api.ListOptions{})
  36. defer c.Close()
  37. c.Validate(t, response, err)
  38. }
  39. func TestListNodesLabels(t *testing.T) {
  40. labelSelectorQueryParamName := unversioned.LabelSelectorQueryParam(testapi.Default.GroupVersion().String())
  41. c := &simple.Client{
  42. Request: simple.Request{
  43. Method: "GET",
  44. Path: testapi.Default.ResourcePath(getNodesResourceName(), "", ""),
  45. Query: simple.BuildQueryValues(url.Values{labelSelectorQueryParamName: []string{"foo=bar,name=baz"}})},
  46. Response: simple.Response{
  47. StatusCode: 200,
  48. Body: &api.NodeList{
  49. Items: []api.Node{
  50. {
  51. ObjectMeta: api.ObjectMeta{
  52. Labels: map[string]string{
  53. "foo": "bar",
  54. "name": "baz",
  55. },
  56. },
  57. },
  58. },
  59. },
  60. },
  61. }
  62. c.Setup(t)
  63. defer c.Close()
  64. c.QueryValidator[labelSelectorQueryParamName] = simple.ValidateLabels
  65. selector := labels.Set{"foo": "bar", "name": "baz"}.AsSelector()
  66. options := api.ListOptions{LabelSelector: selector}
  67. receivedNodeList, err := c.Nodes().List(options)
  68. c.Validate(t, receivedNodeList, err)
  69. }
  70. func TestGetNode(t *testing.T) {
  71. c := &simple.Client{
  72. Request: simple.Request{
  73. Method: "GET",
  74. Path: testapi.Default.ResourcePath(getNodesResourceName(), "", "1"),
  75. },
  76. Response: simple.Response{StatusCode: 200, Body: &api.Node{ObjectMeta: api.ObjectMeta{Name: "node-1"}}},
  77. }
  78. response, err := c.Setup(t).Nodes().Get("1")
  79. defer c.Close()
  80. c.Validate(t, response, err)
  81. }
  82. func TestGetNodeWithNoName(t *testing.T) {
  83. c := &simple.Client{Error: true}
  84. receivedNode, err := c.Setup(t).Nodes().Get("")
  85. defer c.Close()
  86. if (err != nil) && (err.Error() != simple.NameRequiredError) {
  87. t.Errorf("Expected error: %v, but got %v", simple.NameRequiredError, err)
  88. }
  89. c.Validate(t, receivedNode, err)
  90. }
  91. func TestCreateNode(t *testing.T) {
  92. requestNode := &api.Node{
  93. ObjectMeta: api.ObjectMeta{
  94. Name: "node-1",
  95. },
  96. Status: api.NodeStatus{
  97. Capacity: api.ResourceList{
  98. api.ResourceCPU: resource.MustParse("1000m"),
  99. api.ResourceMemory: resource.MustParse("1Mi"),
  100. },
  101. },
  102. Spec: api.NodeSpec{
  103. Unschedulable: false,
  104. },
  105. }
  106. c := &simple.Client{
  107. Request: simple.Request{
  108. Method: "POST",
  109. Path: testapi.Default.ResourcePath(getNodesResourceName(), "", ""),
  110. Body: requestNode},
  111. Response: simple.Response{
  112. StatusCode: 200,
  113. Body: requestNode,
  114. },
  115. }
  116. receivedNode, err := c.Setup(t).Nodes().Create(requestNode)
  117. defer c.Close()
  118. c.Validate(t, receivedNode, err)
  119. }
  120. func TestDeleteNode(t *testing.T) {
  121. c := &simple.Client{
  122. Request: simple.Request{
  123. Method: "DELETE",
  124. Path: testapi.Default.ResourcePath(getNodesResourceName(), "", "foo"),
  125. },
  126. Response: simple.Response{StatusCode: 200},
  127. }
  128. err := c.Setup(t).Nodes().Delete("foo")
  129. defer c.Close()
  130. c.Validate(t, nil, err)
  131. }
  132. func TestUpdateNode(t *testing.T) {
  133. requestNode := &api.Node{
  134. ObjectMeta: api.ObjectMeta{
  135. Name: "foo",
  136. ResourceVersion: "1",
  137. },
  138. Status: api.NodeStatus{
  139. Capacity: api.ResourceList{
  140. api.ResourceCPU: resource.MustParse("1000m"),
  141. api.ResourceMemory: resource.MustParse("1Mi"),
  142. },
  143. },
  144. Spec: api.NodeSpec{
  145. Unschedulable: true,
  146. },
  147. }
  148. c := &simple.Client{
  149. Request: simple.Request{
  150. Method: "PUT",
  151. Path: testapi.Default.ResourcePath(getNodesResourceName(), "", "foo"),
  152. },
  153. Response: simple.Response{StatusCode: 200, Body: requestNode},
  154. }
  155. response, err := c.Setup(t).Nodes().Update(requestNode)
  156. defer c.Close()
  157. c.Validate(t, response, err)
  158. }