examples.go 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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. "path/filepath"
  17. "sync"
  18. "time"
  19. rbacv1beta1 "k8s.io/api/rbac/v1beta1"
  20. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  21. "k8s.io/apimachinery/pkg/runtime/schema"
  22. "k8s.io/apiserver/pkg/authentication/serviceaccount"
  23. clientset "k8s.io/client-go/kubernetes"
  24. podutil "k8s.io/kubernetes/pkg/api/v1/pod"
  25. commonutils "k8s.io/kubernetes/test/e2e/common"
  26. "k8s.io/kubernetes/test/e2e/framework"
  27. "k8s.io/kubernetes/test/e2e/framework/auth"
  28. e2elog "k8s.io/kubernetes/test/e2e/framework/log"
  29. "k8s.io/kubernetes/test/e2e/framework/testfiles"
  30. "github.com/onsi/ginkgo"
  31. )
  32. const (
  33. serverStartTimeout = framework.PodStartTimeout + 3*time.Minute
  34. )
  35. var _ = framework.KubeDescribe("[Feature:Example]", func() {
  36. f := framework.NewDefaultFramework("examples")
  37. var c clientset.Interface
  38. var ns string
  39. ginkgo.BeforeEach(func() {
  40. c = f.ClientSet
  41. ns = f.Namespace.Name
  42. // this test wants powerful permissions. Since the namespace names are unique, we can leave this
  43. // lying around so we don't have to race any caches
  44. err := auth.BindClusterRoleInNamespace(c.RbacV1beta1(), "edit", f.Namespace.Name,
  45. rbacv1beta1.Subject{Kind: rbacv1beta1.ServiceAccountKind, Namespace: f.Namespace.Name, Name: "default"})
  46. framework.ExpectNoError(err)
  47. err = auth.WaitForAuthorizationUpdate(c.AuthorizationV1beta1(),
  48. serviceaccount.MakeUsername(f.Namespace.Name, "default"),
  49. f.Namespace.Name, "create", schema.GroupResource{Resource: "pods"}, true)
  50. framework.ExpectNoError(err)
  51. })
  52. framework.KubeDescribe("Liveness", func() {
  53. ginkgo.It("liveness pods should be automatically restarted", func() {
  54. test := "test/fixtures/doc-yaml/user-guide/liveness"
  55. execYaml := readFile(test, "exec-liveness.yaml.in")
  56. httpYaml := readFile(test, "http-liveness.yaml.in")
  57. nsFlag := fmt.Sprintf("--namespace=%v", ns)
  58. framework.RunKubectlOrDieInput(execYaml, "create", "-f", "-", nsFlag)
  59. framework.RunKubectlOrDieInput(httpYaml, "create", "-f", "-", nsFlag)
  60. // Since both containers start rapidly, we can easily run this test in parallel.
  61. var wg sync.WaitGroup
  62. passed := true
  63. checkRestart := func(podName string, timeout time.Duration) {
  64. err := framework.WaitForPodNameRunningInNamespace(c, podName, ns)
  65. framework.ExpectNoError(err)
  66. for t := time.Now(); time.Since(t) < timeout; time.Sleep(framework.Poll) {
  67. pod, err := c.CoreV1().Pods(ns).Get(podName, metav1.GetOptions{})
  68. framework.ExpectNoError(err, fmt.Sprintf("getting pod %s", podName))
  69. stat := podutil.GetExistingContainerStatus(pod.Status.ContainerStatuses, podName)
  70. e2elog.Logf("Pod: %s, restart count:%d", stat.Name, stat.RestartCount)
  71. if stat.RestartCount > 0 {
  72. e2elog.Logf("Saw %v restart, succeeded...", podName)
  73. wg.Done()
  74. return
  75. }
  76. }
  77. e2elog.Logf("Failed waiting for %v restart! ", podName)
  78. passed = false
  79. wg.Done()
  80. }
  81. ginkgo.By("Check restarts")
  82. // Start the "actual test", and wait for both pods to complete.
  83. // If 2 fail: Something is broken with the test (or maybe even with liveness).
  84. // If 1 fails: Its probably just an error in the examples/ files themselves.
  85. wg.Add(2)
  86. for _, c := range []string{"liveness-http", "liveness-exec"} {
  87. go checkRestart(c, 2*time.Minute)
  88. }
  89. wg.Wait()
  90. if !passed {
  91. framework.Failf("At least one liveness example failed. See the logs above.")
  92. }
  93. })
  94. })
  95. framework.KubeDescribe("Secret", func() {
  96. ginkgo.It("should create a pod that reads a secret", func() {
  97. test := "test/fixtures/doc-yaml/user-guide/secrets"
  98. secretYaml := readFile(test, "secret.yaml")
  99. podYaml := readFile(test, "secret-pod.yaml.in")
  100. nsFlag := fmt.Sprintf("--namespace=%v", ns)
  101. podName := "secret-test-pod"
  102. ginkgo.By("creating secret and pod")
  103. framework.RunKubectlOrDieInput(secretYaml, "create", "-f", "-", nsFlag)
  104. framework.RunKubectlOrDieInput(podYaml, "create", "-f", "-", nsFlag)
  105. err := framework.WaitForPodNoLongerRunningInNamespace(c, podName, ns)
  106. framework.ExpectNoError(err)
  107. ginkgo.By("checking if secret was read correctly")
  108. _, err = framework.LookForStringInLog(ns, "secret-test-pod", "test-container", "value-1", serverStartTimeout)
  109. framework.ExpectNoError(err)
  110. })
  111. })
  112. framework.KubeDescribe("Downward API", func() {
  113. ginkgo.It("should create a pod that prints his name and namespace", func() {
  114. test := "test/fixtures/doc-yaml/user-guide/downward-api"
  115. podYaml := readFile(test, "dapi-pod.yaml.in")
  116. nsFlag := fmt.Sprintf("--namespace=%v", ns)
  117. podName := "dapi-test-pod"
  118. ginkgo.By("creating the pod")
  119. framework.RunKubectlOrDieInput(podYaml, "create", "-f", "-", nsFlag)
  120. err := framework.WaitForPodNoLongerRunningInNamespace(c, podName, ns)
  121. framework.ExpectNoError(err)
  122. ginkgo.By("checking if name and namespace were passed correctly")
  123. _, err = framework.LookForStringInLog(ns, podName, "test-container", fmt.Sprintf("MY_POD_NAMESPACE=%v", ns), serverStartTimeout)
  124. framework.ExpectNoError(err)
  125. _, err = framework.LookForStringInLog(ns, podName, "test-container", fmt.Sprintf("MY_POD_NAME=%v", podName), serverStartTimeout)
  126. framework.ExpectNoError(err)
  127. })
  128. })
  129. })
  130. func readFile(test, file string) string {
  131. from := filepath.Join(test, file)
  132. return commonutils.SubstituteImageName(string(testfiles.ReadOrDie(from, ginkgo.Fail)))
  133. }