wangpengfei
2023-06-02 064c0874e5fd041c4641ef873d1bf72ac98a184d
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
/*
Copyright 2016 The Kubernetes Authors.
 
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
 
    http://www.apache.org/licenses/LICENSE-2.0
 
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
 
package remotecommand
 
import (
    "errors"
    "io"
    "net/http"
    "strings"
    "testing"
    "time"
 
    "k8s.io/api/core/v1"
    "k8s.io/apimachinery/pkg/util/httpstream"
    "k8s.io/apimachinery/pkg/util/wait"
)
 
type fakeReader struct {
    err error
}
 
func (r *fakeReader) Read([]byte) (int, error) { return 0, r.err }
 
type fakeWriter struct{}
 
func (*fakeWriter) Write([]byte) (int, error) { return 0, nil }
 
type fakeStreamCreator struct {
    created map[string]bool
    errors  map[string]error
}
 
var _ streamCreator = &fakeStreamCreator{}
 
func (f *fakeStreamCreator) CreateStream(headers http.Header) (httpstream.Stream, error) {
    streamType := headers.Get(v1.StreamType)
    f.created[streamType] = true
    return nil, f.errors[streamType]
}
 
func TestV2CreateStreams(t *testing.T) {
    tests := []struct {
        name        string
        stdin       bool
        stdinError  error
        stdout      bool
        stdoutError error
        stderr      bool
        stderrError error
        errorError  error
        tty         bool
        expectError bool
    }{
        {
            name:        "stdin error",
            stdin:       true,
            stdinError:  errors.New("stdin error"),
            expectError: true,
        },
        {
            name:        "stdout error",
            stdout:      true,
            stdoutError: errors.New("stdout error"),
            expectError: true,
        },
        {
            name:        "stderr error",
            stderr:      true,
            stderrError: errors.New("stderr error"),
            expectError: true,
        },
        {
            name:        "error stream error",
            stdin:       true,
            stdout:      true,
            stderr:      true,
            errorError:  errors.New("error stream error"),
            expectError: true,
        },
        {
            name:        "no errors",
            stdin:       true,
            stdout:      true,
            stderr:      true,
            expectError: false,
        },
        {
            name:        "no errors, stderr & tty set, don't expect stderr",
            stdin:       true,
            stdout:      true,
            stderr:      true,
            tty:         true,
            expectError: false,
        },
    }
    for _, test := range tests {
        conn := &fakeStreamCreator{
            created: make(map[string]bool),
            errors: map[string]error{
                v1.StreamTypeStdin:  test.stdinError,
                v1.StreamTypeStdout: test.stdoutError,
                v1.StreamTypeStderr: test.stderrError,
                v1.StreamTypeError:  test.errorError,
            },
        }
 
        opts := StreamOptions{Tty: test.tty}
        if test.stdin {
            opts.Stdin = &fakeReader{}
        }
        if test.stdout {
            opts.Stdout = &fakeWriter{}
        }
        if test.stderr {
            opts.Stderr = &fakeWriter{}
        }
 
        h := newStreamProtocolV2(opts).(*streamProtocolV2)
        err := h.createStreams(conn)
 
        if test.expectError {
            if err == nil {
                t.Errorf("%s: expected error", test.name)
                continue
            }
            if e, a := test.stdinError, err; test.stdinError != nil && e != a {
                t.Errorf("%s: expected %v, got %v", test.name, e, a)
            }
            if e, a := test.stdoutError, err; test.stdoutError != nil && e != a {
                t.Errorf("%s: expected %v, got %v", test.name, e, a)
            }
            if e, a := test.stderrError, err; test.stderrError != nil && e != a {
                t.Errorf("%s: expected %v, got %v", test.name, e, a)
            }
            if e, a := test.errorError, err; test.errorError != nil && e != a {
                t.Errorf("%s: expected %v, got %v", test.name, e, a)
            }
            continue
        }
 
        if !test.expectError && err != nil {
            t.Errorf("%s: unexpected error: %v", test.name, err)
            continue
        }
 
        if test.stdin && !conn.created[v1.StreamTypeStdin] {
            t.Errorf("%s: expected stdin stream", test.name)
        }
        if test.stdout && !conn.created[v1.StreamTypeStdout] {
            t.Errorf("%s: expected stdout stream", test.name)
        }
        if test.stderr {
            if test.tty && conn.created[v1.StreamTypeStderr] {
                t.Errorf("%s: unexpected stderr stream because tty is set", test.name)
            } else if !test.tty && !conn.created[v1.StreamTypeStderr] {
                t.Errorf("%s: expected stderr stream", test.name)
            }
        }
        if !conn.created[v1.StreamTypeError] {
            t.Errorf("%s: expected error stream", test.name)
        }
 
    }
}
 
func TestV2ErrorStreamReading(t *testing.T) {
    tests := []struct {
        name          string
        stream        io.Reader
        expectedError error
    }{
        {
            name:          "error reading from stream",
            stream:        &fakeReader{errors.New("foo")},
            expectedError: errors.New("error reading from error stream: foo"),
        },
        {
            name:          "stream returns an error",
            stream:        strings.NewReader("some error"),
            expectedError: errors.New("error executing remote command: some error"),
        },
    }
 
    for _, test := range tests {
        h := newStreamProtocolV2(StreamOptions{}).(*streamProtocolV2)
        h.errorStream = test.stream
 
        ch := watchErrorStream(h.errorStream, &errorDecoderV2{})
        if ch == nil {
            t.Fatalf("%s: unexpected nil channel", test.name)
        }
 
        var err error
        select {
        case err = <-ch:
        case <-time.After(wait.ForeverTestTimeout):
            t.Fatalf("%s: timed out", test.name)
        }
 
        if test.expectedError != nil {
            if err == nil {
                t.Errorf("%s: expected an error", test.name)
            } else if e, a := test.expectedError, err; e.Error() != a.Error() {
                t.Errorf("%s: expected %q, got %q", test.name, e, a)
            }
            continue
        }
 
        if test.expectedError == nil && err != nil {
            t.Errorf("%s: unexpected error: %v", test.name, err)
            continue
        }
    }
}