github.com/ttys3/engine@v17.12.1-ce-rc2+incompatible/pkg/streamformatter/streamformatter_test.go (about)

     1  package streamformatter
     2  
     3  import (
     4  	"bytes"
     5  	"encoding/json"
     6  	"errors"
     7  	"strings"
     8  	"testing"
     9  
    10  	"github.com/docker/docker/pkg/jsonmessage"
    11  	"github.com/stretchr/testify/assert"
    12  	"github.com/stretchr/testify/require"
    13  )
    14  
    15  func TestRawProgressFormatterFormatStatus(t *testing.T) {
    16  	sf := rawProgressFormatter{}
    17  	res := sf.formatStatus("ID", "%s%d", "a", 1)
    18  	assert.Equal(t, "a1\r\n", string(res))
    19  }
    20  
    21  func TestRawProgressFormatterFormatProgress(t *testing.T) {
    22  	sf := rawProgressFormatter{}
    23  	jsonProgress := &jsonmessage.JSONProgress{
    24  		Current: 15,
    25  		Total:   30,
    26  		Start:   1,
    27  	}
    28  	res := sf.formatProgress("id", "action", jsonProgress, nil)
    29  	out := string(res)
    30  	assert.True(t, strings.HasPrefix(out, "action [===="))
    31  	assert.Contains(t, out, "15B/30B")
    32  	assert.True(t, strings.HasSuffix(out, "\r"))
    33  }
    34  
    35  func TestFormatStatus(t *testing.T) {
    36  	res := FormatStatus("ID", "%s%d", "a", 1)
    37  	expected := `{"status":"a1","id":"ID"}` + streamNewline
    38  	assert.Equal(t, expected, string(res))
    39  }
    40  
    41  func TestFormatError(t *testing.T) {
    42  	res := FormatError(errors.New("Error for formatter"))
    43  	expected := `{"errorDetail":{"message":"Error for formatter"},"error":"Error for formatter"}` + "\r\n"
    44  	assert.Equal(t, expected, string(res))
    45  }
    46  
    47  func TestFormatJSONError(t *testing.T) {
    48  	err := &jsonmessage.JSONError{Code: 50, Message: "Json error"}
    49  	res := FormatError(err)
    50  	expected := `{"errorDetail":{"code":50,"message":"Json error"},"error":"Json error"}` + streamNewline
    51  	assert.Equal(t, expected, string(res))
    52  }
    53  
    54  func TestJsonProgressFormatterFormatProgress(t *testing.T) {
    55  	sf := &jsonProgressFormatter{}
    56  	jsonProgress := &jsonmessage.JSONProgress{
    57  		Current: 15,
    58  		Total:   30,
    59  		Start:   1,
    60  	}
    61  	res := sf.formatProgress("id", "action", jsonProgress, &AuxFormatter{Writer: &bytes.Buffer{}})
    62  	msg := &jsonmessage.JSONMessage{}
    63  
    64  	require.NoError(t, json.Unmarshal(res, msg))
    65  	assert.Equal(t, "id", msg.ID)
    66  	assert.Equal(t, "action", msg.Status)
    67  
    68  	// jsonProgress will always be in the format of:
    69  	// [=========================>                         ]      15B/30B 412910h51m30s
    70  	// The last entry '404933h7m11s' is the timeLeftBox.
    71  	// However, the timeLeftBox field may change as jsonProgress.String() depends on time.Now().
    72  	// Therefore, we have to strip the timeLeftBox from the strings to do the comparison.
    73  
    74  	// Compare the jsonProgress strings before the timeLeftBox
    75  	expectedProgress := "[=========================>                         ]      15B/30B"
    76  	// if terminal column is <= 110, expectedProgressShort is expected.
    77  	expectedProgressShort := "      15B/30B"
    78  	if !(strings.HasPrefix(msg.ProgressMessage, expectedProgress) ||
    79  		strings.HasPrefix(msg.ProgressMessage, expectedProgressShort)) {
    80  		t.Fatalf("ProgressMessage without the timeLeftBox must be %s or %s, got: %s",
    81  			expectedProgress, expectedProgressShort, msg.ProgressMessage)
    82  	}
    83  
    84  	assert.Equal(t, jsonProgress, msg.Progress)
    85  }
    86  
    87  func TestJsonProgressFormatterFormatStatus(t *testing.T) {
    88  	sf := jsonProgressFormatter{}
    89  	res := sf.formatStatus("ID", "%s%d", "a", 1)
    90  	assert.Equal(t, `{"status":"a1","id":"ID"}`+streamNewline, string(res))
    91  }
    92  
    93  func TestNewJSONProgressOutput(t *testing.T) {
    94  	b := bytes.Buffer{}
    95  	b.Write(FormatStatus("id", "Downloading"))
    96  	_ = NewJSONProgressOutput(&b, false)
    97  	assert.Equal(t, `{"status":"Downloading","id":"id"}`+streamNewline, b.String())
    98  }
    99  
   100  func TestAuxFormatterEmit(t *testing.T) {
   101  	b := bytes.Buffer{}
   102  	aux := &AuxFormatter{Writer: &b}
   103  	sampleAux := &struct {
   104  		Data string
   105  	}{"Additional data"}
   106  	err := aux.Emit(sampleAux)
   107  	require.NoError(t, err)
   108  	assert.Equal(t, `{"aux":{"Data":"Additional data"}}`+streamNewline, b.String())
   109  }