Skip to content

Commit

Permalink
fixup! refactor: safe dababase initialisation
Browse files Browse the repository at this point in the history
  • Loading branch information
atzoum committed Nov 24, 2022
1 parent 1b81a06 commit 6bacfbc
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 1 deletion.
70 changes: 70 additions & 0 deletions app/apphandlers/apphandlers_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package apphandlers

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

"github.com/ory/dockertest/v3"
"github.com/rudderlabs/rudder-server/app"
"github.com/rudderlabs/rudder-server/config"
"github.com/rudderlabs/rudder-server/services/db"
"github.com/rudderlabs/rudder-server/testhelper/destination"
"github.com/stretchr/testify/require"
)

func TestAppHandlerStartSequence(t *testing.T) {
options := app.LoadOptions([]string{"app"})
application := app.New(options)
versionHandler := func(w http.ResponseWriter, _ *http.Request) {}

suite := func(t *testing.T, appHandler AppHandler) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
t.Run("it shouldn't be able to start without setup being called first", func(t *testing.T) {
require.Error(t, appHandler.StartRudderCore(ctx, options))
})

t.Run("it shouldn't be able to setup if database is down", func(t *testing.T) {
require.Error(t, appHandler.Setup(options))
})

t.Run("it should be able to setup if database is up", func(t *testing.T) {
startJobsDBPostgresql(t)
require.NoError(t, appHandler.Setup(options))
})
}

t.Run("embedded", func(t *testing.T) {
h, err := GetAppHandler(application, app.EMBEDDED, versionHandler)
require.NoError(t, err)
suite(t, h)
})

t.Run("gateway", func(t *testing.T) {
h, err := GetAppHandler(application, app.GATEWAY, versionHandler)
require.NoError(t, err)
suite(t, h)
})

t.Run("processor", func(t *testing.T) {
h, err := GetAppHandler(application, app.PROCESSOR, versionHandler)
require.NoError(t, err)
suite(t, h)
})
}

func startJobsDBPostgresql(t *testing.T) {
pool, err := dockertest.NewPool("")
require.NoError(t, err)
r, err := destination.SetupPostgres(pool, t)
require.NoError(t, err)
config.Set("DB.port", r.Port)
config.Set("DB.user", r.User)
config.Set("DB.name", r.Database)
config.Set("DB.password", r.Password)
}

func init() {
db.Init()
}
2 changes: 1 addition & 1 deletion testhelper/destination/postgres.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ type PostgresResource struct {

func SetupPostgres(pool *dockertest.Pool, d cleaner) (*PostgresResource, error) {
// pulls an image, creates a container based on it and runs it
postgresContainer, err := pool.Run("postgres", "15-alpine", []string{
postgresContainer, err := pool.Run("postgres", "11-alpine", []string{
"POSTGRES_DB=" + postgresDefaultDB,
"POSTGRES_USER=" + postgresDefaultUser,
"POSTGRES_PASSWORD=" + postgresDefaultPassword,
Expand Down

0 comments on commit 6bacfbc

Please sign in to comment.