Skip to content

Commit

Permalink
test: add more test for container operations
Browse files Browse the repository at this point in the history
Signed-off-by: Zou Rui <[email protected]>
  • Loading branch information
ZouRui89 committed Jun 7, 2018
1 parent 4782518 commit 63add7f
Show file tree
Hide file tree
Showing 7 changed files with 265 additions and 31 deletions.
24 changes: 24 additions & 0 deletions client/container_attach.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package client

import (
"bufio"
"context"
"net"
"net/url"
)

// ContainerAttach attachs a container.
func (client *APIClient) ContainerAttach(ctx context.Context, name string, stdin bool) (net.Conn, *bufio.Reader, error) {
q := url.Values{}
if stdin {
q.Set("stdin", "1")
} else {
q.Set("stdin", "0")
}

header := map[string][]string{
"Content-Type": {"text/plain"},
}

return client.hijack(ctx, "/containers/"+name+"/attach", q, nil, header)
}
1 change: 1 addition & 0 deletions client/container_create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"testing"

"github.com/alibaba/pouch/apis/types"

"github.com/stretchr/testify/assert"
)

Expand Down
110 changes: 110 additions & 0 deletions client/container_exec_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
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 TestContainerCreateExecError(t *testing.T) {
client := &APIClient{
HTTPCli: newMockClient(errorMockResponse(http.StatusInternalServerError, "Server error")),
}
_, err := client.ContainerCreateExec(context.Background(), "nothing", &types.ExecCreateConfig{})
if err == nil || !strings.Contains(err.Error(), "Server error") {
t.Fatalf("expected a Server Error, got %v", err)
}
}

func TestContainerCreateExec(t *testing.T) {
expectedURL := "/containers/container_id/exec"

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" {
createExecConfig := &types.ExecCreateConfig{}
if err := json.NewDecoder(req.Body).Decode(createExecConfig); err != nil {
return nil, fmt.Errorf("failed to parse json: %v", err)
}
}
createExecResponse := types.ExecCreateResp{
ID: "container_id",
}
b, err := json.Marshal(createExecResponse)
if err != nil {
return nil, err
}
return &http.Response{
StatusCode: http.StatusOK,
Body: ioutil.NopCloser(bytes.NewReader([]byte(b))),
}, nil
})

client := &APIClient{
HTTPCli: httpClient,
}

res, err := client.ContainerCreateExec(context.Background(), "container_id", &types.ExecCreateConfig{})
if err != nil {
t.Fatal(err)
}
assert.Equal(t, res.ID, "container_id")
}

func TestContainerInspectExecError(t *testing.T) {
client := &APIClient{
HTTPCli: newMockClient(errorMockResponse(http.StatusInternalServerError, "Server error")),
}
_, err := client.ContainerExecInspect(context.Background(), "nothing")
if err == nil || !strings.Contains(err.Error(), "Server error") {
t.Fatalf("expected a Server Error, got %v", err)
}
}

func TestContainerInspectExec(t *testing.T) {
expectedURL := "/exec/container_id/json"

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.Method != "GET" {
return nil, fmt.Errorf("expected GET method, got %s", req.Method)
}
b, err := json.Marshal(types.ContainerExecInspect{
ID: "exec_id",
ContainerID: "container_id",
})
if err != nil {
return nil, err
}

return &http.Response{
StatusCode: http.StatusOK,
Body: ioutil.NopCloser(bytes.NewReader([]byte(b))),
}, nil
})

client := &APIClient{
HTTPCli: httpClient,
}

res, err := client.ContainerExecInspect(context.Background(), "container_id")
if err != nil {
t.Fatal(err)
}

assert.Equal(t, res.ID, "exec_id")
assert.Equal(t, res.ContainerID, "container_id")
}
31 changes: 0 additions & 31 deletions client/container.go → client/container_logs.go
Original file line number Diff line number Diff line change
@@ -1,44 +1,13 @@
package client

import (
"bufio"
"context"
"io"
"net"
"net/url"

"github.com/alibaba/pouch/apis/types"
)

// ContainerAttach attach a container
func (client *APIClient) ContainerAttach(ctx context.Context, name string, stdin bool) (net.Conn, *bufio.Reader, error) {
q := url.Values{}
if stdin {
q.Set("stdin", "1")
} else {
q.Set("stdin", "0")
}

header := map[string][]string{
"Content-Type": {"text/plain"},
}

return client.hijack(ctx, "/containers/"+name+"/attach", q, nil, header)
}

// ContainerUpgrade upgrade a container with new image and args.
func (client *APIClient) ContainerUpgrade(ctx context.Context, name string, config types.ContainerConfig, hostConfig *types.HostConfig) error {
// TODO
upgradeConfig := types.ContainerUpgradeConfig{
ContainerConfig: config,
HostConfig: hostConfig,
}
resp, err := client.post(ctx, "/containers/"+name+"/upgrade", url.Values{}, upgradeConfig, nil)
ensureCloseReader(resp)

return err
}

// ContainerLogs return the logs generated by a container in an io.ReadCloser.
func (client *APIClient) ContainerLogs(ctx context.Context, name string, options types.ContainerLogsOptions) (io.ReadCloser, error) {
query := url.Values{}
Expand Down
20 changes: 20 additions & 0 deletions client/container_upgrade.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package client

import (
"context"
"net/url"

"github.com/alibaba/pouch/apis/types"
)

// ContainerUpgrade upgrade a container with new image and args.
func (client *APIClient) ContainerUpgrade(ctx context.Context, name string, config types.ContainerConfig, hostConfig *types.HostConfig) error {
upgradeConfig := types.ContainerUpgradeConfig{
ContainerConfig: config,
HostConfig: hostConfig,
}
resp, err := client.post(ctx, "/containers/"+name+"/upgrade", url.Values{}, upgradeConfig, nil)
ensureCloseReader(resp)

return err
}
50 changes: 50 additions & 0 deletions client/container_upgrade_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package client

import (
"bytes"
"context"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"strings"
"testing"

"github.com/alibaba/pouch/apis/types"
)

func TestContainerUpgradeError(t *testing.T) {
client := &APIClient{
HTTPCli: newMockClient(errorMockResponse(http.StatusInternalServerError, "Server error")),
}
err := client.ContainerUpgrade(context.Background(), "nothing", types.ContainerConfig{}, &types.HostConfig{})
if err == nil || !strings.Contains(err.Error(), "Server error") {
t.Fatalf("expected a Server Error, got %v", err)
}
}

func TestContainerUpgrade(t *testing.T) {
expectedURL := "/containers/container_id/upgrade"

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" {
var upgradeConfig interface{}
if err := json.NewDecoder(req.Body).Decode(&upgradeConfig); err != nil {
return nil, fmt.Errorf("failed to parse json: %v", err)
}
}
return &http.Response{
StatusCode: http.StatusOK,
Body: ioutil.NopCloser(bytes.NewReader([]byte(""))),
}, nil
})
client := &APIClient{
HTTPCli: httpClient,
}
if err := client.ContainerUpgrade(context.Background(), "container_id", types.ContainerConfig{}, &types.HostConfig{}); err != nil {
t.Fatal(err)
}
}
60 changes: 60 additions & 0 deletions client/network_connect_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package client

import (
"bytes"
"context"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"strings"
"testing"

"github.com/alibaba/pouch/apis/types"
)

func TestNetworkConnectNotFoundError(t *testing.T) {
client := &APIClient{
HTTPCli: newMockClient(errorMockResponse(http.StatusNotFound, "Not Found")),
}
err := client.NetworkConnect(context.Background(), "no_network", &types.NetworkConnect{})
if err == nil || !strings.Contains(err.Error(), "Not Found") {
t.Fatalf("expected a Not Found Error, got %v", err)
}
}

func TestNetworkConnect(t *testing.T) {
expectedURL := "/networks/network_id/connect"

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.Method != "POST" {
return nil, fmt.Errorf("expected POST method, got %s", req.Method)
}

connect := &types.NetworkConnect{}
if err := json.NewDecoder(req.Body).Decode(connect); err != nil {
return nil, err
}

if connect.Container != "container_id" {
return nil, fmt.Errorf("expected 'containerID', got %s", connect.Container)
}

return &http.Response{
StatusCode: http.StatusOK,
Body: ioutil.NopCloser(bytes.NewReader([]byte(""))),
}, nil
})

client := &APIClient{
HTTPCli: httpClient,
}

err := client.NetworkConnect(context.Background(), "network_id", &types.NetworkConnect{Container: "container_id"})
if err != nil {
t.Fatal(err)
}
}

0 comments on commit 63add7f

Please sign in to comment.