Skip to content

Commit

Permalink
feat: Integration testing (#64)
Browse files Browse the repository at this point in the history
Co-authored-by: Geoffrey Ragot <[email protected]>
Co-authored-by: Maxence Maireaux <[email protected]>
  • Loading branch information
3 people authored Feb 23, 2023
1 parent ad73372 commit 4466ec6
Show file tree
Hide file tree
Showing 28 changed files with 183 additions and 389 deletions.
45 changes: 16 additions & 29 deletions cmd/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,54 +8,32 @@ import (

"github.com/formancehq/stack/libs/go-libs/auth"
"github.com/formancehq/stack/libs/go-libs/logging"
"github.com/formancehq/stack/libs/go-libs/logging/logginglogrus"
"github.com/formancehq/stack/libs/go-libs/oauth2/oauth2introspect"
"github.com/formancehq/stack/libs/go-libs/otlp/otlptraces"
"github.com/formancehq/stack/libs/go-libs/publish"
"github.com/formancehq/stack/libs/go-libs/service"
"github.com/gin-contrib/cors"
"github.com/gin-gonic/gin"
"github.com/numary/ledger/cmd/internal"
"github.com/numary/ledger/pkg/api"
"github.com/numary/ledger/pkg/api/middlewares"
"github.com/numary/ledger/pkg/api/routes"
"github.com/numary/ledger/pkg/bus"
"github.com/numary/ledger/pkg/contextlogger"
"github.com/numary/ledger/pkg/ledger"
"github.com/numary/ledger/pkg/redis"
"github.com/numary/ledger/pkg/storage/sqlstorage"
"github.com/sirupsen/logrus"
"github.com/spf13/viper"
"github.com/uptrace/opentelemetry-go-extra/otellogrus"
"go.opentelemetry.io/otel/trace"
"go.uber.org/fx"
)

const ServiceName = "ledger"

func NewContainer(v *viper.Viper, userOptions ...fx.Option) *fx.App {
func resolveOptions(v *viper.Viper, userOptions ...fx.Option) []fx.Option {

options := make([]fx.Option, 0)
if !v.GetBool(debugFlag) {
options = append(options, fx.NopLogger)
}

debug := viper.GetBool(debugFlag)

l := logrus.New()
if debug {
l.Level = logrus.DebugLevel
}
if viper.GetBool(otlptraces.OtelTracesFlag) {
l.AddHook(otellogrus.NewHook(otellogrus.WithLevels(
logrus.PanicLevel,
logrus.FatalLevel,
logrus.ErrorLevel,
logrus.WarnLevel,
)))
}
logging.SetFactory(contextlogger.NewFactory(
logging.StaticLoggerFactory(logginglogrus.New(l)),
))
debug := v.GetBool(service.DebugFlag)
if debug {
sqlstorage.InstrumentalizeSQLDrivers()
}
Expand Down Expand Up @@ -86,7 +64,7 @@ func NewContainer(v *viper.Viper, userOptions ...fx.Option) *fx.App {
options = append(options, api.Module(api.Config{
StorageDriver: v.GetString(storageDriverFlag),
Version: Version,
UseScopes: viper.GetBool(authBearerUseScopesFlag),
UseScopes: v.GetBool(authBearerUseScopesFlag),
}))

// Handle storage driver
Expand Down Expand Up @@ -164,7 +142,7 @@ func NewContainer(v *viper.Viper, userOptions ...fx.Option) *fx.App {
return res
}, fx.ParamTags(`optional:"true"`)))

options = append(options, routes.ProvideMiddlewares(func(tp trace.TracerProvider) []gin.HandlerFunc {
options = append(options, routes.ProvideMiddlewares(func(tp trace.TracerProvider, logger logging.Logger) []gin.HandlerFunc {
res := make([]gin.HandlerFunc, 0)

cc := cors.DefaultConfig()
Expand All @@ -173,10 +151,15 @@ func NewContainer(v *viper.Viper, userOptions ...fx.Option) *fx.App {
cc.AddAllowHeaders("authorization")

res = append(res, cors.New(cc))
res = append(res, func(context *gin.Context) {
context.Request = context.Request.WithContext(
logging.ContextWithLogger(context.Request.Context(), logger),
)
})
res = append(res, func(context *gin.Context) {
context.Next()
for _, err := range context.Errors {
logging.GetLogger(context.Request.Context()).Error(err)
logging.FromContext(context.Request.Context()).Error(err)
}
})
res = append(res, middlewares.Log())
Expand All @@ -191,5 +174,9 @@ func NewContainer(v *viper.Viper, userOptions ...fx.Option) *fx.App {
return res
}, fx.ParamTags(`optional:"true"`)))

return fx.New(append(options, userOptions...)...)
return append(options, userOptions...)
}

func NewContainer(v *viper.Viper, userOptions ...fx.Option) *fx.App {
return fx.New(resolveOptions(v, userOptions...)...)
}
4 changes: 4 additions & 0 deletions cmd/container_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"time"

"github.com/ThreeDotsLabs/watermill/pubsub/gochannel"
"github.com/formancehq/stack/libs/go-libs/logging"
"github.com/formancehq/stack/libs/go-libs/otlp/otlptraces"
"github.com/formancehq/stack/libs/go-libs/pgtesting"
"github.com/numary/ledger/pkg/api/middlewares"
Expand Down Expand Up @@ -236,6 +237,9 @@ func TestContainers(t *testing.T) {
fx.Provide(func() *testing.T {
return t
}),
fx.Provide(func() logging.Logger {
return logging.FromContext(context.Background())
}),
)
v := viper.New()
// Default options
Expand Down
12 changes: 9 additions & 3 deletions cmd/internal/analytics.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,13 +64,19 @@ func NewAnalyticsModule(v *viper.Viper, version string) fx.Option {
interval = viper.GetDuration(segmentHeartbeatIntervalFlag)
}
if writeKey == "" {
logging.GetLogger(context.Background()).Infof("telemetry enabled but no write key provided")
return fx.Invoke(func(l logging.Logger) {
l.Infof("telemetry enabled but no write key provided")
})
} else if interval == 0 {
logging.GetLogger(context.Background()).Error("telemetry heartbeat interval is 0")
return fx.Invoke(func(l logging.Logger) {
l.Error("telemetry heartbeat interval is 0")
})
} else {
_, err := semver.NewVersion(version)
if err != nil {
logging.GetLogger(context.Background()).Infof("telemetry enabled but version '%s' is not semver, skip", version)
return fx.Invoke(func(l logging.Logger) {
l.Infof("telemetry enabled but version '%s' is not semver, skip", version)
})
} else {
return fx.Options(
appIdProviderModule,
Expand Down
4 changes: 2 additions & 2 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

"github.com/formancehq/stack/libs/go-libs/otlp/otlptraces"
"github.com/formancehq/stack/libs/go-libs/publish"
"github.com/formancehq/stack/libs/go-libs/service"
"github.com/numary/ledger/cmd/internal"
"github.com/numary/ledger/pkg/redis"
_ "github.com/numary/ledger/pkg/storage/sqlstorage/migrates/9-add-pre-post-volumes"
Expand All @@ -16,7 +17,6 @@ import (
)

const (
debugFlag = "debug"
storageDirFlag = "storage.dir"
storageDriverFlag = "storage.driver"
storageSQLiteDBNameFlag = "storage.sqlite.db_name"
Expand Down Expand Up @@ -100,7 +100,7 @@ func NewRootCommand() *cobra.Command {
home = "/root"
}

root.PersistentFlags().Bool(debugFlag, false, "Debug mode")
root.PersistentFlags().Bool(service.DebugFlag, false, "Debug mode")
root.PersistentFlags().String(storageDriverFlag, "sqlite", "Storage driver")
root.PersistentFlags().String(storageDirFlag, path.Join(home, ".numary/data"), "Storage directory (for sqlite)")
root.PersistentFlags().String(storageSQLiteDBNameFlag, "numary", "SQLite database name")
Expand Down
12 changes: 9 additions & 3 deletions cmd/root_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package cmd
import (
"bytes"
"context"
"fmt"
"net/http"
"os"
"testing"
Expand All @@ -11,6 +12,7 @@ import (
"github.com/formancehq/stack/libs/go-libs/pgtesting"
"github.com/pborman/uuid"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestServer(t *testing.T) {
Expand Down Expand Up @@ -83,7 +85,9 @@ func TestServer(t *testing.T) {
}()

ctx, cancel := context.WithCancel(context.Background())
defer cancel()
defer func() {
cancel()
}()

go func() {
assert.NoError(t, root.ExecuteContext(ctx))
Expand All @@ -101,8 +105,10 @@ func TestServer(t *testing.T) {
<-time.After(delay)
continue
}
if assert.FailNow(t, err.Error()) {
return
if err != nil {
require.Fail(t, err.Error())
} else {
require.Fail(t, fmt.Sprintf("unexpected status code: %d", rsp.StatusCode))
}
}
break
Expand Down
47 changes: 5 additions & 42 deletions cmd/server_start.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
package cmd

import (
"context"
"net"
"net/http"

"github.com/formancehq/stack/libs/go-libs/logging"
"github.com/formancehq/stack/libs/go-libs/httpserver"
app "github.com/formancehq/stack/libs/go-libs/service"
"github.com/numary/ledger/pkg/api"
"github.com/spf13/cobra"
"github.com/spf13/viper"
Expand All @@ -16,46 +13,12 @@ func NewServerStart() *cobra.Command {
return &cobra.Command{
Use: "start",
RunE: func(cmd *cobra.Command, args []string) error {
app := NewContainer(
return app.New(cmd.OutOrStdout(), resolveOptions(
viper.GetViper(),
fx.Invoke(func(lc fx.Lifecycle, h *api.API) {
var (
err error
listener net.Listener
)
lc.Append(fx.Hook{
OnStart: func(ctx context.Context) error {
listener, err = net.Listen("tcp", viper.GetString(serverHttpBindAddressFlag))
if err != nil {
return err
}
go func() {
httpErr := http.Serve(listener, h)
logging.Errorf("http.Serve: %s", httpErr)
}()
return nil
},
OnStop: func(ctx context.Context) error {
return listener.Close()
},
})
lc.Append(httpserver.NewHook(viper.GetString(serverHttpBindAddressFlag), h))
}),
)
errCh := make(chan error, 1)
go func() {
err := app.Start(cmd.Context())
if err != nil {
errCh <- err
}
}()
select {
case err := <-errCh:
return err
case <-cmd.Context().Done():
return app.Stop(context.Background())
case <-app.Done():
return app.Err()
}
)...).Run(cmd.Context())
},
}
}
Loading

0 comments on commit 4466ec6

Please sign in to comment.