jobs_test.go 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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. "testing"
  16. "k8s.io/kubernetes/pkg/api"
  17. "k8s.io/kubernetes/pkg/api/testapi"
  18. "k8s.io/kubernetes/pkg/apis/batch"
  19. "k8s.io/kubernetes/pkg/apis/extensions"
  20. "k8s.io/kubernetes/pkg/client/unversioned"
  21. "k8s.io/kubernetes/pkg/client/unversioned/testclient/simple"
  22. )
  23. func getJobsResourceName() string {
  24. return "jobs"
  25. }
  26. func getJobClient(t *testing.T, c *simple.Client, ns, resourceGroup string) unversioned.JobInterface {
  27. switch resourceGroup {
  28. case batch.GroupName:
  29. return c.Setup(t).Batch().Jobs(ns)
  30. case extensions.GroupName:
  31. return c.Setup(t).Extensions().Jobs(ns)
  32. default:
  33. t.Fatalf("Unknown group %v", resourceGroup)
  34. }
  35. return nil
  36. }
  37. func testListJob(t *testing.T, group testapi.TestGroup, resourceGroup string) {
  38. ns := api.NamespaceAll
  39. c := &simple.Client{
  40. Request: simple.Request{
  41. Method: "GET",
  42. Path: group.ResourcePath(getJobsResourceName(), ns, ""),
  43. },
  44. Response: simple.Response{StatusCode: 200,
  45. Body: &batch.JobList{
  46. Items: []batch.Job{
  47. {
  48. ObjectMeta: api.ObjectMeta{
  49. Name: "foo",
  50. Labels: map[string]string{
  51. "foo": "bar",
  52. "name": "baz",
  53. },
  54. },
  55. Spec: batch.JobSpec{
  56. Template: api.PodTemplateSpec{},
  57. },
  58. },
  59. },
  60. },
  61. },
  62. ResourceGroup: resourceGroup,
  63. }
  64. receivedJobList, err := getJobClient(t, c, ns, resourceGroup).List(api.ListOptions{})
  65. defer c.Close()
  66. c.Validate(t, receivedJobList, err)
  67. }
  68. func TestListJob(t *testing.T) {
  69. testListJob(t, testapi.Extensions, extensions.GroupName)
  70. testListJob(t, testapi.Batch, batch.GroupName)
  71. }
  72. func testGetJob(t *testing.T, group testapi.TestGroup, resourceGroup string) {
  73. ns := api.NamespaceDefault
  74. c := &simple.Client{
  75. Request: simple.Request{
  76. Method: "GET",
  77. Path: group.ResourcePath(getJobsResourceName(), ns, "foo"),
  78. Query: simple.BuildQueryValues(nil),
  79. },
  80. Response: simple.Response{
  81. StatusCode: 200,
  82. Body: &batch.Job{
  83. ObjectMeta: api.ObjectMeta{
  84. Name: "foo",
  85. Labels: map[string]string{
  86. "foo": "bar",
  87. "name": "baz",
  88. },
  89. },
  90. Spec: batch.JobSpec{
  91. Template: api.PodTemplateSpec{},
  92. },
  93. },
  94. },
  95. ResourceGroup: resourceGroup,
  96. }
  97. receivedJob, err := getJobClient(t, c, ns, resourceGroup).Get("foo")
  98. defer c.Close()
  99. c.Validate(t, receivedJob, err)
  100. }
  101. func TestGetJob(t *testing.T) {
  102. testGetJob(t, testapi.Extensions, extensions.GroupName)
  103. testGetJob(t, testapi.Batch, batch.GroupName)
  104. }
  105. func testUpdateJob(t *testing.T, group testapi.TestGroup, resourceGroup string) {
  106. ns := api.NamespaceDefault
  107. requestJob := &batch.Job{
  108. ObjectMeta: api.ObjectMeta{
  109. Name: "foo",
  110. Namespace: ns,
  111. ResourceVersion: "1",
  112. },
  113. }
  114. c := &simple.Client{
  115. Request: simple.Request{
  116. Method: "PUT",
  117. Path: group.ResourcePath(getJobsResourceName(), ns, "foo"),
  118. Query: simple.BuildQueryValues(nil),
  119. },
  120. Response: simple.Response{
  121. StatusCode: 200,
  122. Body: &batch.Job{
  123. ObjectMeta: api.ObjectMeta{
  124. Name: "foo",
  125. Labels: map[string]string{
  126. "foo": "bar",
  127. "name": "baz",
  128. },
  129. },
  130. Spec: batch.JobSpec{
  131. Template: api.PodTemplateSpec{},
  132. },
  133. },
  134. },
  135. ResourceGroup: resourceGroup,
  136. }
  137. receivedJob, err := getJobClient(t, c, ns, resourceGroup).Update(requestJob)
  138. defer c.Close()
  139. c.Validate(t, receivedJob, err)
  140. }
  141. func TestUpdateJob(t *testing.T) {
  142. testUpdateJob(t, testapi.Extensions, extensions.GroupName)
  143. testUpdateJob(t, testapi.Batch, batch.GroupName)
  144. }
  145. func testUpdateJobStatus(t *testing.T, group testapi.TestGroup, resourceGroup string) {
  146. ns := api.NamespaceDefault
  147. requestJob := &batch.Job{
  148. ObjectMeta: api.ObjectMeta{
  149. Name: "foo",
  150. Namespace: ns,
  151. ResourceVersion: "1",
  152. },
  153. }
  154. c := &simple.Client{
  155. Request: simple.Request{
  156. Method: "PUT",
  157. Path: group.ResourcePath(getJobsResourceName(), ns, "foo") + "/status",
  158. Query: simple.BuildQueryValues(nil),
  159. },
  160. Response: simple.Response{
  161. StatusCode: 200,
  162. Body: &batch.Job{
  163. ObjectMeta: api.ObjectMeta{
  164. Name: "foo",
  165. Labels: map[string]string{
  166. "foo": "bar",
  167. "name": "baz",
  168. },
  169. },
  170. Spec: batch.JobSpec{
  171. Template: api.PodTemplateSpec{},
  172. },
  173. Status: batch.JobStatus{
  174. Active: 1,
  175. },
  176. },
  177. },
  178. ResourceGroup: resourceGroup,
  179. }
  180. receivedJob, err := getJobClient(t, c, ns, resourceGroup).UpdateStatus(requestJob)
  181. defer c.Close()
  182. c.Validate(t, receivedJob, err)
  183. }
  184. func TestUpdateJobStatus(t *testing.T) {
  185. testUpdateJobStatus(t, testapi.Extensions, extensions.GroupName)
  186. testUpdateJobStatus(t, testapi.Batch, batch.GroupName)
  187. }
  188. func testDeleteJob(t *testing.T, group testapi.TestGroup, resourceGroup string) {
  189. ns := api.NamespaceDefault
  190. c := &simple.Client{
  191. Request: simple.Request{
  192. Method: "DELETE",
  193. Path: group.ResourcePath(getJobsResourceName(), ns, "foo"),
  194. Query: simple.BuildQueryValues(nil),
  195. },
  196. Response: simple.Response{StatusCode: 200},
  197. ResourceGroup: resourceGroup,
  198. }
  199. err := getJobClient(t, c, ns, resourceGroup).Delete("foo", nil)
  200. defer c.Close()
  201. c.Validate(t, nil, err)
  202. }
  203. func TestDeleteJob(t *testing.T) {
  204. testDeleteJob(t, testapi.Extensions, extensions.GroupName)
  205. testDeleteJob(t, testapi.Batch, batch.GroupName)
  206. }
  207. func testCreateJob(t *testing.T, group testapi.TestGroup, resourceGroup string) {
  208. ns := api.NamespaceDefault
  209. requestJob := &batch.Job{
  210. ObjectMeta: api.ObjectMeta{
  211. Name: "foo",
  212. Namespace: ns,
  213. },
  214. }
  215. c := &simple.Client{
  216. Request: simple.Request{
  217. Method: "POST",
  218. Path: group.ResourcePath(getJobsResourceName(), ns, ""),
  219. Body: requestJob,
  220. Query: simple.BuildQueryValues(nil),
  221. },
  222. Response: simple.Response{
  223. StatusCode: 200,
  224. Body: &batch.Job{
  225. ObjectMeta: api.ObjectMeta{
  226. Name: "foo",
  227. Labels: map[string]string{
  228. "foo": "bar",
  229. "name": "baz",
  230. },
  231. },
  232. Spec: batch.JobSpec{
  233. Template: api.PodTemplateSpec{},
  234. },
  235. },
  236. },
  237. ResourceGroup: resourceGroup,
  238. }
  239. receivedJob, err := getJobClient(t, c, ns, resourceGroup).Create(requestJob)
  240. defer c.Close()
  241. if err != nil {
  242. t.Fatalf("unexpected error: %v", err)
  243. }
  244. c.Validate(t, receivedJob, err)
  245. }
  246. func TestCreateJob(t *testing.T) {
  247. testCreateJob(t, testapi.Extensions, extensions.GroupName)
  248. testCreateJob(t, testapi.Batch, batch.GroupName)
  249. }