run_local.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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 main
  14. import (
  15. "flag"
  16. "fmt"
  17. "os"
  18. "os/exec"
  19. "path/filepath"
  20. "runtime"
  21. "strings"
  22. "k8s.io/klog"
  23. "k8s.io/kubernetes/test/utils"
  24. )
  25. func bazelBuild() error {
  26. targets := []string{
  27. "//vendor/github.com/onsi/ginkgo/ginkgo",
  28. "//test/e2e_kubeadm:e2e_kubeadm.test",
  29. }
  30. args := append([]string{"build"}, targets...)
  31. return execCommand("bazel", args...)
  32. }
  33. var ginkgoFlags = flag.String("ginkgo-flags", "", "Space-separated list of arguments to pass to Ginkgo test runner.")
  34. var testFlags = flag.String("test-flags", "", "Space-separated list of arguments to pass to node e2e test.")
  35. var build = flag.Bool("build", false, "use Bazel to build binaries before testing")
  36. func main() {
  37. flag.Parse()
  38. if *build {
  39. if err := bazelBuild(); err != nil {
  40. klog.Exitf("couldn't build with bazel: %v", err)
  41. }
  42. }
  43. ginkgo, err := getBazelGinkgo()
  44. if err != nil {
  45. klog.Fatalf("Failed to get ginkgo binary: %v", err)
  46. }
  47. test, err := getBazelTestBin()
  48. if err != nil {
  49. klog.Fatalf("Failed to get test file: %v", err)
  50. }
  51. args := append(strings.Split(*ginkgoFlags, " "), test, "--")
  52. args = append(args, strings.Split(*testFlags, " ")...)
  53. if execCommand(ginkgo, args...); err != nil {
  54. klog.Exitf("Test failed: %v", err)
  55. }
  56. }
  57. func getBazelTestBin() (string, error) {
  58. k8sRoot, err := utils.GetK8sRootDir()
  59. if err != nil {
  60. return "", err
  61. }
  62. buildFile := filepath.Join(k8sRoot, "bazel-bin/test/e2e_kubeadm/e2e_kubeadm.test")
  63. if _, err := os.Stat(buildFile); err != nil {
  64. return "", err
  65. }
  66. return buildFile, nil
  67. }
  68. func getBazelGinkgo() (string, error) {
  69. k8sRoot, err := utils.GetK8sRootDir()
  70. if err != nil {
  71. return "", err
  72. }
  73. buildOutputDir := filepath.Join(k8sRoot, "bazel-bin", "vendor/github.com/onsi/ginkgo/ginkgo", fmt.Sprintf("%s_%s_stripped", runtime.GOOS, runtime.GOARCH), "ginkgo")
  74. if _, err := os.Stat(buildOutputDir); err != nil {
  75. return "", err
  76. }
  77. return buildOutputDir, nil
  78. }
  79. func execCommand(binary string, args ...string) error {
  80. fmt.Printf("Running command: %v %v\n", binary, strings.Join(args, " "))
  81. cmd := exec.Command("sh", "-c", strings.Join(append([]string{binary}, args...), " "))
  82. cmd.Stdout = os.Stdout
  83. cmd.Stderr = os.Stderr
  84. return cmd.Run()
  85. }