/
fake_executor.go
54 lines (47 loc) · 1.66 KB
/
fake_executor.go
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
// SPDX-License-Identifier: GPL-2.0-or-later
package testutils
import (
"context"
"fmt"
"net/url"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/remotecommand"
)
// Return a SPDYExectuor with stdout, stderr and an error embedded
func NewFakeNewSPDYExecutor(
responder func(method string, url *url.URL, options remotecommand.StreamOptions) ([]byte, []byte, error),
execCreationErr error,
) func(config *rest.Config, method string, url *url.URL) (remotecommand.Executor, error) {
return func(config *rest.Config, method string, url *url.URL) (remotecommand.Executor, error) {
return &fakeExecutor{method: method, url: url, responder: responder}, execCreationErr
}
}
type fakeExecutor struct {
url *url.URL
responder func(method string, url *url.URL, options remotecommand.StreamOptions) ([]byte, []byte, error)
method string
}
func (f *fakeExecutor) Stream(options remotecommand.StreamOptions) error {
stdout, stderr, responseErr := f.responder(f.method, f.url, options)
_, err := options.Stdout.Write(stdout)
if err != nil {
return fmt.Errorf("failed to write stdout Error: %w", err)
}
_, err = options.Stderr.Write(stderr)
if err != nil {
return fmt.Errorf("failed to write stderr Error: %w", err)
}
return responseErr
}
func (f *fakeExecutor) StreamWithContext(ctx context.Context, options remotecommand.StreamOptions) error {
stdout, stderr, reponseErr := f.responder(f.method, f.url, options)
_, err := options.Stdout.Write(stdout)
if err != nil {
return fmt.Errorf("failed to write stdout Error: %w", err)
}
_, err = options.Stderr.Write(stderr)
if err != nil {
return fmt.Errorf("failed to write stderr Error: %w", err)
}
return reponseErr
}