Skip to content

Commit

Permalink
Merge branch 'master' into tls_whitelist
Browse files Browse the repository at this point in the history
  • Loading branch information
yyb196 authored Apr 3, 2018
2 parents 2eb6b2a + 46c67ec commit a76cd4a
Show file tree
Hide file tree
Showing 15 changed files with 416 additions and 86 deletions.
2 changes: 2 additions & 0 deletions apis/server/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ func initRoute(s *Server) http.Handler {

// metrics
r.Path(versionMatcher + "/metrics").Methods(http.MethodGet).Handler(prometheus.Handler())
r.Path("/metrics").Methods(http.MethodGet).Handler(prometheus.Handler())

if s.Config.Debug {
profilerSetup(r)
Expand All @@ -83,6 +84,7 @@ func initRoute(s *Server) http.Handler {

func (s *Server) addRoute(r *mux.Router, mothod string, path string, f func(ctx context.Context, rw http.ResponseWriter, req *http.Request) error) {
r.Path(versionMatcher + path).Methods(mothod).Handler(filter(f, s))
r.Path(path).Methods(mothod).Handler(filter(f, s))
}

func profilerSetup(mainRouter *mux.Router) {
Expand Down
14 changes: 0 additions & 14 deletions client/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,20 +121,6 @@ func (client *APIClient) ContainerStartExec(ctx context.Context, execid string,
return client.hijack(ctx, "/exec/"+execid+"/start", url.Values{}, config, header)
}

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

// ContainerRestart restarts a running container.
func (client *APIClient) ContainerRestart(ctx context.Context, name string, timeout string) error {
q := url.Values{}
Expand Down
21 changes: 21 additions & 0 deletions client/container_get.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"
)

// 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
}
52 changes: 52 additions & 0 deletions client/container_get_test.go
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)
}
}
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
}
Loading

0 comments on commit a76cd4a

Please sign in to comment.