Create mock for store and add unit tests for cron (#1146)

* pass remote on init as argument like store

* mock store

* TestCreateBuild
This commit is contained in:
6543 2022-09-01 01:19:49 +02:00 committed by GitHub
parent 383f273392
commit 18c1807f4f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 1606 additions and 18 deletions

View file

@ -187,7 +187,7 @@ func run(c *cli.Context) error {
setupMetrics(&g, _store)
g.Go(func() error {
return cron.Start(c.Context, _store)
return cron.Start(c.Context, _store, _remote)
})
// start the server with tls enabled

View file

@ -22,9 +22,9 @@ import (
"github.com/robfig/cron"
"github.com/rs/zerolog/log"
"github.com/woodpecker-ci/woodpecker/server"
"github.com/woodpecker-ci/woodpecker/server/model"
"github.com/woodpecker-ci/woodpecker/server/pipeline"
"github.com/woodpecker-ci/woodpecker/server/remote"
"github.com/woodpecker-ci/woodpecker/server/store"
)
@ -37,7 +37,7 @@ const (
)
// Start starts the cron scheduler loop
func Start(ctx context.Context, store store.Store) error {
func Start(ctx context.Context, store store.Store, remote remote.Remote) error {
for {
select {
case <-ctx.Done():
@ -54,7 +54,7 @@ func Start(ctx context.Context, store store.Store) error {
}
for _, cron := range crons {
if err := runCron(cron, store, now); err != nil {
if err := runCron(store, remote, cron, now); err != nil {
log.Error().Err(err).Int64("cronID", cron.ID).Msg("run cron failed")
}
}
@ -77,7 +77,7 @@ func CalcNewNext(schedule string, now time.Time) (time.Time, error) {
return c.Next(now), nil
}
func runCron(cron *model.Cron, store store.Store, now time.Time) error {
func runCron(store store.Store, remote remote.Remote, cron *model.Cron, now time.Time) error {
log.Trace().Msgf("Cron: run id[%d]", cron.ID)
ctx := context.Background()
@ -96,7 +96,7 @@ func runCron(cron *model.Cron, store store.Store, now time.Time) error {
return nil
}
repo, newBuild, err := createBuild(ctx, cron, store)
repo, newBuild, err := createBuild(ctx, store, remote, cron)
if err != nil {
return err
}
@ -105,9 +105,7 @@ func runCron(cron *model.Cron, store store.Store, now time.Time) error {
return err
}
func createBuild(ctx context.Context, cron *model.Cron, store store.Store) (*model.Repo, *model.Build, error) {
remote := server.Config.Services.Remote
func createBuild(ctx context.Context, store store.Store, remote remote.Remote, cron *model.Cron) (*model.Repo, *model.Build, error) {
repo, err := store.GetRepo(cron.RepoID)
if err != nil {
return nil, nil, err

View file

@ -15,24 +15,52 @@
package cron
import (
"context"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
"github.com/woodpecker-ci/woodpecker/server"
"github.com/woodpecker-ci/woodpecker/server/remote/mocks"
"github.com/woodpecker-ci/woodpecker/server/model"
mocks_remote "github.com/woodpecker-ci/woodpecker/server/remote/mocks"
mocks_store "github.com/woodpecker-ci/woodpecker/server/store/mocks"
)
func TestCreateBuild(t *testing.T) {
rOld := server.Config.Services.Remote
defer func() {
server.Config.Services.Remote = rOld
}()
server.Config.Services.Remote = mocks.NewRemote(t)
remote := mocks_remote.NewRemote(t)
store := mocks_store.NewStore(t)
ctx := context.Background()
// TODO: mockStore
// createBuild(context.TODO(), &model.Cron{}, mockStore)
creator := &model.User{
ID: 1,
Login: "user1",
}
repo1 := &model.Repo{
ID: 1,
Name: "repo1",
Owner: "owner1",
FullName: "repo1/owner1",
Branch: "default",
}
// mock things
store.On("GetRepo", mock.Anything).Return(repo1, nil)
store.On("GetUser", mock.Anything).Return(creator, nil)
remote.On("BranchHead", mock.Anything, creator, repo1, "default").Return("sha1", nil)
_, build, err := createBuild(ctx, store, remote, &model.Cron{
Name: "test",
})
assert.NoError(t, err)
assert.EqualValues(t, &model.Build{
Event: "cron",
Commit: "sha1",
Branch: "default",
Ref: "refs/heads/default",
Message: "test",
Sender: "test",
}, build)
}
func TestCalcNewNext(t *testing.T) {

1559
server/store/mocks/store.go Normal file

File diff suppressed because it is too large Load diff

View file

@ -14,6 +14,9 @@
package store
//go:generate go install github.com/vektra/mockery/v2@latest
//go:generate mockery --name Store --output mocks --case underscore
import (
"io"