Skip to content
Snippets Groups Projects
Unverified Commit b3e98660 authored by Steve Azzopardi's avatar Steve Azzopardi
Browse files

Add error response code for terminalConn.Start

parent 565d5042
No related branches found
No related tags found
No related merge requests found
...@@ -86,12 +86,16 @@ func (t terminalConn) Start(w http.ResponseWriter, r *http.Request, timeoutCh, d ...@@ -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) exec, err := t.client.ContainerExecCreate(t.ctx, t.containerID, execConfig)
if err != nil { 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) resp, err := t.client.ContainerExecAttach(t.ctx, exec.ID, execConfig)
if err != nil { 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) dockerTTY := newDockerTTY(&resp)
...@@ -105,9 +109,9 @@ func (t terminalConn) Start(w http.ResponseWriter, r *http.Request, timeoutCh, d ...@@ -105,9 +109,9 @@ func (t terminalConn) Start(w http.ResponseWriter, r *http.Request, timeoutCh, d
stopCh := proxy.GetStopCh() stopCh := proxy.GetStopCh()
if err != nil { if err != nil {
stopCh <- fmt.Errorf("Build container exited with %q", err) stopCh <- fmt.Errorf("build container exited with %q", err)
} else { } else {
stopCh <- fmt.Errorf("Build container exited") stopCh <- errors.New("build container exited")
} }
}() }()
......
...@@ -4,6 +4,7 @@ import ( ...@@ -4,6 +4,7 @@ import (
"bufio" "bufio"
"bytes" "bytes"
"context" "context"
"errors"
"net" "net"
"net/http" "net/http"
"net/http/httptest" "net/http/httptest"
...@@ -167,6 +168,78 @@ func TestCommandExecutor_Connect(t *testing.T) { ...@@ -167,6 +168,78 @@ func TestCommandExecutor_Connect(t *testing.T) {
assert.IsType(t, terminalConn{}, conn) 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 { type nopReader struct {
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment