rc.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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 e2e
  14. import (
  15. "fmt"
  16. "time"
  17. "k8s.io/kubernetes/pkg/api"
  18. "k8s.io/kubernetes/pkg/labels"
  19. "k8s.io/kubernetes/pkg/util/uuid"
  20. "k8s.io/kubernetes/pkg/util/wait"
  21. "k8s.io/kubernetes/test/e2e/framework"
  22. . "github.com/onsi/ginkgo"
  23. . "github.com/onsi/gomega"
  24. )
  25. var _ = framework.KubeDescribe("ReplicationController", func() {
  26. f := framework.NewDefaultFramework("replication-controller")
  27. It("should serve a basic image on each replica with a public image [Conformance]", func() {
  28. ServeImageOrFail(f, "basic", "gcr.io/google_containers/serve_hostname:v1.4")
  29. })
  30. It("should serve a basic image on each replica with a private image", func() {
  31. // requires private images
  32. framework.SkipUnlessProviderIs("gce", "gke")
  33. ServeImageOrFail(f, "private", "b.gcr.io/k8s_authenticated_test/serve_hostname:v1.4")
  34. })
  35. })
  36. // A basic test to check the deployment of an image using
  37. // a replication controller. The image serves its hostname
  38. // which is checked for each replica.
  39. func ServeImageOrFail(f *framework.Framework, test string, image string) {
  40. name := "my-hostname-" + test + "-" + string(uuid.NewUUID())
  41. replicas := int32(2)
  42. // Create a replication controller for a service
  43. // that serves its hostname.
  44. // The source for the Docker containter kubernetes/serve_hostname is
  45. // in contrib/for-demos/serve_hostname
  46. By(fmt.Sprintf("Creating replication controller %s", name))
  47. controller, err := f.Client.ReplicationControllers(f.Namespace.Name).Create(&api.ReplicationController{
  48. ObjectMeta: api.ObjectMeta{
  49. Name: name,
  50. },
  51. Spec: api.ReplicationControllerSpec{
  52. Replicas: replicas,
  53. Selector: map[string]string{
  54. "name": name,
  55. },
  56. Template: &api.PodTemplateSpec{
  57. ObjectMeta: api.ObjectMeta{
  58. Labels: map[string]string{"name": name},
  59. },
  60. Spec: api.PodSpec{
  61. Containers: []api.Container{
  62. {
  63. Name: name,
  64. Image: image,
  65. Ports: []api.ContainerPort{{ContainerPort: 9376}},
  66. },
  67. },
  68. },
  69. },
  70. },
  71. })
  72. Expect(err).NotTo(HaveOccurred())
  73. // Cleanup the replication controller when we are done.
  74. defer func() {
  75. // Resize the replication controller to zero to get rid of pods.
  76. if err := framework.DeleteRCAndPods(f.Client, f.Namespace.Name, controller.Name); err != nil {
  77. framework.Logf("Failed to cleanup replication controller %v: %v.", controller.Name, err)
  78. }
  79. }()
  80. // List the pods, making sure we observe all the replicas.
  81. label := labels.SelectorFromSet(labels.Set(map[string]string{"name": name}))
  82. pods, err := framework.PodsCreated(f.Client, f.Namespace.Name, name, replicas)
  83. By("Ensuring each pod is running")
  84. // Wait for the pods to enter the running state. Waiting loops until the pods
  85. // are running so non-running pods cause a timeout for this test.
  86. for _, pod := range pods.Items {
  87. if pod.DeletionTimestamp != nil {
  88. continue
  89. }
  90. err = f.WaitForPodRunning(pod.Name)
  91. Expect(err).NotTo(HaveOccurred())
  92. }
  93. // Verify that something is listening.
  94. By("Trying to dial each unique pod")
  95. retryTimeout := 2 * time.Minute
  96. retryInterval := 5 * time.Second
  97. err = wait.Poll(retryInterval, retryTimeout, framework.PodProxyResponseChecker(f.Client, f.Namespace.Name, label, name, true, pods).CheckAllResponses)
  98. if err != nil {
  99. framework.Failf("Did not get expected responses within the timeout period of %.2f seconds.", retryTimeout.Seconds())
  100. }
  101. }