examples.go 5.8 KB

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