Skip to content

Commit

Permalink
Merge pull request #114 from numary/feature/unify-sql-store
Browse files Browse the repository at this point in the history
feature/unify sql store
  • Loading branch information
flemzord authored Dec 23, 2021
2 parents d743e16 + 9be2f8e commit 67847fb
Show file tree
Hide file tree
Showing 31 changed files with 1,407 additions and 1,180 deletions.
65 changes: 65 additions & 0 deletions .github/workflows/pr_open.yml
Original file line number Diff line number Diff line change
Expand Up @@ -228,3 +228,68 @@ jobs:
run: go mod download
- name: run vet
run: go vet .
Build:
name: 'Build'
needs:
- build_control
- Test_sqlite
- Test_postgres
- Lint
runs-on: ${{ matrix.os }}
strategy:
matrix:
os: [ windows-latest, ubuntu-latest ]
include:
- os: windows-latest
file: windows
- os: ubuntu-latest
file: default
steps:
- uses: actions/setup-go@v2
with:
go-version: '1.16'
- uses: actions/checkout@v2
- uses: actions/cache@v2
with:
path: |
~/.cache/go-build
~/go/pkg/mod
key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}
restore-keys: |
${{ runner.os }}-go-
- name: get deps
run: go get
env:
CGO_ENABLED: 1
- name: fetch numary control
uses: actions/download-artifact@v2
with:
name: control-dist
path: cmd/control/
- name: fetch docs
uses: actions/download-artifact@v2
with:
name: docs-dist
path: docs/
- name: OSXCross for CGO Support
if: matrix.os == 'ubuntu-latest'
run: |
mkdir ../../osxcross
git clone https://github.com/plentico/osxcross-target.git ../../osxcross/target
- name: Downgrade libssl
if: matrix.os == 'ubuntu-latest'
run: |
echo 'deb http://security.ubuntu.com/ubuntu bionic-security main' | sudo tee -a /etc/apt/sources.list
sudo apt update && apt-cache policy libssl1.0-dev
sudo apt-get install libssl1.0-dev
- name: Run GoReleaser
uses: goreleaser/goreleaser-action@v2
with:
version: latest
args: build --parallelism 4 --rm-dist --skip-validate --snapshot --config .github/.goreleaser.${{matrix.file}}.yml
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
- uses: actions/upload-artifact@v2
with:
name: build-${{matrix.file}}
path: ./build/**
43 changes: 16 additions & 27 deletions cmd/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (

type containerConfig struct {
version string
storageDriver string
ledgerLister controllers.LedgerLister
basicAuth string
options []fx.Option
Expand All @@ -28,12 +27,6 @@ func WithVersion(version string) option {
}
}

func WithStorageDriver(driver string) option {
return func(c *containerConfig) {
c.storageDriver = driver
}
}

func WithLedgerLister(lister controllers.LedgerLister) option {
return func(c *containerConfig) {
c.ledgerLister = lister
Expand Down Expand Up @@ -66,7 +59,6 @@ func WithRememberConfig(rememberConfig bool) option {

var DefaultOptions = []option{
WithVersion("latest"),
WithStorageDriver("sqlite"),
WithLedgerLister(controllers.LedgerListerFn(func() []string {
return []string{}
})),
Expand All @@ -82,7 +74,7 @@ func NewContainer(options ...option) *fx.App {
providers := make([]interface{}, 0)
providers = append(providers,
fx.Annotate(func() string { return cfg.version }, fx.ResultTags(`name:"version"`)),
fx.Annotate(func() string { return cfg.storageDriver }, fx.ResultTags(`name:"storageDriver"`)),
fx.Annotate(func(driver storage.Driver) string { return driver.Name() }, fx.ResultTags(`name:"storageDriver"`)),
fx.Annotate(func() controllers.LedgerLister { return cfg.ledgerLister }, fx.ResultTags(`name:"ledgerLister"`)),
fx.Annotate(func() string { return cfg.basicAuth }, fx.ResultTags(`name:"httpBasic"`)),
fx.Annotate(ledger.NewResolver, fx.ParamTags(`group:"resolverOptions"`)),
Expand All @@ -92,35 +84,32 @@ func NewContainer(options ...option) *fx.App {
fx.As(new(ledger.ResolverOption)),
),
api.NewAPI,
func() (f storage.Factory, err error) {
f, err = storage.NewDefaultFactory(cfg.storageDriver)
if err != nil {
return nil, err
}
func(driver storage.Driver) storage.Factory {
f := storage.NewDefaultFactory(driver)
if cfg.cache {
f = storage.NewCachedStorageFactory(f)
}
if cfg.rememberConfig {
f = storage.NewRememberConfigStorageFactory(f)
}
return f, nil
return f
},
)

invokes := make([]interface{}, 0)
invokes = append(invokes, func(driver storage.Driver, lifecycle fx.Lifecycle) error {
err := driver.Initialize(context.Background())
if err != nil {
return errors.Wrap(err, "initializing driver")
}
lifecycle.Append(fx.Hook{
OnStop: driver.Close,
})
return nil
})
fxOptions := append(
[]fx.Option{
fx.Invoke(func(lc fx.Lifecycle, h *api.API, storageFactory storage.Factory) {
lc.Append(fx.Hook{
OnStop: func(ctx context.Context) error {
err := storageFactory.Close(ctx)
if err != nil {
return errors.Wrap(err, "closing storage factory")
}
return nil
},
})
}),
fx.Provide(providers...),
fx.Invoke(invokes...),
api.Module,
},
cfg.options...,
Expand Down
75 changes: 60 additions & 15 deletions cmd/container_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,71 @@ package cmd

import (
"context"
"github.com/numary/ledger/ledgertesting"
"github.com/numary/ledger/storage"
"github.com/numary/ledger/storage/sqlstorage"
"github.com/stretchr/testify/assert"
"go.uber.org/fx"
"testing"
)

func TestContainer(t *testing.T) {
run := make(chan struct{}, 1)
app := NewContainer(
WithOption(fx.Invoke(func() {
run <- struct{}{}
})),
)
err := app.Start(context.Background())
if err != nil {
t.Fatal(err)
func TestContainers(t *testing.T) {

type testCase struct {
name string
options []option
}
defer app.Stop(context.Background())

select {
case <-run:
default:
t.Fatal("application not started correctly")
for _, tc := range []testCase{
{
name: "default",
options: []option{
WithOption(fx.Provide(func() storage.Driver {
return sqlstorage.NewInMemorySQLiteDriver()
})),
},
},
{
name: "pg",
options: []option{
WithRememberConfig(false),
WithOption(fx.Provide(ledgertesting.PostgresServer)),
WithOption(fx.Provide(func(t *testing.T, pgServer *ledgertesting.PGServer) storage.Driver {
return sqlstorage.NewCachedDBDriver("postgres", sqlstorage.PostgreSQL, pgServer.ConnString())
})),
WithOption(fx.Invoke(func(t *testing.T, storageFactory storage.Factory) {
store, err := storageFactory.GetStore("testing")
assert.NoError(t, err)
assert.NoError(t, store.Close(context.Background()))
})),
},
},
} {
t.Run(tc.name, func(t *testing.T) {
run := make(chan struct{}, 1)
options := append(tc.options,
WithRememberConfig(false),
WithOption(fx.Invoke(func() {
run <- struct{}{}
})),
WithOption(fx.Provide(func() *testing.T {
return t
})),
)
app := NewContainer(options...)

err := app.Start(context.Background())
if err != nil {
t.Fatal(err)
}
defer app.Stop(context.Background())

select {
case <-run:
default:
t.Fatal("application not started correctly")
}
})
}

}
Loading

0 comments on commit 67847fb

Please sign in to comment.