v4_test.go 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. /*
  2. Copyright 2016 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 remotecommand
  14. import (
  15. "fmt"
  16. "testing"
  17. )
  18. func TestV4ErrorDecoder(t *testing.T) {
  19. dec := errorDecoderV4{}
  20. type Test struct {
  21. message string
  22. err string
  23. }
  24. for _, test := range []Test{
  25. {
  26. message: "{}",
  27. err: "error stream protocol error: unknown error",
  28. },
  29. {
  30. message: "{",
  31. err: "error stream protocol error: unexpected end of JSON input in \"{\"",
  32. },
  33. {
  34. message: `{"status": "Success" }`,
  35. err: "",
  36. },
  37. {
  38. message: `{"status": "Failure", "message": "foobar" }`,
  39. err: "foobar",
  40. },
  41. {
  42. message: `{"status": "Failure", "message": "foobar", "reason": "NonZeroExitCode", "details": {"causes": [{"reason": "foo"}] } }`,
  43. err: "error stream protocol error: no ExitCode cause given",
  44. },
  45. {
  46. message: `{"status": "Failure", "message": "foobar", "reason": "NonZeroExitCode", "details": {"causes": [{"reason": "ExitCode"}] } }`,
  47. err: "error stream protocol error: invalid exit code value \"\"",
  48. },
  49. {
  50. message: `{"status": "Failure", "message": "foobar", "reason": "NonZeroExitCode", "details": {"causes": [{"reason": "ExitCode", "message": "42"}] } }`,
  51. err: "command terminated with exit code 42",
  52. },
  53. } {
  54. err := dec.decode([]byte(test.message))
  55. want := test.err
  56. if want == "" {
  57. want = "<nil>"
  58. }
  59. if got := fmt.Sprintf("%v", err); got != want {
  60. t.Errorf("wrong error for message %q: want=%q, got=%q", test.message, want, got)
  61. }
  62. }
  63. }