moby/client/container_exec_test.go
Stephen J Day 9a072adff3
client: remove transport package
This package doesn't really seem to do anything of real interest.
Removing it and replacing with a few helper functions. Most of this was
maintaining a fork of ctxhttp to support a mock that was unnecessary.

We could probably do with a further refactor of the client interface.
There is a lot of confusion of between transport, http layer and
application layer that makes for some awkward code. This change
improves the situation to the point where no breaking changes are
introduced.

Signed-off-by: Stephen J Day <stephen.day@docker.com>
2016-09-19 11:19:55 -07:00

157 lines
4.5 KiB
Go

package client
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"strings"
"testing"
"golang.org/x/net/context"
"github.com/docker/docker/api/types"
)
func TestContainerExecCreateError(t *testing.T) {
client := &Client{
client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")),
}
_, err := client.ContainerExecCreate(context.Background(), "container_id", types.ExecConfig{})
if err == nil || err.Error() != "Error response from daemon: Server error" {
t.Fatalf("expected a Server Error, got %v", err)
}
}
func TestContainerExecCreate(t *testing.T) {
expectedURL := "/containers/container_id/exec"
client := &Client{
client: newMockClient(func(req *http.Request) (*http.Response, error) {
if !strings.HasPrefix(req.URL.Path, expectedURL) {
return nil, fmt.Errorf("expected URL '%s', got '%s'", expectedURL, req.URL)
}
if req.Method != "POST" {
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)
}
b, err := json.Marshal(types.ContainerExecCreateResponse{
ID: "exec_id",
})
if err != nil {
return nil, err
}
return &http.Response{
StatusCode: http.StatusOK,
Body: ioutil.NopCloser(bytes.NewReader(b)),
}, 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{
client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")),
}
err := client.ContainerExecStart(context.Background(), "nothing", types.ExecStartCheck{})
if err == nil || err.Error() != "Error response from daemon: Server error" {
t.Fatalf("expected a Server Error, got %v", err)
}
}
func TestContainerExecStart(t *testing.T) {
expectedURL := "/exec/exec_id/start"
client := &Client{
client: newMockClient(func(req *http.Request) (*http.Response, error) {
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,
Body: ioutil.NopCloser(bytes.NewReader([]byte(""))),
}, 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{
client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")),
}
_, err := client.ContainerExecInspect(context.Background(), "nothing")
if err == nil || err.Error() != "Error response from daemon: Server error" {
t.Fatalf("expected a Server Error, got %v", err)
}
}
func TestContainerExecInspect(t *testing.T) {
expectedURL := "/exec/exec_id/json"
client := &Client{
client: newMockClient(func(req *http.Request) (*http.Response, error) {
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,
Body: ioutil.NopCloser(bytes.NewReader(b)),
}, 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)
}
}