util.go 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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 kubeadm
  14. import (
  15. "bytes"
  16. "os/exec"
  17. "testing"
  18. "github.com/pkg/errors"
  19. "github.com/spf13/cobra"
  20. )
  21. // Forked from test/e2e/framework because the e2e framework is quite bloated
  22. // for our purposes here, and modified to remove undesired logging.
  23. func runCmdNoWrap(command string, args ...string) (string, string, int, error) {
  24. var bout, berr bytes.Buffer
  25. cmd := exec.Command(command, args...)
  26. cmd.Stdout = &bout
  27. cmd.Stderr = &berr
  28. err := cmd.Run()
  29. stdout, stderr := bout.String(), berr.String()
  30. return stdout, stderr, cmd.ProcessState.ExitCode(), err
  31. }
  32. // RunCmd is a utility function for kubeadm testing that executes a specified command
  33. func RunCmd(command string, args ...string) (string, string, int, error) {
  34. stdout, stderr, retcode, err := runCmdNoWrap(command, args...)
  35. if err != nil {
  36. return stdout, stderr, retcode, errors.Wrapf(err, "error running %s %v; \nretcode %d, \nstdout %q, \nstderr %q, \ngot error",
  37. command, args, retcode, stdout, stderr)
  38. }
  39. return stdout, stderr, retcode, nil
  40. }
  41. // RunSubCommand is a utility function for kubeadm testing that executes a Cobra sub command
  42. func RunSubCommand(t *testing.T, subCmds []*cobra.Command, command string, args ...string) {
  43. subCmd := getSubCommand(t, subCmds, command)
  44. subCmd.SetArgs(args)
  45. if err := subCmd.Execute(); err != nil {
  46. t.Fatalf("Could not execute subcommand: %s", command)
  47. }
  48. }
  49. // AssertSubCommandHasFlags is a utility function for kubeadm testing that assert if a Cobra sub command has expected flags
  50. func AssertSubCommandHasFlags(t *testing.T, subCmds []*cobra.Command, command string, flags ...string) {
  51. subCmd := getSubCommand(t, subCmds, command)
  52. for _, flag := range flags {
  53. if subCmd.Flags().Lookup(flag) == nil {
  54. t.Errorf("Could not find expecte flag %s for command %s", flag, command)
  55. }
  56. }
  57. }
  58. func getSubCommand(t *testing.T, subCmds []*cobra.Command, name string) *cobra.Command {
  59. for _, subCmd := range subCmds {
  60. if subCmd.Name() == name {
  61. return subCmd
  62. }
  63. }
  64. t.Fatalf("Unable to find sub command %s", name)
  65. return nil
  66. }