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