generated_clientset.go 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  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 e2e
  14. import (
  15. "strconv"
  16. "time"
  17. clientapi "k8s.io/client-go/1.4/pkg/api"
  18. clientv1 "k8s.io/client-go/1.4/pkg/api/v1"
  19. "k8s.io/kubernetes/pkg/api"
  20. "k8s.io/kubernetes/pkg/api/v1"
  21. "k8s.io/kubernetes/pkg/labels"
  22. "k8s.io/kubernetes/pkg/runtime"
  23. "k8s.io/kubernetes/pkg/util/intstr"
  24. "k8s.io/kubernetes/pkg/util/uuid"
  25. "k8s.io/kubernetes/pkg/util/wait"
  26. "k8s.io/kubernetes/pkg/watch"
  27. "k8s.io/kubernetes/test/e2e/framework"
  28. . "github.com/onsi/ginkgo"
  29. . "github.com/onsi/gomega"
  30. )
  31. func stagingClientPod(name, value string) clientv1.Pod {
  32. return clientv1.Pod{
  33. ObjectMeta: clientv1.ObjectMeta{
  34. Name: name,
  35. Labels: map[string]string{
  36. "name": "foo",
  37. "time": value,
  38. },
  39. },
  40. Spec: clientv1.PodSpec{
  41. Containers: []clientv1.Container{
  42. {
  43. Name: "nginx",
  44. Image: "gcr.io/google_containers/nginx-slim:0.7",
  45. Ports: []clientv1.ContainerPort{{ContainerPort: 80}},
  46. },
  47. },
  48. },
  49. }
  50. }
  51. func testingPod(name, value string) v1.Pod {
  52. return v1.Pod{
  53. ObjectMeta: v1.ObjectMeta{
  54. Name: name,
  55. Labels: map[string]string{
  56. "name": "foo",
  57. "time": value,
  58. },
  59. },
  60. Spec: v1.PodSpec{
  61. Containers: []v1.Container{
  62. {
  63. Name: "nginx",
  64. Image: "gcr.io/google_containers/nginx-slim:0.7",
  65. Ports: []v1.ContainerPort{{ContainerPort: 80}},
  66. LivenessProbe: &v1.Probe{
  67. Handler: v1.Handler{
  68. HTTPGet: &v1.HTTPGetAction{
  69. Path: "/index.html",
  70. Port: intstr.FromInt(8080),
  71. },
  72. },
  73. InitialDelaySeconds: 30,
  74. },
  75. },
  76. },
  77. },
  78. }
  79. }
  80. func observePodCreation(w watch.Interface) {
  81. select {
  82. case event, _ := <-w.ResultChan():
  83. if event.Type != watch.Added {
  84. framework.Failf("Failed to observe pod creation: %v", event)
  85. }
  86. case <-time.After(framework.PodStartTimeout):
  87. framework.Failf("Timeout while waiting for pod creation")
  88. }
  89. }
  90. func observeObjectDeletion(w watch.Interface) (obj runtime.Object) {
  91. deleted := false
  92. timeout := false
  93. timer := time.After(60 * time.Second)
  94. for !deleted && !timeout {
  95. select {
  96. case event, _ := <-w.ResultChan():
  97. if event.Type == watch.Deleted {
  98. obj = event.Object
  99. deleted = true
  100. }
  101. case <-timer:
  102. timeout = true
  103. }
  104. }
  105. if !deleted {
  106. framework.Failf("Failed to observe pod deletion")
  107. }
  108. return
  109. }
  110. var _ = framework.KubeDescribe("Generated release_1_2 clientset", func() {
  111. f := framework.NewDefaultFramework("clientset")
  112. It("should create pods, delete pods, watch pods", func() {
  113. podClient := f.Clientset_1_2.Core().Pods(f.Namespace.Name)
  114. By("constructing the pod")
  115. name := "pod" + string(uuid.NewUUID())
  116. value := strconv.Itoa(time.Now().Nanosecond())
  117. podCopy := testingPod(name, value)
  118. pod := &podCopy
  119. By("setting up watch")
  120. selector := labels.SelectorFromSet(labels.Set(map[string]string{"time": value}))
  121. options := api.ListOptions{LabelSelector: selector}
  122. pods, err := podClient.List(options)
  123. if err != nil {
  124. framework.Failf("Failed to query for pods: %v", err)
  125. }
  126. Expect(len(pods.Items)).To(Equal(0))
  127. options = api.ListOptions{
  128. LabelSelector: selector,
  129. ResourceVersion: pods.ListMeta.ResourceVersion,
  130. }
  131. w, err := podClient.Watch(options)
  132. if err != nil {
  133. framework.Failf("Failed to set up watch: %v", err)
  134. }
  135. By("creating the pod")
  136. pod, err = podClient.Create(pod)
  137. if err != nil {
  138. framework.Failf("Failed to create pod: %v", err)
  139. }
  140. // We call defer here in case there is a problem with
  141. // the test so we can ensure that we clean up after
  142. // ourselves
  143. defer podClient.Delete(pod.Name, api.NewDeleteOptions(0))
  144. By("verifying the pod is in kubernetes")
  145. options = api.ListOptions{
  146. LabelSelector: selector,
  147. ResourceVersion: pod.ResourceVersion,
  148. }
  149. pods, err = podClient.List(options)
  150. if err != nil {
  151. framework.Failf("Failed to query for pods: %v", err)
  152. }
  153. Expect(len(pods.Items)).To(Equal(1))
  154. By("verifying pod creation was observed")
  155. observePodCreation(w)
  156. // We need to wait for the pod to be scheduled, otherwise the deletion
  157. // will be carried out immediately rather than gracefully.
  158. framework.ExpectNoError(f.WaitForPodRunning(pod.Name))
  159. By("deleting the pod gracefully")
  160. if err := podClient.Delete(pod.Name, api.NewDeleteOptions(30)); err != nil {
  161. framework.Failf("Failed to delete pod: %v", err)
  162. }
  163. By("verifying pod deletion was observed")
  164. obj := observeObjectDeletion(w)
  165. lastPod := obj.(*api.Pod)
  166. Expect(lastPod.DeletionTimestamp).ToNot(BeNil())
  167. Expect(lastPod.Spec.TerminationGracePeriodSeconds).ToNot(BeZero())
  168. options = api.ListOptions{LabelSelector: selector}
  169. pods, err = podClient.List(options)
  170. if err != nil {
  171. framework.Failf("Failed to list pods to verify deletion: %v", err)
  172. }
  173. Expect(len(pods.Items)).To(Equal(0))
  174. })
  175. })
  176. var _ = framework.KubeDescribe("Generated release_1_3 clientset", func() {
  177. f := framework.NewDefaultFramework("clientset")
  178. It("should create pods, delete pods, watch pods", func() {
  179. podClient := f.Clientset_1_3.Core().Pods(f.Namespace.Name)
  180. By("constructing the pod")
  181. name := "pod" + string(uuid.NewUUID())
  182. value := strconv.Itoa(time.Now().Nanosecond())
  183. podCopy := testingPod(name, value)
  184. pod := &podCopy
  185. By("setting up watch")
  186. selector := labels.SelectorFromSet(labels.Set(map[string]string{"time": value}))
  187. options := api.ListOptions{LabelSelector: selector}
  188. pods, err := podClient.List(options)
  189. if err != nil {
  190. framework.Failf("Failed to query for pods: %v", err)
  191. }
  192. Expect(len(pods.Items)).To(Equal(0))
  193. options = api.ListOptions{
  194. LabelSelector: selector,
  195. ResourceVersion: pods.ListMeta.ResourceVersion,
  196. }
  197. w, err := podClient.Watch(options)
  198. if err != nil {
  199. framework.Failf("Failed to set up watch: %v", err)
  200. }
  201. By("creating the pod")
  202. pod, err = podClient.Create(pod)
  203. if err != nil {
  204. framework.Failf("Failed to create pod: %v", err)
  205. }
  206. // We call defer here in case there is a problem with
  207. // the test so we can ensure that we clean up after
  208. // ourselves
  209. defer podClient.Delete(pod.Name, api.NewDeleteOptions(0))
  210. By("verifying the pod is in kubernetes")
  211. options = api.ListOptions{
  212. LabelSelector: selector,
  213. ResourceVersion: pod.ResourceVersion,
  214. }
  215. pods, err = podClient.List(options)
  216. if err != nil {
  217. framework.Failf("Failed to query for pods: %v", err)
  218. }
  219. Expect(len(pods.Items)).To(Equal(1))
  220. By("verifying pod creation was observed")
  221. observePodCreation(w)
  222. // We need to wait for the pod to be scheduled, otherwise the deletion
  223. // will be carried out immediately rather than gracefully.
  224. framework.ExpectNoError(f.WaitForPodRunning(pod.Name))
  225. By("deleting the pod gracefully")
  226. if err := podClient.Delete(pod.Name, api.NewDeleteOptions(30)); err != nil {
  227. framework.Failf("Failed to delete pod: %v", err)
  228. }
  229. By("verifying pod deletion was observed")
  230. obj := observeObjectDeletion(w)
  231. lastPod := obj.(*v1.Pod)
  232. Expect(lastPod.DeletionTimestamp).ToNot(BeNil())
  233. Expect(lastPod.Spec.TerminationGracePeriodSeconds).ToNot(BeZero())
  234. options = api.ListOptions{LabelSelector: selector}
  235. pods, err = podClient.List(options)
  236. if err != nil {
  237. framework.Failf("Failed to list pods to verify deletion: %v", err)
  238. }
  239. Expect(len(pods.Items)).To(Equal(0))
  240. })
  241. })
  242. var _ = framework.KubeDescribe("Staging client repo client", func() {
  243. f := framework.NewDefaultFramework("clientset")
  244. It("should create pods, delete pods, watch pods", func() {
  245. podClient := f.StagingClient.Core().Pods(f.Namespace.Name)
  246. By("constructing the pod")
  247. name := "pod" + string(uuid.NewUUID())
  248. value := strconv.Itoa(time.Now().Nanosecond())
  249. podCopy := stagingClientPod(name, value)
  250. pod := &podCopy
  251. By("verifying no pod exists before the test")
  252. pods, err := podClient.List(clientapi.ListOptions{})
  253. if err != nil {
  254. framework.Failf("Failed to query for pods: %v", err)
  255. }
  256. Expect(len(pods.Items)).To(Equal(0))
  257. By("creating the pod")
  258. pod, err = podClient.Create(pod)
  259. if err != nil {
  260. framework.Failf("Failed to create pod: %v", err)
  261. }
  262. // We call defer here in case there is a problem with
  263. // the test so we can ensure that we clean up after
  264. // ourselves
  265. defer podClient.Delete(pod.Name, clientapi.NewDeleteOptions(0))
  266. By("verifying the pod is in kubernetes")
  267. timeout := 1 * time.Minute
  268. if err := wait.PollImmediate(time.Second, timeout, func() (bool, error) {
  269. pods, err = podClient.List(clientapi.ListOptions{})
  270. if err != nil {
  271. return false, err
  272. }
  273. if len(pods.Items) == 1 {
  274. return true, nil
  275. }
  276. return false, nil
  277. }); err != nil {
  278. framework.Failf("Err : %s\n. Failed to wait for 1 pod to be created", err)
  279. }
  280. })
  281. })