more tests

This commit is contained in:
Adam Majer 2024-11-12 14:26:36 +01:00
parent dc96392b40
commit e8e51e21ca
4 changed files with 274 additions and 13 deletions

View File

@ -41,17 +41,17 @@ type GitHandler struct {
} }
type GitHandlerGenerator interface { type GitHandlerGenerator interface {
CreateGitHandler(git_author, email, name string) (*GitHandler, error); CreateGitHandler(git_author, email, prj_name string) (*GitHandler, error);
} }
type GitHandlerImpl struct {} type GitHandlerImpl struct {}
func (*GitHandlerImpl) CreateGitHandler(git_author, email, name string) (*GitHandler, error) { func (*GitHandlerImpl) CreateGitHandler(git_author, email, prj_name string) (*GitHandler, error) {
var err error var err error
git := new(GitHandler) git := new(GitHandler)
git.GitCommiter = git_author git.GitCommiter = git_author
git.GitPath, err = os.MkdirTemp("", name) git.GitPath, err = os.MkdirTemp("", prj_name)
if err != nil { if err != nil {
return nil, fmt.Errorf("Cannot create temp dir: %w", err) return nil, fmt.Errorf("Cannot create temp dir: %w", err)
} }

View File

@ -61,7 +61,7 @@ func gitExecs(t *testing.T, git *common.GitHandler, cmds [][]string) {
} }
func commandsForPackages(dir, prefix string, startN, endN int) [][]string { func commandsForPackages(dir, prefix string, startN, endN int) [][]string {
commands := make([][]string, (endN - startN + 2) * 6) commands := make([][]string, (endN-startN+2)*6)
if dir == "" { if dir == "" {
dir = "." dir = "."
@ -88,7 +88,7 @@ func commandsForPackages(dir, prefix string, startN, endN int) [][]string {
func setupGitForTests(t *testing.T, git *common.GitHandler) { func setupGitForTests(t *testing.T, git *common.GitHandler) {
common.ExtraGitParams = []string{ common.ExtraGitParams = []string{
"GIT_CONFIG_COUNT=1", "GIT_CONFIG_COUNT=1",
"GIT_CONFIG_KEY_0=protocol.file.allow", "GIT_CONFIG_KEY_0=protocol.file.allow",
"GIT_CONFIG_VALUE_0=always", "GIT_CONFIG_VALUE_0=always",
} }
@ -105,7 +105,6 @@ func setupGitForTests(t *testing.T, git *common.GitHandler) {
{"prj", "add", ".gitmodules", "testRepo"}, {"prj", "add", ".gitmodules", "testRepo"},
{"prj", "commit", "-m", "First instance"}, {"prj", "commit", "-m", "First instance"},
{"prj", "submodule", "deinit", "testRepo"}, {"prj", "submodule", "deinit", "testRepo"},
{"", "clone", "prj", common.DefaultGitPrj},
{LocalCMD, "foo", "/usr/bin/touch", "file2"}, {LocalCMD, "foo", "/usr/bin/touch", "file2"},
{"foo", "add", "file2"}, {"foo", "add", "file2"},
{"foo", "commit", "-m", "added file2"}, {"foo", "commit", "-m", "added file2"},
@ -122,8 +121,7 @@ func TestUpdatePrBranch(t *testing.T) {
Repository: &common.Repository{ Repository: &common.Repository{
Name: "testRepo", Name: "testRepo",
}, },
Pull_Request: &common.PullRequest { Pull_Request: &common.PullRequest{},
},
} }
git := &common.GitHandler{ git := &common.GitHandler{
@ -134,6 +132,8 @@ func TestUpdatePrBranch(t *testing.T) {
} }
setupGitForTests(t, git) setupGitForTests(t, git)
gitExecs(t, git, [][]string{{"", "clone", "prj", common.DefaultGitPrj}})
revs := strings.Split(git.GitExecWithOutputOrPanic("foo", "rev-list", "HEAD"), "\n") revs := strings.Split(git.GitExecWithOutputOrPanic("foo", "rev-list", "HEAD"), "\n")
req.Pull_Request.Base.Sha = strings.TrimSpace(revs[1]) req.Pull_Request.Base.Sha = strings.TrimSpace(revs[1])
req.Pull_Request.Head.Sha = strings.TrimSpace(revs[0]) req.Pull_Request.Head.Sha = strings.TrimSpace(revs[0])
@ -160,8 +160,7 @@ func TestCreatePrBranch(t *testing.T) {
Repository: &common.Repository{ Repository: &common.Repository{
Name: "testRepo", Name: "testRepo",
}, },
Pull_Request: &common.PullRequest { Pull_Request: &common.PullRequest{},
},
} }
git := &common.GitHandler{ git := &common.GitHandler{
@ -172,6 +171,8 @@ func TestCreatePrBranch(t *testing.T) {
} }
setupGitForTests(t, git) setupGitForTests(t, git)
gitExecs(t, git, [][]string{{"", "clone", "prj", common.DefaultGitPrj}})
revs := strings.Split(git.GitExecWithOutputOrPanic("foo", "rev-list", "HEAD"), "\n") revs := strings.Split(git.GitExecWithOutputOrPanic("foo", "rev-list", "HEAD"), "\n")
req.Pull_Request.Base.Sha = strings.TrimSpace(revs[1]) req.Pull_Request.Base.Sha = strings.TrimSpace(revs[1])
req.Pull_Request.Head.Sha = strings.TrimSpace(revs[0]) req.Pull_Request.Head.Sha = strings.TrimSpace(revs[0])
@ -187,11 +188,10 @@ func TestCreatePrBranch(t *testing.T) {
} }
git.GitExecOrPanic("prj", "reset", "--hard", "testingCreated") git.GitExecOrPanic("prj", "reset", "--hard", "testingCreated")
rev = strings.TrimSpace(git.GitExecWithOutputOrPanic("prj", "submodule", "status", "testRepo"))[1:len(req.Pull_Request.Head.Sha)+1] rev = strings.TrimSpace(git.GitExecWithOutputOrPanic("prj", "submodule", "status", "testRepo"))[1 : len(req.Pull_Request.Head.Sha)+1]
if rev != req.Pull_Request.Head.Sha { if rev != req.Pull_Request.Head.Sha {
t.Error("prj/testRepo not updated to", req.Pull_Request.Head.Sha, "but is at", rev) t.Error("prj/testRepo not updated to", req.Pull_Request.Head.Sha, "but is at", rev)
t.Error(buf.String()) t.Error(buf.String())
// os.CopyFS("/tmp/test", os.DirFS(git.GitPath)) // os.CopyFS("/tmp/test", os.DirFS(git.GitPath))
} }
} }

View File

@ -0,0 +1,84 @@
package main
import (
"testing"
"go.uber.org/mock/gomock"
"src.opensuse.org/autogits/common"
mock_common "src.opensuse.org/autogits/common/mock"
)
func TestClosePR(t *testing.T) {
pr := PullRequestClosed{}
config := &common.AutogitConfig{
Reviewers: []string{"reviewer1", "reviewer2"},
Branch: "branch",
Organization: "test",
GitProjectName: "prj",
}
event := &common.PullRequestWebhookEvent{
Action: "closed",
Number: 1,
Pull_Request: &common.PullRequest{
Id: 1,
Base: common.Head{
Ref: "branch",
Sha: "testing",
Repo: &common.Repository{
Name: "testRepo",
Default_Branch: "main1",
},
},
Head: common.Head{
Ref: "branch",
Sha: "testing",
Repo: &common.Repository{
Name: "testRepo",
Default_Branch: "main1",
},
},
},
Repository: &common.Repository{
Owner: &common.Organization{
Username: "test",
},
},
}
git := &common.GitHandler{
GitCommiter: "tester",
GitEmail: "test@suse.com",
}
t.Run("PR git closed request against PrjGit == no action", func(t *testing.T) {
ctl := gomock.NewController(t)
pr.gitea = mock_common.NewMockGitea(ctl)
git.GitPath = t.TempDir()
config.GitProjectName = "testRepo"
event.Repository.Name = "testRepo"
if err := pr.Process(event, git, config); err != nil {
t.Error("Error PrjGit closed request. Should be no error.", err)
}
})
t.Run("PR git closed", func(t *testing.T) {
ctl := gomock.NewController(t)
pr.gitea = mock_common.NewMockGitea(ctl)
git.GitPath = t.TempDir()
config.GitProjectName = "prjGit"
event.Repository.Name = "tester"
if err := pr.Process(event, git, config); err != nil {
t.Error("Error PrjGit closed request. Should be no error.", err)
}
})
}

View File

@ -0,0 +1,177 @@
package main
import (
"errors"
"testing"
"go.uber.org/mock/gomock"
"src.opensuse.org/autogits/common"
"src.opensuse.org/autogits/common/gitea-generated/models"
mock_common "src.opensuse.org/autogits/common/mock"
)
func TestOpenPR(t *testing.T) {
pr := PullRequestOpened{}
config := &common.AutogitConfig{
Reviewers: []string{"reviewer1", "reviewer2"},
Branch: "branch",
Organization: "test",
GitProjectName: "prj",
}
event := &common.PullRequestWebhookEvent{
Action: "opened",
Number: 1,
Pull_Request: &common.PullRequest{
Id: 1,
Base: common.Head{
Ref: "branch",
Sha: "testing",
Repo: &common.Repository{
Name: "testRepo",
Default_Branch: "main1",
},
},
Head: common.Head{
Ref: "branch",
Sha: "testing",
Repo: &common.Repository{
Name: "testRepo",
Default_Branch: "main1",
},
},
},
Repository: &common.Repository{
Owner: &common.Organization{
Username: "test",
},
},
}
git := &common.GitHandler{
GitCommiter: "tester",
GitEmail: "test@suse.com",
}
t.Run("PR git opened request against PrjGit == no action", func(t *testing.T) {
ctl := gomock.NewController(t)
pr.gitea = mock_common.NewMockGitea(ctl)
git.GitPath = t.TempDir()
config.GitProjectName = "testRepo"
event.Repository.Name = "testRepo"
if err := pr.Process(event, git, config); err != nil {
t.Error("Error PrjGit opened request. Should be no error.", err)
}
})
t.Run("Open PrjGit PR", func(t *testing.T) {
ctl := gomock.NewController(t)
gitea := mock_common.NewMockGitea(ctl)
pr.gitea = gitea
event.Repository.Name = "testRepo"
config.GitProjectName = "prjcopy"
git.GitPath = t.TempDir()
setupGitForTests(t, git)
prjgit := &models.Repository{
SSHURL: "./prj",
DefaultBranch: "testing",
}
giteaPR := &models.PullRequest{}
gitea.EXPECT().CreateRepositoryIfNotExist(git, *event.Repository.Owner, "prjcopy").Return(prjgit, nil)
gitea.EXPECT().CreatePullRequestIfNotExist(prjgit, gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return(giteaPR, nil)
gitea.EXPECT().RequestReviews(giteaPR, "reviewer1").Return(nil, nil)
gitea.EXPECT().RequestReviews(giteaPR, "reviewer2").Return(nil, nil)
err := pr.Process(event, git, config)
if err != nil {
t.Error("error:", err)
}
})
t.Run("Cannot create prjgit repository", func(t *testing.T) {
ctl := gomock.NewController(t)
gitea := mock_common.NewMockGitea(ctl)
pr.gitea = gitea
event.Repository.Name = "testRepo"
config.GitProjectName = "prjcopy"
git.GitPath = t.TempDir()
setupGitForTests(t, git)
failedErr := errors.New("Returned error here")
gitea.EXPECT().CreateRepositoryIfNotExist(git, *event.Repository.Owner, "prjcopy").Return(nil, failedErr)
err := pr.Process(event, git, config)
if err != failedErr {
t.Error("error:", err)
}
})
t.Run("Cannot create PR", func(t *testing.T) {
ctl := gomock.NewController(t)
gitea := mock_common.NewMockGitea(ctl)
pr.gitea = gitea
event.Repository.Name = "testRepo"
config.GitProjectName = "prjcopy"
git.GitPath = t.TempDir()
setupGitForTests(t, git)
prjgit := &models.Repository{
SSHURL: "./prj",
DefaultBranch: "testing",
}
failedErr := errors.New("Returned error here")
gitea.EXPECT().CreateRepositoryIfNotExist(git, *event.Repository.Owner, "prjcopy").Return(prjgit, nil)
gitea.EXPECT().CreatePullRequestIfNotExist(prjgit, gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return(nil, failedErr)
err := pr.Process(event, git, config)
if err != failedErr {
t.Error("error:", err)
}
})
t.Run("Open PrjGit PR", func(t *testing.T) {
ctl := gomock.NewController(t)
gitea := mock_common.NewMockGitea(ctl)
pr.gitea = gitea
event.Repository.Name = "testRepo"
config.GitProjectName = "prjcopy"
git.GitPath = t.TempDir()
setupGitForTests(t, git)
prjgit := &models.Repository{
Name: "SomeRepo",
Owner: &models.User {
UserName: "org",
},
SSHURL: "./prj",
DefaultBranch: "testing",
}
giteaPR := &models.PullRequest{
Base: &models.PRBranchInfo {
Repo: prjgit,
},
Index: 13,
}
failedErr := errors.New("Returned error here")
gitea.EXPECT().CreateRepositoryIfNotExist(git, *event.Repository.Owner, "prjcopy").Return(prjgit, nil)
gitea.EXPECT().CreatePullRequestIfNotExist(prjgit, gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return(giteaPR, nil)
gitea.EXPECT().RequestReviews(giteaPR, "reviewer1").Return(nil, failedErr)
// gitea.EXPECT().RequestReviews(giteaPR, "reviewer2").Return(nil, nil)
err := pr.Process(event, git, config)
if errors.Unwrap(err) != failedErr {
t.Error("error:", err)
}
})
}