-
Notifications
You must be signed in to change notification settings - Fork 949
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1032 from ZouRui89/mock_image
test: add mock test for image operations on client side
- Loading branch information
Showing
9 changed files
with
338 additions
and
69 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
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,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 | ||
} |
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,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)) | ||
|
||
} |
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,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 | ||
|
||
} |
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,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) | ||
} |
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,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 | ||
} |
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,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) | ||
} | ||
|
||
} |
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,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 | ||
} |
Oops, something went wrong.