v2_test.go 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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 2016 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 remotecommand
  27. import (
  28. "io"
  29. "net/http"
  30. "strings"
  31. "testing"
  32. "time"
  33. "github.com/pkg/errors"
  34. v1 "k8s.io/api/core/v1"
  35. "yunion.io/x/pkg/util/wait"
  36. "yunion.io/x/onecloud/pkg/util/httpstream"
  37. )
  38. type fakeReader struct {
  39. err error
  40. }
  41. func (r *fakeReader) Read([]byte) (int, error) { return 0, r.err }
  42. type fakeWriter struct{}
  43. func (*fakeWriter) Write([]byte) (int, error) { return 0, nil }
  44. type fakeStreamCreator struct {
  45. created map[string]bool
  46. errors map[string]error
  47. }
  48. var _ streamCreator = &fakeStreamCreator{}
  49. func (f *fakeStreamCreator) CreateStream(headers http.Header) (httpstream.Stream, error) {
  50. streamType := headers.Get(v1.StreamType)
  51. f.created[streamType] = true
  52. return nil, f.errors[streamType]
  53. }
  54. func TestV2CreateStreams(t *testing.T) {
  55. tests := []struct {
  56. name string
  57. stdin bool
  58. stdinError error
  59. stdout bool
  60. stdoutError error
  61. stderr bool
  62. stderrError error
  63. errorError error
  64. tty bool
  65. expectError bool
  66. }{
  67. {
  68. name: "stdin error",
  69. stdin: true,
  70. stdinError: errors.New("stdin error"),
  71. expectError: true,
  72. },
  73. {
  74. name: "stdout error",
  75. stdout: true,
  76. stdoutError: errors.New("stdout error"),
  77. expectError: true,
  78. },
  79. {
  80. name: "stderr error",
  81. stderr: true,
  82. stderrError: errors.New("stderr error"),
  83. expectError: true,
  84. },
  85. {
  86. name: "error stream error",
  87. stdin: true,
  88. stdout: true,
  89. stderr: true,
  90. errorError: errors.New("error stream error"),
  91. expectError: true,
  92. },
  93. {
  94. name: "no errors",
  95. stdin: true,
  96. stdout: true,
  97. stderr: true,
  98. expectError: false,
  99. },
  100. {
  101. name: "no errors, stderr & tty set, don't expect stderr",
  102. stdin: true,
  103. stdout: true,
  104. stderr: true,
  105. tty: true,
  106. expectError: false,
  107. },
  108. }
  109. for _, test := range tests {
  110. conn := &fakeStreamCreator{
  111. created: make(map[string]bool),
  112. errors: map[string]error{
  113. v1.StreamTypeStdin: test.stdinError,
  114. v1.StreamTypeStdout: test.stdoutError,
  115. v1.StreamTypeStderr: test.stderrError,
  116. v1.StreamTypeError: test.errorError,
  117. },
  118. }
  119. opts := StreamOptions{Tty: test.tty}
  120. if test.stdin {
  121. opts.Stdin = &fakeReader{}
  122. }
  123. if test.stdout {
  124. opts.Stdout = &fakeWriter{}
  125. }
  126. if test.stderr {
  127. opts.Stderr = &fakeWriter{}
  128. }
  129. h := newStreamProtocolV2(opts).(*streamProtocolV2)
  130. err := h.createStreams(conn)
  131. if test.expectError {
  132. if err == nil {
  133. t.Errorf("%s: expected error", test.name)
  134. continue
  135. }
  136. if e, a := test.stdinError, err; test.stdinError != nil && e != a {
  137. t.Errorf("%s: expected %v, got %v", test.name, e, a)
  138. }
  139. if e, a := test.stdoutError, err; test.stdoutError != nil && e != a {
  140. t.Errorf("%s: expected %v, got %v", test.name, e, a)
  141. }
  142. if e, a := test.stderrError, err; test.stderrError != nil && e != a {
  143. t.Errorf("%s: expected %v, got %v", test.name, e, a)
  144. }
  145. if e, a := test.errorError, err; test.errorError != nil && e != a {
  146. t.Errorf("%s: expected %v, got %v", test.name, e, a)
  147. }
  148. continue
  149. }
  150. if !test.expectError && err != nil {
  151. t.Errorf("%s: unexpected error: %v", test.name, err)
  152. continue
  153. }
  154. if test.stdin && !conn.created[v1.StreamTypeStdin] {
  155. t.Errorf("%s: expected stdin stream", test.name)
  156. }
  157. if test.stdout && !conn.created[v1.StreamTypeStdout] {
  158. t.Errorf("%s: expected stdout stream", test.name)
  159. }
  160. if test.stderr {
  161. if test.tty && conn.created[v1.StreamTypeStderr] {
  162. t.Errorf("%s: unexpected stderr stream because tty is set", test.name)
  163. } else if !test.tty && !conn.created[v1.StreamTypeStderr] {
  164. t.Errorf("%s: expected stderr stream", test.name)
  165. }
  166. }
  167. if !conn.created[v1.StreamTypeError] {
  168. t.Errorf("%s: expected error stream", test.name)
  169. }
  170. }
  171. }
  172. func TestV2ErrorStreamReading(t *testing.T) {
  173. tests := []struct {
  174. name string
  175. stream io.Reader
  176. expectedError error
  177. }{
  178. {
  179. name: "error reading from stream",
  180. stream: &fakeReader{errors.New("foo")},
  181. expectedError: errors.New("error reading from error stream: foo"),
  182. },
  183. {
  184. name: "stream returns an error",
  185. stream: strings.NewReader("some error"),
  186. expectedError: errors.New("error executing remote command: some error"),
  187. },
  188. }
  189. for _, test := range tests {
  190. h := newStreamProtocolV2(StreamOptions{}).(*streamProtocolV2)
  191. h.errorStream = test.stream
  192. ch := watchErrorStream(h.errorStream, &errorDecoderV2{})
  193. if ch == nil {
  194. t.Fatalf("%s: unexpected nil channel", test.name)
  195. }
  196. var err error
  197. select {
  198. case err = <-ch:
  199. case <-time.After(wait.ForeverTestTimeout):
  200. t.Fatalf("%s: timed out", test.name)
  201. }
  202. if test.expectedError != nil {
  203. if err == nil {
  204. t.Errorf("%s: expected an error", test.name)
  205. } else if e, a := test.expectedError, err; e.Error() != a.Error() {
  206. t.Errorf("%s: expected %q, got %q", test.name, e, a)
  207. }
  208. continue
  209. }
  210. if test.expectedError == nil && err != nil {
  211. t.Errorf("%s: unexpected error: %v", test.name, err)
  212. continue
  213. }
  214. }
  215. }