From 84ea7363023351d4b948f3eba2a2c0e1ebebdda1 Mon Sep 17 00:00:00 2001 From: Dewey-Ding Date: Wed, 11 Apr 2018 21:12:59 +0800 Subject: [PATCH] test:add container create test Signed-off-by: Dewey-Ding --- client/container.go | 26 ------------- client/container_create.go | 34 ++++++++++++++++ client/container_create_test.go | 69 +++++++++++++++++++++++++++++++++ 3 files changed, 103 insertions(+), 26 deletions(-) create mode 100644 client/container_create.go create mode 100644 client/container_create_test.go diff --git a/client/container.go b/client/container.go index 4bfd50c9e..1bb700727 100644 --- a/client/container.go +++ b/client/container.go @@ -10,32 +10,6 @@ import ( "github.com/alibaba/pouch/apis/types" ) -// ContainerCreate creates a new container based in the given configuration. -func (client *APIClient) ContainerCreate(ctx context.Context, config types.ContainerConfig, hostConfig *types.HostConfig, networkingConfig *types.NetworkingConfig, containerName string) (*types.ContainerCreateResp, error) { - createConfig := types.ContainerCreateConfig{ - ContainerConfig: config, - HostConfig: hostConfig, - NetworkingConfig: networkingConfig, - } - - q := url.Values{} - if containerName != "" { - q.Set("name", containerName) - } - - resp, err := client.post(ctx, "/containers/create", q, createConfig, nil) - if err != nil { - return nil, err - } - - container := &types.ContainerCreateResp{} - - err = decodeBody(container, resp.Body) - ensureCloseReader(resp) - - return container, err -} - // ContainerAttach attach a container func (client *APIClient) ContainerAttach(ctx context.Context, name string, stdin bool) (net.Conn, *bufio.Reader, error) { q := url.Values{} diff --git a/client/container_create.go b/client/container_create.go new file mode 100644 index 000000000..eea8180e2 --- /dev/null +++ b/client/container_create.go @@ -0,0 +1,34 @@ +package client + +import ( + "context" + "net/url" + + "github.com/alibaba/pouch/apis/types" +) + +// ContainerCreate creates a new container based in the given configuration. +func (client *APIClient) ContainerCreate(ctx context.Context, config types.ContainerConfig, hostConfig *types.HostConfig, networkingConfig *types.NetworkingConfig, containerName string) (*types.ContainerCreateResp, error) { + createConfig := types.ContainerCreateConfig{ + ContainerConfig: config, + HostConfig: hostConfig, + NetworkingConfig: networkingConfig, + } + + q := url.Values{} + if containerName != "" { + q.Set("name", containerName) + } + + resp, err := client.post(ctx, "/containers/create", q, createConfig, nil) + if err != nil { + return nil, err + } + + container := &types.ContainerCreateResp{} + + err = decodeBody(container, resp.Body) + ensureCloseReader(resp) + + return container, err +} diff --git a/client/container_create_test.go b/client/container_create_test.go new file mode 100644 index 000000000..14bd60704 --- /dev/null +++ b/client/container_create_test.go @@ -0,0 +1,69 @@ +package client + +import ( + "bytes" + "context" + "encoding/json" + "fmt" + "io/ioutil" + "net/http" + "strings" + "testing" + + "github.com/alibaba/pouch/apis/types" + "github.com/stretchr/testify/assert" +) + +func TestContainerCreateError(t *testing.T) { + client := &APIClient{ + HTTPCli: newMockClient(errorMockResponse(http.StatusInternalServerError, "Server error")), + } + _, err := client.ContainerCreate(context.Background(), types.ContainerConfig{}, nil, nil, "nothing") + if err == nil || !strings.Contains(err.Error(), "Server error") { + t.Fatalf("expected a Server Error, got %v", err) + } +} + +func TestContainerCreate(t *testing.T) { + expectedURL := "/containers/create" + + httpClient := 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.Header.Get("Content-Type") == "application/json" { + createConfig := types.ContainerCreateConfig{} + if err := json.NewDecoder(req.Body).Decode(&createConfig); err != nil { + return nil, fmt.Errorf("failed to parse json: %v", err) + } + } + + name := req.URL.Query().Get("name") + if name != "container_name" { + return nil, fmt.Errorf("container name not set in URL query properly. Expected `container_name`, got %s", name) + } + containerCreateResp := types.ContainerCreateResp{ + ID: "container_id", + Name: "container_name", + } + b, err := json.Marshal(containerCreateResp) + if err != nil { + return nil, err + } + return &http.Response{ + StatusCode: http.StatusCreated, + Body: ioutil.NopCloser(bytes.NewReader([]byte(b))), + }, nil + }) + + client := &APIClient{ + HTTPCli: httpClient, + } + + container, err := client.ContainerCreate(context.Background(), types.ContainerConfig{}, &types.HostConfig{}, &types.NetworkingConfig{}, "container_name") + if err != nil { + t.Fatal(err) + } + assert.Equal(t, container.ID, "container_id") + assert.Equal(t, container.Name, "container_name") +}