Skip to content

Commit

Permalink
Merge pull request agola-io#194 from sgotti/tests_improve_services_lo…
Browse files Browse the repository at this point in the history
…gging

tests: improve services logging
  • Loading branch information
sgotti authored Jan 15, 2020
2 parents df0e758 + 2eb1604 commit ea667ff
Show file tree
Hide file tree
Showing 16 changed files with 160 additions and 93 deletions.
14 changes: 7 additions & 7 deletions cmd/agola/cmd/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,55 +142,55 @@ func serve(cmd *cobra.Command, args []string) error {

var rs *rsscheduler.Runservice
if isComponentEnabled("runservice") {
rs, err = rsscheduler.NewRunservice(ctx, &c.Runservice)
rs, err = rsscheduler.NewRunservice(ctx, nil, &c.Runservice)
if err != nil {
return errors.Errorf("failed to start run service scheduler: %w", err)
}
}

var ex *rsexecutor.Executor
if isComponentEnabled("executor") {
ex, err = executor.NewExecutor(&c.Executor)
ex, err = executor.NewExecutor(ctx, nil, &c.Executor)
if err != nil {
return errors.Errorf("failed to start run service executor: %w", err)
}
}

var cs *configstore.Configstore
if isComponentEnabled("configstore") {
cs, err = configstore.NewConfigstore(ctx, &c.Configstore)
cs, err = configstore.NewConfigstore(ctx, nil, &c.Configstore)
if err != nil {
return errors.Errorf("failed to start config store: %w", err)
}
}

var sched *scheduler.Scheduler
if isComponentEnabled("scheduler") {
sched, err = scheduler.NewScheduler(&c.Scheduler)
sched, err = scheduler.NewScheduler(ctx, nil, &c.Scheduler)
if err != nil {
return errors.Errorf("failed to start scheduler: %w", err)
}
}

var ns *notification.NotificationService
if isComponentEnabled("notification") {
ns, err = notification.NewNotificationService(c)
ns, err = notification.NewNotificationService(ctx, nil, c)
if err != nil {
return errors.Errorf("failed to start notification service: %w", err)
}
}

var gw *gateway.Gateway
if isComponentEnabled("gateway") {
gw, err = gateway.NewGateway(c)
gw, err = gateway.NewGateway(ctx, nil, c)
if err != nil {
return errors.Errorf("failed to start gateway: %w", err)
}
}

var gs *gitserver.Gitserver
if isComponentEnabled("gitserver") {
gs, err = gitserver.NewGitserver(&c.Gitserver)
gs, err = gitserver.NewGitserver(ctx, nil, &c.Gitserver)
if err != nil {
return errors.Errorf("failed to start git server: %w", err)
}
Expand Down
60 changes: 39 additions & 21 deletions internal/datamanager/datamanager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,21 +29,17 @@ import (
"testing"
"time"

slog "agola.io/agola/internal/log"
"agola.io/agola/internal/objectstorage"
"agola.io/agola/internal/testutil"
"agola.io/agola/internal/util"

"github.com/google/go-cmp/cmp"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
"go.uber.org/zap/zaptest"
errors "golang.org/x/xerrors"
)

var level = zap.NewAtomicLevelAt(zapcore.InfoLevel)
var logger = slog.New(level)

func setupEtcd(t *testing.T, dir string) *testutil.TestEmbeddedEtcd {
func setupEtcd(t *testing.T, logger *zap.Logger, dir string) *testutil.TestEmbeddedEtcd {
tetcd, err := testutil.NewTestEmbeddedEtcd(t, logger, dir)
if err != nil {
t.Fatalf("unexpected err: %v", err)
Expand All @@ -52,7 +48,7 @@ func setupEtcd(t *testing.T, dir string) *testutil.TestEmbeddedEtcd {
t.Fatalf("unexpected err: %v", err)
}
if err := tetcd.WaitUp(30 * time.Second); err != nil {
t.Fatalf("error waiting on store up: %v", err)
t.Fatalf("error waiting on etcd up: %v", err)
}
return tetcd
}
Expand All @@ -70,11 +66,13 @@ func TestEtcdReset(t *testing.T) {
}
defer os.RemoveAll(dir)

logger := zaptest.NewLogger(t, zaptest.Level(zap.InfoLevel))

etcdDir, err := ioutil.TempDir(dir, "etcd")
if err != nil {
t.Fatalf("unexpected err: %v", err)
}
tetcd := setupEtcd(t, etcdDir)
tetcd := setupEtcd(t, logger, etcdDir)

ctx, cancel := context.WithCancel(context.Background())

Expand Down Expand Up @@ -134,7 +132,7 @@ func TestEtcdReset(t *testing.T) {
t.Logf("resetting etcd")
os.RemoveAll(etcdDir)
t.Logf("starting etcd")
tetcd = setupEtcd(t, etcdDir)
tetcd = setupEtcd(t, logger, etcdDir)
if err := tetcd.Start(); err != nil {
t.Fatalf("unexpected err: %v", err)
}
Expand Down Expand Up @@ -177,11 +175,13 @@ func TestEtcdResetWalsGap(t *testing.T) {
}
defer os.RemoveAll(dir)

logger := zaptest.NewLogger(t, zaptest.Level(zap.InfoLevel))

etcdDir, err := ioutil.TempDir(dir, "etcd")
if err != nil {
t.Fatalf("unexpected err: %v", err)
}
tetcd := setupEtcd(t, etcdDir)
tetcd := setupEtcd(t, logger, etcdDir)

ctx, cancel := context.WithCancel(context.Background())

Expand Down Expand Up @@ -241,7 +241,7 @@ func TestEtcdResetWalsGap(t *testing.T) {
t.Logf("resetting etcd")
os.RemoveAll(etcdDir)
t.Logf("starting etcd")
tetcd = setupEtcd(t, etcdDir)
tetcd = setupEtcd(t, logger, etcdDir)
if err := tetcd.Start(); err != nil {
t.Fatalf("unexpected err: %v", err)
}
Expand Down Expand Up @@ -303,11 +303,13 @@ func TestConcurrentUpdate(t *testing.T) {
}
defer os.RemoveAll(dir)

logger := zaptest.NewLogger(t, zaptest.Level(zap.InfoLevel))

etcdDir, err := ioutil.TempDir(dir, "etcd")
if err != nil {
t.Fatalf("unexpected err: %v", err)
}
tetcd := setupEtcd(t, etcdDir)
tetcd := setupEtcd(t, logger, etcdDir)
defer shutdownEtcd(tetcd)

ctx := context.Background()
Expand Down Expand Up @@ -394,11 +396,13 @@ func TestEtcdWalCleaner(t *testing.T) {
}
defer os.RemoveAll(dir)

logger := zaptest.NewLogger(t, zaptest.Level(zap.InfoLevel))

etcdDir, err := ioutil.TempDir(dir, "etcd")
if err != nil {
t.Fatalf("unexpected err: %v", err)
}
tetcd := setupEtcd(t, etcdDir)
tetcd := setupEtcd(t, logger, etcdDir)
defer shutdownEtcd(tetcd)

ctx := context.Background()
Expand Down Expand Up @@ -468,11 +472,13 @@ func TestReadObject(t *testing.T) {
}
defer os.RemoveAll(dir)

logger := zaptest.NewLogger(t, zaptest.Level(zap.InfoLevel))

etcdDir, err := ioutil.TempDir(dir, "etcd")
if err != nil {
t.Fatalf("unexpected err: %v", err)
}
tetcd := setupEtcd(t, etcdDir)
tetcd := setupEtcd(t, logger, etcdDir)
defer shutdownEtcd(tetcd)

ctx := context.Background()
Expand Down Expand Up @@ -761,11 +767,13 @@ func testCheckpoint(t *testing.T, basePath string) {
}
defer os.RemoveAll(dir)

logger := zaptest.NewLogger(t, zaptest.Level(zap.InfoLevel))

etcdDir, err := ioutil.TempDir(dir, "etcd")
if err != nil {
t.Fatalf("unexpected err: %v", err)
}
tetcd := setupEtcd(t, etcdDir)
tetcd := setupEtcd(t, logger, etcdDir)
defer shutdownEtcd(tetcd)

ctx := context.Background()
Expand Down Expand Up @@ -977,11 +985,13 @@ func TestRead(t *testing.T) {
}
defer os.RemoveAll(dir)

logger := zaptest.NewLogger(t, zaptest.Level(zap.InfoLevel))

etcdDir, err := ioutil.TempDir(dir, "etcd")
if err != nil {
t.Fatalf("unexpected err: %v", err)
}
tetcd := setupEtcd(t, etcdDir)
tetcd := setupEtcd(t, logger, etcdDir)
defer shutdownEtcd(tetcd)

ctx := context.Background()
Expand Down Expand Up @@ -1091,11 +1101,13 @@ func testClean(t *testing.T, basePath string) {
}
defer os.RemoveAll(dir)

logger := zaptest.NewLogger(t, zaptest.Level(zap.InfoLevel))

etcdDir, err := ioutil.TempDir(dir, "etcd")
if err != nil {
t.Fatalf("unexpected err: %v", err)
}
tetcd := setupEtcd(t, etcdDir)
tetcd := setupEtcd(t, logger, etcdDir)
defer shutdownEtcd(tetcd)

ctx := context.Background()
Expand Down Expand Up @@ -1209,11 +1221,13 @@ func testCleanConcurrentCheckpoint(t *testing.T, basePath string) {
}
defer os.RemoveAll(dir)

logger := zaptest.NewLogger(t, zaptest.Level(zap.InfoLevel))

etcdDir, err := ioutil.TempDir(dir, "etcd")
if err != nil {
t.Fatalf("unexpected err: %v", err)
}
tetcd := setupEtcd(t, etcdDir)
tetcd := setupEtcd(t, logger, etcdDir)
defer shutdownEtcd(tetcd)

ctx := context.Background()
Expand Down Expand Up @@ -1338,11 +1352,13 @@ func testStorageWalCleaner(t *testing.T, basePath string) {
}
defer os.RemoveAll(dir)

logger := zaptest.NewLogger(t, zaptest.Level(zap.InfoLevel))

etcdDir, err := ioutil.TempDir(dir, "etcd")
if err != nil {
t.Fatalf("unexpected err: %v", err)
}
tetcd := setupEtcd(t, etcdDir)
tetcd := setupEtcd(t, logger, etcdDir)
defer shutdownEtcd(tetcd)

ctx := context.Background()
Expand Down Expand Up @@ -1478,11 +1494,13 @@ func TestExportImport(t *testing.T) {
}
defer os.RemoveAll(dir)

logger := zaptest.NewLogger(t, zaptest.Level(zap.InfoLevel))

etcdDir, err := ioutil.TempDir(dir, "etcd")
if err != nil {
t.Fatalf("unexpected err: %v", err)
}
tetcd := setupEtcd(t, etcdDir)
tetcd := setupEtcd(t, logger, etcdDir)

ctx, cancel := context.WithCancel(context.Background())

Expand Down Expand Up @@ -1586,7 +1604,7 @@ func TestExportImport(t *testing.T) {
t.Logf("resetting etcd")
os.RemoveAll(etcdDir)
t.Logf("starting etcd")
tetcd = setupEtcd(t, etcdDir)
tetcd = setupEtcd(t, logger, etcdDir)
if err := tetcd.Start(); err != nil {
t.Fatalf("unexpected err: %v", err)
}
Expand Down
6 changes: 5 additions & 1 deletion internal/services/configstore/configstore.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,10 +120,14 @@ type Configstore struct {
maintenanceMode bool
}

func NewConfigstore(ctx context.Context, c *config.Configstore) (*Configstore, error) {
func NewConfigstore(ctx context.Context, l *zap.Logger, c *config.Configstore) (*Configstore, error) {
if l != nil {
logger = l
}
if c.Debug {
level.SetLevel(zapcore.DebugLevel)
}
log = logger.Sugar()

ost, err := scommon.NewObjectStorage(&c.ObjectStorage)
if err != nil {
Expand Down
Loading

0 comments on commit ea667ff

Please sign in to comment.