Debian Codegraph @

tools/remotecommand/v2_test.go

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