autogits/workflow-pr/pr_processor_test.go

221 lines
5.0 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) {
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
req := &RequestProcessor{
2024-11-08 16:08:53 +01:00
configuredRepos: testConfiguration,
2024-11-11 15:52:34 +01:00
git: &common.GitHandlerImpl{},
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",
},
},
}
2024-11-10 23:19:23 +01:00
t.Run("Open routine called for PR opening", 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 = "opened"
err := req.ProcessFunc(&common.Request{
Data: event,
})
if err != nil {
t.Error("Error processing open PR:", err)
t.Error(logBuf.String())
}
2024-11-08 16:08:53 +01:00
})
2024-11-11 15:52:34 +01:00
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())
}
})
2024-11-10 23:19:23 +01:00
t.Run("Close routine called for PR closing", func(t *testing.T) {
ctl := gomock.NewController(t)
closedMock := mock_main.NewMockPullRequestProcessor(ctl)
closedMock.EXPECT().Process(event, gomock.Any(), testConfiguration["test"][0]).Return(nil)
2024-11-08 16:08:53 +01:00
2024-11-10 23:19:23 +01:00
req.Closed = closedMock
event.Action = "closed"
2024-11-08 16:08:53 +01:00
2024-11-10 23:19:23 +01:00
err := req.ProcessFunc(&common.Request{
Data: event,
})
if err != nil {
2024-11-11 15:52:34 +01:00
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 {
2024-11-10 23:19:23 +01:00
t.Error(logBuf.String())
}
})
}