2018-02-05 21:05:59 +00:00
|
|
|
package client // import "github.com/docker/docker/client"
|
2016-09-06 18:46:37 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2018-04-19 22:30:59 +00:00
|
|
|
"context"
|
2016-09-06 18:46:37 +00:00
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
2021-08-24 10:10:50 +00:00
|
|
|
"io"
|
2016-09-06 18:46:37 +00:00
|
|
|
"net/http"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/docker/docker/api/types"
|
2018-12-31 17:22:43 +00:00
|
|
|
"github.com/docker/docker/errdefs"
|
2023-05-10 11:17:40 +00:00
|
|
|
"gotest.tools/v3/assert"
|
|
|
|
is "gotest.tools/v3/assert/cmp"
|
2016-09-06 18:46:37 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestContainerExecCreateError(t *testing.T) {
|
|
|
|
client := &Client{
|
2016-09-09 03:44:25 +00:00
|
|
|
client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")),
|
2016-09-06 18:46:37 +00:00
|
|
|
}
|
|
|
|
_, err := client.ContainerExecCreate(context.Background(), "container_id", types.ExecConfig{})
|
2023-05-10 11:17:40 +00:00
|
|
|
assert.Check(t, is.ErrorType(err, errdefs.IsSystem))
|
2016-09-06 18:46:37 +00:00
|
|
|
}
|
|
|
|
|
2024-02-23 11:20:06 +00:00
|
|
|
// TestContainerExecCreateConnectionError verifies that connection errors occurring
|
|
|
|
// during API-version negotiation are not shadowed by API-version errors.
|
|
|
|
//
|
|
|
|
// Regression test for https://github.com/docker/cli/issues/4890
|
|
|
|
func TestContainerExecCreateConnectionError(t *testing.T) {
|
|
|
|
client, err := NewClientWithOpts(WithAPIVersionNegotiation(), WithHost("tcp://no-such-host.invalid"))
|
|
|
|
assert.NilError(t, err)
|
|
|
|
|
|
|
|
_, err = client.ContainerExecCreate(context.Background(), "", types.ExecConfig{})
|
|
|
|
assert.Check(t, is.ErrorType(err, IsErrConnectionFailed))
|
|
|
|
}
|
|
|
|
|
2016-09-06 18:46:37 +00:00
|
|
|
func TestContainerExecCreate(t *testing.T) {
|
|
|
|
expectedURL := "/containers/container_id/exec"
|
|
|
|
client := &Client{
|
2016-09-09 03:44:25 +00:00
|
|
|
client: newMockClient(func(req *http.Request) (*http.Response, error) {
|
2016-09-06 18:46:37 +00:00
|
|
|
if !strings.HasPrefix(req.URL.Path, expectedURL) {
|
|
|
|
return nil, fmt.Errorf("expected URL '%s', got '%s'", expectedURL, req.URL)
|
|
|
|
}
|
2019-10-12 18:41:14 +00:00
|
|
|
if req.Method != http.MethodPost {
|
2016-09-06 18:46:37 +00:00
|
|
|
return nil, fmt.Errorf("expected POST method, got %s", req.Method)
|
|
|
|
}
|
|
|
|
// FIXME validate the content is the given ExecConfig ?
|
|
|
|
if err := req.ParseForm(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
execConfig := &types.ExecConfig{}
|
|
|
|
if err := json.NewDecoder(req.Body).Decode(execConfig); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if execConfig.User != "user" {
|
|
|
|
return nil, fmt.Errorf("expected an execConfig with User == 'user', got %v", execConfig)
|
|
|
|
}
|
2016-10-18 22:56:45 +00:00
|
|
|
b, err := json.Marshal(types.IDResponse{
|
2016-09-06 18:46:37 +00:00
|
|
|
ID: "exec_id",
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &http.Response{
|
|
|
|
StatusCode: http.StatusOK,
|
2021-08-24 10:10:50 +00:00
|
|
|
Body: io.NopCloser(bytes.NewReader(b)),
|
2016-09-06 18:46:37 +00:00
|
|
|
}, nil
|
|
|
|
}),
|
|
|
|
}
|
|
|
|
|
|
|
|
r, err := client.ContainerExecCreate(context.Background(), "container_id", types.ExecConfig{
|
|
|
|
User: "user",
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if r.ID != "exec_id" {
|
|
|
|
t.Fatalf("expected `exec_id`, got %s", r.ID)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestContainerExecStartError(t *testing.T) {
|
|
|
|
client := &Client{
|
2016-09-09 03:44:25 +00:00
|
|
|
client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")),
|
2016-09-06 18:46:37 +00:00
|
|
|
}
|
|
|
|
err := client.ContainerExecStart(context.Background(), "nothing", types.ExecStartCheck{})
|
2023-05-10 11:17:40 +00:00
|
|
|
assert.Check(t, is.ErrorType(err, errdefs.IsSystem))
|
2016-09-06 18:46:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestContainerExecStart(t *testing.T) {
|
|
|
|
expectedURL := "/exec/exec_id/start"
|
|
|
|
client := &Client{
|
2016-09-09 03:44:25 +00:00
|
|
|
client: newMockClient(func(req *http.Request) (*http.Response, error) {
|
2016-09-06 18:46:37 +00:00
|
|
|
if !strings.HasPrefix(req.URL.Path, expectedURL) {
|
|
|
|
return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL)
|
|
|
|
}
|
|
|
|
if err := req.ParseForm(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
execStartCheck := &types.ExecStartCheck{}
|
|
|
|
if err := json.NewDecoder(req.Body).Decode(execStartCheck); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if execStartCheck.Tty || !execStartCheck.Detach {
|
|
|
|
return nil, fmt.Errorf("expected execStartCheck{Detach:true,Tty:false}, got %v", execStartCheck)
|
|
|
|
}
|
|
|
|
|
|
|
|
return &http.Response{
|
|
|
|
StatusCode: http.StatusOK,
|
2021-08-24 10:10:50 +00:00
|
|
|
Body: io.NopCloser(bytes.NewReader([]byte(""))),
|
2016-09-06 18:46:37 +00:00
|
|
|
}, nil
|
|
|
|
}),
|
|
|
|
}
|
|
|
|
|
|
|
|
err := client.ContainerExecStart(context.Background(), "exec_id", types.ExecStartCheck{
|
|
|
|
Detach: true,
|
|
|
|
Tty: false,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestContainerExecInspectError(t *testing.T) {
|
|
|
|
client := &Client{
|
2016-09-09 03:44:25 +00:00
|
|
|
client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")),
|
2016-09-06 18:46:37 +00:00
|
|
|
}
|
|
|
|
_, err := client.ContainerExecInspect(context.Background(), "nothing")
|
2023-05-10 11:17:40 +00:00
|
|
|
assert.Check(t, is.ErrorType(err, errdefs.IsSystem))
|
2016-09-06 18:46:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestContainerExecInspect(t *testing.T) {
|
|
|
|
expectedURL := "/exec/exec_id/json"
|
|
|
|
client := &Client{
|
2016-09-09 03:44:25 +00:00
|
|
|
client: newMockClient(func(req *http.Request) (*http.Response, error) {
|
2016-09-06 18:46:37 +00:00
|
|
|
if !strings.HasPrefix(req.URL.Path, expectedURL) {
|
|
|
|
return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL)
|
|
|
|
}
|
|
|
|
b, err := json.Marshal(types.ContainerExecInspect{
|
|
|
|
ExecID: "exec_id",
|
|
|
|
ContainerID: "container_id",
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &http.Response{
|
|
|
|
StatusCode: http.StatusOK,
|
2021-08-24 10:10:50 +00:00
|
|
|
Body: io.NopCloser(bytes.NewReader(b)),
|
2016-09-06 18:46:37 +00:00
|
|
|
}, nil
|
|
|
|
}),
|
|
|
|
}
|
|
|
|
|
|
|
|
inspect, err := client.ContainerExecInspect(context.Background(), "exec_id")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if inspect.ExecID != "exec_id" {
|
|
|
|
t.Fatalf("expected ExecID to be `exec_id`, got %s", inspect.ExecID)
|
|
|
|
}
|
|
|
|
if inspect.ContainerID != "container_id" {
|
|
|
|
t.Fatalf("expected ContainerID `container_id`, got %s", inspect.ContainerID)
|
|
|
|
}
|
|
|
|
}
|