exec_test.go 3.1 KB

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