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"
|
2018-03-13 19:28:34 +00:00
|
|
|
"github.com/gotestyourself/gotestyourself/assert"
|
|
|
|
is "github.com/gotestyourself/gotestyourself/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-06-13 09:15:39 +00:00
|
|
|
res := sf.formatProgress("id", "action", jsonProgress, &AuxFormatter{Writer: &bytes.Buffer{}})
|
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))
|
|
|
|
assert.Check(t, is.Equal("id", msg.ID))
|
|
|
|
assert.Check(t, is.Equal("action", msg.Status))
|
2016-03-12 05:50:02 +00:00
|
|
|
|
2017-06-13 09:15:39 +00:00
|
|
|
// jsonProgress will always be in the format of:
|
2017-02-07 20:58:56 +00:00
|
|
|
// [=========================> ] 15B/30B 412910h51m30s
|
2016-03-12 05:50:02 +00:00
|
|
|
// The last entry '404933h7m11s' is the timeLeftBox.
|
2017-06-13 09:15:39 +00:00
|
|
|
// However, the timeLeftBox field may change as jsonProgress.String() depends on time.Now().
|
2016-03-12 05:50:02 +00:00
|
|
|
// Therefore, we have to strip the timeLeftBox from the strings to do the comparison.
|
|
|
|
|
2017-06-13 09:15:39 +00:00
|
|
|
// Compare the jsonProgress strings before the timeLeftBox
|
2017-02-07 20:58:56 +00:00
|
|
|
expectedProgress := "[=========================> ] 15B/30B"
|
2016-05-31 09:09:06 +00:00
|
|
|
// if terminal column is <= 110, expectedProgressShort is expected.
|
2017-02-07 20:58:56 +00:00
|
|
|
expectedProgressShort := " 15B/30B"
|
2016-05-31 09:09:06 +00:00
|
|
|
if !(strings.HasPrefix(msg.ProgressMessage, expectedProgress) ||
|
|
|
|
strings.HasPrefix(msg.ProgressMessage, expectedProgressShort)) {
|
|
|
|
t.Fatalf("ProgressMessage without the timeLeftBox must be %s or %s, got: %s",
|
|
|
|
expectedProgress, expectedProgressShort, msg.ProgressMessage)
|
2014-06-02 10:41:47 +00:00
|
|
|
}
|
2016-03-12 05:50:02 +00:00
|
|
|
|
2018-03-13 19:28:34 +00:00
|
|
|
assert.Check(t, is.DeepEqual(jsonProgress, msg.Progress))
|
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"}
|
|
|
|
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
|
|
|
}
|