From dc96392b40ac36a5027c6b82db8ab9e99a06e27981c5089bc515cf1c3ebc6d6b Mon Sep 17 00:00:00 2001 From: Adam Majer Date: Mon, 11 Nov 2024 15:52:34 +0100 Subject: [PATCH] pr: more unit tests --- bots-common/git_utils.go | 10 ++- workflow-pr/main.go | 13 ++-- workflow-pr/pr.go | 3 +- workflow-pr/pr_opened.go | 12 +++- workflow-pr/pr_test.go | 134 +++++++++++++++++++++++++++++++++++++-- 5 files changed, 158 insertions(+), 14 deletions(-) diff --git a/bots-common/git_utils.go b/bots-common/git_utils.go index 654ef97..81678a3 100644 --- a/bots-common/git_utils.go +++ b/bots-common/git_utils.go @@ -30,6 +30,8 @@ import ( "sync" ) +//go:generate mockgen -source=git_utils.go -destination=mock/git_utils.go -typed + type GitHandler struct { DebugLogger bool @@ -38,7 +40,13 @@ type GitHandler struct { GitEmail string } -func CreateGitHandler(git_author, email, name string) (*GitHandler, error) { +type GitHandlerGenerator interface { + CreateGitHandler(git_author, email, name string) (*GitHandler, error); +} + +type GitHandlerImpl struct {} + +func (*GitHandlerImpl) CreateGitHandler(git_author, email, name string) (*GitHandler, error) { var err error git := new(GitHandler) diff --git a/workflow-pr/main.go b/workflow-pr/main.go index 704c3e3..2f495c0 100644 --- a/workflow-pr/main.go +++ b/workflow-pr/main.go @@ -110,7 +110,9 @@ nextSubmodule: event.Repository = common.RepositoryFromModel(pr.Base.Repo) event.Sender = *common.UserFromModel(pr.User) event.Requested_reviewer = nil - git, err := common.CreateGitHandler(GitAuthor, GitEmail, AppName) + + g := &common.GitHandlerImpl{} + git, err := g.CreateGitHandler(GitAuthor, GitEmail, AppName) if err != nil { return fmt.Errorf("Error allocating GitHandler. Err: %w", err) } @@ -178,7 +180,8 @@ func checkRepos(processor *RequestProcessor) { } log.Printf(" ++ starting verification, org: `%s` config: `%s`\n", org, config.GitProjectName) - git, err := common.CreateGitHandler(GitAuthor, GitEmail, AppName) + g := &common.GitHandlerImpl{} + git, err := g.CreateGitHandler(GitAuthor, GitEmail, AppName) if err != nil { log.Println("Faield to allocate GitHandler:", err) return @@ -257,13 +260,13 @@ func main() { } req.Synced = &PullRequestSynced{ - gitea: req.gitea, + gitea: req.gitea, } req.Opened = &PullRequestOpened{ - gitea: req.gitea, + gitea: req.gitea, } req.Closed = &PullRequestClosed{ - gitea: req.gitea, + gitea: req.gitea, } req.gitea = common.AllocateGiteaTransport(*giteaHost) diff --git a/workflow-pr/pr.go b/workflow-pr/pr.go index b9a1be1..4dd5c2f 100644 --- a/workflow-pr/pr.go +++ b/workflow-pr/pr.go @@ -17,6 +17,7 @@ type RequestProcessor struct { configuredRepos map[string][]*common.AutogitConfig gitea common.Gitea + git common.GitHandlerGenerator } func (w *RequestProcessor) ProcessFunc(request *common.Request) error { @@ -44,7 +45,7 @@ func (w *RequestProcessor) ProcessFunc(request *common.Request) error { return fmt.Errorf("Cannot find config for branch '%s'", req.Pull_Request.Base.Ref) } - git, err := common.CreateGitHandler(GitAuthor, GitEmail, AppName) + git, err := w.git.CreateGitHandler(GitAuthor, GitEmail, AppName) if err != nil { return fmt.Errorf("Error allocating GitHandler. Err: %w", err) } diff --git a/workflow-pr/pr_opened.go b/workflow-pr/pr_opened.go index 27ac1a0..3d0c8c9 100644 --- a/workflow-pr/pr_opened.go +++ b/workflow-pr/pr_opened.go @@ -23,8 +23,13 @@ func (o *PullRequestOpened) Process(req *common.PullRequestWebhookEvent, git *co This commit was autocreated by %s referencing -PullRequest: %s/%s#%d`, req.Repository.Owner.Username, - req.Repository.Name, GitAuthor, req.Repository.Name, req.Pull_Request.Number) +PullRequest: %s/%s#%d`, + req.Repository.Owner.Username, + req.Repository.Name, + GitAuthor, + req.Repository.Name, + req.Pull_Request.Number, + ) prjGit, err := o.gitea.CreateRepositoryIfNotExist(git, *req.Repository.Owner, config.GitProjectName) if err != nil { @@ -55,5 +60,6 @@ referencing the following pull request: return fmt.Errorf("Failed to create reviewer '%s' for request: %s/%s/%d Err: %w", reviewer, PR.Base.Repo.Owner.UserName, PR.Base.Repo.Name, PR.Index, err) } } - return err + + return nil } diff --git a/workflow-pr/pr_test.go b/workflow-pr/pr_test.go index 501ec75..1683559 100644 --- a/workflow-pr/pr_test.go +++ b/workflow-pr/pr_test.go @@ -2,16 +2,15 @@ package main import ( "bytes" + "fmt" "log" "testing" - // "go.uber.org/mock/gomock" "go.uber.org/mock/gomock" "src.opensuse.org/autogits/common" - // mock_common "src.opensuse.org/autogits/common/mock" + mock_common "src.opensuse.org/autogits/common/mock" mock_main "src.opensuse.org/workflow-pr/mock" - // "src.opensuse.org/autogits/common/mock" ) func TestPRProcessor(t *testing.T) { @@ -29,6 +28,7 @@ func TestPRProcessor(t *testing.T) { req := &RequestProcessor{ configuredRepos: testConfiguration, + git: &common.GitHandlerImpl{}, } event := &common.PullRequestWebhookEvent{ @@ -74,6 +74,42 @@ func TestPRProcessor(t *testing.T) { } }) + t.Run("Re-Open routine called for PR reopening", func(t *testing.T) { + ctl := gomock.NewController(t) + openedMock := mock_main.NewMockPullRequestProcessor(ctl) + openedMock.EXPECT().Process(event, gomock.Any(), testConfiguration["test"][0]).Return(nil) + + req.Opened = openedMock + event.Action = "reopened" + + err := req.ProcessFunc(&common.Request{ + Data: event, + }) + + if err != nil { + t.Error("Error processing open PR:", err) + t.Error(logBuf.String()) + } + }) + + t.Run("Sync routine called for PR sync requests", func(t *testing.T) { + ctl := gomock.NewController(t) + syncMock := mock_main.NewMockPullRequestProcessor(ctl) + syncMock.EXPECT().Process(event, gomock.Any(), testConfiguration["test"][0]).Return(nil) + + req.Synced = syncMock + event.Action = "synchronized" + + err := req.ProcessFunc(&common.Request{ + Data: event, + }) + + if err != nil { + t.Error("Error processing sync PR:", err) + t.Error(logBuf.String()) + } + }) + t.Run("Close routine called for PR closing", func(t *testing.T) { ctl := gomock.NewController(t) closedMock := mock_main.NewMockPullRequestProcessor(ctl) @@ -87,7 +123,97 @@ func TestPRProcessor(t *testing.T) { }) if err != nil { - t.Error("Error processing open PR:", err) + t.Error("Error processing close PR:", err) + t.Error(logBuf.String()) + } + }) + + t.Run("Edit PR handling", func(t *testing.T) { + /* ctl := gomock.NewController(t) + closedMock := mock_main.NewMockPullRequestProcessor(ctl) + closedMock.EXPECT().Process(event, gomock.Any(), testConfiguration["test"][0]).Return(nil) + */ + + // req.Closed = closedMock + event.Action = "edited" + + err := req.ProcessFunc(&common.Request{ + Data: event, + }) + + if err != nil { + t.Error("Error processing edit PR:", err) + t.Error(logBuf.String()) + } + }) + + t.Run("Unknown PR-type handling", func(t *testing.T) { + event.Action = "not existing action" + + err := req.ProcessFunc(&common.Request{ + Data: event, + }) + + if err == nil { + t.Error(logBuf.String()) + } + }) + + t.Run("Missing branch in config present in PR", func(t *testing.T) { + baseRef := event.Pull_Request.Base.Ref + event.Pull_Request.Base.Ref = "not present" + err := req.ProcessFunc(&common.Request{ + Data: event, + }) + event.Pull_Request.Base.Ref = baseRef + + if err == nil { + t.Error(logBuf.String()) + } + }) + + t.Run("Invalid data present in PR", func(t *testing.T) { + baseConfig := req.configuredRepos + req.configuredRepos = make(map[string][]*common.AutogitConfig) + err := req.ProcessFunc(&common.Request{ + Data: nil, + }) + req.configuredRepos = baseConfig + + if err == nil { + t.Error(logBuf.String()) + } + }) + + t.Run("Ignoring requests against unconfigured repos", func(t *testing.T) { + baseConfig := req.configuredRepos + req.configuredRepos = make(map[string][]*common.AutogitConfig) + err := req.ProcessFunc(&common.Request{ + Data: event, + }) + req.configuredRepos = baseConfig + + if err != nil { + t.Error(logBuf.String()) + } + }) + + t.Run("Failures of git handler creation", func(t *testing.T) { + ctl := gomock.NewController(t) + gitHandler := mock_common.NewMockGitHandlerGenerator(ctl) + + gitHandler.EXPECT().CreateGitHandler(gomock.Any(), gomock.Any(), gomock.Any()).Return(nil, fmt.Errorf("some error")) + + origHandler := req.git + req.git = gitHandler + + err := req.ProcessFunc(&common.Request{ + Data: event, + }) + + req.git = origHandler + + if err == nil { t.Error(logBuf.String()) } })