github.com/yankunsam/loki/v2@v2.6.3-0.20220817130409-389df5235c27/cmd/querytee/response_comparator_test.go (about)

     1  package main
     2  
     3  import (
     4  	"encoding/json"
     5  	"errors"
     6  	"testing"
     7  
     8  	"github.com/stretchr/testify/require"
     9  
    10  	"github.com/grafana/loki/tools/querytee"
    11  )
    12  
    13  func TestCompareStreams(t *testing.T) {
    14  	for _, tc := range []struct {
    15  		name     string
    16  		expected json.RawMessage
    17  		actual   json.RawMessage
    18  		err      error
    19  	}{
    20  		{
    21  			name:     "no streams",
    22  			expected: json.RawMessage(`[]`),
    23  			actual:   json.RawMessage(`[]`),
    24  		},
    25  		{
    26  			name: "no streams in actual response",
    27  			expected: json.RawMessage(`[
    28  							{"stream":{"foo":"bar"},"values":[["1","1"]]}
    29  						]`),
    30  			actual: json.RawMessage(`[]`),
    31  			err:    errors.New("expected 1 streams but got 0"),
    32  		},
    33  		{
    34  			name: "extra stream in actual response",
    35  			expected: json.RawMessage(`[
    36  							{"stream":{"foo":"bar"},"values":[["1","1"]]}
    37  						]`),
    38  			actual: json.RawMessage(`[
    39  							{"stream":{"foo":"bar"},"values":[["1","1"]]},
    40  							{"stream":{"foo1":"bar1"},"values":[["1","1"]]}
    41  						]`),
    42  			err: errors.New("expected 1 streams but got 2"),
    43  		},
    44  		{
    45  			name: "same number of streams but with different labels",
    46  			expected: json.RawMessage(`[
    47  							{"stream":{"foo":"bar"},"values":[["1","1"]]}
    48  						]`),
    49  			actual: json.RawMessage(`[
    50  							{"stream":{"foo1":"bar1"},"values":[["1","1"]]}
    51  						]`),
    52  			err: errors.New("expected stream {foo=\"bar\"} missing from actual response"),
    53  		},
    54  		{
    55  			name: "difference in number of samples",
    56  			expected: json.RawMessage(`[
    57  							{"stream":{"foo":"bar"},"values":[["1","1"],["2","2"]]}
    58  						]`),
    59  			actual: json.RawMessage(`[
    60  							{"stream":{"foo":"bar"},"values":[["1","1"]]}
    61  						]`),
    62  			err: errors.New("expected 2 values for stream {foo=\"bar\"} but got 1"),
    63  		},
    64  		{
    65  			name: "difference in sample timestamp",
    66  			expected: json.RawMessage(`[
    67  							{"stream":{"foo":"bar"},"values":[["1","1"],["2","2"]]}
    68  						]`),
    69  			actual: json.RawMessage(`[
    70  							{"stream":{"foo":"bar"},"values":[["1","1"],["3","2"]]}
    71  						]`),
    72  			err: errors.New("expected timestamp 2 but got 3 for stream {foo=\"bar\"}"),
    73  		},
    74  		{
    75  			name: "difference in sample value",
    76  			expected: json.RawMessage(`[
    77  							{"stream":{"foo":"bar"},"values":[["1","1"],["2","2"]]}
    78  						]`),
    79  			actual: json.RawMessage(`[
    80  							{"stream":{"foo":"bar"},"values":[["1","1"],["2","3"]]}
    81  						]`),
    82  			err: errors.New("expected line 2 for timestamp 2 but got 3 for stream {foo=\"bar\"}"),
    83  		},
    84  		{
    85  			name: "correct samples",
    86  			expected: json.RawMessage(`[
    87  							{"stream":{"foo":"bar"},"values":[["1","1"],["2","2"]]}
    88  						]`),
    89  			actual: json.RawMessage(`[
    90  							{"stream":{"foo":"bar"},"values":[["1","1"],["2","2"]]}
    91  						]`),
    92  		},
    93  	} {
    94  		t.Run(tc.name, func(t *testing.T) {
    95  			err := compareStreams(tc.expected, tc.actual, querytee.SampleComparisonOptions{Tolerance: 0})
    96  			if tc.err == nil {
    97  				require.NoError(t, err)
    98  				return
    99  			}
   100  			require.Error(t, err)
   101  			require.Equal(t, tc.err.Error(), err.Error())
   102  		})
   103  	}
   104  }