node_e2e.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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 remote
  14. import (
  15. "fmt"
  16. "os"
  17. "os/exec"
  18. "path/filepath"
  19. "strings"
  20. "time"
  21. "k8s.io/klog"
  22. "k8s.io/kubernetes/test/e2e_node/builder"
  23. "k8s.io/kubernetes/test/e2e_node/system"
  24. "k8s.io/kubernetes/test/utils"
  25. )
  26. // NodeE2ERemote contains the specific functions in the node e2e test suite.
  27. type NodeE2ERemote struct{}
  28. // InitNodeE2ERemote initializes the node e2e test suite.
  29. func InitNodeE2ERemote() TestSuite {
  30. // TODO: Register flags.
  31. return &NodeE2ERemote{}
  32. }
  33. // SetupTestPackage sets up the test package with binaries k8s required for node e2e tests
  34. func (n *NodeE2ERemote) SetupTestPackage(tardir, systemSpecName string) error {
  35. // Build the executables
  36. if err := builder.BuildGo(); err != nil {
  37. return fmt.Errorf("failed to build the dependencies: %v", err)
  38. }
  39. // Make sure we can find the newly built binaries
  40. buildOutputDir, err := utils.GetK8sBuildOutputDir()
  41. if err != nil {
  42. return fmt.Errorf("failed to locate kubernetes build output directory: %v", err)
  43. }
  44. rootDir, err := utils.GetK8sRootDir()
  45. if err != nil {
  46. return fmt.Errorf("failed to locate kubernetes root directory: %v", err)
  47. }
  48. // Copy binaries
  49. requiredBins := []string{"kubelet", "e2e_node.test", "ginkgo", "mounter"}
  50. for _, bin := range requiredBins {
  51. source := filepath.Join(buildOutputDir, bin)
  52. if _, err := os.Stat(source); err != nil {
  53. return fmt.Errorf("failed to locate test binary %s: %v", bin, err)
  54. }
  55. out, err := exec.Command("cp", source, filepath.Join(tardir, bin)).CombinedOutput()
  56. if err != nil {
  57. return fmt.Errorf("failed to copy %q: %v Output: %q", bin, err, out)
  58. }
  59. }
  60. if systemSpecName != "" {
  61. // Copy system spec file
  62. source := filepath.Join(rootDir, system.SystemSpecPath, systemSpecName+".yaml")
  63. if _, err := os.Stat(source); err != nil {
  64. return fmt.Errorf("failed to locate system spec %q: %v", source, err)
  65. }
  66. out, err := exec.Command("cp", source, tardir).CombinedOutput()
  67. if err != nil {
  68. return fmt.Errorf("failed to copy system spec %q: %v, output: %q", source, err, out)
  69. }
  70. }
  71. return nil
  72. }
  73. // prependCOSMounterFlag prepends the flag for setting the GCI mounter path to
  74. // args and returns the result.
  75. func prependCOSMounterFlag(args, host, workspace string) (string, error) {
  76. klog.V(2).Infof("GCI/COS node and GCI/COS mounter both detected, modifying --experimental-mounter-path accordingly")
  77. mounterPath := filepath.Join(workspace, "mounter")
  78. args = fmt.Sprintf("--kubelet-flags=--experimental-mounter-path=%s ", mounterPath) + args
  79. return args, nil
  80. }
  81. // prependMemcgNotificationFlag prepends the flag for enabling memcg
  82. // notification to args and returns the result.
  83. func prependMemcgNotificationFlag(args string) string {
  84. return "--kubelet-flags=--experimental-kernel-memcg-notification=true " + args
  85. }
  86. // updateOSSpecificKubeletFlags updates the Kubelet args with OS specific
  87. // settings.
  88. func updateOSSpecificKubeletFlags(args, host, workspace string) (string, error) {
  89. output, err := SSH(host, "cat", "/etc/os-release")
  90. if err != nil {
  91. return "", fmt.Errorf("issue detecting node's OS via node's /etc/os-release. Err: %v, Output:\n%s", err, output)
  92. }
  93. switch {
  94. case strings.Contains(output, "ID=gci"), strings.Contains(output, "ID=cos"):
  95. args = prependMemcgNotificationFlag(args)
  96. return prependCOSMounterFlag(args, host, workspace)
  97. case strings.Contains(output, "ID=ubuntu"):
  98. return prependMemcgNotificationFlag(args), nil
  99. }
  100. return args, nil
  101. }
  102. // RunTest runs test on the node.
  103. func (n *NodeE2ERemote) RunTest(host, workspace, results, imageDesc, junitFilePrefix, testArgs, ginkgoArgs, systemSpecName, extraEnvs string, timeout time.Duration) (string, error) {
  104. // Install the cni plugins and add a basic CNI configuration.
  105. // TODO(random-liu): Do this in cloud init after we remove containervm test.
  106. if err := setupCNI(host, workspace); err != nil {
  107. return "", err
  108. }
  109. // Configure iptables firewall rules
  110. if err := configureFirewall(host); err != nil {
  111. return "", err
  112. }
  113. // Kill any running node processes
  114. cleanupNodeProcesses(host)
  115. testArgs, err := updateOSSpecificKubeletFlags(testArgs, host, workspace)
  116. if err != nil {
  117. return "", err
  118. }
  119. systemSpecFile := ""
  120. if systemSpecName != "" {
  121. systemSpecFile = systemSpecName + ".yaml"
  122. }
  123. // Run the tests
  124. klog.V(2).Infof("Starting tests on %q", host)
  125. cmd := getSSHCommand(" && ",
  126. fmt.Sprintf("cd %s", workspace),
  127. fmt.Sprintf("timeout -k 30s %fs ./ginkgo %s ./e2e_node.test -- --system-spec-name=%s --system-spec-file=%s --extra-envs=%s --logtostderr --v 4 --node-name=%s --report-dir=%s --report-prefix=%s --image-description=\"%s\" %s",
  128. timeout.Seconds(), ginkgoArgs, systemSpecName, systemSpecFile, extraEnvs, host, results, junitFilePrefix, imageDesc, testArgs),
  129. )
  130. return SSH(host, "sh", "-c", cmd)
  131. }