autogits/workflow-pr/pr_processor_test.go

216 lines
4.8 KiB
Go
Raw Normal View History

2024-11-08 16:08:53 +01:00
package main
import (
"bytes"
2024-11-11 15:52:34 +01:00
"fmt"
2024-11-08 16:08:53 +01:00
"log"
"testing"
"go.uber.org/mock/gomock"
"src.opensuse.org/autogits/common"
2024-11-10 23:19:23 +01:00
2024-11-11 15:52:34 +01:00
mock_common "src.opensuse.org/autogits/common/mock"
2024-11-08 16:08:53 +01:00
mock_main "src.opensuse.org/workflow-pr/mock"
)
func TestPRProcessor(t *testing.T) {
2025-01-21 17:19:18 +01:00
tests := []struct {
title string
action string
req func(req *RequestProcessor, mock PullRequestProcessor)
}{
{
title: "Open routine called for PR opening",
action: "opened",
req: func(req *RequestProcessor, mock PullRequestProcessor) {
req.Opened = mock
},
},
{
title: "Re-Open routine called for PR reopening",
action: "reopened",
req: func(req *RequestProcessor, mock PullRequestProcessor) {
req.Opened = mock
},
},
{
title: "Sync routine called for PR sync requests",
action: "synchronized",
req: func(req *RequestProcessor, mock PullRequestProcessor) {
req.Synced = mock
},
},
{
title: "Close routine called for PR closing",
action: "closed",
req: func(req *RequestProcessor, mock PullRequestProcessor) {
req.Closed = mock
},
},
{
title: "Close routine called for PR closing",
action: "reviewed",
req: func(req *RequestProcessor, mock PullRequestProcessor) {
req.Review = mock
},
},
}
2024-11-08 16:08:53 +01:00
var logBuf bytes.Buffer
oldOut := log.Writer()
log.SetOutput(&logBuf)
defer log.SetOutput(oldOut)
testConfiguration := make(map[string][]*common.AutogitConfig)
testConfiguration["test"] = make([]*common.AutogitConfig, 1, 1)
2024-11-10 23:19:23 +01:00
testConfiguration["test"][0] = &common.AutogitConfig{
Branch: "branch",
2024-11-08 16:08:53 +01:00
}
2024-11-10 23:19:23 +01:00
event := &common.PullRequestWebhookEvent{
// Action: "opened",
2024-11-08 16:08:53 +01:00
Number: 1,
2024-11-10 23:19:23 +01:00
Pull_Request: &common.PullRequest{
2024-11-08 16:08:53 +01:00
Id: 1,
2024-11-10 23:19:23 +01:00
Base: common.Head{
Ref: "branch",
Repo: &common.Repository{
2024-11-08 16:08:53 +01:00
Name: "testRepo",
},
},
2024-11-10 23:19:23 +01:00
Head: common.Head{
Ref: "branch",
Repo: &common.Repository{
2024-11-08 16:08:53 +01:00
Name: "testRepo",
},
},
},
2024-11-10 23:19:23 +01:00
Repository: &common.Repository{
Owner: &common.Organization{
2024-11-08 16:08:53 +01:00
Username: "test",
},
},
}
2025-01-21 17:19:18 +01:00
for _, test := range tests {
t.Run(test.title, func(t *testing.T) {
ctl := gomock.NewController(t)
mock := mock_main.NewMockPullRequestProcessor(ctl)
mock.EXPECT().Process(event, gomock.Any(), testConfiguration["test"][0]).Return(nil)
2024-11-10 23:19:23 +01:00
2025-01-21 17:19:18 +01:00
req := &RequestProcessor{
configuredRepos: testConfiguration,
git: &common.GitHandlerGeneratorImpl{},
}
test.req(req, mock)
2024-11-10 23:19:23 +01:00
2025-01-21 17:19:18 +01:00
event.Action = test.action
2024-11-10 23:19:23 +01:00
2025-01-21 17:19:18 +01:00
err := req.ProcessFunc(&common.Request{
Data: event,
})
2024-11-08 16:08:53 +01:00
2025-01-21 17:19:18 +01:00
if err != nil {
t.Error("Error processing open PR:", err)
t.Error(logBuf.String())
}
2024-11-10 23:19:23 +01:00
})
2025-01-21 17:19:18 +01:00
}
2024-11-10 23:19:23 +01:00
2025-01-21 17:19:18 +01:00
req := &RequestProcessor{
configuredRepos: testConfiguration,
git: &common.GitHandlerGeneratorImpl{},
}
2024-11-11 15:52:34 +01:00
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 {
2024-11-10 23:19:23 +01:00
t.Error(logBuf.String())
}
})
}