-
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 branch 'master' into tls_whitelist
- Loading branch information
Showing
15 changed files
with
416 additions
and
86 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
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" | ||
) | ||
|
||
// ContainerGet returns the detailed information of container. | ||
func (client *APIClient) ContainerGet(ctx context.Context, name string) (*types.ContainerJSON, error) { | ||
resp, err := client.get(ctx, "/containers/"+name+"/json", nil, nil) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
container := types.ContainerJSON{} | ||
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,52 @@ | ||
package client | ||
|
||
import ( | ||
"bytes" | ||
"context" | ||
"encoding/json" | ||
"fmt" | ||
"io/ioutil" | ||
"net/http" | ||
"strings" | ||
"testing" | ||
|
||
"github.com/alibaba/pouch/apis/types" | ||
) | ||
|
||
func TestContainerGetError(t *testing.T) { | ||
client := &APIClient{ | ||
HTTPCli: newMockClient(errorMockResponse(http.StatusInternalServerError, "Server error")), | ||
} | ||
_, err := client.ContainerGet(context.Background(), "nothing") | ||
if err == nil || !strings.Contains(err.Error(), "Server error") { | ||
t.Fatalf("expected a Server Error, got %v", err) | ||
} | ||
} | ||
|
||
func TestContainerGet(t *testing.T) { | ||
expectedURL := "/containers/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) | ||
} | ||
containerJSON := types.ContainerJSON{ | ||
Driver: "Driver", | ||
Image: "Image", | ||
} | ||
b, err := json.Marshal(containerJSON) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return &http.Response{ | ||
StatusCode: http.StatusOK, | ||
Body: ioutil.NopCloser(bytes.NewReader([]byte(b))), | ||
}, nil | ||
}) | ||
client := &APIClient{ | ||
HTTPCli: httpClient, | ||
} | ||
if _, err := client.ContainerGet(context.Background(), "container_id"); err != nil { | ||
t.Fatal(err) | ||
} | ||
} |
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 | ||
} |
Oops, something went wrong.