mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-25 04:33:26 +00:00
Merge pull request #10837 from ncdc/fix-9119
Deflake TestRequestExecuteRemoteCommand
This commit is contained in:
commit
f51c27e6f3
@ -18,271 +18,175 @@ package remotecommand
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"errors"
|
"fmt"
|
||||||
"io"
|
"io/ioutil"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"net/http/httptest"
|
||||||
|
"net/url"
|
||||||
"strings"
|
"strings"
|
||||||
"sync"
|
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
|
||||||
|
|
||||||
"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
|
||||||
"github.com/GoogleCloudPlatform/kubernetes/pkg/client"
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/client"
|
||||||
"github.com/GoogleCloudPlatform/kubernetes/pkg/util/httpstream"
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/util/httpstream"
|
||||||
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/util/httpstream/spdy"
|
||||||
)
|
)
|
||||||
|
|
||||||
type fakeUpgrader struct {
|
func fakeExecServer(t *testing.T, i int, stdinData, stdoutData, stderrData, errorData string, tty bool) http.HandlerFunc {
|
||||||
conn *fakeUpgradeConnection
|
// error + stdin + stdout
|
||||||
err error
|
expectedStreams := 3
|
||||||
}
|
if !tty {
|
||||||
|
// stderr
|
||||||
func (u *fakeUpgrader) upgrade(req *client.Request, config *client.Config) (httpstream.Connection, error) {
|
expectedStreams++
|
||||||
return u.conn, u.err
|
|
||||||
}
|
|
||||||
|
|
||||||
type fakeUpgradeConnection struct {
|
|
||||||
closeCalled bool
|
|
||||||
lock sync.Mutex
|
|
||||||
|
|
||||||
stdin *fakeUpgradeStream
|
|
||||||
stdout *fakeUpgradeStream
|
|
||||||
stdoutData string
|
|
||||||
stderr *fakeUpgradeStream
|
|
||||||
stderrData string
|
|
||||||
errorStream *fakeUpgradeStream
|
|
||||||
errorData string
|
|
||||||
unexpectedStreamCreated bool
|
|
||||||
}
|
|
||||||
|
|
||||||
func newFakeUpgradeConnection() *fakeUpgradeConnection {
|
|
||||||
return &fakeUpgradeConnection{}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c *fakeUpgradeConnection) CreateStream(headers http.Header) (httpstream.Stream, error) {
|
|
||||||
c.lock.Lock()
|
|
||||||
defer c.lock.Unlock()
|
|
||||||
|
|
||||||
stream := &fakeUpgradeStream{}
|
|
||||||
switch headers.Get(api.StreamType) {
|
|
||||||
case api.StreamTypeStdin:
|
|
||||||
c.stdin = stream
|
|
||||||
case api.StreamTypeStdout:
|
|
||||||
c.stdout = stream
|
|
||||||
stream.data = c.stdoutData
|
|
||||||
case api.StreamTypeStderr:
|
|
||||||
c.stderr = stream
|
|
||||||
stream.data = c.stderrData
|
|
||||||
case api.StreamTypeError:
|
|
||||||
c.errorStream = stream
|
|
||||||
stream.data = c.errorData
|
|
||||||
default:
|
|
||||||
c.unexpectedStreamCreated = true
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return stream, nil
|
return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
|
||||||
}
|
streamCh := make(chan httpstream.Stream)
|
||||||
|
|
||||||
func (c *fakeUpgradeConnection) Close() error {
|
upgrader := spdy.NewResponseUpgrader()
|
||||||
c.lock.Lock()
|
conn := upgrader.UpgradeResponse(w, req, func(stream httpstream.Stream) error {
|
||||||
defer c.lock.Unlock()
|
streamCh <- stream
|
||||||
|
return nil
|
||||||
|
})
|
||||||
|
// from this point on, we can no longer call methods on w
|
||||||
|
if conn == nil {
|
||||||
|
// The upgrader is responsible for notifying the client of any errors that
|
||||||
|
// occurred during upgrading. All we can do is return here at this point
|
||||||
|
// if we weren't successful in upgrading.
|
||||||
|
return
|
||||||
|
}
|
||||||
|
defer conn.Close()
|
||||||
|
|
||||||
c.closeCalled = true
|
var errorStream, stdinStream, stdoutStream, stderrStream httpstream.Stream
|
||||||
return nil
|
receivedStreams := 0
|
||||||
}
|
WaitForStreams:
|
||||||
|
for {
|
||||||
|
select {
|
||||||
|
case stream := <-streamCh:
|
||||||
|
streamType := stream.Headers().Get(api.StreamType)
|
||||||
|
switch streamType {
|
||||||
|
case api.StreamTypeError:
|
||||||
|
errorStream = stream
|
||||||
|
receivedStreams++
|
||||||
|
case api.StreamTypeStdin:
|
||||||
|
stdinStream = stream
|
||||||
|
stdinStream.Close()
|
||||||
|
receivedStreams++
|
||||||
|
case api.StreamTypeStdout:
|
||||||
|
stdoutStream = stream
|
||||||
|
receivedStreams++
|
||||||
|
case api.StreamTypeStderr:
|
||||||
|
stderrStream = stream
|
||||||
|
receivedStreams++
|
||||||
|
default:
|
||||||
|
t.Errorf("%d: unexpected stream type: %q", i, streamType)
|
||||||
|
}
|
||||||
|
|
||||||
func (c *fakeUpgradeConnection) CloseChan() <-chan bool {
|
defer stream.Reset()
|
||||||
return make(chan bool)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c *fakeUpgradeConnection) SetIdleTimeout(timeout time.Duration) {
|
if receivedStreams == expectedStreams {
|
||||||
}
|
break WaitForStreams
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
type fakeUpgradeStream struct {
|
if len(errorData) > 0 {
|
||||||
readCalled bool
|
fmt.Fprint(errorStream, errorData)
|
||||||
writeCalled bool
|
errorStream.Close()
|
||||||
dataWritten []byte
|
}
|
||||||
closeCalled bool
|
|
||||||
resetCalled bool
|
|
||||||
data string
|
|
||||||
lock sync.Mutex
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s *fakeUpgradeStream) Read(p []byte) (int, error) {
|
if len(stdoutData) > 0 {
|
||||||
s.lock.Lock()
|
fmt.Fprint(stdoutStream, stdoutData)
|
||||||
defer s.lock.Unlock()
|
stdoutStream.Close()
|
||||||
s.readCalled = true
|
}
|
||||||
b := []byte(s.data)
|
if len(stderrData) > 0 {
|
||||||
n := copy(p, b)
|
fmt.Fprint(stderrStream, stderrData)
|
||||||
return n, io.EOF
|
stderrStream.Close()
|
||||||
}
|
}
|
||||||
|
if len(stdinData) > 0 {
|
||||||
func (s *fakeUpgradeStream) Write(p []byte) (int, error) {
|
data, err := ioutil.ReadAll(stdinStream)
|
||||||
s.lock.Lock()
|
if err != nil {
|
||||||
defer s.lock.Unlock()
|
t.Errorf("%d: error reading stdin stream: %v", i, err)
|
||||||
s.writeCalled = true
|
}
|
||||||
s.dataWritten = make([]byte, len(p))
|
if e, a := stdinData, string(data); e != a {
|
||||||
copy(s.dataWritten, p)
|
t.Errorf("%d: stdin: expected %q, got %q", i, e, a)
|
||||||
return len(p), io.EOF
|
}
|
||||||
}
|
}
|
||||||
|
})
|
||||||
func (s *fakeUpgradeStream) Close() error {
|
|
||||||
s.lock.Lock()
|
|
||||||
defer s.lock.Unlock()
|
|
||||||
s.closeCalled = true
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s *fakeUpgradeStream) Reset() error {
|
|
||||||
s.lock.Lock()
|
|
||||||
defer s.lock.Unlock()
|
|
||||||
s.resetCalled = true
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s *fakeUpgradeStream) Headers() http.Header {
|
|
||||||
s.lock.Lock()
|
|
||||||
defer s.lock.Unlock()
|
|
||||||
return http.Header{}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestRequestExecuteRemoteCommand(t *testing.T) {
|
func TestRequestExecuteRemoteCommand(t *testing.T) {
|
||||||
testCases := []struct {
|
testCases := []struct {
|
||||||
Upgrader *fakeUpgrader
|
Stdin string
|
||||||
Stdin string
|
Stdout string
|
||||||
Stdout string
|
Stderr string
|
||||||
Stderr string
|
Error string
|
||||||
Error string
|
Tty bool
|
||||||
Tty bool
|
|
||||||
ShouldError bool
|
|
||||||
}{
|
}{
|
||||||
{
|
{
|
||||||
Upgrader: &fakeUpgrader{err: errors.New("bail")},
|
Error: "bail",
|
||||||
ShouldError: true,
|
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
Upgrader: &fakeUpgrader{conn: newFakeUpgradeConnection()},
|
Stdin: "a",
|
||||||
Stdin: "a",
|
Stdout: "b",
|
||||||
Stdout: "b",
|
Stderr: "c",
|
||||||
Stderr: "c",
|
|
||||||
Error: "bail",
|
|
||||||
ShouldError: true,
|
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
Upgrader: &fakeUpgrader{conn: newFakeUpgradeConnection()},
|
Stdin: "a",
|
||||||
Stdin: "a",
|
Stdout: "b",
|
||||||
Stdout: "b",
|
Tty: true,
|
||||||
Stderr: "c",
|
|
||||||
},
|
|
||||||
{
|
|
||||||
Upgrader: &fakeUpgrader{conn: newFakeUpgradeConnection()},
|
|
||||||
Stdin: "a",
|
|
||||||
Stdout: "b",
|
|
||||||
Stderr: "c",
|
|
||||||
Tty: true,
|
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
for i, testCase := range testCases {
|
for i, testCase := range testCases {
|
||||||
if testCase.Error != "" {
|
localOut := &bytes.Buffer{}
|
||||||
testCase.Upgrader.conn.errorData = testCase.Error
|
localErr := &bytes.Buffer{}
|
||||||
|
|
||||||
|
server := httptest.NewServer(fakeExecServer(t, i, testCase.Stdin, testCase.Stdout, testCase.Stderr, testCase.Error, testCase.Tty))
|
||||||
|
|
||||||
|
url, _ := url.ParseRequestURI(server.URL)
|
||||||
|
c := client.NewRESTClient(url, "x", nil, -1, -1)
|
||||||
|
req := c.Post().Resource("testing")
|
||||||
|
|
||||||
|
conf := &client.Config{
|
||||||
|
Host: server.URL,
|
||||||
}
|
}
|
||||||
if testCase.Stdout != "" {
|
e := New(req, conf, []string{"ls", "/"}, strings.NewReader(testCase.Stdin), localOut, localErr, testCase.Tty)
|
||||||
testCase.Upgrader.conn.stdoutData = testCase.Stdout
|
//e.upgrader = testCase.Upgrader
|
||||||
}
|
|
||||||
if testCase.Stderr != "" {
|
|
||||||
testCase.Upgrader.conn.stderrData = testCase.Stderr
|
|
||||||
}
|
|
||||||
var localOut, localErr *bytes.Buffer
|
|
||||||
if testCase.Stdout != "" {
|
|
||||||
localOut = &bytes.Buffer{}
|
|
||||||
}
|
|
||||||
if testCase.Stderr != "" {
|
|
||||||
localErr = &bytes.Buffer{}
|
|
||||||
}
|
|
||||||
e := New(&client.Request{}, &client.Config{}, []string{"ls", "/"}, strings.NewReader(testCase.Stdin), localOut, localErr, testCase.Tty)
|
|
||||||
e.upgrader = testCase.Upgrader
|
|
||||||
err := e.Execute()
|
err := e.Execute()
|
||||||
hasErr := err != nil
|
hasErr := err != nil
|
||||||
if hasErr != testCase.ShouldError {
|
|
||||||
t.Fatalf("%d: expected %t, got %t: %v", i, testCase.ShouldError, hasErr, err)
|
|
||||||
}
|
|
||||||
|
|
||||||
conn := testCase.Upgrader.conn
|
if len(testCase.Error) > 0 {
|
||||||
if testCase.Error != "" {
|
if !hasErr {
|
||||||
if conn.errorStream == nil {
|
t.Errorf("%d: expected an error", i)
|
||||||
t.Fatalf("%d: expected error stream creation", i)
|
} else {
|
||||||
|
if e, a := testCase.Error, err.Error(); !strings.Contains(a, e) {
|
||||||
|
t.Errorf("%d: expected error stream read '%v', got '%v'", i, e, a)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if !conn.errorStream.readCalled {
|
|
||||||
t.Fatalf("%d: expected error stream read", i)
|
|
||||||
}
|
|
||||||
if e, a := testCase.Error, err.Error(); !strings.Contains(a, e) {
|
|
||||||
t.Fatalf("%d: expected error stream read '%v', got '%v'", i, e, a)
|
|
||||||
}
|
|
||||||
if !conn.errorStream.resetCalled {
|
|
||||||
t.Fatalf("%d: expected error reset", i)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if testCase.ShouldError {
|
server.Close()
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
if testCase.Stdin != "" {
|
if hasErr {
|
||||||
if conn.stdin == nil {
|
t.Errorf("%d: unexpected error: %v", i, err)
|
||||||
t.Fatalf("%d: expected stdin stream creation", i)
|
server.Close()
|
||||||
}
|
continue
|
||||||
if !conn.stdin.writeCalled {
|
|
||||||
t.Fatalf("%d: expected stdin stream write", i)
|
|
||||||
}
|
|
||||||
if e, a := testCase.Stdin, string(conn.stdin.dataWritten); e != a {
|
|
||||||
t.Fatalf("%d: expected stdin write %v, got %v", i, e, a)
|
|
||||||
}
|
|
||||||
if !conn.stdin.resetCalled {
|
|
||||||
t.Fatalf("%d: expected stdin reset", i)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if testCase.Stdout != "" {
|
if len(testCase.Stdout) > 0 {
|
||||||
if conn.stdout == nil {
|
|
||||||
t.Fatalf("%d: expected stdout stream creation", i)
|
|
||||||
}
|
|
||||||
if !conn.stdout.readCalled {
|
|
||||||
t.Fatalf("%d: expected stdout stream read", i)
|
|
||||||
}
|
|
||||||
if e, a := testCase.Stdout, localOut; e != a.String() {
|
if e, a := testCase.Stdout, localOut; e != a.String() {
|
||||||
t.Fatalf("%d: expected stdout data '%s', got '%s'", i, e, a)
|
t.Errorf("%d: expected stdout data '%s', got '%s'", i, e, a)
|
||||||
}
|
|
||||||
if !conn.stdout.resetCalled {
|
|
||||||
t.Fatalf("%d: expected stdout reset", i)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if testCase.Stderr != "" {
|
if testCase.Stderr != "" {
|
||||||
if testCase.Tty {
|
if e, a := testCase.Stderr, localErr; e != a.String() {
|
||||||
if conn.stderr != nil {
|
t.Errorf("%d: expected stderr data '%s', got '%s'", i, e, a)
|
||||||
t.Fatalf("%d: unexpected stderr stream creation", i)
|
|
||||||
}
|
|
||||||
if localErr.String() != "" {
|
|
||||||
t.Fatalf("%d: unexpected stderr data '%s'", i, localErr)
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
if conn.stderr == nil {
|
|
||||||
t.Fatalf("%d: expected stderr stream creation", i)
|
|
||||||
}
|
|
||||||
if !conn.stderr.readCalled {
|
|
||||||
t.Fatalf("%d: expected stderr stream read", i)
|
|
||||||
}
|
|
||||||
if e, a := testCase.Stderr, localErr; e != a.String() {
|
|
||||||
t.Fatalf("%d: expected stderr data '%s', got '%s'", i, e, a)
|
|
||||||
}
|
|
||||||
if !conn.stderr.resetCalled {
|
|
||||||
t.Fatalf("%d: expected stderr reset", i)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if !conn.closeCalled {
|
server.Close()
|
||||||
t.Fatalf("%d: expected upgraded connection to get closed", i)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user