221 lines
5.0 KiB
Go
221 lines
5.0 KiB
Go
package main
|
|
|
|
import (
|
|
"bytes"
|
|
"fmt"
|
|
"log"
|
|
"testing"
|
|
|
|
"go.uber.org/mock/gomock"
|
|
"src.opensuse.org/autogits/common"
|
|
|
|
mock_common "src.opensuse.org/autogits/common/mock"
|
|
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)
|
|
testConfiguration["test"][0] = &common.AutogitConfig{
|
|
Branch: "branch",
|
|
}
|
|
|
|
req := &RequestProcessor{
|
|
configuredRepos: testConfiguration,
|
|
git: &common.GitHandlerImpl{},
|
|
}
|
|
|
|
event := &common.PullRequestWebhookEvent{
|
|
// Action: "opened",
|
|
Number: 1,
|
|
Pull_Request: &common.PullRequest{
|
|
Id: 1,
|
|
Base: common.Head{
|
|
Ref: "branch",
|
|
Repo: &common.Repository{
|
|
Name: "testRepo",
|
|
},
|
|
},
|
|
Head: common.Head{
|
|
Ref: "branch",
|
|
Repo: &common.Repository{
|
|
Name: "testRepo",
|
|
},
|
|
},
|
|
},
|
|
Repository: &common.Repository{
|
|
Owner: &common.Organization{
|
|
Username: "test",
|
|
},
|
|
},
|
|
}
|
|
|
|
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())
|
|
}
|
|
})
|
|
|
|
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)
|
|
closedMock.EXPECT().Process(event, gomock.Any(), testConfiguration["test"][0]).Return(nil)
|
|
|
|
req.Closed = closedMock
|
|
event.Action = "closed"
|
|
|
|
err := req.ProcessFunc(&common.Request{
|
|
Data: event,
|
|
})
|
|
|
|
if err != nil {
|
|
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())
|
|
}
|
|
})
|
|
}
|