github.com/jingleWang/moby@v1.13.1/pkg/streamformatter/streamformatter_test.go (about) 1 package streamformatter 2 3 import ( 4 "encoding/json" 5 "errors" 6 "reflect" 7 "strings" 8 "testing" 9 10 "github.com/docker/docker/pkg/jsonmessage" 11 ) 12 13 func TestFormatStream(t *testing.T) { 14 sf := NewStreamFormatter() 15 res := sf.FormatStream("stream") 16 if string(res) != "stream"+"\r" { 17 t.Fatalf("%q", res) 18 } 19 } 20 21 func TestFormatJSONStatus(t *testing.T) { 22 sf := NewStreamFormatter() 23 res := sf.FormatStatus("ID", "%s%d", "a", 1) 24 if string(res) != "a1\r\n" { 25 t.Fatalf("%q", res) 26 } 27 } 28 29 func TestFormatSimpleError(t *testing.T) { 30 sf := NewStreamFormatter() 31 res := sf.FormatError(errors.New("Error for formatter")) 32 if string(res) != "Error: Error for formatter\r\n" { 33 t.Fatalf("%q", res) 34 } 35 } 36 37 func TestJSONFormatStream(t *testing.T) { 38 sf := NewJSONStreamFormatter() 39 res := sf.FormatStream("stream") 40 if string(res) != `{"stream":"stream"}`+"\r\n" { 41 t.Fatalf("%q", res) 42 } 43 } 44 45 func TestJSONFormatStatus(t *testing.T) { 46 sf := NewJSONStreamFormatter() 47 res := sf.FormatStatus("ID", "%s%d", "a", 1) 48 if string(res) != `{"status":"a1","id":"ID"}`+"\r\n" { 49 t.Fatalf("%q", res) 50 } 51 } 52 53 func TestJSONFormatSimpleError(t *testing.T) { 54 sf := NewJSONStreamFormatter() 55 res := sf.FormatError(errors.New("Error for formatter")) 56 if string(res) != `{"errorDetail":{"message":"Error for formatter"},"error":"Error for formatter"}`+"\r\n" { 57 t.Fatalf("%q", res) 58 } 59 } 60 61 func TestJSONFormatJSONError(t *testing.T) { 62 sf := NewJSONStreamFormatter() 63 err := &jsonmessage.JSONError{Code: 50, Message: "Json error"} 64 res := sf.FormatError(err) 65 if string(res) != `{"errorDetail":{"code":50,"message":"Json error"},"error":"Json error"}`+"\r\n" { 66 t.Fatalf("%q", res) 67 } 68 } 69 70 func TestJSONFormatProgress(t *testing.T) { 71 sf := NewJSONStreamFormatter() 72 progress := &jsonmessage.JSONProgress{ 73 Current: 15, 74 Total: 30, 75 Start: 1, 76 } 77 res := sf.FormatProgress("id", "action", progress, nil) 78 msg := &jsonmessage.JSONMessage{} 79 if err := json.Unmarshal(res, msg); err != nil { 80 t.Fatal(err) 81 } 82 if msg.ID != "id" { 83 t.Fatalf("ID must be 'id', got: %s", msg.ID) 84 } 85 if msg.Status != "action" { 86 t.Fatalf("Status must be 'action', got: %s", msg.Status) 87 } 88 89 // The progress will always be in the format of: 90 // [=========================> ] 15 B/30 B 404933h7m11s 91 // The last entry '404933h7m11s' is the timeLeftBox. 92 // However, the timeLeftBox field may change as progress.String() depends on time.Now(). 93 // Therefore, we have to strip the timeLeftBox from the strings to do the comparison. 94 95 // Compare the progress strings before the timeLeftBox 96 expectedProgress := "[=========================> ] 15 B/30 B" 97 // if terminal column is <= 110, expectedProgressShort is expected. 98 expectedProgressShort := " 15 B/30 B" 99 if !(strings.HasPrefix(msg.ProgressMessage, expectedProgress) || 100 strings.HasPrefix(msg.ProgressMessage, expectedProgressShort)) { 101 t.Fatalf("ProgressMessage without the timeLeftBox must be %s or %s, got: %s", 102 expectedProgress, expectedProgressShort, msg.ProgressMessage) 103 } 104 105 if !reflect.DeepEqual(msg.Progress, progress) { 106 t.Fatal("Original progress not equals progress from FormatProgress") 107 } 108 }