1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
|
package docker
import (
"bytes"
"encoding/json"
"io"
"net/http"
"reflect"
"testing"
)
func TestCommitContainer(t *testing.T) {
t.Parallel()
response := `{"Id":"596069db4bf5"}`
client := newTestClient(&FakeRoundTripper{message: response, status: http.StatusOK})
id := "596069db4bf5"
image, err := client.CommitContainer(CommitContainerOptions{})
if err != nil {
t.Fatal(err)
}
if image.ID != id {
t.Errorf("CommitContainer: Wrong image id. Want %q. Got %q.", id, image.ID)
}
}
func TestCommitContainerParams(t *testing.T) {
t.Parallel()
cfg := Config{Memory: 67108864}
json, _ := json.Marshal(&cfg)
tests := []struct {
input CommitContainerOptions
params map[string][]string
body []byte
}{
{CommitContainerOptions{}, map[string][]string{}, nil},
{CommitContainerOptions{Container: "44c004db4b17"}, map[string][]string{"container": {"44c004db4b17"}}, nil},
{
CommitContainerOptions{Container: "44c004db4b17", Repository: "tsuru/python", Message: "something"},
map[string][]string{"container": {"44c004db4b17"}, "repo": {"tsuru/python"}, "comment": {"something"}},
nil,
},
{
CommitContainerOptions{Container: "44c004db4b17", Run: &cfg},
map[string][]string{"container": {"44c004db4b17"}},
json,
},
}
const expectedPath = "/commit"
for _, tt := range tests {
test := tt
t.Run("", func(t *testing.T) {
t.Parallel()
fakeRT := &FakeRoundTripper{message: "{}", status: http.StatusOK}
client := newTestClient(fakeRT)
if _, err := client.CommitContainer(test.input); err != nil {
t.Error(err)
}
got := map[string][]string(fakeRT.requests[0].URL.Query())
if !reflect.DeepEqual(got, test.params) {
t.Errorf("Expected %#v, got %#v.", test.params, got)
}
if path := fakeRT.requests[0].URL.Path; path != expectedPath {
t.Errorf("Wrong path on request. Want %q. Got %q.", expectedPath, path)
}
if meth := fakeRT.requests[0].Method; meth != http.MethodPost {
t.Errorf("Wrong HTTP method. Want POST. Got %s.", meth)
}
if test.body != nil {
if requestBody, err := io.ReadAll(fakeRT.requests[0].Body); err == nil {
if !bytes.Equal(requestBody, test.body) {
t.Errorf("Expected body %#v, got %#v", test.body, requestBody)
}
} else {
t.Errorf("Error reading request body: %#v", err)
}
}
})
}
}
func TestCommitContainerFailure(t *testing.T) {
t.Parallel()
client := newTestClient(&FakeRoundTripper{message: "no such container", status: http.StatusInternalServerError})
_, err := client.CommitContainer(CommitContainerOptions{})
if err == nil {
t.Error("Expected non-nil error, got <nil>.")
}
}
func TestCommitContainerNotFound(t *testing.T) {
t.Parallel()
client := newTestClient(&FakeRoundTripper{message: "no such container", status: http.StatusNotFound})
_, err := client.CommitContainer(CommitContainerOptions{})
expectNoSuchContainer(t, "", err)
}
|