2018-02-05 21:05:59 +00:00
|
|
|
package streamformatter // import "github.com/docker/docker/pkg/streamformatter"
|
2014-06-02 10:41:47 +00:00
|
|
|
|
|
|
|
import (
|
2017-06-13 09:15:39 +00:00
|
|
|
"bytes"
|
2014-06-02 10:41:47 +00:00
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
2016-03-12 05:50:02 +00:00
|
|
|
"strings"
|
2014-06-02 10:41:47 +00:00
|
|
|
"testing"
|
2015-04-16 19:22:32 +00:00
|
|
|
|
|
|
|
"github.com/docker/docker/pkg/jsonmessage"
|
2017-12-29 20:13:13 +00:00
|
|
|
"github.com/google/go-cmp/cmp"
|
|
|
|
"github.com/google/go-cmp/cmp/cmpopts"
|
2018-06-11 13:32:11 +00:00
|
|
|
"gotest.tools/assert"
|
|
|
|
is "gotest.tools/assert/cmp"
|
2014-06-02 10:41:47 +00:00
|
|
|
)
|
|
|
|
|
2017-05-01 18:54:56 +00:00
|
|
|
func TestRawProgressFormatterFormatStatus(t *testing.T) {
|
|
|
|
sf := rawProgressFormatter{}
|
|
|
|
res := sf.formatStatus("ID", "%s%d", "a", 1)
|
2018-03-13 19:28:34 +00:00
|
|
|
assert.Check(t, is.Equal("a1\r\n", string(res)))
|
2015-05-12 18:18:54 +00:00
|
|
|
}
|
|
|
|
|
2017-05-01 18:54:56 +00:00
|
|
|
func TestRawProgressFormatterFormatProgress(t *testing.T) {
|
|
|
|
sf := rawProgressFormatter{}
|
2017-06-13 09:15:39 +00:00
|
|
|
jsonProgress := &jsonmessage.JSONProgress{
|
2017-05-01 18:54:56 +00:00
|
|
|
Current: 15,
|
|
|
|
Total: 30,
|
|
|
|
Start: 1,
|
2014-06-02 10:41:47 +00:00
|
|
|
}
|
2017-06-13 09:15:39 +00:00
|
|
|
res := sf.formatProgress("id", "action", jsonProgress, nil)
|
2017-05-01 18:54:56 +00:00
|
|
|
out := string(res)
|
2018-03-13 19:28:34 +00:00
|
|
|
assert.Check(t, strings.HasPrefix(out, "action [===="))
|
|
|
|
assert.Check(t, is.Contains(out, "15B/30B"))
|
|
|
|
assert.Check(t, strings.HasSuffix(out, "\r"))
|
2014-06-02 10:41:47 +00:00
|
|
|
}
|
|
|
|
|
2017-05-01 18:54:56 +00:00
|
|
|
func TestFormatStatus(t *testing.T) {
|
|
|
|
res := FormatStatus("ID", "%s%d", "a", 1)
|
|
|
|
expected := `{"status":"a1","id":"ID"}` + streamNewline
|
2018-03-13 19:28:34 +00:00
|
|
|
assert.Check(t, is.Equal(expected, string(res)))
|
2014-06-02 10:41:47 +00:00
|
|
|
}
|
|
|
|
|
2017-05-01 18:54:56 +00:00
|
|
|
func TestFormatError(t *testing.T) {
|
|
|
|
res := FormatError(errors.New("Error for formatter"))
|
|
|
|
expected := `{"errorDetail":{"message":"Error for formatter"},"error":"Error for formatter"}` + "\r\n"
|
2018-03-13 19:28:34 +00:00
|
|
|
assert.Check(t, is.Equal(expected, string(res)))
|
2014-06-02 10:41:47 +00:00
|
|
|
}
|
|
|
|
|
2017-05-01 18:54:56 +00:00
|
|
|
func TestFormatJSONError(t *testing.T) {
|
2015-03-18 02:18:41 +00:00
|
|
|
err := &jsonmessage.JSONError{Code: 50, Message: "Json error"}
|
2017-05-01 18:54:56 +00:00
|
|
|
res := FormatError(err)
|
|
|
|
expected := `{"errorDetail":{"code":50,"message":"Json error"},"error":"Json error"}` + streamNewline
|
2018-03-13 19:28:34 +00:00
|
|
|
assert.Check(t, is.Equal(expected, string(res)))
|
2014-06-02 10:41:47 +00:00
|
|
|
}
|
|
|
|
|
2017-05-01 18:54:56 +00:00
|
|
|
func TestJsonProgressFormatterFormatProgress(t *testing.T) {
|
|
|
|
sf := &jsonProgressFormatter{}
|
2017-06-13 09:15:39 +00:00
|
|
|
jsonProgress := &jsonmessage.JSONProgress{
|
2014-06-02 10:41:47 +00:00
|
|
|
Current: 15,
|
|
|
|
Total: 30,
|
|
|
|
Start: 1,
|
|
|
|
}
|
2017-12-29 20:13:13 +00:00
|
|
|
aux := "aux message"
|
|
|
|
res := sf.formatProgress("id", "action", jsonProgress, aux)
|
2015-03-18 02:18:41 +00:00
|
|
|
msg := &jsonmessage.JSONMessage{}
|
2017-06-13 09:15:39 +00:00
|
|
|
|
2018-03-13 19:28:34 +00:00
|
|
|
assert.NilError(t, json.Unmarshal(res, msg))
|
2016-03-12 05:50:02 +00:00
|
|
|
|
2017-12-29 20:13:13 +00:00
|
|
|
rawAux := json.RawMessage(`"` + aux + `"`)
|
|
|
|
expected := &jsonmessage.JSONMessage{
|
|
|
|
ID: "id",
|
|
|
|
Status: "action",
|
|
|
|
Aux: &rawAux,
|
|
|
|
Progress: jsonProgress,
|
2014-06-02 10:41:47 +00:00
|
|
|
}
|
2017-12-29 20:13:13 +00:00
|
|
|
assert.DeepEqual(t, msg, expected, cmpJSONMessageOpt())
|
|
|
|
}
|
2016-03-12 05:50:02 +00:00
|
|
|
|
2017-12-29 20:13:13 +00:00
|
|
|
func cmpJSONMessageOpt() cmp.Option {
|
|
|
|
progressMessagePath := func(path cmp.Path) bool {
|
|
|
|
return path.String() == "ProgressMessage"
|
|
|
|
}
|
|
|
|
return cmp.Options{
|
|
|
|
cmpopts.IgnoreUnexported(jsonmessage.JSONProgress{}),
|
|
|
|
// Ignore deprecated property that is a derivative of Progress
|
|
|
|
cmp.FilterPath(progressMessagePath, cmp.Ignore()),
|
|
|
|
}
|
2017-06-13 09:15:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestJsonProgressFormatterFormatStatus(t *testing.T) {
|
|
|
|
sf := jsonProgressFormatter{}
|
|
|
|
res := sf.formatStatus("ID", "%s%d", "a", 1)
|
2018-03-13 19:28:34 +00:00
|
|
|
assert.Check(t, is.Equal(`{"status":"a1","id":"ID"}`+streamNewline, string(res)))
|
2017-06-13 09:15:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestNewJSONProgressOutput(t *testing.T) {
|
|
|
|
b := bytes.Buffer{}
|
|
|
|
b.Write(FormatStatus("id", "Downloading"))
|
|
|
|
_ = NewJSONProgressOutput(&b, false)
|
2018-03-13 19:28:34 +00:00
|
|
|
assert.Check(t, is.Equal(`{"status":"Downloading","id":"id"}`+streamNewline, b.String()))
|
2017-06-13 09:15:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestAuxFormatterEmit(t *testing.T) {
|
|
|
|
b := bytes.Buffer{}
|
|
|
|
aux := &AuxFormatter{Writer: &b}
|
|
|
|
sampleAux := &struct {
|
|
|
|
Data string
|
|
|
|
}{"Additional data"}
|
2018-06-11 18:48:42 +00:00
|
|
|
err := aux.Emit("", sampleAux)
|
2018-03-13 19:28:34 +00:00
|
|
|
assert.NilError(t, err)
|
|
|
|
assert.Check(t, is.Equal(`{"aux":{"Data":"Additional data"}}`+streamNewline, b.String()))
|
2014-06-02 10:41:47 +00:00
|
|
|
}
|