Skip to content

Commit

Permalink
test:add container create test
Browse files Browse the repository at this point in the history
Signed-off-by: Dewey-Ding <[email protected]>
  • Loading branch information
Dewey-Ding committed Apr 12, 2018
1 parent 204c39c commit 84ea736
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 26 deletions.
26 changes: 0 additions & 26 deletions client/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}
Expand Down
34 changes: 34 additions & 0 deletions client/container_create.go
Original file line number Diff line number Diff line change
@@ -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
}
69 changes: 69 additions & 0 deletions client/container_create_test.go
Original file line number Diff line number Diff line change
@@ -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")
}

0 comments on commit 84ea736

Please sign in to comment.