exec_test.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. // Copyright 2019 Yunion
  2. //
  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. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. /*
  15. Copyright 2015 The Kubernetes Authors.
  16. Licensed under the Apache License, Version 2.0 (the "License");
  17. you may not use this file except in compliance with the License.
  18. You may obtain a copy of the License at
  19. http://www.apache.org/licenses/LICENSE-2.0
  20. Unless required by applicable law or agreed to in writing, software
  21. distributed under the License is distributed on an "AS IS" BASIS,
  22. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  23. See the License for the specific language governing permissions and
  24. limitations under the License.
  25. */
  26. package exec
  27. import (
  28. "fmt"
  29. "io"
  30. "strings"
  31. "testing"
  32. "yunion.io/x/onecloud/pkg/util/probe"
  33. )
  34. type FakeCmd struct {
  35. out []byte
  36. stdout []byte
  37. err error
  38. writer io.Writer
  39. }
  40. func (f *FakeCmd) Run() error {
  41. return nil
  42. }
  43. func (f *FakeCmd) CombinedOutput() ([]byte, error) {
  44. return f.out, f.err
  45. }
  46. func (f *FakeCmd) Output() ([]byte, error) {
  47. return f.stdout, f.err
  48. }
  49. func (f *FakeCmd) SetDir(dir string) {}
  50. func (f *FakeCmd) SetStdin(in io.Reader) {}
  51. func (f *FakeCmd) SetStdout(out io.Writer) {
  52. f.writer = out
  53. }
  54. func (f *FakeCmd) SetStderr(out io.Writer) {
  55. f.writer = out
  56. }
  57. func (f *FakeCmd) SetEnv(env []string) {}
  58. func (f *FakeCmd) Stop() {}
  59. func (f *FakeCmd) Start() error {
  60. if f.writer != nil {
  61. f.writer.Write(f.out)
  62. return f.err
  63. }
  64. return f.err
  65. }
  66. func (f *FakeCmd) Wait() error { return nil }
  67. func (f *FakeCmd) StdoutPipe() (io.ReadCloser, error) {
  68. return nil, nil
  69. }
  70. func (f *FakeCmd) StderrPipe() (io.ReadCloser, error) {
  71. return nil, nil
  72. }
  73. type fakeExitError struct {
  74. exited bool
  75. statusCode int
  76. }
  77. func (f *fakeExitError) String() string {
  78. return f.Error()
  79. }
  80. func (f *fakeExitError) Error() string {
  81. return "fake exit"
  82. }
  83. func (f *fakeExitError) Exited() bool {
  84. return f.exited
  85. }
  86. func (f *fakeExitError) ExitStatus() int {
  87. return f.statusCode
  88. }
  89. func TestExec(t *testing.T) {
  90. prober := New()
  91. tenKilobyte := strings.Repeat("logs-123", 128*10) // 8*128*10=10240 = 10KB of text.
  92. elevenKilobyte := strings.Repeat("logs-123", 8*128*11) // 8*128*11=11264 = 11KB of text.
  93. tests := []struct {
  94. expectedStatus probe.Result
  95. expectError bool
  96. input string
  97. output string
  98. err error
  99. }{
  100. // Ok
  101. {probe.Success, false, "OK", "OK", nil},
  102. // Ok
  103. {probe.Success, false, "OK", "OK", &fakeExitError{true, 0}},
  104. // Ok - truncated output
  105. {probe.Success, false, elevenKilobyte, tenKilobyte, nil},
  106. // Run returns error
  107. {probe.Unknown, true, "", "", fmt.Errorf("test error")},
  108. // Unhealthy
  109. {probe.Failure, false, "Fail", "", &fakeExitError{true, 1}},
  110. }
  111. for i, test := range tests {
  112. fake := FakeCmd{
  113. out: []byte(test.output),
  114. err: test.err,
  115. }
  116. status, output, err := prober.Probe(&fake, "")
  117. if status != test.expectedStatus {
  118. t.Errorf("[%d] expected %v, got %v", i, test.expectedStatus, status)
  119. }
  120. if err != nil && test.expectError == false {
  121. t.Errorf("[%d] unexpected error: %v", i, err)
  122. }
  123. if err == nil && test.expectError == true {
  124. t.Errorf("[%d] unexpected non-error", i)
  125. }
  126. if status == probe.Success && test.output != output {
  127. t.Errorf("[%d] expected %q, got %q", i, test.output, output)
  128. }
  129. }
  130. }