2018-02-05 16:05:59 -05:00
|
|
|
package client // import "github.com/docker/docker/client"
|
2016-09-06 14:46:37 -04:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2018-04-19 18:30:59 -04:00
|
|
|
"context"
|
2016-09-06 14:46:37 -04:00
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"net/http"
|
|
|
|
"reflect"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/docker/docker/api/types"
|
2018-12-31 12:22:43 -05:00
|
|
|
"github.com/docker/docker/errdefs"
|
2016-09-06 14:46:37 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestImageImportError(t *testing.T) {
|
|
|
|
client := &Client{
|
2016-09-08 23:44:25 -04:00
|
|
|
client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")),
|
2016-09-06 14:46:37 -04:00
|
|
|
}
|
|
|
|
_, err := client.ImageImport(context.Background(), types.ImageImportSource{}, "image:tag", types.ImageImportOptions{})
|
2018-12-31 12:22:43 -05:00
|
|
|
if !errdefs.IsSystem(err) {
|
2019-10-12 18:31:53 -04:00
|
|
|
t.Fatalf("expected a Server Error, got %[1]T: %[1]v", err)
|
2018-12-31 12:22:43 -05:00
|
|
|
}
|
2016-09-06 14:46:37 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestImageImport(t *testing.T) {
|
|
|
|
expectedURL := "/images/create"
|
|
|
|
client := &Client{
|
2016-09-08 23:44:25 -04:00
|
|
|
client: newMockClient(func(r *http.Request) (*http.Response, error) {
|
2016-09-06 14:46:37 -04:00
|
|
|
if !strings.HasPrefix(r.URL.Path, expectedURL) {
|
|
|
|
return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, r.URL)
|
|
|
|
}
|
|
|
|
query := r.URL.Query()
|
|
|
|
fromSrc := query.Get("fromSrc")
|
|
|
|
if fromSrc != "image_source" {
|
|
|
|
return nil, fmt.Errorf("fromSrc not set in URL query properly. Expected 'image_source', got %s", fromSrc)
|
|
|
|
}
|
|
|
|
repo := query.Get("repo")
|
|
|
|
if repo != "repository_name:imported" {
|
2016-11-17 02:50:38 -05:00
|
|
|
return nil, fmt.Errorf("repo not set in URL query properly. Expected 'repository_name:imported', got %s", repo)
|
2016-09-06 14:46:37 -04:00
|
|
|
}
|
|
|
|
tag := query.Get("tag")
|
|
|
|
if tag != "imported" {
|
|
|
|
return nil, fmt.Errorf("tag not set in URL query properly. Expected 'imported', got %s", tag)
|
|
|
|
}
|
|
|
|
message := query.Get("message")
|
|
|
|
if message != "A message" {
|
|
|
|
return nil, fmt.Errorf("message not set in URL query properly. Expected 'A message', got %s", message)
|
|
|
|
}
|
|
|
|
changes := query["changes"]
|
|
|
|
expectedChanges := []string{"change1", "change2"}
|
|
|
|
if !reflect.DeepEqual(expectedChanges, changes) {
|
|
|
|
return nil, fmt.Errorf("changes not set in URL query properly. Expected %v, got %v", expectedChanges, changes)
|
|
|
|
}
|
|
|
|
|
|
|
|
return &http.Response{
|
|
|
|
StatusCode: http.StatusOK,
|
|
|
|
Body: ioutil.NopCloser(bytes.NewReader([]byte("response"))),
|
|
|
|
}, nil
|
|
|
|
}),
|
|
|
|
}
|
|
|
|
importResponse, err := client.ImageImport(context.Background(), types.ImageImportSource{
|
|
|
|
Source: strings.NewReader("source"),
|
|
|
|
SourceName: "image_source",
|
|
|
|
}, "repository_name:imported", types.ImageImportOptions{
|
|
|
|
Tag: "imported",
|
|
|
|
Message: "A message",
|
|
|
|
Changes: []string{"change1", "change2"},
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
response, err := ioutil.ReadAll(importResponse)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
importResponse.Close()
|
|
|
|
if string(response) != "response" {
|
|
|
|
t.Fatalf("expected response to contain 'response', got %s", string(response))
|
|
|
|
}
|
|
|
|
}
|