cmd.go 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /*
  2. Copyright 2019 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. "fmt"
  16. "io"
  17. "os/exec"
  18. "strings"
  19. )
  20. // getCmd uses the given environment to form the ginkgo command to run tests. It will
  21. // set the stdout/stderr to the given writer.
  22. func getCmd(env Getenver, w io.Writer) *exec.Cmd {
  23. ginkgoArgs := []string{}
  24. // The logic of the parallel env var impacting the skip value necessitates it
  25. // being placed before the rest of the flag resolution.
  26. skip := env.Getenv(skipEnvKey)
  27. switch env.Getenv(parallelEnvKey) {
  28. case "y", "Y", "true":
  29. ginkgoArgs = append(ginkgoArgs, "--p")
  30. if len(skip) == 0 {
  31. skip = serialTestsRegexp
  32. }
  33. }
  34. ginkgoArgs = append(ginkgoArgs, []string{
  35. "--focus=" + env.Getenv(focusEnvKey),
  36. "--skip=" + skip,
  37. "--noColor=true",
  38. }...)
  39. extraArgs := []string{
  40. "--disable-log-dump",
  41. "--repo-root=/kubernetes",
  42. "--provider=" + env.Getenv(providerEnvKey),
  43. "--report-dir=" + env.Getenv(resultsDirEnvKey),
  44. "--kubeconfig=" + env.Getenv(kubeconfigEnvKey),
  45. }
  46. // Extra args handling
  47. sep := " "
  48. if len(env.Getenv(extraArgsSeparaterEnvKey)) > 0 {
  49. sep = env.Getenv(extraArgsSeparaterEnvKey)
  50. }
  51. if len(env.Getenv(extraGinkgoArgsEnvKey)) > 0 {
  52. ginkgoArgs = append(ginkgoArgs, strings.Split(env.Getenv(extraGinkgoArgsEnvKey), sep)...)
  53. }
  54. if len(env.Getenv(extraArgsEnvKey)) > 0 {
  55. fmt.Printf("sep is %q args are %q", sep, env.Getenv(extraArgsEnvKey))
  56. fmt.Println("split", strings.Split(env.Getenv(extraArgsEnvKey), sep))
  57. extraArgs = append(extraArgs, strings.Split(env.Getenv(extraArgsEnvKey), sep)...)
  58. }
  59. if len(env.Getenv(dryRunEnvKey)) > 0 {
  60. ginkgoArgs = append(ginkgoArgs, "--dryRun=true")
  61. }
  62. args := []string{}
  63. args = append(args, ginkgoArgs...)
  64. args = append(args, env.Getenv(testBinEnvKey))
  65. args = append(args, "--")
  66. args = append(args, extraArgs...)
  67. cmd := exec.Command(env.Getenv(ginkgoEnvKey), args...)
  68. cmd.Stdout = w
  69. cmd.Stderr = w
  70. return cmd
  71. }
  72. // cmdInfo generates a useful look at what the command is for printing/debug.
  73. func cmdInfo(cmd *exec.Cmd) string {
  74. return fmt.Sprintf(
  75. `Command env: %v
  76. Run from directory: %v
  77. Executable path: %v
  78. Args (comma-delimited): %v`, cmd.Env, cmd.Dir, cmd.Path, strings.Join(cmd.Args, ","),
  79. )
  80. }