2015-03-17 22:18:41 -04:00
|
|
|
package streamformatter
|
2014-06-02 06:41:47 -04:00
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
|
|
|
"reflect"
|
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"
|
2014-06-02 06:41:47 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestFormatStream(t *testing.T) {
|
2015-05-12 14:18:54 -04:00
|
|
|
sf := NewStreamFormatter()
|
|
|
|
res := sf.FormatStream("stream")
|
|
|
|
if string(res) != "stream"+"\r" {
|
|
|
|
t.Fatalf("%q", res)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestFormatJSONStatus(t *testing.T) {
|
|
|
|
sf := NewStreamFormatter()
|
|
|
|
res := sf.FormatStatus("ID", "%s%d", "a", 1)
|
|
|
|
if string(res) != "a1\r\n" {
|
|
|
|
t.Fatalf("%q", res)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestFormatSimpleError(t *testing.T) {
|
|
|
|
sf := NewStreamFormatter()
|
|
|
|
res := sf.FormatError(errors.New("Error for formatter"))
|
|
|
|
if string(res) != "Error: Error for formatter\r\n" {
|
|
|
|
t.Fatalf("%q", res)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestJSONFormatStream(t *testing.T) {
|
|
|
|
sf := NewJSONStreamFormatter()
|
2014-06-02 06:41:47 -04:00
|
|
|
res := sf.FormatStream("stream")
|
|
|
|
if string(res) != `{"stream":"stream"}`+"\r\n" {
|
|
|
|
t.Fatalf("%q", res)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-12 14:18:54 -04:00
|
|
|
func TestJSONFormatStatus(t *testing.T) {
|
|
|
|
sf := NewJSONStreamFormatter()
|
2014-06-02 06:41:47 -04:00
|
|
|
res := sf.FormatStatus("ID", "%s%d", "a", 1)
|
|
|
|
if string(res) != `{"status":"a1","id":"ID"}`+"\r\n" {
|
|
|
|
t.Fatalf("%q", res)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-12 14:18:54 -04:00
|
|
|
func TestJSONFormatSimpleError(t *testing.T) {
|
|
|
|
sf := NewJSONStreamFormatter()
|
2014-06-02 06:41:47 -04:00
|
|
|
res := sf.FormatError(errors.New("Error for formatter"))
|
|
|
|
if string(res) != `{"errorDetail":{"message":"Error for formatter"},"error":"Error for formatter"}`+"\r\n" {
|
|
|
|
t.Fatalf("%q", res)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-12 14:18:54 -04:00
|
|
|
func TestJSONFormatJSONError(t *testing.T) {
|
|
|
|
sf := NewJSONStreamFormatter()
|
2015-03-17 22:18:41 -04:00
|
|
|
err := &jsonmessage.JSONError{Code: 50, Message: "Json error"}
|
2014-06-02 06:41:47 -04:00
|
|
|
res := sf.FormatError(err)
|
|
|
|
if string(res) != `{"errorDetail":{"code":50,"message":"Json error"},"error":"Json error"}`+"\r\n" {
|
|
|
|
t.Fatalf("%q", res)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-12 14:18:54 -04:00
|
|
|
func TestJSONFormatProgress(t *testing.T) {
|
|
|
|
sf := NewJSONStreamFormatter()
|
2015-03-17 22:18:41 -04:00
|
|
|
progress := &jsonmessage.JSONProgress{
|
2014-06-02 06:41:47 -04:00
|
|
|
Current: 15,
|
|
|
|
Total: 30,
|
|
|
|
Start: 1,
|
|
|
|
}
|
2015-12-21 18:02:44 -05:00
|
|
|
res := sf.FormatProgress("id", "action", progress, nil)
|
2015-03-17 22:18:41 -04:00
|
|
|
msg := &jsonmessage.JSONMessage{}
|
2014-06-02 06:41:47 -04:00
|
|
|
if err := json.Unmarshal(res, msg); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if msg.ID != "id" {
|
|
|
|
t.Fatalf("ID must be 'id', got: %s", msg.ID)
|
|
|
|
}
|
|
|
|
if msg.Status != "action" {
|
|
|
|
t.Fatalf("Status must be 'action', got: %s", msg.Status)
|
|
|
|
}
|
2016-03-12 00:50:02 -05:00
|
|
|
|
|
|
|
// The progress 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.
|
|
|
|
// 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 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
|
|
|
|
2014-06-02 06:41:47 -04:00
|
|
|
if !reflect.DeepEqual(msg.Progress, progress) {
|
|
|
|
t.Fatal("Original progress not equals progress from FormatProgress")
|
|
|
|
}
|
|
|
|
}
|