mockexec.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /*
  2. Copyright 2016 The Rook Authors. All rights reserved.
  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 test
  14. import (
  15. "fmt"
  16. "os"
  17. "os/exec"
  18. "strconv"
  19. "testing"
  20. "time"
  21. )
  22. // MockExecutor mocks all the exec commands
  23. type MockExecutor struct {
  24. MockExecuteCommand func(command string, arg ...string) error
  25. MockExecuteCommandWithEnv func(env []string, command string, arg ...string) error
  26. MockStartExecuteCommand func(command string, arg ...string) (*exec.Cmd, error)
  27. MockExecuteCommandWithOutput func(command string, arg ...string) (string, error)
  28. MockExecuteCommandWithCombinedOutput func(command string, arg ...string) (string, error)
  29. MockExecuteCommandWithTimeout func(timeout time.Duration, command string, arg ...string) (string, error)
  30. MockExecuteCommandWithStdin func(timeout time.Duration, command string, stdin *string, arg ...string) error
  31. }
  32. // ExecuteCommand mocks ExecuteCommand
  33. func (e *MockExecutor) ExecuteCommand(command string, arg ...string) error {
  34. if e.MockExecuteCommand != nil {
  35. return e.MockExecuteCommand(command, arg...)
  36. }
  37. return nil
  38. }
  39. // ExecuteCommandWithStdin starts a process, provides stdin and wait for its completion with timeout.
  40. func (e *MockExecutor) ExecuteCommandWithStdin(timeout time.Duration, command string, stdin *string, arg ...string) error {
  41. if e.MockExecuteCommand != nil {
  42. return e.MockExecuteCommandWithStdin(timeout, command, stdin, arg...)
  43. }
  44. return nil
  45. }
  46. // ExecuteCommandWithEnv mocks ExecuteCommandWithEnv
  47. func (e *MockExecutor) ExecuteCommandWithEnv(env []string, command string, arg ...string) error {
  48. if e.MockExecuteCommandWithEnv != nil {
  49. return e.MockExecuteCommandWithEnv(env, command, arg...)
  50. }
  51. return nil
  52. }
  53. // ExecuteCommandWithOutput mocks ExecuteCommandWithOutput
  54. func (e *MockExecutor) ExecuteCommandWithOutput(command string, arg ...string) (string, error) {
  55. if e.MockExecuteCommandWithOutput != nil {
  56. return e.MockExecuteCommandWithOutput(command, arg...)
  57. }
  58. return "", nil
  59. }
  60. // ExecuteCommandWithTimeout mocks ExecuteCommandWithTimeout
  61. func (e *MockExecutor) ExecuteCommandWithTimeout(timeout time.Duration, command string, arg ...string) (string, error) {
  62. if e.MockExecuteCommandWithTimeout != nil {
  63. return e.MockExecuteCommandWithTimeout(time.Second, command, arg...)
  64. }
  65. return "", nil
  66. }
  67. // ExecuteCommandWithCombinedOutput mocks ExecuteCommandWithCombinedOutput
  68. func (e *MockExecutor) ExecuteCommandWithCombinedOutput(command string, arg ...string) (string, error) {
  69. if e.MockExecuteCommandWithCombinedOutput != nil {
  70. return e.MockExecuteCommandWithCombinedOutput(command, arg...)
  71. }
  72. return "", nil
  73. }
  74. // Mock an executed command with the desired return values.
  75. // STDERR is returned *before* STDOUT.
  76. //
  77. // This will return an error if the given exit code is nonzero. The error return is the primary
  78. // benefit of using this method.
  79. //
  80. // In order for this to work in a `*_test.go` file, you MUST import TestMockExecHelperProcess
  81. // exactly as shown below:
  82. //
  83. // import exectest "github.com/rook/rook/pkg/util/exec/test"
  84. // // import TestMockExecHelperProcess
  85. // func TestMockExecHelperProcess(t *testing.T) {
  86. // exectest.TestMockExecHelperProcess(t)
  87. // }
  88. //
  89. // Inspired by: https://github.com/golang/go/blob/master/src/os/exec/exec_test.go
  90. func MockExecCommandReturns(t *testing.T, stdout, stderr string, retcode int) error {
  91. cmd := exec.Command(os.Args[0], "-test.run=TestMockExecHelperProcess") //nolint:gosec //Rook controls the input to the exec arguments
  92. cmd.Env = append(os.Environ(),
  93. "GO_WANT_HELPER_PROCESS=1",
  94. fmt.Sprintf("GO_HELPER_PROCESS_STDOUT=%s", stdout),
  95. fmt.Sprintf("GO_HELPER_PROCESS_STDERR=%s", stderr),
  96. fmt.Sprintf("GO_HELPER_PROCESS_RETCODE=%d", retcode),
  97. )
  98. err := cmd.Run()
  99. return err
  100. }
  101. // TestHelperProcess isn't a real test. It's used as a helper process for MockExecCommandReturns to
  102. // simulate output from a command. Notably, this can return a realistic os/exec error.
  103. // Inspired by: https://github.com/golang/go/blob/master/src/os/exec/exec_test.go
  104. func TestMockExecHelperProcess(t *testing.T) {
  105. if os.Getenv("GO_WANT_HELPER_PROCESS") != "1" {
  106. return
  107. }
  108. // test should set these in its environment to control the output of the test commands
  109. fmt.Fprint(os.Stderr, os.Getenv("GO_HELPER_PROCESS_STDERR")) // return stderr before stdout
  110. fmt.Fprint(os.Stdout, os.Getenv("GO_HELPER_PROCESS_STDOUT"))
  111. rc, err := strconv.Atoi(os.Getenv("GO_HELPER_PROCESS_RETCODE"))
  112. if err != nil {
  113. panic(err)
  114. }
  115. os.Exit(rc)
  116. }
  117. func FakeTimeoutError(text string) error {
  118. return fmt.Errorf("exec timeout waiting for %s", text)
  119. }