-
Notifications
You must be signed in to change notification settings - Fork 950
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Dewey-Ding <[email protected]>
- Loading branch information
1 parent
204c39c
commit 84ea736
Showing
3 changed files
with
103 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
} |