github.com/google/syzkaller@v0.0.0-20251211124644-a066d2bc4b02/pkg/coveragedb/coveragedb_mock_test.go (about)

     1  // Copyright 2024 syzkaller project authors. All rights reserved.
     2  // Use of this source code is governed by Apache 2 LICENSE that can be found in the LICENSE file.
     3  
     4  package coveragedb
     5  
     6  import (
     7  	"encoding/json"
     8  	"io"
     9  	"strings"
    10  	"testing"
    11  	"time"
    12  
    13  	"cloud.google.com/go/spanner"
    14  	"github.com/google/syzkaller/pkg/coveragedb/mocks"
    15  	"github.com/stretchr/testify/assert"
    16  	"github.com/stretchr/testify/mock"
    17  )
    18  
    19  type spannerMockTune func(*testing.T, *mocks.SpannerClient)
    20  
    21  func TestSaveMergeResult(t *testing.T) {
    22  	tests := []struct {
    23  		name     string
    24  		jsonl    io.Reader
    25  		descr    *HistoryRecord
    26  		mockTune spannerMockTune
    27  		wantErr  bool
    28  		wantRows int
    29  	}{
    30  		{
    31  			name:    "empty jsonl",
    32  			jsonl:   strings.NewReader(`{}`),
    33  			wantErr: true,
    34  		},
    35  		{
    36  			name:    "wrong jsonl content",
    37  			jsonl:   strings.NewReader(`{a}`),
    38  			wantErr: true,
    39  		},
    40  		// nolint: dupl
    41  		{
    42  			name:     "1 MCR record, Ok",
    43  			jsonl:    strings.NewReader(`{"MCR":{"FileData":{}}}`),
    44  			descr:    &HistoryRecord{},
    45  			wantRows: 2, // 1 in files and 1 in merge_history
    46  			mockTune: func(t *testing.T, m *mocks.SpannerClient) {
    47  				m.
    48  					On("Apply", mock.Anything, mock.Anything).
    49  					Return(time.Now(), nil).
    50  					Once()
    51  			},
    52  		},
    53  		// nolint: dupl
    54  		{
    55  			name:     "1 FC record, Ok",
    56  			jsonl:    strings.NewReader(`{"FL":{}}`),
    57  			descr:    &HistoryRecord{},
    58  			wantRows: 2, // 1 in functions and 1 in merge_history
    59  			mockTune: func(t *testing.T, m *mocks.SpannerClient) {
    60  				m.
    61  					On("Apply", mock.Anything, mock.Anything).
    62  					Return(time.Now(), nil).
    63  					Once()
    64  			},
    65  		},
    66  		{
    67  			name: "2 records, Ok",
    68  			jsonl: strings.NewReader(`	{"MCR":{"FileData":{}}}
    69  																		{"MCR":{"FileData":{}}}`),
    70  			descr:    &HistoryRecord{},
    71  			wantRows: 3,
    72  			mockTune: func(t *testing.T, m *mocks.SpannerClient) {
    73  				m.
    74  					On("Apply",
    75  						mock.Anything,
    76  						mock.MatchedBy(func(ms []*spanner.Mutation) bool {
    77  							// 2 in files and 1 in merge_history
    78  							return len(ms) == 3
    79  						})).
    80  					Return(time.Now(), nil).
    81  					Once()
    82  			},
    83  		},
    84  		{
    85  			name:     "2k records, Ok",
    86  			jsonl:    strings.NewReader(strings.Repeat("{\"MCR\":{\"FileData\":{}}}\n", 2000)),
    87  			descr:    &HistoryRecord{},
    88  			wantRows: 2001,
    89  			mockTune: func(t *testing.T, m *mocks.SpannerClient) {
    90  				m.
    91  					On("Apply",
    92  						mock.Anything,
    93  						mock.MatchedBy(func(ms []*spanner.Mutation) bool {
    94  							// 2k in files
    95  							return len(ms) == 1000
    96  						})).
    97  					Return(time.Now(), nil).
    98  					Times(2).
    99  					On("Apply",
   100  						mock.Anything,
   101  						mock.MatchedBy(func(ms []*spanner.Mutation) bool {
   102  							// And 1 in merge_history.
   103  							return len(ms) == 1
   104  						})).
   105  					Return(time.Now(), nil).
   106  					Once()
   107  			},
   108  		},
   109  	}
   110  	for _, test := range tests {
   111  		t.Run(test.name, func(t *testing.T) {
   112  			t.Parallel()
   113  			spannerMock := mocks.NewSpannerClient(t)
   114  			if test.mockTune != nil {
   115  				test.mockTune(t, spannerMock)
   116  			}
   117  			gotRows, err := SaveMergeResult(t.Context(), spannerMock, test.descr, json.NewDecoder(test.jsonl))
   118  			if test.wantErr {
   119  				assert.Error(t, err)
   120  			} else {
   121  				assert.NoError(t, err)
   122  			}
   123  			assert.Equal(t, test.wantRows, gotRows)
   124  		})
   125  	}
   126  }