node_e2e.go 5.2 KB

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