2015-03-18 02:18:41 +00:00
|
|
|
package streamformatter
|
2014-06-02 10:41:47 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"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-05-01 18:54:56 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
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)
|
|
|
|
assert.Equal(t, "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{}
|
|
|
|
progress := &jsonmessage.JSONProgress{
|
|
|
|
Current: 15,
|
|
|
|
Total: 30,
|
|
|
|
Start: 1,
|
2014-06-02 10:41:47 +00:00
|
|
|
}
|
2017-05-01 18:54:56 +00:00
|
|
|
res := sf.formatProgress("id", "action", progress, nil)
|
|
|
|
out := string(res)
|
|
|
|
assert.True(t, strings.HasPrefix(out, "action [===="))
|
|
|
|
assert.Contains(t, out, "15B/30B")
|
|
|
|
assert.True(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
|
|
|
|
assert.Equal(t, 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"
|
|
|
|
assert.Equal(t, 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
|
|
|
|
assert.Equal(t, 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{}
|
2015-03-18 02:18:41 +00:00
|
|
|
progress := &jsonmessage.JSONProgress{
|
2014-06-02 10:41:47 +00:00
|
|
|
Current: 15,
|
|
|
|
Total: 30,
|
|
|
|
Start: 1,
|
|
|
|
}
|
2017-05-01 18:54:56 +00:00
|
|
|
res := sf.formatProgress("id", "action", progress, nil)
|
2015-03-18 02:18:41 +00:00
|
|
|
msg := &jsonmessage.JSONMessage{}
|
2017-05-01 18:54:56 +00:00
|
|
|
require.NoError(t, json.Unmarshal(res, msg))
|
|
|
|
assert.Equal(t, "id", msg.ID)
|
|
|
|
assert.Equal(t, "action", msg.Status)
|
2016-03-12 05:50:02 +00:00
|
|
|
|
|
|
|
// The progress 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.
|
|
|
|
// However, the timeLeftBox field may change as progress.String() depends on time.Now().
|
|
|
|
// Therefore, we have to strip the timeLeftBox from the strings to do the comparison.
|
|
|
|
|
|
|
|
// Compare the progress 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
|
|
|
|
2017-05-01 18:54:56 +00:00
|
|
|
assert.Equal(t, progress, msg.Progress)
|
2014-06-02 10:41:47 +00:00
|
|
|
}
|