streamformatter_test.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. package streamformatter // import "github.com/docker/docker/pkg/streamformatter"
  2. import (
  3. "bytes"
  4. "encoding/json"
  5. "errors"
  6. "strings"
  7. "testing"
  8. "github.com/docker/docker/pkg/jsonmessage"
  9. "github.com/google/go-cmp/cmp"
  10. "github.com/google/go-cmp/cmp/cmpopts"
  11. "gotest.tools/v3/assert"
  12. is "gotest.tools/v3/assert/cmp"
  13. )
  14. func TestRawProgressFormatterFormatStatus(t *testing.T) {
  15. sf := rawProgressFormatter{}
  16. res := sf.formatStatus("ID", "%s%d", "a", 1)
  17. assert.Check(t, is.Equal("a1\r\n", string(res)))
  18. }
  19. func TestRawProgressFormatterFormatProgress(t *testing.T) {
  20. sf := rawProgressFormatter{}
  21. jsonProgress := &jsonmessage.JSONProgress{
  22. Current: 15,
  23. Total: 30,
  24. Start: 1,
  25. }
  26. res := sf.formatProgress("id", "action", jsonProgress, nil)
  27. out := string(res)
  28. assert.Check(t, strings.HasPrefix(out, "action [===="))
  29. assert.Check(t, is.Contains(out, "15B/30B"))
  30. assert.Check(t, strings.HasSuffix(out, "\r"))
  31. }
  32. func TestFormatStatus(t *testing.T) {
  33. res := FormatStatus("ID", "%s%d", "a", 1)
  34. expected := `{"status":"a1","id":"ID"}` + streamNewline
  35. assert.Check(t, is.Equal(expected, string(res)))
  36. }
  37. func TestFormatError(t *testing.T) {
  38. res := FormatError(errors.New("Error for formatter"))
  39. expected := `{"errorDetail":{"message":"Error for formatter"},"error":"Error for formatter"}` + "\r\n"
  40. assert.Check(t, is.Equal(expected, string(res)))
  41. }
  42. func TestFormatJSONError(t *testing.T) {
  43. err := &jsonmessage.JSONError{Code: 50, Message: "Json error"}
  44. res := FormatError(err)
  45. expected := `{"errorDetail":{"code":50,"message":"Json error"},"error":"Json error"}` + streamNewline
  46. assert.Check(t, is.Equal(expected, string(res)))
  47. }
  48. func TestJsonProgressFormatterFormatProgress(t *testing.T) {
  49. sf := &jsonProgressFormatter{}
  50. jsonProgress := &jsonmessage.JSONProgress{
  51. Current: 15,
  52. Total: 30,
  53. Start: 1,
  54. }
  55. aux := "aux message"
  56. res := sf.formatProgress("id", "action", jsonProgress, aux)
  57. msg := &jsonmessage.JSONMessage{}
  58. assert.NilError(t, json.Unmarshal(res, msg))
  59. rawAux := json.RawMessage(`"` + aux + `"`)
  60. expected := &jsonmessage.JSONMessage{
  61. ID: "id",
  62. Status: "action",
  63. Aux: &rawAux,
  64. Progress: jsonProgress,
  65. }
  66. assert.DeepEqual(t, msg, expected, cmpJSONMessageOpt())
  67. }
  68. func cmpJSONMessageOpt() cmp.Option {
  69. progressMessagePath := func(path cmp.Path) bool {
  70. return path.String() == "ProgressMessage"
  71. }
  72. return cmp.Options{
  73. cmpopts.IgnoreUnexported(jsonmessage.JSONProgress{}),
  74. // Ignore deprecated property that is a derivative of Progress
  75. cmp.FilterPath(progressMessagePath, cmp.Ignore()),
  76. }
  77. }
  78. func TestJsonProgressFormatterFormatStatus(t *testing.T) {
  79. sf := jsonProgressFormatter{}
  80. res := sf.formatStatus("ID", "%s%d", "a", 1)
  81. assert.Check(t, is.Equal(`{"status":"a1","id":"ID"}`+streamNewline, string(res)))
  82. }
  83. func TestNewJSONProgressOutput(t *testing.T) {
  84. b := bytes.Buffer{}
  85. b.Write(FormatStatus("id", "Downloading"))
  86. _ = NewJSONProgressOutput(&b, false)
  87. assert.Check(t, is.Equal(`{"status":"Downloading","id":"id"}`+streamNewline, b.String()))
  88. }
  89. func TestAuxFormatterEmit(t *testing.T) {
  90. b := bytes.Buffer{}
  91. aux := &AuxFormatter{Writer: &b}
  92. sampleAux := &struct {
  93. Data string
  94. }{"Additional data"}
  95. err := aux.Emit("", sampleAux)
  96. assert.NilError(t, err)
  97. assert.Check(t, is.Equal(`{"aux":{"Data":"Additional data"}}`+streamNewline, b.String()))
  98. }