github.com/ssdev-go/moby@v17.12.1-ce-rc2+incompatible/client/image_import_test.go (about) 1 package client 2 3 import ( 4 "bytes" 5 "fmt" 6 "io/ioutil" 7 "net/http" 8 "reflect" 9 "strings" 10 "testing" 11 12 "github.com/docker/docker/api/types" 13 "golang.org/x/net/context" 14 ) 15 16 func TestImageImportError(t *testing.T) { 17 client := &Client{ 18 client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")), 19 } 20 _, err := client.ImageImport(context.Background(), types.ImageImportSource{}, "image:tag", types.ImageImportOptions{}) 21 if err == nil || err.Error() != "Error response from daemon: Server error" { 22 t.Fatalf("expected a Server error, got %v", err) 23 } 24 } 25 26 func TestImageImport(t *testing.T) { 27 expectedURL := "/images/create" 28 client := &Client{ 29 client: newMockClient(func(r *http.Request) (*http.Response, error) { 30 if !strings.HasPrefix(r.URL.Path, expectedURL) { 31 return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, r.URL) 32 } 33 query := r.URL.Query() 34 fromSrc := query.Get("fromSrc") 35 if fromSrc != "image_source" { 36 return nil, fmt.Errorf("fromSrc not set in URL query properly. Expected 'image_source', got %s", fromSrc) 37 } 38 repo := query.Get("repo") 39 if repo != "repository_name:imported" { 40 return nil, fmt.Errorf("repo not set in URL query properly. Expected 'repository_name:imported', got %s", repo) 41 } 42 tag := query.Get("tag") 43 if tag != "imported" { 44 return nil, fmt.Errorf("tag not set in URL query properly. Expected 'imported', got %s", tag) 45 } 46 message := query.Get("message") 47 if message != "A message" { 48 return nil, fmt.Errorf("message not set in URL query properly. Expected 'A message', got %s", message) 49 } 50 changes := query["changes"] 51 expectedChanges := []string{"change1", "change2"} 52 if !reflect.DeepEqual(expectedChanges, changes) { 53 return nil, fmt.Errorf("changes not set in URL query properly. Expected %v, got %v", expectedChanges, changes) 54 } 55 56 return &http.Response{ 57 StatusCode: http.StatusOK, 58 Body: ioutil.NopCloser(bytes.NewReader([]byte("response"))), 59 }, nil 60 }), 61 } 62 importResponse, err := client.ImageImport(context.Background(), types.ImageImportSource{ 63 Source: strings.NewReader("source"), 64 SourceName: "image_source", 65 }, "repository_name:imported", types.ImageImportOptions{ 66 Tag: "imported", 67 Message: "A message", 68 Changes: []string{"change1", "change2"}, 69 }) 70 if err != nil { 71 t.Fatal(err) 72 } 73 response, err := ioutil.ReadAll(importResponse) 74 if err != nil { 75 t.Fatal(err) 76 } 77 importResponse.Close() 78 if string(response) != "response" { 79 t.Fatalf("expected response to contain 'response', got %s", string(response)) 80 } 81 }