e2e.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  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. "os"
  18. "path"
  19. "testing"
  20. "time"
  21. "k8s.io/klog"
  22. "github.com/onsi/ginkgo"
  23. "github.com/onsi/ginkgo/config"
  24. "github.com/onsi/ginkgo/reporters"
  25. "github.com/onsi/gomega"
  26. v1 "k8s.io/api/core/v1"
  27. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  28. runtimeutils "k8s.io/apimachinery/pkg/util/runtime"
  29. "k8s.io/apimachinery/pkg/util/wait"
  30. "k8s.io/component-base/logs"
  31. "k8s.io/component-base/version"
  32. commontest "k8s.io/kubernetes/test/e2e/common"
  33. "k8s.io/kubernetes/test/e2e/framework"
  34. e2ekubectl "k8s.io/kubernetes/test/e2e/framework/kubectl"
  35. e2enode "k8s.io/kubernetes/test/e2e/framework/node"
  36. e2epod "k8s.io/kubernetes/test/e2e/framework/pod"
  37. "k8s.io/kubernetes/test/e2e/manifest"
  38. e2ereporters "k8s.io/kubernetes/test/e2e/reporters"
  39. testutils "k8s.io/kubernetes/test/utils"
  40. utilnet "k8s.io/utils/net"
  41. clientset "k8s.io/client-go/kubernetes"
  42. // ensure auth plugins are loaded
  43. _ "k8s.io/client-go/plugin/pkg/client/auth"
  44. // ensure that cloud providers are loaded
  45. _ "k8s.io/kubernetes/test/e2e/framework/providers/aws"
  46. _ "k8s.io/kubernetes/test/e2e/framework/providers/azure"
  47. _ "k8s.io/kubernetes/test/e2e/framework/providers/gce"
  48. _ "k8s.io/kubernetes/test/e2e/framework/providers/kubemark"
  49. _ "k8s.io/kubernetes/test/e2e/framework/providers/openstack"
  50. _ "k8s.io/kubernetes/test/e2e/framework/providers/vsphere"
  51. )
  52. const (
  53. // namespaceCleanupTimeout is how long to wait for the namespace to be deleted.
  54. // If there are any orphaned namespaces to clean up, this test is running
  55. // on a long lived cluster. A long wait here is preferably to spurious test
  56. // failures caused by leaked resources from a previous test run.
  57. namespaceCleanupTimeout = 15 * time.Minute
  58. )
  59. var _ = ginkgo.SynchronizedBeforeSuite(func() []byte {
  60. // Reference common test to make the import valid.
  61. commontest.CurrentSuite = commontest.E2E
  62. setupSuite()
  63. return nil
  64. }, func(data []byte) {
  65. // Run on all Ginkgo nodes
  66. setupSuitePerGinkgoNode()
  67. })
  68. var _ = ginkgo.SynchronizedAfterSuite(func() {
  69. CleanupSuite()
  70. }, func() {
  71. AfterSuiteActions()
  72. })
  73. // RunE2ETests checks configuration parameters (specified through flags) and then runs
  74. // E2E tests using the Ginkgo runner.
  75. // If a "report directory" is specified, one or more JUnit test reports will be
  76. // generated in this directory, and cluster logs will also be saved.
  77. // This function is called on each Ginkgo node in parallel mode.
  78. func RunE2ETests(t *testing.T) {
  79. runtimeutils.ReallyCrash = true
  80. logs.InitLogs()
  81. defer logs.FlushLogs()
  82. gomega.RegisterFailHandler(framework.Fail)
  83. // Disable skipped tests unless they are explicitly requested.
  84. if config.GinkgoConfig.FocusString == "" && config.GinkgoConfig.SkipString == "" {
  85. config.GinkgoConfig.SkipString = `\[Flaky\]|\[Feature:.+\]`
  86. }
  87. // Run tests through the Ginkgo runner with output to console + JUnit for Jenkins
  88. var r []ginkgo.Reporter
  89. if framework.TestContext.ReportDir != "" {
  90. // TODO: we should probably only be trying to create this directory once
  91. // rather than once-per-Ginkgo-node.
  92. if err := os.MkdirAll(framework.TestContext.ReportDir, 0755); err != nil {
  93. klog.Errorf("Failed creating report directory: %v", err)
  94. } else {
  95. r = append(r, reporters.NewJUnitReporter(path.Join(framework.TestContext.ReportDir, fmt.Sprintf("junit_%v%02d.xml", framework.TestContext.ReportPrefix, config.GinkgoConfig.ParallelNode))))
  96. }
  97. }
  98. // Stream the progress to stdout and optionally a URL accepting progress updates.
  99. r = append(r, e2ereporters.NewProgressReporter(framework.TestContext.ProgressReportURL))
  100. klog.Infof("Starting e2e run %q on Ginkgo node %d", framework.RunID, config.GinkgoConfig.ParallelNode)
  101. ginkgo.RunSpecsWithDefaultAndCustomReporters(t, "Kubernetes e2e suite", r)
  102. }
  103. // Run a test container to try and contact the Kubernetes api-server from a pod, wait for it
  104. // to flip to Ready, log its output and delete it.
  105. func runKubernetesServiceTestContainer(c clientset.Interface, ns string) {
  106. path := "test/images/clusterapi-tester/pod.yaml"
  107. framework.Logf("Parsing pod from %v", path)
  108. p, err := manifest.PodFromManifest(path)
  109. if err != nil {
  110. framework.Logf("Failed to parse clusterapi-tester from manifest %v: %v", path, err)
  111. return
  112. }
  113. p.Namespace = ns
  114. if _, err := c.CoreV1().Pods(ns).Create(context.TODO(), p, metav1.CreateOptions{}); err != nil {
  115. framework.Logf("Failed to create %v: %v", p.Name, err)
  116. return
  117. }
  118. defer func() {
  119. if err := c.CoreV1().Pods(ns).Delete(context.TODO(), p.Name, nil); err != nil {
  120. framework.Logf("Failed to delete pod %v: %v", p.Name, err)
  121. }
  122. }()
  123. timeout := 5 * time.Minute
  124. if err := e2epod.WaitForPodCondition(c, ns, p.Name, "clusterapi-tester", timeout, testutils.PodRunningReady); err != nil {
  125. framework.Logf("Pod %v took longer than %v to enter running/ready: %v", p.Name, timeout, err)
  126. return
  127. }
  128. logs, err := e2epod.GetPodLogs(c, ns, p.Name, p.Spec.Containers[0].Name)
  129. if err != nil {
  130. framework.Logf("Failed to retrieve logs from %v: %v", p.Name, err)
  131. } else {
  132. framework.Logf("Output of clusterapi-tester:\n%v", logs)
  133. }
  134. }
  135. // getDefaultClusterIPFamily obtains the default IP family of the cluster
  136. // using the Cluster IP address of the kubernetes service created in the default namespace
  137. // This unequivocally identifies the default IP family because services are single family
  138. // TODO: dual-stack may support multiple families per service
  139. // but we can detect if a cluster is dual stack because pods have two addresses (one per family)
  140. func getDefaultClusterIPFamily(c clientset.Interface) string {
  141. // Get the ClusterIP of the kubernetes service created in the default namespace
  142. svc, err := c.CoreV1().Services(metav1.NamespaceDefault).Get(context.TODO(), "kubernetes", metav1.GetOptions{})
  143. if err != nil {
  144. framework.Failf("Failed to get kubernetes service ClusterIP: %v", err)
  145. }
  146. if utilnet.IsIPv6String(svc.Spec.ClusterIP) {
  147. return "ipv6"
  148. }
  149. return "ipv4"
  150. }
  151. // waitForDaemonSets for all daemonsets in the given namespace to be ready
  152. // (defined as all but 'allowedNotReadyNodes' pods associated with that
  153. // daemonset are ready).
  154. func waitForDaemonSets(c clientset.Interface, ns string, allowedNotReadyNodes int32, timeout time.Duration) error {
  155. start := time.Now()
  156. framework.Logf("Waiting up to %v for all daemonsets in namespace '%s' to start",
  157. timeout, ns)
  158. return wait.PollImmediate(framework.Poll, timeout, func() (bool, error) {
  159. dsList, err := c.AppsV1().DaemonSets(ns).List(context.TODO(), metav1.ListOptions{})
  160. if err != nil {
  161. framework.Logf("Error getting daemonsets in namespace: '%s': %v", ns, err)
  162. if testutils.IsRetryableAPIError(err) {
  163. return false, nil
  164. }
  165. return false, err
  166. }
  167. var notReadyDaemonSets []string
  168. for _, ds := range dsList.Items {
  169. framework.Logf("%d / %d pods ready in namespace '%s' in daemonset '%s' (%d seconds elapsed)", ds.Status.NumberReady, ds.Status.DesiredNumberScheduled, ns, ds.ObjectMeta.Name, int(time.Since(start).Seconds()))
  170. if ds.Status.DesiredNumberScheduled-ds.Status.NumberReady > allowedNotReadyNodes {
  171. notReadyDaemonSets = append(notReadyDaemonSets, ds.ObjectMeta.Name)
  172. }
  173. }
  174. if len(notReadyDaemonSets) > 0 {
  175. framework.Logf("there are not ready daemonsets: %v", notReadyDaemonSets)
  176. return false, nil
  177. }
  178. return true, nil
  179. })
  180. }
  181. // setupSuite is the boilerplate that can be used to setup ginkgo test suites, on the SynchronizedBeforeSuite step.
  182. // There are certain operations we only want to run once per overall test invocation
  183. // (such as deleting old namespaces, or verifying that all system pods are running.
  184. // Because of the way Ginkgo runs tests in parallel, we must use SynchronizedBeforeSuite
  185. // to ensure that these operations only run on the first parallel Ginkgo node.
  186. //
  187. // This function takes two parameters: one function which runs on only the first Ginkgo node,
  188. // returning an opaque byte array, and then a second function which runs on all Ginkgo nodes,
  189. // accepting the byte array.
  190. func setupSuite() {
  191. // Run only on Ginkgo node 1
  192. switch framework.TestContext.Provider {
  193. case "gce", "gke":
  194. framework.LogClusterImageSources()
  195. }
  196. c, err := framework.LoadClientset()
  197. if err != nil {
  198. klog.Fatal("Error loading client: ", err)
  199. }
  200. // Delete any namespaces except those created by the system. This ensures no
  201. // lingering resources are left over from a previous test run.
  202. if framework.TestContext.CleanStart {
  203. deleted, err := framework.DeleteNamespaces(c, nil, /* deleteFilter */
  204. []string{
  205. metav1.NamespaceSystem,
  206. metav1.NamespaceDefault,
  207. metav1.NamespacePublic,
  208. v1.NamespaceNodeLease,
  209. })
  210. if err != nil {
  211. framework.Failf("Error deleting orphaned namespaces: %v", err)
  212. }
  213. klog.Infof("Waiting for deletion of the following namespaces: %v", deleted)
  214. if err := framework.WaitForNamespacesDeleted(c, deleted, namespaceCleanupTimeout); err != nil {
  215. framework.Failf("Failed to delete orphaned namespaces %v: %v", deleted, err)
  216. }
  217. }
  218. // In large clusters we may get to this point but still have a bunch
  219. // of nodes without Routes created. Since this would make a node
  220. // unschedulable, we need to wait until all of them are schedulable.
  221. framework.ExpectNoError(framework.WaitForAllNodesSchedulable(c, framework.TestContext.NodeSchedulableTimeout))
  222. // If NumNodes is not specified then auto-detect how many are scheduleable and not tainted
  223. if framework.TestContext.CloudConfig.NumNodes == framework.DefaultNumNodes {
  224. nodes, err := e2enode.GetReadySchedulableNodes(c)
  225. framework.ExpectNoError(err)
  226. framework.TestContext.CloudConfig.NumNodes = len(nodes.Items)
  227. }
  228. // Ensure all pods are running and ready before starting tests (otherwise,
  229. // cluster infrastructure pods that are being pulled or started can block
  230. // test pods from running, and tests that ensure all pods are running and
  231. // ready will fail).
  232. podStartupTimeout := framework.TestContext.SystemPodsStartupTimeout
  233. // TODO: In large clusters, we often observe a non-starting pods due to
  234. // #41007. To avoid those pods preventing the whole test runs (and just
  235. // wasting the whole run), we allow for some not-ready pods (with the
  236. // number equal to the number of allowed not-ready nodes).
  237. if err := e2epod.WaitForPodsRunningReady(c, metav1.NamespaceSystem, int32(framework.TestContext.MinStartupPods), int32(framework.TestContext.AllowedNotReadyNodes), podStartupTimeout, map[string]string{}); err != nil {
  238. framework.DumpAllNamespaceInfo(c, metav1.NamespaceSystem)
  239. e2ekubectl.LogFailedContainers(c, metav1.NamespaceSystem, framework.Logf)
  240. runKubernetesServiceTestContainer(c, metav1.NamespaceDefault)
  241. framework.Failf("Error waiting for all pods to be running and ready: %v", err)
  242. }
  243. if err := waitForDaemonSets(c, metav1.NamespaceSystem, int32(framework.TestContext.AllowedNotReadyNodes), framework.TestContext.SystemDaemonsetStartupTimeout); err != nil {
  244. framework.Logf("WARNING: Waiting for all daemonsets to be ready failed: %v", err)
  245. }
  246. // Log the version of the server and this client.
  247. framework.Logf("e2e test version: %s", version.Get().GitVersion)
  248. dc := c.DiscoveryClient
  249. serverVersion, serverErr := dc.ServerVersion()
  250. if serverErr != nil {
  251. framework.Logf("Unexpected server error retrieving version: %v", serverErr)
  252. }
  253. if serverVersion != nil {
  254. framework.Logf("kube-apiserver version: %s", serverVersion.GitVersion)
  255. }
  256. if framework.TestContext.NodeKiller.Enabled {
  257. nodeKiller := framework.NewNodeKiller(framework.TestContext.NodeKiller, c, framework.TestContext.Provider)
  258. go nodeKiller.Run(framework.TestContext.NodeKiller.NodeKillerStopCh)
  259. }
  260. }
  261. // setupSuitePerGinkgoNode is the boilerplate that can be used to setup ginkgo test suites, on the SynchronizedBeforeSuite step.
  262. // There are certain operations we only want to run once per overall test invocation on each Ginkgo node
  263. // such as making some global variables accessible to all parallel executions
  264. // Because of the way Ginkgo runs tests in parallel, we must use SynchronizedBeforeSuite
  265. // Ref: https://onsi.github.io/ginkgo/#parallel-specs
  266. func setupSuitePerGinkgoNode() {
  267. // Obtain the default IP family of the cluster
  268. // Some e2e test are designed to work on IPv4 only, this global variable
  269. // allows to adapt those tests to work on both IPv4 and IPv6
  270. // TODO: dual-stack
  271. // the dual stack clusters can be ipv4-ipv6 or ipv6-ipv4, order matters,
  272. // and services use the primary IP family by default
  273. c, err := framework.LoadClientset()
  274. if err != nil {
  275. klog.Fatal("Error loading client: ", err)
  276. }
  277. framework.TestContext.IPFamily = getDefaultClusterIPFamily(c)
  278. framework.Logf("Cluster IP family: %s", framework.TestContext.IPFamily)
  279. }