Skip to content

Commit

Permalink
Testcontainer db (#1045)
Browse files Browse the repository at this point in the history
* WIP

* More wip

* fix mysql tests

* Actions

* Rename test

* more names

* Cleanup; allow running tests with dif DBs via task

* Rename webhook action file

* Rm test-db prefix

* Rm prefix; more db test cov

* Fix redis container err messag
  • Loading branch information
markphelps authored Sep 28, 2022
1 parent 8f3dbdd commit 6fef58b
Show file tree
Hide file tree
Showing 12 changed files with 636 additions and 561 deletions.
84 changes: 0 additions & 84 deletions .github/workflows/database-test.yml

This file was deleted.

23 changes: 21 additions & 2 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ jobs:
strategy:
matrix:
go: ["1.18", "1.19"]

steps:
- uses: actions/checkout@v3

Expand All @@ -55,8 +54,28 @@ jobs:
check-latest: true
cache: true

- name: Unit Test (SQLite)
- name: Unit Test ${{ matrix.go }}
run: go test -race -covermode=atomic -coverprofile=coverage.txt -count=1 ./...

- name: Upload Coverage
uses: codecov/[email protected]

database:
name: Database Test
runs-on: ubuntu-latest
strategy:
matrix:
database: ["mysql", "postgres"]
steps:
- uses: actions/checkout@v3

- uses: actions/setup-go@v3
with:
go-version: "1.18"
check-latest: true
cache: true

- name: Unit Test ${{ matrix.database }}
env:
FLIPT_TEST_DATABASE_PROTOCOL: ${{ matrix.database }}
run: go test -count=1 ./...
32 changes: 18 additions & 14 deletions Taskfile.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ vars:
PROJECT: flipt
SOURCE_FILES: ./...
UI: ui
COVERAGE_FILE: coverage.txt

tasks:
default:
Expand Down Expand Up @@ -67,14 +68,6 @@ tasks:
cmds:
- npm run dev

bench:
desc: Run benchmarks
cmds:
- go test -bench={{.BENCH_PATTERN}} {{.SOURCE_FILES}} -run=XXX {{.TEST_FLAGS}}
vars:
BENCH_PATTERN: .
TEST_FLAGS: -v

bootstrap:
desc: Install necessary development tools
cmds:
Expand Down Expand Up @@ -129,8 +122,6 @@ tasks:
deps: [test]
cmds:
- go tool cover -html={{.COVERAGE_FILE}}
vars:
COVERAGE_FILE: coverage.txt

fmt:
desc: Run goimports
Expand All @@ -146,8 +137,21 @@ tasks:
test:
desc: Run all the tests
cmds:
- go test {{.TEST_OPTS}} -covermode=atomic -count=1 -coverprofile={{.COVERAGE_FILE}} {{.SOURCE_FILES}} -run={{.TEST_PATTERN}} -timeout=30s {{.TEST_FLAGS}}
- go test {{.TEST_OPTS}} -covermode=atomic -count=1 -coverprofile={{.COVERAGE_FILE}} {{.SOURCE_FILES}} -run={{.TEST_PATTERN}} -timeout=30s
vars:
COVERAGE_FILE: coverage.txt
TEST_PATTERN: .
TEST_OPTS: -race
TEST_PATTERN: '{{.TEST_PATTERN | default "."}}'
TEST_OPTS: '{{.TEST_OPTS | default "-race"}}'
env:
FLIPT_TEST_DATABASE_PROTOCOL: '{{.FLIPT_TEST_DATABASE_PROTOCOL | default "sqlite"}}'

test:mysql:
desc: Run all the tests with MySQL db backend
cmds:
- task: test
vars: { FLIPT_TEST_DATABASE_PROTOCOL: "mysql" }

test:postgres:
desc: Run all the tests with Postgres db backend
cmds:
- task: test
vars: { FLIPT_TEST_DATABASE_PROTOCOL: "postgres" }
2 changes: 1 addition & 1 deletion server/cache/redis/cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ func newCache(t *testing.T, ctx context.Context) (*Cache, func()) {

redisContainer, err := setupRedis(ctx)
if err != nil {
assert.FailNowf(t, "failed to setup redis container: %s", err.Error())
assert.FailNow(t, "failed to setup redis container", err.Error())
}

rdb := goredis.NewClient(&goredis.Options{
Expand Down
29 changes: 19 additions & 10 deletions storage/sql/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import (

// Open opens a connection to the db
func Open(cfg config.Config) (*sql.DB, Driver, error) {
sql, driver, err := open(cfg, false)
sql, driver, err := open(cfg, options{})
if err != nil {
return nil, 0, err
}
Expand All @@ -36,8 +36,13 @@ func Open(cfg config.Config) (*sql.DB, Driver, error) {
return sql, driver, nil
}

func open(cfg config.Config, migrate bool) (*sql.DB, Driver, error) {
d, url, err := parse(cfg, migrate)
type options struct {
sslDisabled bool
migrate bool
}

func open(cfg config.Config, opts options) (*sql.DB, Driver, error) {
d, url, err := parse(cfg, opts)
if err != nil {
return nil, 0, err
}
Expand Down Expand Up @@ -107,7 +112,7 @@ const (
MySQL
)

func parse(cfg config.Config, migrate bool) (Driver, *dburl.URL, error) {
func parse(cfg config.Config, opts options) (Driver, *dburl.URL, error) {
u := cfg.Database.URL

if u == "" {
Expand All @@ -134,13 +139,9 @@ func parse(cfg config.Config, migrate bool) (Driver, *dburl.URL, error) {
u = uu.String()
}

errURL := func(rawurl string, err error) error {
return fmt.Errorf("error parsing url: %q, %w", rawurl, err)
}

url, err := dburl.Parse(u)
if err != nil {
return 0, nil, errURL(u, err)
return 0, nil, fmt.Errorf("error parsing url: %q, %w", url, err)
}

driver := stringToDriver[url.Driver]
Expand All @@ -149,11 +150,19 @@ func parse(cfg config.Config, migrate bool) (Driver, *dburl.URL, error) {
}

switch driver {
case Postgres:
if opts.sslDisabled {
v := url.Query()
v.Set("sslmode", "disable")
url.RawQuery = v.Encode()
// we need to re-parse since we modified the query params
url, err = dburl.Parse(url.URL.String())
}
case MySQL:
v := url.Query()
v.Set("multiStatements", "true")
v.Set("parseTime", "true")
if !migrate {
if !opts.migrate {
v.Set("sql_mode", "ANSI")
}
url.RawQuery = v.Encode()
Expand Down
Loading

0 comments on commit 6fef58b

Please sign in to comment.