github.com/wozhu6104/docker@v20.10.10+incompatible/client/container_commit_test.go (about) 1 package client // import "github.com/docker/docker/client" 2 3 import ( 4 "bytes" 5 "context" 6 "encoding/json" 7 "fmt" 8 "io/ioutil" 9 "net/http" 10 "strings" 11 "testing" 12 13 "github.com/docker/docker/api/types" 14 "github.com/docker/docker/errdefs" 15 ) 16 17 func TestContainerCommitError(t *testing.T) { 18 client := &Client{ 19 client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")), 20 } 21 _, err := client.ContainerCommit(context.Background(), "nothing", types.ContainerCommitOptions{}) 22 if !errdefs.IsSystem(err) { 23 t.Fatalf("expected a Server Error, got %[1]T: %[1]v", err) 24 } 25 } 26 27 func TestContainerCommit(t *testing.T) { 28 expectedURL := "/commit" 29 expectedContainerID := "container_id" 30 specifiedReference := "repository_name:tag" 31 expectedRepositoryName := "repository_name" 32 expectedTag := "tag" 33 expectedComment := "comment" 34 expectedAuthor := "author" 35 expectedChanges := []string{"change1", "change2"} 36 37 client := &Client{ 38 client: newMockClient(func(req *http.Request) (*http.Response, error) { 39 if !strings.HasPrefix(req.URL.Path, expectedURL) { 40 return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL) 41 } 42 query := req.URL.Query() 43 containerID := query.Get("container") 44 if containerID != expectedContainerID { 45 return nil, fmt.Errorf("container id not set in URL query properly. Expected '%s', got %s", expectedContainerID, containerID) 46 } 47 repo := query.Get("repo") 48 if repo != expectedRepositoryName { 49 return nil, fmt.Errorf("container repo not set in URL query properly. Expected '%s', got %s", expectedRepositoryName, repo) 50 } 51 tag := query.Get("tag") 52 if tag != expectedTag { 53 return nil, fmt.Errorf("container tag not set in URL query properly. Expected '%s', got %s'", expectedTag, tag) 54 } 55 comment := query.Get("comment") 56 if comment != expectedComment { 57 return nil, fmt.Errorf("container comment not set in URL query properly. Expected '%s', got %s'", expectedComment, comment) 58 } 59 author := query.Get("author") 60 if author != expectedAuthor { 61 return nil, fmt.Errorf("container author not set in URL query properly. Expected '%s', got %s'", expectedAuthor, author) 62 } 63 pause := query.Get("pause") 64 if pause != "0" { 65 return nil, fmt.Errorf("container pause not set in URL query properly. Expected 'true', got %v'", pause) 66 } 67 changes := query["changes"] 68 if len(changes) != len(expectedChanges) { 69 return nil, fmt.Errorf("expected container changes size to be '%d', got %d", len(expectedChanges), len(changes)) 70 } 71 b, err := json.Marshal(types.IDResponse{ 72 ID: "new_container_id", 73 }) 74 if err != nil { 75 return nil, err 76 } 77 return &http.Response{ 78 StatusCode: http.StatusOK, 79 Body: ioutil.NopCloser(bytes.NewReader(b)), 80 }, nil 81 }), 82 } 83 84 r, err := client.ContainerCommit(context.Background(), expectedContainerID, types.ContainerCommitOptions{ 85 Reference: specifiedReference, 86 Comment: expectedComment, 87 Author: expectedAuthor, 88 Changes: expectedChanges, 89 Pause: false, 90 }) 91 if err != nil { 92 t.Fatal(err) 93 } 94 if r.ID != "new_container_id" { 95 t.Fatalf("expected `new_container_id`, got %s", r.ID) 96 } 97 }