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