streamformatter_test.go 2.7 KB

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