fake_exec.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /*
  2. Copyright 2014 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 exec
  14. import (
  15. "fmt"
  16. "io"
  17. )
  18. // A simple scripted Interface type.
  19. type FakeExec struct {
  20. CommandScript []FakeCommandAction
  21. CommandCalls int
  22. LookPathFunc func(string) (string, error)
  23. }
  24. type FakeCommandAction func(cmd string, args ...string) Cmd
  25. func (fake *FakeExec) Command(cmd string, args ...string) Cmd {
  26. if fake.CommandCalls > len(fake.CommandScript)-1 {
  27. panic(fmt.Sprintf("ran out of Command() actions. Could not handle command [%d]: %s args: %v", fake.CommandCalls, cmd, args))
  28. }
  29. i := fake.CommandCalls
  30. fake.CommandCalls++
  31. return fake.CommandScript[i](cmd, args...)
  32. }
  33. func (fake *FakeExec) LookPath(file string) (string, error) {
  34. return fake.LookPathFunc(file)
  35. }
  36. // A simple scripted Cmd type.
  37. type FakeCmd struct {
  38. Argv []string
  39. CombinedOutputScript []FakeCombinedOutputAction
  40. CombinedOutputCalls int
  41. CombinedOutputLog [][]string
  42. Dirs []string
  43. Stdin io.Reader
  44. Stdout io.Writer
  45. }
  46. func InitFakeCmd(fake *FakeCmd, cmd string, args ...string) Cmd {
  47. fake.Argv = append([]string{cmd}, args...)
  48. return fake
  49. }
  50. type FakeCombinedOutputAction func() ([]byte, error)
  51. func (fake *FakeCmd) SetDir(dir string) {
  52. fake.Dirs = append(fake.Dirs, dir)
  53. }
  54. func (fake *FakeCmd) SetStdin(in io.Reader) {
  55. fake.Stdin = in
  56. }
  57. func (fake *FakeCmd) SetStdout(out io.Writer) {
  58. fake.Stdout = out
  59. }
  60. func (fake *FakeCmd) CombinedOutput() ([]byte, error) {
  61. if fake.CombinedOutputCalls > len(fake.CombinedOutputScript)-1 {
  62. panic("ran out of CombinedOutput() actions")
  63. }
  64. if fake.CombinedOutputLog == nil {
  65. fake.CombinedOutputLog = [][]string{}
  66. }
  67. i := fake.CombinedOutputCalls
  68. fake.CombinedOutputLog = append(fake.CombinedOutputLog, append([]string{}, fake.Argv...))
  69. fake.CombinedOutputCalls++
  70. return fake.CombinedOutputScript[i]()
  71. }
  72. func (fake *FakeCmd) Output() ([]byte, error) {
  73. return nil, fmt.Errorf("unimplemented")
  74. }
  75. // A simple fake ExitError type.
  76. type FakeExitError struct {
  77. Status int
  78. }
  79. func (fake *FakeExitError) String() string {
  80. return fmt.Sprintf("exit %d", fake.Status)
  81. }
  82. func (fake *FakeExitError) Error() string {
  83. return fake.String()
  84. }
  85. func (fake *FakeExitError) Exited() bool {
  86. return true
  87. }
  88. func (fake *FakeExitError) ExitStatus() int {
  89. return fake.Status
  90. }