autogits/workflow-pr/maintainership_test.go

315 lines
9.0 KiB
Go
Raw Normal View History

2024-11-27 17:50:55 +01:00
package main
import (
"errors"
"testing"
"go.uber.org/mock/gomock"
"src.opensuse.org/autogits/common"
2024-11-29 17:33:01 +01:00
"src.opensuse.org/autogits/common/gitea-generated/models"
2024-12-09 00:39:55 +01:00
mock_common "src.opensuse.org/autogits/common/mock"
2024-11-27 17:50:55 +01:00
mock_main "src.opensuse.org/workflow-pr/mock"
)
func TestMaintainership(t *testing.T) {
2024-12-09 00:39:55 +01:00
allocateMaintainershipInterface := func(t *testing.T) (*mock_common.MockGiteaMaintainershipInterface, *mock_main.MockGiteaPRInterface) {
2024-11-28 17:10:26 +01:00
ctl := gomock.NewController(t)
2024-12-09 00:39:55 +01:00
mi := mock_common.NewMockGiteaMaintainershipInterface(ctl)
pri := mock_main.NewMockGiteaPRInterface(ctl)
2024-11-28 00:15:32 +01:00
2024-12-09 00:39:55 +01:00
return mi, pri
2024-11-28 17:10:26 +01:00
}
2024-11-28 00:15:32 +01:00
2024-11-29 17:33:01 +01:00
config := common.AutogitConfig{
Branch: "bar",
Organization: "foo",
GitProjectName: common.DefaultGitPrj,
}
2024-12-09 18:20:56 +01:00
packageTests := []struct {
name string
}{
{
name: "No maintainer in empty package",
},
}
2024-11-27 17:50:55 +01:00
2024-12-09 18:20:56 +01:00
for _, test := range packageTests {
t.Run(test.name, func(t *testing.T) {
mi, _ := allocateMaintainershipInterface(t)
mi.EXPECT().FetchMaintainershipFile("foo", common.DefaultGitPrj, "bar").Return(nil, nil)
maintainers, err := ProjectMaintainershipData(mi, config.Organization, config.GitProjectName, config.Branch)
if err != nil {
t.Error("Invalid number of maintainers for package", err)
}
2024-11-27 17:50:55 +01:00
2024-12-09 18:20:56 +01:00
m := MaintainerListForPackage(maintainers, "bar")
if len(m) != 0 {
t.Error("Invalid number of maintainers for package", err)
}
})
}
2024-11-27 17:50:55 +01:00
2024-12-09 18:20:56 +01:00
projectTests := []struct {
name string
maintainersFile []byte
}{
{
name: "No maintainer for empty project",
},
{
name: "No maintainer for empty project maintainer file",
maintainersFile: []byte("{}"),
},
}
2024-11-28 00:15:32 +01:00
2024-12-09 18:20:56 +01:00
for _, test := range projectTests {
t.Run(test.name, func(t *testing.T) {
mi, _ := allocateMaintainershipInterface(t)
mi.EXPECT().FetchMaintainershipFile("foo", common.DefaultGitPrj, "bar").Return(test.maintainersFile, nil)
maintainers, err := ProjectMaintainershipData(mi, config.Organization, config.GitProjectName, config.Branch)
if err != nil {
t.Error("Invalid number of maintainers for package", err)
}
2024-11-28 00:15:32 +01:00
2024-12-09 18:20:56 +01:00
m := MaintainerListForProject(maintainers)
if len(m) != 0 {
t.Error("Invalid number of maintainers for project", err)
}
})
}
2024-11-27 17:50:55 +01:00
t.Run("Error in MaintainerListForPackage when remote has an error", func(t *testing.T) {
2024-12-09 00:39:55 +01:00
mi, _ := allocateMaintainershipInterface(t)
2024-11-27 17:50:55 +01:00
err := errors.New("some error here")
mi.EXPECT().FetchMaintainershipFile("foo", common.DefaultGitPrj, "bar").Return(nil, err)
2024-11-27 17:50:55 +01:00
2024-11-29 17:33:01 +01:00
_, errRet := ProjectMaintainershipData(mi, config.Organization, config.GitProjectName, config.Branch)
2024-11-27 17:50:55 +01:00
switch errRet {
case nil:
t.Error("Should have returned an error")
case err:
break
default:
t.Error("Unexpected error received", err)
}
})
2024-11-28 17:10:26 +01:00
2024-11-27 17:50:55 +01:00
t.Run("Error in MaintainerListForProject when remote has an error", func(t *testing.T) {
2024-12-09 00:39:55 +01:00
mi, _ := allocateMaintainershipInterface(t)
2024-11-27 17:50:55 +01:00
err := errors.New("some error here")
mi.EXPECT().FetchMaintainershipFile("foo", common.DefaultGitPrj, "bar").Return(nil, err)
2024-11-29 17:33:01 +01:00
_, errRet := ProjectMaintainershipData(mi, config.Organization, config.GitProjectName, config.Branch)
2024-11-27 17:50:55 +01:00
switch errRet {
case nil:
t.Error("Should have returned an error")
case err:
break
default:
t.Error("Unexpected error received", err)
}
})
2024-11-28 00:15:32 +01:00
t.Run("Multiple project maintainers", func(t *testing.T) {
2024-12-09 00:39:55 +01:00
mi, _ := allocateMaintainershipInterface(t)
2024-11-27 17:50:55 +01:00
2024-11-28 17:10:26 +01:00
mi.EXPECT().FetchMaintainershipFile("foo", common.DefaultGitPrj, "bar").Return([]byte(`
{
"": ["user1", "user2"]
2024-11-28 17:10:26 +01:00
}
`), nil)
2024-11-27 17:50:55 +01:00
2024-11-29 17:33:01 +01:00
maintainers, err := ProjectMaintainershipData(mi, config.Organization, config.GitProjectName, config.Branch)
if err != nil {
t.Error("Invalid number of maintainers for project", err)
}
m := MaintainerListForProject(maintainers)
if len(m) != 2 {
2024-11-27 17:50:55 +01:00
t.Error("Invalid number of maintainers for project", err)
}
2024-11-28 00:15:32 +01:00
if m[0] != "user1" || m[1] != "user2" {
t.Error("Can't find expected users. Found", m)
}
})
t.Run("Single project maintainer", func(t *testing.T) {
2024-12-09 00:39:55 +01:00
mi, _ := allocateMaintainershipInterface(t)
2024-11-28 00:15:32 +01:00
2024-11-28 17:10:26 +01:00
mi.EXPECT().FetchMaintainershipFile("foo", common.DefaultGitPrj, "bar").Return([]byte(`
{
"": ["user"]
2024-11-28 17:10:26 +01:00
}
`), nil)
2024-11-28 00:15:32 +01:00
2024-11-29 17:33:01 +01:00
maintainers, err := ProjectMaintainershipData(mi, config.Organization, config.GitProjectName, config.Branch)
if err != nil {
t.Error("Invalid number of maintainers for project", err)
}
m := MaintainerListForProject(maintainers)
if len(m) != 1 {
2024-11-28 00:15:32 +01:00
t.Error("Invalid number of maintainers for project", err)
}
if m[0] != "user" {
t.Error("Can't find expected users. Found", m)
}
})
t.Run("Invalid list of project maintainers", func(t *testing.T) {
2024-12-09 00:39:55 +01:00
mi, _ := allocateMaintainershipInterface(t)
2024-11-28 00:15:32 +01:00
2024-11-28 17:10:26 +01:00
mi.EXPECT().FetchMaintainershipFile("foo", common.DefaultGitPrj, "bar").Return([]byte(`
{
"": ["user", 4]
2024-11-28 17:10:26 +01:00
}
`), nil)
2024-11-28 00:15:32 +01:00
2024-11-29 17:33:01 +01:00
_, err := ProjectMaintainershipData(mi, config.Organization, config.GitProjectName, config.Branch)
if err == nil {
2024-11-28 00:15:32 +01:00
t.Error("Invalid number of maintainers for project", err)
}
})
t.Run("Invalid list of project maintainers", func(t *testing.T) {
2024-12-09 00:39:55 +01:00
mi, _ := allocateMaintainershipInterface(t)
2024-11-28 00:15:32 +01:00
2024-11-28 17:10:26 +01:00
mi.EXPECT().FetchMaintainershipFile("foo", common.DefaultGitPrj, "bar").Return([]byte(`
{
"": 4
2024-11-28 17:10:26 +01:00
}
`), nil)
2024-11-28 00:15:32 +01:00
2024-11-29 17:33:01 +01:00
_, err := ProjectMaintainershipData(mi, config.Organization, config.GitProjectName, config.Branch)
2024-11-28 17:10:26 +01:00
if err == nil {
2024-11-28 00:15:32 +01:00
t.Error("Invalid number of maintainers for project", err)
}
2024-11-27 17:50:55 +01:00
})
2024-11-28 17:10:26 +01:00
t.Run("Multiple package maintainers", func(t *testing.T) {
2024-12-09 00:39:55 +01:00
mi, _ := allocateMaintainershipInterface(t)
2024-11-28 17:10:26 +01:00
mi.EXPECT().FetchMaintainershipFile("foo", common.DefaultGitPrj, "bar").Return([]byte(`
2024-11-28 17:10:26 +01:00
{
"pkg": ["user1", "user2"],
"": ["user1", "user3"]
2024-11-28 17:10:26 +01:00
}
`), nil)
2024-11-29 17:33:01 +01:00
maintainers, err := ProjectMaintainershipData(mi, config.Organization, config.GitProjectName, config.Branch)
if err != nil {
t.Error("Invalid number of maintainers for package")
}
m := MaintainerListForPackage(maintainers, "pkg")
if len(m) != 3 {
2024-11-28 17:10:26 +01:00
t.Error("Invalid number of maintainers for package", m)
}
if m[0] != "user1" || m[1] != "user2" || m[2] != "user3" {
t.Error("Can't find expected users. Found", m)
}
})
t.Run("No package maintainers and only project maintainer", func(t *testing.T) {
2024-12-09 00:39:55 +01:00
mi, _ := allocateMaintainershipInterface(t)
2024-11-28 17:10:26 +01:00
mi.EXPECT().FetchMaintainershipFile("foo", common.DefaultGitPrj, "bar").Return([]byte(`
2024-11-28 17:10:26 +01:00
{
"pkg": ["user1", "user2"],
"": ["user1", "user3"]
2024-11-28 17:10:26 +01:00
}
`), nil)
2024-11-29 17:33:01 +01:00
maintainers, err := ProjectMaintainershipData(mi, config.Organization, config.GitProjectName, config.Branch)
if err != nil {
t.Error("Invalid number of maintainers for package", maintainers)
}
m := MaintainerListForPackage(maintainers, "bar")
if len(m) != 2 {
2024-11-28 17:10:26 +01:00
t.Error("Invalid number of maintainers for package", m)
}
if m[0] != "user1" || m[1] != "user3" {
t.Error("Can't find expected users. Found", m)
}
})
t.Run("Invalid list of package maintainers", func(t *testing.T) {
2024-12-09 00:39:55 +01:00
mi, _ := allocateMaintainershipInterface(t)
2024-11-28 17:10:26 +01:00
mi.EXPECT().FetchMaintainershipFile("foo", common.DefaultGitPrj, "bar").Return([]byte(`
2024-11-28 17:10:26 +01:00
{
"pkg": 3,
"": ["user", 4]
2024-11-28 17:10:26 +01:00
}
`), nil)
2024-11-29 17:33:01 +01:00
_, err := ProjectMaintainershipData(mi, config.Organization, config.GitProjectName, config.Branch)
if err == nil {
2024-11-28 17:10:26 +01:00
t.Error("Invalid number of maintainers for project", err)
}
})
2024-12-09 18:20:56 +01:00
}
func TestReviewApproval(t *testing.T) {
allocateMaintainershipInterface := func(t *testing.T) (*mock_common.MockGiteaMaintainershipInterface, *mock_main.MockGiteaPRInterface) {
ctl := gomock.NewController(t)
mi := mock_common.NewMockGiteaMaintainershipInterface(ctl)
pri := mock_main.NewMockGiteaPRInterface(ctl)
return mi, pri
}
config := common.AutogitConfig{
Branch: "bar",
Organization: "foo",
GitProjectName: common.DefaultGitPrj,
}
2024-11-28 17:10:26 +01:00
2024-11-29 17:33:01 +01:00
t.Run("Maintainers not appoved", func(t *testing.T) {
2024-12-09 00:39:55 +01:00
mi, pri := allocateMaintainershipInterface(t)
2024-11-29 17:33:01 +01:00
2024-12-09 00:39:55 +01:00
pri.EXPECT().GetPullRequestAndReviews("foo", common.DefaultGitPrj, int64(10)).Return(
2024-11-29 17:33:01 +01:00
&models.PullRequest{
2024-12-09 00:39:55 +01:00
Body: "",
2024-11-29 17:33:01 +01:00
Index: 10,
RequestedReviewers: []*models.User{},
},
[]*models.PullReview{},
nil,
)
2024-12-09 00:39:55 +01:00
mi.EXPECT().FetchMaintainershipFile("foo", common.DefaultGitPrj, "bar").Return([]byte(`{"foo": ["bingo"]}`), nil)
2024-11-29 17:33:01 +01:00
2024-12-09 18:20:56 +01:00
approved, err := IsPrjGitPRApproved(mi, pri, config, 10)
2024-11-29 17:33:01 +01:00
if approved || err != nil {
t.Error("Unexpected approved or err:", err)
}
})
t.Run("Maintainers approved", func(t *testing.T) {
2024-12-09 00:39:55 +01:00
mi, pri := allocateMaintainershipInterface(t)
2024-11-29 17:33:01 +01:00
2024-12-09 00:39:55 +01:00
pri.EXPECT().GetPullRequestAndReviews("foo", common.DefaultGitPrj, int64(10)).Return(
2024-11-29 17:33:01 +01:00
&models.PullRequest{
2024-12-09 00:39:55 +01:00
Body: "",
2024-11-29 17:33:01 +01:00
Index: 10,
RequestedReviewers: []*models.User{},
},
2024-12-09 00:39:55 +01:00
[]*models.PullReview{{Body: "ok", User: &models.User{UserName: "test"}, Stale: false, State: "approved"}},
2024-11-29 17:33:01 +01:00
nil,
)
mi.EXPECT().FetchMaintainershipFile("foo", common.DefaultGitPrj, "bar").Return([]byte(`
"": ["test"]
`), nil)
2024-12-09 18:20:56 +01:00
approved, err := IsPrjGitPRApproved(mi, pri, config, 10)
2024-11-29 17:33:01 +01:00
if !approved || err != nil {
t.Error("Unexpected disapproval or err:", err)
}
})
2024-11-27 17:50:55 +01:00
}