Skip to content

Commit

Permalink
feat: use gnomock to run postgres to test app
Browse files Browse the repository at this point in the history
  • Loading branch information
mathnogueira committed May 5, 2022
1 parent 7b6af40 commit ce944f5
Show file tree
Hide file tree
Showing 9 changed files with 250 additions and 733 deletions.
2 changes: 0 additions & 2 deletions .github/workflows/run-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,5 @@ jobs:
go-version: ${{ matrix.go-version }}
- name: Checkout code
uses: actions/checkout@v2
- name: Setup config file
run: cp server/config.yaml.ci server/config.yaml
- name: Run tests
run: cd server && go test ./...
6 changes: 0 additions & 6 deletions server/config.yaml.ci

This file was deleted.

15 changes: 7 additions & 8 deletions server/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,19 @@ go 1.13
replace k8s.io/client-go => k8s.io/client-go v0.18.0

require (
github.com/alecthomas/participle/v2 v2.0.0-alpha8 // indirect
github.com/denisbrodbeck/machineid v1.0.1 // indirect
github.com/alecthomas/participle/v2 v2.0.0-alpha8
github.com/denisbrodbeck/machineid v1.0.1
github.com/gogo/protobuf v1.3.2
github.com/golang-migrate/migrate/v4 v4.15.2 // indirect
github.com/golang/mock v1.6.0
github.com/golang-migrate/migrate/v4 v4.15.2
github.com/google/uuid v1.3.0
github.com/gorilla/mux v1.8.0
github.com/gorilla/websocket v1.5.0 // indirect
github.com/gorilla/websocket v1.5.0
github.com/j2gg0s/otsql v0.14.0
github.com/lib/pq v1.10.4
github.com/lib/pq v1.10.5
github.com/mitchellh/mapstructure v1.4.3
github.com/orlangure/gnomock v0.20.0
github.com/prometheus/prometheus v1.8.2-0.20211217191541-41f1a8125e66
github.com/rogpeppe/go-internal v1.6.2 // indirect
github.com/stretchr/testify v1.7.0
github.com/stretchr/testify v1.7.1
go.opentelemetry.io/collector v0.44.0
go.opentelemetry.io/contrib/instrumentation/github.com/gorilla/mux/otelmux v0.29.0
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.28.0
Expand Down
811 changes: 120 additions & 691 deletions server/go.sum

Large diffs are not rendered by default.

4 changes: 3 additions & 1 deletion server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,9 @@ func main() {
tp := initOtelTracing(ctx)
defer func() { _ = tp.Shutdown(ctx) }()

testDB, err := testdb.Postgres(cfg.PostgresConnString)
testDB, err := testdb.Postgres(
testdb.WithDSN(cfg.PostgresConnString),
)
if err != nil {
log.Fatal(err)
}
Expand Down
69 changes: 69 additions & 0 deletions server/test/database.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package test

import (
"database/sql"
"fmt"
"time"

"github.com/orlangure/gnomock"
"github.com/orlangure/gnomock/preset/postgres"
)

var container *gnomock.Container

func GetTestingDatabase() (*sql.DB, error) {
container, err := getPostgresContainer()
db, err := getMainDatabaseConnection(container)
newDbConnection, err := createRandomDatabaseForTest(db, "tracetest")

if err != nil {
return nil, err
}

return newDbConnection, nil
}

func getMainDatabaseConnection(container *gnomock.Container) (*sql.DB, error) {
connStr := fmt.Sprintf(
"host=%s port=%d user=%s password=%s dbname=%s sslmode=disable",
container.Host, container.DefaultPort(), "tracetest", "tracetest", "postgres",
)

return sql.Open("postgres", connStr)
}

func createRandomDatabaseForTest(db *sql.DB, baseDatabase string) (*sql.DB, error) {
epoch := time.Now().UnixNano()
newDatabaseName := fmt.Sprintf("%s_%d", baseDatabase, epoch)
_, err := db.Exec(fmt.Sprintf("CREATE DATABASE %s WITH TEMPLATE %s", newDatabaseName, baseDatabase))
if err != nil {
return nil, fmt.Errorf("could not create database %s: %w", newDatabaseName, err)
}

connStr := fmt.Sprintf(
"host=%s port=%d user=%s password=%s dbname=%s sslmode=disable",
container.Host, container.DefaultPort(), "tracetest", "tracetest", newDatabaseName,
)

return sql.Open("postgres", connStr)
}

func getPostgresContainer() (*gnomock.Container, error) {
if container != nil {
return container, nil
}

preset := postgres.Preset(
postgres.WithUser("tracetest", "tracetest"),
postgres.WithDatabase("tracetest"),
)

dbContainer, err := gnomock.Start(preset)
if err != nil {
return nil, fmt.Errorf("could not start postgres container")
}

container = dbContainer

return dbContainer, nil
}
26 changes: 3 additions & 23 deletions server/testdb/postgres.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,45 +10,25 @@ import (
"github.com/golang-migrate/migrate/v4/database/postgres"
_ "github.com/golang-migrate/migrate/v4/source/file"
"github.com/google/uuid"
"github.com/j2gg0s/otsql"
"github.com/j2gg0s/otsql/hook/trace"
"github.com/kubeshop/tracetest/openapi"
pq "github.com/lib/pq"
)

type postgresDB struct {
db *sql.DB
migrationsFolder string
}

func Postgres(dsn string, options ...PostgresOption) (Repository, error) {
func Postgres(options ...PostgresOption) (Repository, error) {
postgres := &postgresDB{}
connector, err := pq.NewConnector(dsn)
if err != nil {
return nil, fmt.Errorf("sql open: %w", err)
}
db := sql.OpenDB(
otsql.WrapConnector(connector,
otsql.WithHooks(
trace.New(
trace.WithQuery(true),
trace.WithQueryParams(true),
trace.WithRowsAffected(true),
),
),
),
)
postgres.db = db
postgres.migrationsFolder = "file://./migrations"

for _, option := range options {
err = option(postgres)
err := option(postgres)
if err != nil {
return nil, err
}
}

err = postgres.ensureLatestMigration()
err := postgres.ensureLatestMigration()
if err != nil {
return nil, fmt.Errorf("could not execute migrations: %w", err)
}
Expand Down
38 changes: 38 additions & 0 deletions server/testdb/postgres_options.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,45 @@
package testdb

import (
"database/sql"
"fmt"

"github.com/j2gg0s/otsql"
"github.com/j2gg0s/otsql/hook/trace"
pq "github.com/lib/pq"
)

type PostgresOption func(*postgresDB) error

func WithDSN(dsn string) PostgresOption {
return func(pd *postgresDB) error {
connector, err := pq.NewConnector(dsn)
if err != nil {
return fmt.Errorf("sql open: %w", err)
}
db := sql.OpenDB(
otsql.WrapConnector(connector,
otsql.WithHooks(
trace.New(
trace.WithQuery(true),
trace.WithQueryParams(true),
trace.WithRowsAffected(true),
),
),
),
)
pd.db = db
return nil
}
}

func WithDB(db *sql.DB) PostgresOption {
return func(pd *postgresDB) error {
pd.db = db
return nil
}
}

func WithMigrations(migrationFolder string) PostgresOption {
return func(pd *postgresDB) error {
pd.migrationsFolder = migrationFolder
Expand Down
12 changes: 10 additions & 2 deletions server/testdb/testdb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,21 @@ import (

"github.com/google/uuid"
"github.com/kubeshop/tracetest/openapi"
"github.com/kubeshop/tracetest/test"
"github.com/kubeshop/tracetest/testdb"
"github.com/stretchr/testify/assert"
)

func getDB() (testdb.Repository, error) {
dsn := "host=localhost user=postgres password=postgres port=5432 sslmode=disable"
return testdb.Postgres(dsn, testdb.WithMigrations("file://../migrations"))
db, err := test.GetTestingDatabase()
if err != nil {
return nil, err
}

return testdb.Postgres(
testdb.WithDB(db),
testdb.WithMigrations("file://../migrations"),
)
}

func TestCreateTest(t *testing.T) {
Expand Down

0 comments on commit ce944f5

Please sign in to comment.