Add error response code for terminalConn.Start

parent 565d5042
......@@ -86,12 +86,16 @@ func (t terminalConn) Start(w http.ResponseWriter, r *http.Request, timeoutCh, d
exec, err := t.client.ContainerExecCreate(t.ctx, t.containerID, execConfig)
if err != nil {
t.logger.Errorln("failed to create exec container for terminal:", err)
t.logger.Errorln("Failed to create exec container for terminal:", err)
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
return
}
resp, err := t.client.ContainerExecAttach(t.ctx, exec.ID, execConfig)
if err != nil {
t.logger.Errorln("failed to exec attach to container for terminal:", err)
t.logger.Errorln("Failed to exec attach to container for terminal:", err)
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
return
}
dockerTTY := newDockerTTY(&resp)
......@@ -105,9 +109,9 @@ func (t terminalConn) Start(w http.ResponseWriter, r *http.Request, timeoutCh, d
stopCh := proxy.GetStopCh()
if err != nil {
stopCh <- fmt.Errorf("Build container exited with %q", err)
stopCh <- fmt.Errorf("build container exited with %q", err)
} else {
stopCh <- fmt.Errorf("Build container exited")
stopCh <- errors.New("build container exited")
}
}()
......
......@@ -4,6 +4,7 @@ import (
"bufio"
"bytes"
"context"
"errors"
"net"
"net/http"
"net/http/httptest"
......@@ -167,6 +168,78 @@ func TestCommandExecutor_Connect(t *testing.T) {
assert.IsType(t, terminalConn{}, conn)
}
func TestTerminalConn_FailToStart(t *testing.T) {
tests := []struct {
name string
containerExecCreateErr error
containerExecAttachErr error
}{
{
name: "Failed to create exec container",
containerExecCreateErr: errors.New("failed to create exec container"),
containerExecAttachErr: nil,
},
{
name: "Failed to attach exec container",
containerExecCreateErr: nil,
containerExecAttachErr: errors.New("failed to attach exec container"),
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
c := &docker_helpers.MockClient{}
s := commandExecutor{
executor: executor{
AbstractExecutor: executors.AbstractExecutor{
Context: context.Background(),
BuildShell: &common.ShellConfiguration{
DockerCommand: []string{"/bin/sh"},
},
},
client: c,
},
buildContainer: &types.ContainerJSON{
ContainerJSONBase: &types.ContainerJSONBase{
ID: "1234",
},
},
}
c.On("ContainerInspect", mock.Anything, mock.Anything).Return(types.ContainerJSON{
ContainerJSONBase: &types.ContainerJSONBase{
State: &types.ContainerState{
Running: true,
},
},
}, nil)
c.On("ContainerExecCreate", mock.Anything, mock.Anything, mock.Anything).Return(
types.IDResponse{},
test.containerExecCreateErr,
)
c.On("ContainerExecAttach", mock.Anything, mock.Anything, mock.Anything).Return(
types.HijackedResponse{},
test.containerExecAttachErr,
)
conn, err := s.Connect()
require.NoError(t, err)
timeoutCh := make(chan error)
disconnectCh := make(chan error)
w := httptest.NewRecorder()
req := httptest.NewRequest(http.MethodGet, "wss://example.com/foo", nil)
conn.Start(w, req, timeoutCh, disconnectCh)
resp := w.Result()
assert.Equal(t, http.StatusInternalServerError, resp.StatusCode)
})
}
}
type nopReader struct {
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment