github.com/yankunsam/loki/v2@v2.6.3-0.20220817130409-389df5235c27/pkg/logcli/output/raw_test.go (about)

     1  package output
     2  
     3  import (
     4  	"bytes"
     5  	"testing"
     6  	"time"
     7  
     8  	"github.com/stretchr/testify/assert"
     9  
    10  	"github.com/grafana/loki/pkg/loghttp"
    11  )
    12  
    13  func TestRawOutput_Format(t *testing.T) {
    14  	t.Parallel()
    15  
    16  	timestamp, _ := time.Parse(time.RFC3339, "2006-01-02T15:04:05+07:00")
    17  	someLabels := loghttp.LabelSet(map[string]string{
    18  		"type": "test",
    19  	})
    20  
    21  	tests := map[string]struct {
    22  		options      *LogOutputOptions
    23  		timestamp    time.Time
    24  		lbls         loghttp.LabelSet
    25  		maxLabelsLen int
    26  		line         string
    27  		expected     string
    28  	}{
    29  		"empty line": {
    30  			&LogOutputOptions{Timezone: time.UTC, NoLabels: false},
    31  			timestamp,
    32  			someLabels,
    33  			0,
    34  			"",
    35  			"\n",
    36  		},
    37  		"non empty line": {
    38  			&LogOutputOptions{Timezone: time.UTC, NoLabels: false},
    39  			timestamp,
    40  			someLabels,
    41  			0,
    42  			"Hello world",
    43  			"Hello world\n",
    44  		},
    45  		"line with single newline at the end": {
    46  			&LogOutputOptions{Timezone: time.UTC, NoLabels: false},
    47  			timestamp,
    48  			someLabels,
    49  			0,
    50  			"Hello world\n",
    51  			"Hello world\n",
    52  		},
    53  		"line with multiple newlines at the end": {
    54  			&LogOutputOptions{Timezone: time.UTC, NoLabels: false},
    55  			timestamp,
    56  			someLabels,
    57  			0,
    58  			"Hello world\n\n\n",
    59  			"Hello world\n\n\n",
    60  		},
    61  	}
    62  
    63  	for testName, testData := range tests {
    64  		testData := testData
    65  
    66  		t.Run(testName, func(t *testing.T) {
    67  			t.Parallel()
    68  
    69  			writer := &bytes.Buffer{}
    70  			out := &RawOutput{writer, testData.options}
    71  			out.FormatAndPrintln(testData.timestamp, testData.lbls, testData.maxLabelsLen, testData.line)
    72  
    73  			assert.Equal(t, testData.expected, writer.String())
    74  		})
    75  	}
    76  }