github.com/yankunsam/loki/v2@v2.6.3-0.20220817130409-389df5235c27/pkg/util/unmarshal/legacy/unmarshal_test.go (about)

     1  package unmarshal
     2  
     3  import (
     4  	"io/ioutil"
     5  	"log"
     6  	"strings"
     7  	"testing"
     8  	"time"
     9  
    10  	"github.com/stretchr/testify/require"
    11  
    12  	"github.com/grafana/loki/pkg/logproto"
    13  )
    14  
    15  // covers requests to /api/prom/push
    16  var pushTests = []struct {
    17  	expected []logproto.Stream
    18  	actual   string
    19  }{
    20  	{
    21  		[]logproto.Stream{
    22  			{
    23  				Entries: []logproto.Entry{
    24  					{
    25  						Timestamp: mustParse(time.RFC3339Nano, "2019-09-13T18:32:22.380001319Z"),
    26  						Line:      "super line",
    27  					},
    28  				},
    29  				Labels: `{test="test"}`,
    30  			},
    31  		},
    32  		`{
    33  			"streams":[
    34  				{
    35  					"labels":"{test=\"test\"}",
    36  					"entries":[
    37  						{
    38  							"ts": "2019-09-13T18:32:22.380001319Z",
    39  							"line": "super line"
    40  						}
    41  					]
    42  				}
    43  			]
    44  		}`,
    45  	},
    46  }
    47  
    48  func Test_DecodePushRequest(t *testing.T) {
    49  
    50  	for i, pushTest := range pushTests {
    51  		var actual logproto.PushRequest
    52  		closer := ioutil.NopCloser(strings.NewReader(pushTest.actual))
    53  
    54  		err := DecodePushRequest(closer, &actual)
    55  		require.NoError(t, err)
    56  
    57  		require.Equalf(t, pushTest.expected, actual.Streams, "Push Test %d failed", i)
    58  	}
    59  }
    60  
    61  func mustParse(l string, t string) time.Time {
    62  	ret, err := time.Parse(l, t)
    63  	if err != nil {
    64  		log.Fatalf("Failed to parse %s", t)
    65  	}
    66  
    67  	return ret
    68  }