2015-03-17 22:18:41 -04:00
|
|
|
package streamformatter
|
2014-06-02 06:41:47 -04:00
|
|
|
|
|
|
|
import (
|
2017-06-13 05:15:39 -04:00
|
|
|
"bytes"
|
2014-06-02 06:41:47 -04:00
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
2016-03-12 00:50:02 -05:00
|
|
|
"strings"
|
2014-06-02 06:41:47 -04:00
|
|
|
"testing"
|
2015-04-16 15:22:32 -04:00
|
|
|
|
|
|
|
"github.com/docker/docker/pkg/jsonmessage"
|
2017-05-01 14:54:56 -04:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
2014-06-02 06:41:47 -04:00
|
|
|
)
|
|
|
|
|
2017-05-01 14:54:56 -04: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 14:18:54 -04:00
|
|
|
}
|
|
|
|
|
2017-05-01 14:54:56 -04:00
|
|
|
func TestRawProgressFormatterFormatProgress(t *testing.T) {
|
|
|
|
sf := rawProgressFormatter{}
|
2017-06-13 05:15:39 -04:00
|
|
|
jsonProgress := &jsonmessage.JSONProgress{
|
2017-05-01 14:54:56 -04:00
|
|
|
Current: 15,
|
|
|
|
Total: 30,
|
|
|
|
Start: 1,
|
2014-06-02 06:41:47 -04:00
|
|
|
}
|
2017-06-13 05:15:39 -04:00
|
|
|
res := sf.formatProgress("id", "action", jsonProgress, nil)
|
2017-05-01 14:54:56 -04:00
|
|
|
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 06:41:47 -04:00
|
|
|
}
|
|
|
|
|
2017-05-01 14:54:56 -04: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 06:41:47 -04:00
|
|
|
}
|
|
|
|
|
2017-05-01 14:54:56 -04: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 06:41:47 -04:00
|
|
|
}
|
|
|
|
|
2017-05-01 14:54:56 -04:00
|
|
|
func TestFormatJSONError(t *testing.T) {
|
2015-03-17 22:18:41 -04:00
|
|
|
err := &jsonmessage.JSONError{Code: 50, Message: "Json error"}
|
2017-05-01 14:54:56 -04:00
|
|
|
res := FormatError(err)
|
|
|
|
expected := `{"errorDetail":{"code":50,"message":"Json error"},"error":"Json error"}` + streamNewline
|
|
|
|
assert.Equal(t, expected, string(res))
|
2014-06-02 06:41:47 -04:00
|
|
|
}
|
|
|
|
|
2017-05-01 14:54:56 -04:00
|
|
|
func TestJsonProgressFormatterFormatProgress(t *testing.T) {
|
|
|
|
sf := &jsonProgressFormatter{}
|
2017-06-13 05:15:39 -04:00
|
|
|
jsonProgress := &jsonmessage.JSONProgress{
|
2014-06-02 06:41:47 -04:00
|
|
|
Current: 15,
|
|
|
|
Total: 30,
|
|
|
|
Start: 1,
|
|
|
|
}
|
2017-06-13 05:15:39 -04:00
|
|
|
res := sf.formatProgress("id", "action", jsonProgress, &AuxFormatter{Writer: &bytes.Buffer{}})
|
2015-03-17 22:18:41 -04:00
|
|
|
msg := &jsonmessage.JSONMessage{}
|
2017-06-13 05:15:39 -04:00
|
|
|
|
2017-05-01 14:54:56 -04:00
|
|
|
require.NoError(t, json.Unmarshal(res, msg))
|
|
|
|
assert.Equal(t, "id", msg.ID)
|
|
|
|
assert.Equal(t, "action", msg.Status)
|
2016-03-12 00:50:02 -05:00
|
|
|
|
2017-06-13 05:15:39 -04:00
|
|
|
// jsonProgress will always be in the format of:
|
2017-02-07 15:58:56 -05:00
|
|
|
// [=========================> ] 15B/30B 412910h51m30s
|
2016-03-12 00:50:02 -05:00
|
|
|
// The last entry '404933h7m11s' is the timeLeftBox.
|
2017-06-13 05:15:39 -04:00
|
|
|
// However, the timeLeftBox field may change as jsonProgress.String() depends on time.Now().
|
2016-03-12 00:50:02 -05:00
|
|
|
// Therefore, we have to strip the timeLeftBox from the strings to do the comparison.
|
|
|
|
|
2017-06-13 05:15:39 -04:00
|
|
|
// Compare the jsonProgress strings before the timeLeftBox
|
2017-02-07 15:58:56 -05:00
|
|
|
expectedProgress := "[=========================> ] 15B/30B"
|
2016-05-31 05:09:06 -04:00
|
|
|
// if terminal column is <= 110, expectedProgressShort is expected.
|
2017-02-07 15:58:56 -05:00
|
|
|
expectedProgressShort := " 15B/30B"
|
2016-05-31 05:09:06 -04: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 06:41:47 -04:00
|
|
|
}
|
2016-03-12 00:50:02 -05:00
|
|
|
|
2017-06-13 05:15:39 -04:00
|
|
|
assert.Equal(t, jsonProgress, msg.Progress)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestJsonProgressFormatterFormatStatus(t *testing.T) {
|
|
|
|
sf := jsonProgressFormatter{}
|
|
|
|
res := sf.formatStatus("ID", "%s%d", "a", 1)
|
|
|
|
assert.Equal(t, `{"status":"a1","id":"ID"}`+streamNewline, string(res))
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNewJSONProgressOutput(t *testing.T) {
|
|
|
|
b := bytes.Buffer{}
|
|
|
|
b.Write(FormatStatus("id", "Downloading"))
|
|
|
|
_ = NewJSONProgressOutput(&b, false)
|
|
|
|
assert.Equal(t, `{"status":"Downloading","id":"id"}`+streamNewline, b.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAuxFormatterEmit(t *testing.T) {
|
|
|
|
b := bytes.Buffer{}
|
|
|
|
aux := &AuxFormatter{Writer: &b}
|
|
|
|
sampleAux := &struct {
|
|
|
|
Data string
|
|
|
|
}{"Additional data"}
|
|
|
|
err := aux.Emit(sampleAux)
|
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, `{"aux":{"Data":"Additional data"}}`+streamNewline, b.String())
|
2014-06-02 06:41:47 -04:00
|
|
|
}
|