Skip to content

Commit

Permalink
manualhooks tests (#245)
Browse files Browse the repository at this point in the history
* write manualhooks tests

Signed-off-by: Stephen Hwang <[email protected]>

* add init script

Signed-off-by: Stephen Hwang <[email protected]>

* get init script

Signed-off-by: Stephen Hwang <[email protected]>

* terminate container only if running

Signed-off-by: Stephen Hwang <[email protected]>

* refactor setup and teardown

Signed-off-by: Stephen Hwang <[email protected]>

* remove leftover testdata

Signed-off-by: Stephen Hwang <[email protected]>

* refactor to re-use code

Signed-off-by: Stephen Hwang <[email protected]>

* use pointer receivers

Signed-off-by: Stephen Hwang <[email protected]>

* use TestMain pragma, swap ifErrPanic, add t to sub

Signed-off-by: Stephen Hwang <[email protected]>

* add more assertions, add createXXX tests

Signed-off-by: Stephen Hwang <[email protected]>

* less code

Signed-off-by: Stephen Hwang <[email protected]>

---------

Signed-off-by: Stephen Hwang <[email protected]>
  • Loading branch information
sthwang-metal authored Oct 17, 2023
1 parent 0218550 commit f986072
Show file tree
Hide file tree
Showing 6 changed files with 676 additions and 112 deletions.
126 changes: 14 additions & 112 deletions internal/graphapi/tools_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,35 +2,26 @@ package graphapi_test

import (
"context"
"log"
"net/http"
"net/http/httptest"
"os"
"path/filepath"
"strings"
"testing"

"entgo.io/ent/dialect"
"github.com/99designs/gqlgen/graphql/handler"
"github.com/labstack/echo/v4"
_ "github.com/lib/pq"
_ "github.com/mattn/go-sqlite3"
"github.com/testcontainers/testcontainers-go/modules/postgres"
"go.uber.org/zap"

"go.infratographer.com/permissions-api/pkg/permissions"
"go.infratographer.com/x/echojwtx"
"go.infratographer.com/x/echox"
"go.infratographer.com/x/events"
"go.infratographer.com/x/goosex"
"go.infratographer.com/x/testing/eventtools"

"go.infratographer.com/load-balancer-api/db"
ent "go.infratographer.com/load-balancer-api/internal/ent/generated"
"go.infratographer.com/load-balancer-api/internal/graphapi"
"go.infratographer.com/load-balancer-api/internal/graphclient"
"go.infratographer.com/load-balancer-api/internal/manualhooks"
"go.infratographer.com/load-balancer-api/x/testcontainersx"
"go.infratographer.com/load-balancer-api/internal/testutils"
)

const (
Expand All @@ -39,115 +30,26 @@ const (
lbPrefix = "loadbal"
)

var (
TestDBURI = os.Getenv("LOADBALANCERAPI_TESTDB_URI")
EntClient *ent.Client
DBContainer *testcontainersx.DBContainer
)
var EntClient *ent.Client

func TestMain(m *testing.M) {
// setup the database if needed
setupDB()
// run the tests
code := m.Run()
// teardown the database
teardownDB()
// return the test response code
os.Exit(code)
}

func parseDBURI(ctx context.Context) (string, string, *testcontainersx.DBContainer) {
switch {
// if you don't pass in a database we default to an in memory sqlite
case TestDBURI == "":
return dialect.SQLite, "file:ent?mode=memory&cache=shared&_fk=1", nil
case strings.HasPrefix(TestDBURI, "sqlite://"):
return dialect.SQLite, strings.TrimPrefix(TestDBURI, "sqlite://"), nil
case strings.HasPrefix(TestDBURI, "postgres://"), strings.HasPrefix(TestDBURI, "postgresql://"):
return dialect.Postgres, TestDBURI, nil
case strings.HasPrefix(TestDBURI, "docker://"):
dbImage := strings.TrimPrefix(TestDBURI, "docker://")

switch {
case strings.HasPrefix(dbImage, "cockroach"), strings.HasPrefix(dbImage, "cockroachdb"), strings.HasPrefix(dbImage, "crdb"):
cntr, err := testcontainersx.NewCockroachDB(ctx, dbImage)
errPanic("error starting db test container", err)

return dialect.Postgres, cntr.URI, cntr
case strings.HasPrefix(dbImage, "postgres"):
cntr, err := testcontainersx.NewPostgresDB(ctx, dbImage,
postgres.WithInitScripts(filepath.Join("testdata", "postgres_init.sh")),
)
errPanic("error starting db test container", err)

return dialect.Postgres, cntr.URI, cntr
default:
panic("invalid testcontainer URI, uri: " + TestDBURI)
}

default:
panic("invalid DB URI, uri: " + TestDBURI)
}
}

func setupDB() {
// don't setup the datastore if we already have one
if EntClient != nil {
return
}

ctx := context.Background()
// setup the database
testutils.SetupDB()

dia, uri, cntr := parseDBURI(ctx)
// assign package variables
EntClient = testutils.EntClient

nats, err := eventtools.NewNatsServer()
if err != nil {
errPanic("failed to start nats server", err)
}

conn, err := events.NewConnection(nats.Config)
if err != nil {
errPanic("failed to create events publisher", err)
}
// setup the resolver hooks
manualhooks.PubsubHooks(EntClient)

c, err := ent.Open(dia, uri, ent.Debug(), ent.EventsPublisher(conn))
if err != nil {
errPanic("failed terminating test db container after failing to connect to the db", cntr.Container.Terminate(ctx))
errPanic("failed opening connection to database:", err)
}

switch dia {
case dialect.SQLite:
// Run automatic migrations for SQLite
errPanic("failed creating db scema", c.Schema.Create(ctx))
case dialect.Postgres:
log.Println("Running database migrations")
goosex.MigrateUp(uri, db.Migrations)
}

// TODO: fix generated pubsubhooks
// pubsubhooks.PubsubHooks(c)
manualhooks.PubsubHooks(c)

EntClient = c
}

func teardownDB() {
ctx := context.Background()

if EntClient != nil {
errPanic("teardown failed to close database connection", EntClient.Close())
}
// run the tests
code := m.Run()

if DBContainer != nil {
errPanic("teardown failed to terminate test db container", DBContainer.Container.Terminate(ctx))
}
}
// teardown the database
testutils.TeardownDB()

func errPanic(msg string, err error) {
if err != nil {
log.Panicf("%s err: %s", msg, err.Error())
}
// return the test response code
os.Exit(code)
}

type graphClient struct {
Expand Down
Loading

0 comments on commit f986072

Please sign in to comment.