github.com/oyvindsk/docker@v1.5.0/utils/streamformatter_test.go (about) 1 package utils 2 3 import ( 4 "encoding/json" 5 "errors" 6 "reflect" 7 "testing" 8 ) 9 10 func TestFormatStream(t *testing.T) { 11 sf := NewStreamFormatter(true) 12 res := sf.FormatStream("stream") 13 if string(res) != `{"stream":"stream"}`+"\r\n" { 14 t.Fatalf("%q", res) 15 } 16 } 17 18 func TestFormatStatus(t *testing.T) { 19 sf := NewStreamFormatter(true) 20 res := sf.FormatStatus("ID", "%s%d", "a", 1) 21 if string(res) != `{"status":"a1","id":"ID"}`+"\r\n" { 22 t.Fatalf("%q", res) 23 } 24 } 25 26 func TestFormatSimpleError(t *testing.T) { 27 sf := NewStreamFormatter(true) 28 res := sf.FormatError(errors.New("Error for formatter")) 29 if string(res) != `{"errorDetail":{"message":"Error for formatter"},"error":"Error for formatter"}`+"\r\n" { 30 t.Fatalf("%q", res) 31 } 32 } 33 34 func TestFormatJSONError(t *testing.T) { 35 sf := NewStreamFormatter(true) 36 err := &JSONError{Code: 50, Message: "Json error"} 37 res := sf.FormatError(err) 38 if string(res) != `{"errorDetail":{"code":50,"message":"Json error"},"error":"Json error"}`+"\r\n" { 39 t.Fatalf("%q", res) 40 } 41 } 42 43 func TestFormatProgress(t *testing.T) { 44 sf := NewStreamFormatter(true) 45 progress := &JSONProgress{ 46 Current: 15, 47 Total: 30, 48 Start: 1, 49 } 50 res := sf.FormatProgress("id", "action", progress) 51 msg := &JSONMessage{} 52 if err := json.Unmarshal(res, msg); err != nil { 53 t.Fatal(err) 54 } 55 if msg.ID != "id" { 56 t.Fatalf("ID must be 'id', got: %s", msg.ID) 57 } 58 if msg.Status != "action" { 59 t.Fatalf("Status must be 'action', got: %s", msg.Status) 60 } 61 if msg.ProgressMessage != progress.String() { 62 t.Fatalf("ProgressMessage must be %s, got: %s", progress.String(), msg.ProgressMessage) 63 } 64 if !reflect.DeepEqual(msg.Progress, progress) { 65 t.Fatal("Original progress not equals progress from FormatProgress") 66 } 67 }