Skip to content

Commit

Permalink
Merge pull request #1032 from ZouRui89/mock_image
Browse files Browse the repository at this point in the history
test: add mock test for image operations on client side
  • Loading branch information
allencloud authored Apr 3, 2018
2 parents 48bb847 + 26dc593 commit 5dbe6ea
Show file tree
Hide file tree
Showing 9 changed files with 338 additions and 69 deletions.
69 changes: 0 additions & 69 deletions client/image.go

This file was deleted.

21 changes: 21 additions & 0 deletions client/image_inspect.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package client

import (
"context"

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

// ImageInspect requests daemon to inspect an image.
func (client *APIClient) ImageInspect(ctx context.Context, name string) (types.ImageInfo, error) {
image := types.ImageInfo{}

resp, err := client.get(ctx, "/images/"+name+"/json", nil, nil)
if err != nil {
return image, err
}

defer ensureCloseReader(resp)
err = decodeBody(&image, resp.Body)
return image, err
}
74 changes: 74 additions & 0 deletions client/image_inspect_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
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 TestImageInspectServerError(t *testing.T) {
client := &APIClient{
HTTPCli: newMockClient(errorMockResponse(http.StatusInternalServerError, "Server error")),
}
_, err := client.ImageInspect(context.Background(), "image_id")
if err == nil || !strings.Contains(err.Error(), "Server error") {
t.Fatalf("expected a Server Error, got %v", err)
}
}

func TestImageInspectNotFoundError(t *testing.T) {
client := &APIClient{
HTTPCli: newMockClient(errorMockResponse(http.StatusConflict, "Not Found")),
}
_, err := client.ImageInspect(context.Background(), "no image")
if err == nil || !strings.Contains(err.Error(), "Not Found") {
t.Fatalf("expected a Server Error, got %v", err)
}
}

func TestImageInspect(t *testing.T) {
expectedURL := "/images/image_id"

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)
}

imageInspectResp, err := json.Marshal(types.ImageInfo{
ID: "1",
Size: int64(94),
})
if err != nil {
return nil, err
}

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

client := &APIClient{
HTTPCli: httpClient,
}

image, err := client.ImageInspect(context.Background(), "image_id")
if err != nil {
t.Fatal(err)
}
assert.Equal(t, image.ID, "1")
assert.Equal(t, image.Size, int64(94))

}
22 changes: 22 additions & 0 deletions client/image_list.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package client

import (
"context"
"github.com/alibaba/pouch/apis/types"
)

// ImageList requests daemon to list all images
func (client *APIClient) ImageList(ctx context.Context) ([]types.ImageInfo, error) {
resp, err := client.get(ctx, "/images/json", nil, nil)
if err != nil {
return nil, err
}

imageList := []types.ImageInfo{}

err = decodeBody(&imageList, resp.Body)
ensureCloseReader(resp)

return imageList, err

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

func TestImageList(t *testing.T) {
expectedURL := "/images"

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)
}

imageListResp, err := json.Marshal([]types.ImageInfo{
{
ID: "1",
Size: 703,
Os: "CentOS",
},
{
ID: "2",
Size: 44,
Os: "Ubuntu TLS 16.04",
},
})
if err != nil {
return nil, err
}

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

client := &APIClient{
HTTPCli: httpClient,
}

image, err := client.ImageList(context.Background())
if err != nil {
t.Fatal(err)
}
assert.Equal(t, len(image), 2)
}
24 changes: 24 additions & 0 deletions client/image_pull.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package client

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

// ImagePull requests daemon to pull an image from registry.
func (client *APIClient) ImagePull(ctx context.Context, name, tag, encodedAuth string) (io.ReadCloser, error) {
q := url.Values{}
q.Set("fromImage", name)
q.Set("tag", tag)

headers := map[string][]string{}
if encodedAuth != "" {
headers["X-Registry-Auth"] = []string{encodedAuth}
}
resp, err := client.post(ctx, "/images/create", q, nil, headers)
if err != nil {
return nil, err
}
return resp.Body, nil
}
60 changes: 60 additions & 0 deletions client/image_pull_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package client

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

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

func TestImagePullWrongError(t *testing.T) {
client := &APIClient{
HTTPCli: newMockClient(errorMockResponse(http.StatusNotFound, "Image not found")),
}
_, err := client.ImagePull(context.Background(), "image_name", "image_tag", "auth")
if err == nil || !strings.Contains(err.Error(), "Image not found") {
t.Fatalf("expected an Image Not Found Error, got %v", err)
}
}

func TestImagePull(t *testing.T) {
expectedURL := "/images/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.Method != "POST" {
return nil, fmt.Errorf("Expected POST method, got %s", req.Method)
}

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

client := &APIClient{
HTTPCli: httpClient,
}

_, err := client.ImagePull(context.Background(), "image_name", "image_tag", "auth")
if err != nil {
t.Fatal(err)
}

}
19 changes: 19 additions & 0 deletions client/image_remove.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package client

import (
"context"
"net/url"
)

// ImageRemove deletes an image.
func (client *APIClient) ImageRemove(ctx context.Context, name string, force bool) error {
q := url.Values{}
if force {
q.Set("force", "true")
}

resp, err := client.delete(ctx, "/images/"+name, q, nil)
ensureCloseReader(resp)

return err
}
Loading

0 comments on commit 5dbe6ea

Please sign in to comment.