autogits/workflow-pr/repo_check_test.go

138 lines
3.2 KiB
Go

package main
import (
"bytes"
"errors"
"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 TestRepoCheck(t *testing.T) {
var logBuf bytes.Buffer
oldOut := log.Writer()
log.SetOutput(&logBuf)
defer log.SetOutput(oldOut)
t.Run("Consistency Check On Start", func(t *testing.T) {
c := CreateDefaultStateChecker(true, nil, nil, 100)
ctl := gomock.NewController(t)
state := mock_main.NewMockStateChecker(ctl)
c.i = state
state.EXPECT().CheckRepos().Do(func() error {
// only checkOnStart has checkInterval = 0
if c.checkInterval != 0 {
t.Fail()
}
c.exitCheckLoop = true
return nil
})
c.ConsistencyCheckProcess()
if c.checkInterval != 100 {
t.Fail()
}
})
t.Run("No consistency Check On Start", func(t *testing.T) {
c := CreateDefaultStateChecker(true, nil, nil, 100)
ctl := gomock.NewController(t)
state := mock_main.NewMockStateChecker(ctl)
c.i = state
nCalls := 10
state.EXPECT().CheckRepos().Do(func() error {
// only checkOnStart has checkInterval = 0
if c.checkInterval != 100 {
t.Fail()
}
nCalls--
if nCalls == 0 {
c.exitCheckLoop = true
}
return nil
}).Times(nCalls)
c.checkOnStart = false
c.ConsistencyCheckProcess()
})
t.Run("CheckRepos() calls CheckProjectState() for each project", func(t *testing.T) {
ctl := gomock.NewController(t)
state := mock_main.NewMockStateChecker(ctl)
gitea := mock_common.NewMockGitea(ctl)
config1 := &common.AutogitConfig{
GitProjectName: "git_repo1",
Organization: "repo1_org",
}
config2 := &common.AutogitConfig{
GitProjectName: "git_repo2",
Organization: "repo2_org",
}
config3 := &common.AutogitConfig{
GitProjectName: "git_repo3",
Organization: "repo3_org",
}
configs := &RequestProcessor{
configuredRepos: map[string][]*common.AutogitConfig{
"repo1_org": []*common.AutogitConfig{config1},
"repo2_org": []*common.AutogitConfig{config2},
"repo3_org": []*common.AutogitConfig{config3},
},
}
r := configs.configuredRepos
c := CreateDefaultStateChecker(true, configs, gitea, 100)
c.i = state
state.EXPECT().VerifyProjectState("repo1_org", r["repo1_org"], 0)
state.EXPECT().VerifyProjectState("repo2_org", r["repo2_org"], 0)
state.EXPECT().VerifyProjectState("repo3_org", r["repo3_org"], 0)
if err := c.CheckRepos(); err != nil {
t.Error(err)
}
})
t.Run("CheckRepos errors", func(t *testing.T) {
ctl := gomock.NewController(t)
state := mock_main.NewMockStateChecker(ctl)
gitea := mock_common.NewMockGitea(ctl)
git := mock_common.NewMockGitHandlerGenerator(ctl)
config1 := &common.AutogitConfig{
GitProjectName: "git_repo1",
Organization: "repo1_org",
}
configs := &RequestProcessor{
configuredRepos: map[string][]*common.AutogitConfig{
"repo1_org": []*common.AutogitConfig{config1},
},
}
//r := configs.configuredRepos
c := CreateDefaultStateChecker(true, configs, gitea, 100)
c.i = state
c.git = git
err := errors.New("test error")
state.EXPECT().VerifyProjectState("repo1_org", gomock.Any(), 0).Return(err)
r := c.CheckRepos()
if !errors.Is(r, err) {
t.Error(err)
}
})
}