replica_set.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /*
  2. Copyright 2016 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/api/unversioned"
  19. "k8s.io/kubernetes/pkg/apis/extensions"
  20. "k8s.io/kubernetes/pkg/labels"
  21. "k8s.io/kubernetes/pkg/util/uuid"
  22. "k8s.io/kubernetes/pkg/util/wait"
  23. "k8s.io/kubernetes/test/e2e/framework"
  24. . "github.com/onsi/ginkgo"
  25. . "github.com/onsi/gomega"
  26. )
  27. var _ = framework.KubeDescribe("ReplicaSet", func() {
  28. f := framework.NewDefaultFramework("replicaset")
  29. It("should serve a basic image on each replica with a public image [Conformance]", func() {
  30. ReplicaSetServeImageOrFail(f, "basic", "gcr.io/google_containers/serve_hostname:v1.4")
  31. })
  32. It("should serve a basic image on each replica with a private image", func() {
  33. // requires private images
  34. framework.SkipUnlessProviderIs("gce", "gke")
  35. ReplicaSetServeImageOrFail(f, "private", "b.gcr.io/k8s_authenticated_test/serve_hostname:v1.4")
  36. })
  37. })
  38. // A basic test to check the deployment of an image using a ReplicaSet. The
  39. // image serves its hostname which is checked for each replica.
  40. func ReplicaSetServeImageOrFail(f *framework.Framework, test string, image string) {
  41. name := "my-hostname-" + test + "-" + string(uuid.NewUUID())
  42. replicas := int32(2)
  43. // Create a ReplicaSet for a service 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 ReplicaSet %s", name))
  47. rs, err := f.Client.Extensions().ReplicaSets(f.Namespace.Name).Create(&extensions.ReplicaSet{
  48. ObjectMeta: api.ObjectMeta{
  49. Name: name,
  50. },
  51. Spec: extensions.ReplicaSetSpec{
  52. Replicas: replicas,
  53. Selector: &unversioned.LabelSelector{MatchLabels: 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 ReplicaSet when we are done.
  74. defer func() {
  75. // Resize the ReplicaSet to zero to get rid of pods.
  76. if err := framework.DeleteReplicaSet(f.Client, f.Namespace.Name, rs.Name); err != nil {
  77. framework.Logf("Failed to cleanup ReplicaSet %v: %v.", rs.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. Expect(err).NotTo(HaveOccurred())
  84. By("Ensuring each pod is running")
  85. // Wait for the pods to enter the running state. Waiting loops until the pods
  86. // are running so non-running pods cause a timeout for this test.
  87. for _, pod := range pods.Items {
  88. if pod.DeletionTimestamp != nil {
  89. continue
  90. }
  91. err = f.WaitForPodRunning(pod.Name)
  92. Expect(err).NotTo(HaveOccurred())
  93. }
  94. // Verify that something is listening.
  95. By("Trying to dial each unique pod")
  96. retryTimeout := 2 * time.Minute
  97. retryInterval := 5 * time.Second
  98. err = wait.Poll(retryInterval, retryTimeout, framework.PodProxyResponseChecker(f.Client, f.Namespace.Name, label, name, true, pods).CheckAllResponses)
  99. if err != nil {
  100. framework.Failf("Did not get expected responses within the timeout period of %.2f seconds.", retryTimeout.Seconds())
  101. }
  102. }