Skip to content

Commit 2a4299c

Browse files
committed
test:add container create test
Signed-off-by: Dewey-Ding <[email protected]>
1 parent 204c39c commit 2a4299c

File tree

3 files changed

+102
-26
lines changed

3 files changed

+102
-26
lines changed

client/container.go

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -10,32 +10,6 @@ import (
1010
"github.com/alibaba/pouch/apis/types"
1111
)
1212

13-
// ContainerCreate creates a new container based in the given configuration.
14-
func (client *APIClient) ContainerCreate(ctx context.Context, config types.ContainerConfig, hostConfig *types.HostConfig, networkingConfig *types.NetworkingConfig, containerName string) (*types.ContainerCreateResp, error) {
15-
createConfig := types.ContainerCreateConfig{
16-
ContainerConfig: config,
17-
HostConfig: hostConfig,
18-
NetworkingConfig: networkingConfig,
19-
}
20-
21-
q := url.Values{}
22-
if containerName != "" {
23-
q.Set("name", containerName)
24-
}
25-
26-
resp, err := client.post(ctx, "/containers/create", q, createConfig, nil)
27-
if err != nil {
28-
return nil, err
29-
}
30-
31-
container := &types.ContainerCreateResp{}
32-
33-
err = decodeBody(container, resp.Body)
34-
ensureCloseReader(resp)
35-
36-
return container, err
37-
}
38-
3913
// ContainerAttach attach a container
4014
func (client *APIClient) ContainerAttach(ctx context.Context, name string, stdin bool) (net.Conn, *bufio.Reader, error) {
4115
q := url.Values{}

client/container_create.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package client
2+
3+
import (
4+
"context"
5+
"net/url"
6+
7+
"github.com/alibaba/pouch/apis/types"
8+
)
9+
10+
// ContainerCreate creates a new container based in the given configuration.
11+
func (client *APIClient) ContainerCreate(ctx context.Context, config types.ContainerConfig, hostConfig *types.HostConfig, networkingConfig *types.NetworkingConfig, containerName string) (*types.ContainerCreateResp, error) {
12+
createConfig := types.ContainerCreateConfig{
13+
ContainerConfig: config,
14+
HostConfig: hostConfig,
15+
NetworkingConfig: networkingConfig,
16+
}
17+
18+
q := url.Values{}
19+
if containerName != "" {
20+
q.Set("name", containerName)
21+
}
22+
23+
resp, err := client.post(ctx, "/containers/create", q, createConfig, nil)
24+
if err != nil {
25+
return nil, err
26+
}
27+
28+
container := &types.ContainerCreateResp{}
29+
30+
err = decodeBody(container, resp.Body)
31+
ensureCloseReader(resp)
32+
33+
return container, err
34+
}

client/container_create_test.go

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package client
2+
3+
import (
4+
"bytes"
5+
"context"
6+
"encoding/json"
7+
"fmt"
8+
"io/ioutil"
9+
"net/http"
10+
"strings"
11+
"testing"
12+
13+
"github.com/alibaba/pouch/apis/types"
14+
)
15+
16+
func TestContainerCreateError(t *testing.T) {
17+
client := &APIClient{
18+
HTTPCli: newMockClient(errorMockResponse(http.StatusInternalServerError, "Server error")),
19+
}
20+
_, err := client.ContainerCreate(context.Background(), types.ContainerConfig{}, nil, nil, "nothing")
21+
if err == nil || !strings.Contains(err.Error(), "Server error") {
22+
t.Fatalf("expected a Server Error, got %v", err)
23+
}
24+
}
25+
26+
func TestContainerCreate(t *testing.T) {
27+
expectedURL := "/containers/create"
28+
29+
httpClient := newMockClient(func(req *http.Request) (*http.Response, error) {
30+
if !strings.HasPrefix(req.URL.Path, expectedURL) {
31+
return nil, fmt.Errorf("expected URL '%s', got '%s'", expectedURL, req.URL)
32+
}
33+
if req.Method != "POST" {
34+
return nil, fmt.Errorf("expected POST method, got %s", req.Method)
35+
}
36+
name := req.URL.Query().Get("name")
37+
if name != "container_name" {
38+
return nil, fmt.Errorf("container name not set in URL query properly. Expected `container_name`, got %s", name)
39+
}
40+
containerCreateResp := types.ContainerCreateResp{
41+
ID: "container_id",
42+
Name: "container_name",
43+
}
44+
b, err := json.Marshal(containerCreateResp)
45+
if err != nil {
46+
return nil, err
47+
}
48+
return &http.Response{
49+
StatusCode: http.StatusCreated,
50+
Body: ioutil.NopCloser(bytes.NewReader([]byte(b))),
51+
}, nil
52+
})
53+
54+
client := &APIClient{
55+
HTTPCli: httpClient,
56+
}
57+
58+
container, err := client.ContainerCreate(context.Background(), types.ContainerConfig{}, &types.HostConfig{}, &types.NetworkingConfig{}, "container_name")
59+
if err != nil {
60+
t.Fatal(err)
61+
}
62+
if container.ID != "container_id" {
63+
t.Fatalf("expected `container_id`, got %s", container.ID)
64+
}
65+
if container.Name != "container_name" {
66+
t.Fatalf("expected `container_name`, got %s", container.ID)
67+
}
68+
}

0 commit comments

Comments
 (0)