github.com/jiasir/docker@v1.3.3-0.20170609024000-252e610103e7/pkg/streamformatter/streamformatter_test.go (about) 1 package streamformatter 2 3 import ( 4 "encoding/json" 5 "errors" 6 "strings" 7 "testing" 8 9 "github.com/docker/docker/pkg/jsonmessage" 10 "github.com/stretchr/testify/assert" 11 "github.com/stretchr/testify/require" 12 ) 13 14 func TestRawProgressFormatterFormatStatus(t *testing.T) { 15 sf := rawProgressFormatter{} 16 res := sf.formatStatus("ID", "%s%d", "a", 1) 17 assert.Equal(t, "a1\r\n", string(res)) 18 } 19 20 func TestRawProgressFormatterFormatProgress(t *testing.T) { 21 sf := rawProgressFormatter{} 22 progress := &jsonmessage.JSONProgress{ 23 Current: 15, 24 Total: 30, 25 Start: 1, 26 } 27 res := sf.formatProgress("id", "action", progress, nil) 28 out := string(res) 29 assert.True(t, strings.HasPrefix(out, "action [====")) 30 assert.Contains(t, out, "15B/30B") 31 assert.True(t, strings.HasSuffix(out, "\r")) 32 } 33 34 func TestFormatStatus(t *testing.T) { 35 res := FormatStatus("ID", "%s%d", "a", 1) 36 expected := `{"status":"a1","id":"ID"}` + streamNewline 37 assert.Equal(t, expected, string(res)) 38 } 39 40 func TestFormatError(t *testing.T) { 41 res := FormatError(errors.New("Error for formatter")) 42 expected := `{"errorDetail":{"message":"Error for formatter"},"error":"Error for formatter"}` + "\r\n" 43 assert.Equal(t, expected, string(res)) 44 } 45 46 func TestFormatJSONError(t *testing.T) { 47 err := &jsonmessage.JSONError{Code: 50, Message: "Json error"} 48 res := FormatError(err) 49 expected := `{"errorDetail":{"code":50,"message":"Json error"},"error":"Json error"}` + streamNewline 50 assert.Equal(t, expected, string(res)) 51 } 52 53 func TestJsonProgressFormatterFormatProgress(t *testing.T) { 54 sf := &jsonProgressFormatter{} 55 progress := &jsonmessage.JSONProgress{ 56 Current: 15, 57 Total: 30, 58 Start: 1, 59 } 60 res := sf.formatProgress("id", "action", progress, nil) 61 msg := &jsonmessage.JSONMessage{} 62 require.NoError(t, json.Unmarshal(res, msg)) 63 assert.Equal(t, "id", msg.ID) 64 assert.Equal(t, "action", msg.Status) 65 66 // The progress will always be in the format of: 67 // [=========================> ] 15B/30B 412910h51m30s 68 // The last entry '404933h7m11s' is the timeLeftBox. 69 // However, the timeLeftBox field may change as progress.String() depends on time.Now(). 70 // Therefore, we have to strip the timeLeftBox from the strings to do the comparison. 71 72 // Compare the progress strings before the timeLeftBox 73 expectedProgress := "[=========================> ] 15B/30B" 74 // if terminal column is <= 110, expectedProgressShort is expected. 75 expectedProgressShort := " 15B/30B" 76 if !(strings.HasPrefix(msg.ProgressMessage, expectedProgress) || 77 strings.HasPrefix(msg.ProgressMessage, expectedProgressShort)) { 78 t.Fatalf("ProgressMessage without the timeLeftBox must be %s or %s, got: %s", 79 expectedProgress, expectedProgressShort, msg.ProgressMessage) 80 } 81 82 assert.Equal(t, progress, msg.Progress) 83 }