k8s.io/client-go@v0.31.1/tools/remotecommand/v4_test.go (about) 1 /* 2 Copyright 2016 The Kubernetes Authors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package remotecommand 18 19 import ( 20 "fmt" 21 "strings" 22 "testing" 23 ) 24 25 func TestV4ErrorDecoder(t *testing.T) { 26 dec := errorDecoderV4{} 27 28 type Test struct { 29 message string 30 err string 31 } 32 33 for _, test := range []Test{ 34 { 35 message: "{}", 36 err: "error stream protocol error: unknown error", 37 }, 38 { 39 message: "{", 40 err: "unexpected end of JSON input in \"{\"", 41 }, 42 { 43 message: `{"status": "Success" }`, 44 err: "", 45 }, 46 { 47 message: `{"status": "Failure", "message": "foobar" }`, 48 err: "foobar", 49 }, 50 { 51 message: `{"status": "Failure", "message": "foobar", "reason": "NonZeroExitCode", "details": {"causes": [{"reason": "foo"}] } }`, 52 err: "error stream protocol error: no ExitCode cause given", 53 }, 54 { 55 message: `{"status": "Failure", "message": "foobar", "reason": "NonZeroExitCode", "details": {"causes": [{"reason": "ExitCode"}] } }`, 56 err: "error stream protocol error: invalid exit code value \"\"", 57 }, 58 { 59 message: `{"status": "Failure", "message": "foobar", "reason": "NonZeroExitCode", "details": {"causes": [{"reason": "ExitCode", "message": "42"}] } }`, 60 err: "command terminated with exit code 42", 61 }, 62 } { 63 err := dec.decode([]byte(test.message)) 64 want := test.err 65 if want == "" { 66 want = "<nil>" 67 } 68 if got := fmt.Sprintf("%v", err); !strings.Contains(got, want) { 69 t.Errorf("wrong error for message %q: want=%q, got=%q", test.message, want, got) 70 } 71 } 72 }