github.com/google/syzkaller@v0.0.0-20251211124644-a066d2bc4b02/syz-cluster/pkg/triage/commit_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 triage
     5  
     6  import (
     7  	"fmt"
     8  	"testing"
     9  	"time"
    10  
    11  	"github.com/google/syzkaller/pkg/debugtracer"
    12  	"github.com/google/syzkaller/pkg/vcs"
    13  	"github.com/google/syzkaller/syz-cluster/pkg/api"
    14  	"github.com/stretchr/testify/assert"
    15  )
    16  
    17  func TestCommitSelector(t *testing.T) {
    18  	allApply := map[string]bool{"head": true, "build": true}
    19  	tests := []struct {
    20  		name   string
    21  		ops    TreeOps
    22  		series *api.Series
    23  		last   *api.Build
    24  		result SelectResult
    25  	}{
    26  		{
    27  			name:   "fresh series, no last build",
    28  			series: &api.Series{PublishedAt: date("2020-Jan-15")},
    29  			ops:    newTestGitOps(&vcs.Commit{Hash: "head", CommitDate: date("2020-Jan-10")}, allApply),
    30  			result: SelectResult{Commit: "head"},
    31  		},
    32  		{
    33  			name:   "fresh series with a fresh last build",
    34  			series: &api.Series{PublishedAt: date("2020-Jan-15")},
    35  			ops:    newTestGitOps(&vcs.Commit{Hash: "head", CommitDate: date("2020-Jan-10")}, allApply),
    36  			last:   &api.Build{CommitHash: "build", CommitDate: date("2020-Jan-06")},
    37  			result: SelectResult{Commit: "build"},
    38  		},
    39  		{
    40  			name:   "fresh series with a too old last build",
    41  			series: &api.Series{PublishedAt: date("2020-Jan-15")},
    42  			ops:    newTestGitOps(&vcs.Commit{Hash: "head", CommitDate: date("2020-Jan-10")}, allApply),
    43  			last:   &api.Build{CommitHash: "build", CommitDate: date("2019-Dec-20")},
    44  			result: SelectResult{Commit: "head"},
    45  		},
    46  		{
    47  			name:   "slightly old series, no last build",
    48  			series: &api.Series{PublishedAt: date("2020-Jan-15")},
    49  			ops:    newTestGitOps(&vcs.Commit{Hash: "head", CommitDate: date("2020-Jan-20")}, allApply),
    50  			result: SelectResult{Commit: "head"},
    51  		},
    52  		{
    53  			name:   "a too old series",
    54  			series: &api.Series{PublishedAt: date("2020-Jan-15")},
    55  			ops:    newTestGitOps(&vcs.Commit{Hash: "head", CommitDate: date("2020-Feb-15")}, allApply),
    56  			result: SelectResult{Reason: reasonSeriesTooOld},
    57  		},
    58  		{
    59  			name:   "doesn't apply to the known build",
    60  			series: &api.Series{PublishedAt: date("2020-Jan-15")},
    61  			ops: newTestGitOps(
    62  				&vcs.Commit{Hash: "head", CommitDate: date("2020-Jan-13")},
    63  				map[string]bool{"head": true, "build": false},
    64  			),
    65  			last:   &api.Build{CommitHash: "build", CommitDate: date("2020-Jan-10")},
    66  			result: SelectResult{Commit: "head"},
    67  		},
    68  		{
    69  			name:   "doesn't apply anywhere",
    70  			series: &api.Series{PublishedAt: date("2020-Jan-15")},
    71  			ops: newTestGitOps(
    72  				&vcs.Commit{Hash: "head", CommitDate: date("2020-Jan-13")},
    73  				nil,
    74  			),
    75  			last:   &api.Build{CommitHash: "build", CommitDate: date("2020-Jan-10")},
    76  			result: SelectResult{Reason: reasonNotApplies},
    77  		},
    78  	}
    79  
    80  	for _, test := range tests {
    81  		t.Run(test.name, func(t *testing.T) {
    82  			selector := NewCommitSelector(test.ops, &debugtracer.NullTracer{})
    83  			result, err := selector.Select(test.series, testTree, test.last)
    84  			assert.NoError(t, err)
    85  			assert.Equal(t, test.result, result)
    86  		})
    87  	}
    88  }
    89  
    90  func date(date string) time.Time {
    91  	t, err := time.Parse("2006-Jan-02", date)
    92  	if err != nil {
    93  		panic(err)
    94  	}
    95  	return t
    96  }
    97  
    98  var testTree = &api.Tree{} // all tests will use the same tree
    99  
   100  type testGitOps struct {
   101  	applies map[string]bool
   102  	head    map[*api.Tree]*vcs.Commit
   103  }
   104  
   105  func newTestGitOps(head *vcs.Commit, applies map[string]bool) *testGitOps {
   106  	return &testGitOps{
   107  		applies: applies,
   108  		head: map[*api.Tree]*vcs.Commit{
   109  			testTree: head,
   110  		},
   111  	}
   112  }
   113  
   114  func (ops *testGitOps) HeadCommit(tree *api.Tree) (*vcs.Commit, error) {
   115  	return ops.head[tree], nil
   116  }
   117  
   118  func (ops *testGitOps) ApplySeries(commit string, _ [][]byte) error {
   119  	if ops.applies[commit] {
   120  		return nil
   121  	}
   122  	return fmt.Errorf("didn't apply")
   123  }