github.com/google/syzkaller@v0.0.0-20240517125934-c0f1611a36d6/syz-ci/manager_test.go (about) 1 // Copyright 2023 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 main 5 6 import ( 7 "fmt" 8 "testing" 9 10 "github.com/google/syzkaller/dashboard/dashapi" 11 "github.com/google/syzkaller/pkg/vcs" 12 "github.com/google/syzkaller/sys/targets" 13 "github.com/stretchr/testify/assert" 14 "github.com/stretchr/testify/mock" 15 ) 16 17 type dashapiMock struct { 18 mock.Mock 19 } 20 21 func (dm *dashapiMock) BuilderPoll(manager string) (*dashapi.BuilderPollResp, error) { 22 args := dm.Called(manager) 23 return args.Get(0).(*dashapi.BuilderPollResp), args.Error(1) 24 } 25 26 // We don't care about the methods below for now. 27 func (dm *dashapiMock) ReportBuildError(req *dashapi.BuildErrorReq) error { return nil } 28 func (dm *dashapiMock) UploadBuild(build *dashapi.Build) error { return nil } 29 func (dm *dashapiMock) LogError(name, msg string, args ...interface{}) {} 30 func (dm *dashapiMock) CommitPoll() (*dashapi.CommitPollResp, error) { return nil, nil } 31 func (dm *dashapiMock) UploadCommits(commits []dashapi.Commit) error { return nil } 32 33 func TestManagerPollCommits(t *testing.T) { 34 // Mock a repository. 35 baseDir := t.TempDir() 36 repo := vcs.CreateTestRepo(t, baseDir, "") 37 var lastCommit *vcs.Commit 38 for _, title := range []string{ 39 "unrelated commit one", 40 "commit1 title", 41 "unrelated commit two", 42 "commit3 title", 43 `title with fix 44 45 Reported-by: foo+abcd000@bar.com`, 46 "unrelated commit three", 47 } { 48 lastCommit = repo.CommitChange(title) 49 } 50 51 vcsRepo, err := vcs.NewRepo(targets.TestOS, targets.TestArch64, baseDir, vcs.OptPrecious) 52 if err != nil { 53 t.Fatal(err) 54 } 55 56 mock := new(dashapiMock) 57 mgr := Manager{ 58 name: "test-manager", 59 dash: mock, 60 repo: vcsRepo, 61 mgrcfg: &ManagerConfig{}, 62 } 63 64 // Mock BuilderPoll(). 65 commits := []string{ 66 "commit1 title", 67 "commit2 title", 68 "commit3 title", 69 "commit4 title", 70 } 71 // Let's trigger sampling as well. 72 for i := 0; i < 100; i++ { 73 commits = append(commits, fmt.Sprintf("test%d", i)) 74 } 75 mock.On("BuilderPoll", "test-manager").Return(&dashapi.BuilderPollResp{ 76 PendingCommits: commits, 77 ReportEmail: "foo@bar.com", 78 }, nil) 79 80 matches, fixCommits, err := mgr.pollCommits(lastCommit.Hash) 81 if err != nil { 82 t.Fatal(err) 83 } 84 85 foundCommits := map[string]bool{} 86 // Call it several more times to catch all commits. 87 for i := 0; i < 100; i++ { 88 for _, name := range matches { 89 foundCommits[name] = true 90 } 91 matches, _, err = mgr.pollCommits(lastCommit.Hash) 92 if err != nil { 93 t.Fatal(err) 94 } 95 } 96 97 var foundCommitsSlice []string 98 for title := range foundCommits { 99 foundCommitsSlice = append(foundCommitsSlice, title) 100 } 101 assert.ElementsMatch(t, foundCommitsSlice, []string{ 102 "commit1 title", "commit3 title", 103 }) 104 assert.Len(t, fixCommits, 1) 105 commit := fixCommits[0] 106 assert.Equal(t, commit.Title, "title with fix") 107 assert.ElementsMatch(t, commit.BugIDs, []string{"abcd000"}) 108 }