mirror of
https://github.com/woodpecker-ci/woodpecker.git
synced 2025-01-03 06:08:42 +00:00
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:
parent
383f273392
commit
18c1807f4f
5 changed files with 1606 additions and 18 deletions
|
@ -187,7 +187,7 @@ func run(c *cli.Context) error {
|
||||||
setupMetrics(&g, _store)
|
setupMetrics(&g, _store)
|
||||||
|
|
||||||
g.Go(func() error {
|
g.Go(func() error {
|
||||||
return cron.Start(c.Context, _store)
|
return cron.Start(c.Context, _store, _remote)
|
||||||
})
|
})
|
||||||
|
|
||||||
// start the server with tls enabled
|
// start the server with tls enabled
|
||||||
|
|
|
@ -22,9 +22,9 @@ import (
|
||||||
"github.com/robfig/cron"
|
"github.com/robfig/cron"
|
||||||
"github.com/rs/zerolog/log"
|
"github.com/rs/zerolog/log"
|
||||||
|
|
||||||
"github.com/woodpecker-ci/woodpecker/server"
|
|
||||||
"github.com/woodpecker-ci/woodpecker/server/model"
|
"github.com/woodpecker-ci/woodpecker/server/model"
|
||||||
"github.com/woodpecker-ci/woodpecker/server/pipeline"
|
"github.com/woodpecker-ci/woodpecker/server/pipeline"
|
||||||
|
"github.com/woodpecker-ci/woodpecker/server/remote"
|
||||||
"github.com/woodpecker-ci/woodpecker/server/store"
|
"github.com/woodpecker-ci/woodpecker/server/store"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -37,7 +37,7 @@ const (
|
||||||
)
|
)
|
||||||
|
|
||||||
// Start starts the cron scheduler loop
|
// 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 {
|
for {
|
||||||
select {
|
select {
|
||||||
case <-ctx.Done():
|
case <-ctx.Done():
|
||||||
|
@ -54,7 +54,7 @@ func Start(ctx context.Context, store store.Store) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, cron := range crons {
|
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")
|
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
|
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)
|
log.Trace().Msgf("Cron: run id[%d]", cron.ID)
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
|
|
||||||
|
@ -96,7 +96,7 @@ func runCron(cron *model.Cron, store store.Store, now time.Time) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
repo, newBuild, err := createBuild(ctx, cron, store)
|
repo, newBuild, err := createBuild(ctx, store, remote, cron)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -105,9 +105,7 @@ func runCron(cron *model.Cron, store store.Store, now time.Time) error {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
func createBuild(ctx context.Context, cron *model.Cron, store store.Store) (*model.Repo, *model.Build, error) {
|
func createBuild(ctx context.Context, store store.Store, remote remote.Remote, cron *model.Cron) (*model.Repo, *model.Build, error) {
|
||||||
remote := server.Config.Services.Remote
|
|
||||||
|
|
||||||
repo, err := store.GetRepo(cron.RepoID)
|
repo, err := store.GetRepo(cron.RepoID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, nil, err
|
return nil, nil, err
|
||||||
|
|
|
@ -15,24 +15,52 @@
|
||||||
package cron
|
package cron
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
|
"github.com/stretchr/testify/mock"
|
||||||
|
|
||||||
"github.com/woodpecker-ci/woodpecker/server"
|
"github.com/woodpecker-ci/woodpecker/server/model"
|
||||||
"github.com/woodpecker-ci/woodpecker/server/remote/mocks"
|
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) {
|
func TestCreateBuild(t *testing.T) {
|
||||||
rOld := server.Config.Services.Remote
|
remote := mocks_remote.NewRemote(t)
|
||||||
defer func() {
|
store := mocks_store.NewStore(t)
|
||||||
server.Config.Services.Remote = rOld
|
ctx := context.Background()
|
||||||
}()
|
|
||||||
server.Config.Services.Remote = mocks.NewRemote(t)
|
|
||||||
|
|
||||||
// TODO: mockStore
|
creator := &model.User{
|
||||||
// createBuild(context.TODO(), &model.Cron{}, mockStore)
|
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) {
|
func TestCalcNewNext(t *testing.T) {
|
||||||
|
|
1559
server/store/mocks/store.go
Normal file
1559
server/store/mocks/store.go
Normal file
File diff suppressed because it is too large
Load diff
|
@ -14,6 +14,9 @@
|
||||||
|
|
||||||
package store
|
package store
|
||||||
|
|
||||||
|
//go:generate go install github.com/vektra/mockery/v2@latest
|
||||||
|
//go:generate mockery --name Store --output mocks --case underscore
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"io"
|
"io"
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue