exec_test.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /*
  2. Copyright 2015 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. "testing"
  18. "k8s.io/kubernetes/pkg/probe"
  19. )
  20. type FakeCmd struct {
  21. out []byte
  22. stdout []byte
  23. err error
  24. }
  25. func (f *FakeCmd) Run() error {
  26. return nil
  27. }
  28. func (f *FakeCmd) CombinedOutput() ([]byte, error) {
  29. return f.out, f.err
  30. }
  31. func (f *FakeCmd) Output() ([]byte, error) {
  32. return f.stdout, f.err
  33. }
  34. func (f *FakeCmd) SetDir(dir string) {}
  35. func (f *FakeCmd) SetStdin(in io.Reader) {}
  36. func (f *FakeCmd) SetStdout(out io.Writer) {}
  37. func (f *FakeCmd) SetStderr(out io.Writer) {}
  38. func (f *FakeCmd) SetEnv(env []string) {}
  39. func (f *FakeCmd) Stop() {}
  40. func (f *FakeCmd) Start() error { return nil }
  41. func (f *FakeCmd) Wait() error { return nil }
  42. func (f *FakeCmd) StdoutPipe() (io.ReadCloser, error) {
  43. return nil, nil
  44. }
  45. func (f *FakeCmd) StderrPipe() (io.ReadCloser, error) {
  46. return nil, nil
  47. }
  48. type fakeExitError struct {
  49. exited bool
  50. statusCode int
  51. }
  52. func (f *fakeExitError) String() string {
  53. return f.Error()
  54. }
  55. func (f *fakeExitError) Error() string {
  56. return "fake exit"
  57. }
  58. func (f *fakeExitError) Exited() bool {
  59. return f.exited
  60. }
  61. func (f *fakeExitError) ExitStatus() int {
  62. return f.statusCode
  63. }
  64. func TestExec(t *testing.T) {
  65. prober := New()
  66. tests := []struct {
  67. expectedStatus probe.Result
  68. expectError bool
  69. output string
  70. err error
  71. }{
  72. // Ok
  73. {probe.Success, false, "OK", nil},
  74. // Ok
  75. {probe.Success, false, "OK", &fakeExitError{true, 0}},
  76. // Run returns error
  77. {probe.Unknown, true, "", fmt.Errorf("test error")},
  78. // Unhealthy
  79. {probe.Failure, false, "Fail", &fakeExitError{true, 1}},
  80. }
  81. for i, test := range tests {
  82. fake := FakeCmd{
  83. out: []byte(test.output),
  84. err: test.err,
  85. }
  86. status, output, err := prober.Probe(&fake)
  87. if status != test.expectedStatus {
  88. t.Errorf("[%d] expected %v, got %v", i, test.expectedStatus, status)
  89. }
  90. if err != nil && test.expectError == false {
  91. t.Errorf("[%d] unexpected error: %v", i, err)
  92. }
  93. if err == nil && test.expectError == true {
  94. t.Errorf("[%d] unexpected non-error", i)
  95. }
  96. if test.output != output {
  97. t.Errorf("[%d] expected %s, got %s", i, test.output, output)
  98. }
  99. }
  100. }