Skip to content

Commit

Permalink
refactor setup and teardown
Browse files Browse the repository at this point in the history
Signed-off-by: Stephen Hwang <[email protected]>
  • Loading branch information
sthwang-metal committed Oct 6, 2023
1 parent c8ca436 commit 5f4ae51
Show file tree
Hide file tree
Showing 2 changed files with 130 additions and 94 deletions.
1 change: 0 additions & 1 deletion internal/graphapi/tools_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ const (
)

var (
TestDBURI = os.Getenv("LOADBALANCERAPI_TESTDB_URI")
EntClient *ent.Client
DBContainer *testcontainersx.DBContainer
)
Expand Down
223 changes: 130 additions & 93 deletions internal/manualhooks/hooks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,33 @@ import (
"go.infratographer.com/load-balancer-api/x/testcontainersx"
)

var (
const (
ownerPrefix = "testown"
locationPrefix = "testloc"
defualtTimeout = 2 * time.Second
)

func setup(subChangesTopic string) (context.Context, *ent.Client, <-chan events.Message[events.ChangeMessage], func()) {
var (
EventsConn events.Connection
EntClient *ent.Client
DBContainer *testcontainersx.DBContainer
)

func TestManualHooks(t *testing.T) {
setup()
defer teardown()

t.Run("LoadbalancerCreateUpdateHook", LoadbalancerCreateUpdateHookTest)
t.Run("LoadbalancerDeleteHookTest", LoadbalancerDeleteHookTest)
t.Run("OriginCreateUpdateHookTest", OriginCreateUpdateHookTest)
t.Run("OriginDeleteHookTest", OriginDeleteHookTest)
t.Run("PoolCreateUpdateHookTest", PoolCreateUpdateHookTest)
t.Run("PoolDeleteHookTest", PoolDeleteHookTest)
t.Run("PortCreateUpdateHookTest", PortCreateUpdateHookTest)
t.Run("PortDeleteHookTest", PortDeleteHookTest)
}

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

// NATS setup
Expand All @@ -39,13 +59,10 @@ func setup(subChangesTopic string) (context.Context, *ent.Client, <-chan events.
conn, err := events.NewConnection(nats.Config)
testutils.IfErrPanic("failed to create events connection", err)

subChan, err := conn.SubscribeChanges(ctx, subChangesTopic)
testutils.IfErrPanic("failed to subscribe to changes", err)

// DB and EntClient setup
dia, uri, cntr := testutils.ParseDBURI(ctx)

entClient, err := ent.Open(dia, uri, ent.Debug(), ent.EventsPublisher(conn))
c, err := ent.Open(dia, uri, ent.Debug(), ent.EventsPublisher(conn))
if err != nil {
log.Println(err)
testutils.IfErrPanic("failed terminating test db container after failing to connect to the db", cntr.Container.Terminate(ctx))
Expand All @@ -55,49 +72,55 @@ func setup(subChangesTopic string) (context.Context, *ent.Client, <-chan events.
switch dia {
case dialect.SQLite:
// Run automatic migrations for SQLite
testutils.IfErrPanic("failed creating db schema", entClient.Schema.Create(ctx))
testutils.IfErrPanic("failed creating db schema", c.Schema.Create(ctx))
case dialect.Postgres:
log.Println("Running database migrations")
goosex.MigrateUp(uri, db.Migrations)
}

// mock permissions
perms := new(mockpermissions.MockPermissions)
perms.On("CreateAuthRelationships", mock.Anything, mock.Anything, mock.Anything).Return(nil)
perms.On("DeleteAuthRelationships", mock.Anything, mock.Anything, mock.Anything).Return(nil)

ctx = perms.ContextWithHandler(ctx)

teardown := func() { teardown(entClient, cntr) }

return ctx, entClient, subChan, teardown
EventsConn = conn
EntClient = c
DBContainer = cntr
}

func teardown(entClient *ent.Client, dbc *testcontainersx.DBContainer) {
func teardown() {
ctx := context.Background()

if entClient != nil {
testutils.IfErrPanic("teardown failed to close database connection", entClient.Close())
if EntClient != nil {
testutils.IfErrPanic("teardown failed to close database connection", EntClient.Close())
}

if dbc != nil && dbc.Container.IsRunning() {
testutils.IfErrPanic("teardown failed to terminate test db container", dbc.Container.Terminate(ctx))
if DBContainer != nil && DBContainer.Container.IsRunning() {
testutils.IfErrPanic("teardown failed to terminate test db container", DBContainer.Container.Terminate(ctx))
}
}

func Test_LoadbalancerCreateUpdateHook(t *testing.T) {
func mockPermissions(ctx context.Context) context.Context {
// mock permissions
perms := new(mockpermissions.MockPermissions)
perms.On("CreateAuthRelationships", mock.Anything, mock.Anything, mock.Anything).Return(nil)
perms.On("DeleteAuthRelationships", mock.Anything, mock.Anything, mock.Anything).Return(nil)

ctx = perms.ContextWithHandler(ctx)

return ctx
}

func LoadbalancerCreateUpdateHookTest(t *testing.T) {
// Arrange
ctx, entClient, changesChannel, teardown := setup("update.load-balancer")
defer teardown()
ctx := mockPermissions(context.Background())

changesChannel, err := EventsConn.SubscribeChanges(ctx, "update.load-balancer")
testutils.IfErrPanic("failed to subscribe to changes", err)

ownerId := gidx.MustNewID(ownerPrefix)
provider := entClient.Provider.Create().SetName("provider-name").SetOwnerID(ownerId).SaveX(ctx)
lb := entClient.LoadBalancer.Create().SetName("lb-name").SetProvider(provider).SetOwnerID(ownerId).SetLocationID(gidx.MustNewID(locationPrefix)).SaveX(ctx)
provider := EntClient.Provider.Create().SetName("provider-name").SetOwnerID(ownerId).SaveX(ctx)
lb := EntClient.LoadBalancer.Create().SetName("lb-name").SetProvider(provider).SetOwnerID(ownerId).SetLocationID(gidx.MustNewID(locationPrefix)).SaveX(ctx)

entClient.LoadBalancer.Use(manualhooks.LoadBalancerHooks()...)
EntClient.LoadBalancer.Use(manualhooks.LoadBalancerHooks()...)

// Act
entClient.LoadBalancer.UpdateOne(lb).SetName(("other-lb-name")).ExecX(ctx)
EntClient.LoadBalancer.UpdateOne(lb).SetName(("other-lb-name")).ExecX(ctx)

msg := testutils.ChannelReceiveWithTimeout[events.Message[events.ChangeMessage]](changesChannel, defualtTimeout)

Expand All @@ -108,19 +131,21 @@ func Test_LoadbalancerCreateUpdateHook(t *testing.T) {
assert.ElementsMatch(t, expectedAdditionalSubjectIDs, actualAdditionalSubjectIDs)
}

func Test_LoadbalancerDeleteHook(t *testing.T) {
func LoadbalancerDeleteHookTest(t *testing.T) {
// Arrange
ctx, entClient, changesChannel, teardown := setup("delete.load-balancer")
defer teardown()
ctx := mockPermissions(context.Background())

changesChannel, err := EventsConn.SubscribeChanges(ctx, "delete.load-balancer")
testutils.IfErrPanic("failed to subscribe to changes", err)

ownerId := gidx.MustNewID(ownerPrefix)
provider := entClient.Provider.Create().SetName("provider-name").SetOwnerID(ownerId).SaveX(ctx)
lb := entClient.LoadBalancer.Create().SetName("lb-name").SetProvider(provider).SetOwnerID(ownerId).SetLocationID(gidx.MustNewID(locationPrefix)).SaveX(ctx)
provider := EntClient.Provider.Create().SetName("provider-name").SetOwnerID(ownerId).SaveX(ctx)
lb := EntClient.LoadBalancer.Create().SetName("lb-name").SetProvider(provider).SetOwnerID(ownerId).SetLocationID(gidx.MustNewID(locationPrefix)).SaveX(ctx)

entClient.LoadBalancer.Use(manualhooks.LoadBalancerHooks()...)
EntClient.LoadBalancer.Use(manualhooks.LoadBalancerHooks()...)

// Act
entClient.LoadBalancer.DeleteOneID(lb.ID).ExecX(ctx)
EntClient.LoadBalancer.DeleteOneID(lb.ID).ExecX(ctx)

msg := testutils.ChannelReceiveWithTimeout[events.Message[events.ChangeMessage]](changesChannel, defualtTimeout)

Expand All @@ -131,23 +156,25 @@ func Test_LoadbalancerDeleteHook(t *testing.T) {
assert.ElementsMatch(t, expectedAdditionalSubjectIDs, actualAdditionalSubjectIDs)
}

func Test_OriginCreateUpdateHook(t *testing.T) {
func OriginCreateUpdateHookTest(t *testing.T) {
// Arrange
ctx, entClient, changesChannel, teardown := setup("update.load-balancer-origin")
defer teardown()
ctx := mockPermissions(context.Background())

changesChannel, err := EventsConn.SubscribeChanges(ctx, "update.load-balancer-origin")
testutils.IfErrPanic("failed to subscribe to changes", err)

ownerId := gidx.MustNewID(ownerPrefix)

provider := entClient.Provider.Create().SetName("provider-name").SetOwnerID(ownerId).SaveX(ctx)
lb := entClient.LoadBalancer.Create().SetName("lb-name").SetProvider(provider).SetOwnerID(ownerId).SetLocationID(gidx.MustNewID(locationPrefix)).SaveX(ctx)
pool := entClient.Pool.Create().SetName("pool-name").SetOwnerID(ownerId).SetProtocol(pool.ProtocolTCP).SaveX(ctx)
entClient.Port.Create().SetName("port-name").AddPoolIDs(pool.ID).SetLoadBalancer(lb).SetNumber(11).SaveX(ctx)
origin := entClient.Origin.Create().SetName("origin-name").SetPool(pool).SetTarget("127.0.0.1").SetPortNumber(12).SaveX(ctx)
provider := EntClient.Provider.Create().SetName("provider-name").SetOwnerID(ownerId).SaveX(ctx)
lb := EntClient.LoadBalancer.Create().SetName("lb-name").SetProvider(provider).SetOwnerID(ownerId).SetLocationID(gidx.MustNewID(locationPrefix)).SaveX(ctx)
pool := EntClient.Pool.Create().SetName("pool-name").SetOwnerID(ownerId).SetProtocol(pool.ProtocolTCP).SaveX(ctx)
EntClient.Port.Create().SetName("port-name").AddPoolIDs(pool.ID).SetLoadBalancer(lb).SetNumber(11).SaveX(ctx)
origin := EntClient.Origin.Create().SetName("origin-name").SetPool(pool).SetTarget("127.0.0.1").SetPortNumber(12).SaveX(ctx)

entClient.Origin.Use(manualhooks.OriginHooks()...)
EntClient.Origin.Use(manualhooks.OriginHooks()...)

// Act
entClient.Origin.UpdateOne(origin).SetName("other-origin-name").ExecX(ctx)
EntClient.Origin.UpdateOne(origin).SetName("other-origin-name").ExecX(ctx)

msg := testutils.ChannelReceiveWithTimeout[events.Message[events.ChangeMessage]](changesChannel, defualtTimeout)

Expand All @@ -158,23 +185,25 @@ func Test_OriginCreateUpdateHook(t *testing.T) {
assert.ElementsMatch(t, expectedAdditionalSubjectIDs, actualAdditionalSubjectIDs)
}

func Test_OriginDeleteHook(t *testing.T) {
func OriginDeleteHookTest(t *testing.T) {
// Arrange
ctx, entClient, changesChannel, teardown := setup("delete.load-balancer-origin")
defer teardown()
ctx := mockPermissions(context.Background())

changesChannel, err := EventsConn.SubscribeChanges(ctx, "delete.load-balancer-origin")
testutils.IfErrPanic("failed to subscribe to changes", err)

ownerId := gidx.MustNewID(ownerPrefix)

provider := entClient.Provider.Create().SetName("provider-name").SetOwnerID(ownerId).SaveX(ctx)
lb := entClient.LoadBalancer.Create().SetName("lb-name").SetProvider(provider).SetOwnerID(ownerId).SetLocationID(gidx.MustNewID(locationPrefix)).SaveX(ctx)
pool := entClient.Pool.Create().SetName("pool-name").SetOwnerID(ownerId).SetProtocol(pool.ProtocolTCP).SaveX(ctx)
entClient.Port.Create().SetName("port-name").AddPoolIDs(pool.ID).SetLoadBalancer(lb).SetNumber(11).SaveX(ctx)
origin := entClient.Origin.Create().SetName("origin-name").SetPool(pool).SetTarget("127.0.0.1").SetPortNumber(12).SaveX(ctx)
provider := EntClient.Provider.Create().SetName("provider-name").SetOwnerID(ownerId).SaveX(ctx)
lb := EntClient.LoadBalancer.Create().SetName("lb-name").SetProvider(provider).SetOwnerID(ownerId).SetLocationID(gidx.MustNewID(locationPrefix)).SaveX(ctx)
pool := EntClient.Pool.Create().SetName("pool-name").SetOwnerID(ownerId).SetProtocol(pool.ProtocolTCP).SaveX(ctx)
EntClient.Port.Create().SetName("port-name").AddPoolIDs(pool.ID).SetLoadBalancer(lb).SetNumber(11).SaveX(ctx)
origin := EntClient.Origin.Create().SetName("origin-name").SetPool(pool).SetTarget("127.0.0.1").SetPortNumber(12).SaveX(ctx)

entClient.Origin.Use(manualhooks.OriginHooks()...)
EntClient.Origin.Use(manualhooks.OriginHooks()...)

// Act
entClient.Origin.DeleteOne(origin).ExecX(ctx)
EntClient.Origin.DeleteOne(origin).ExecX(ctx)

msg := testutils.ChannelReceiveWithTimeout[events.Message[events.ChangeMessage]](changesChannel, defualtTimeout)

Expand All @@ -185,23 +214,25 @@ func Test_OriginDeleteHook(t *testing.T) {
assert.ElementsMatch(t, expectedAdditionalSubjectIDs, actualAdditionalSubjectIDs)
}

func Test_PoolCreateUpdateHook(t *testing.T) {
func PoolCreateUpdateHookTest(t *testing.T) {
// Arrange
ctx, entClient, changesChannel, teardown := setup("update.load-balancer-pool")
defer teardown()
ctx := mockPermissions(context.Background())

changesChannel, err := EventsConn.SubscribeChanges(ctx, "update.load-balancer-pool")
testutils.IfErrPanic("failed to subscribe to changes", err)

ownerId := gidx.MustNewID(ownerPrefix)

provider := entClient.Provider.Create().SetName("provider-name").SetOwnerID(ownerId).SaveX(ctx)
lb := entClient.LoadBalancer.Create().SetName("lb-name").SetProvider(provider).SetOwnerID(ownerId).SetLocationID(gidx.MustNewID(locationPrefix)).SaveX(ctx)
pool := entClient.Pool.Create().SetName("pool-name").SetOwnerID(ownerId).SetProtocol(pool.ProtocolTCP).SaveX(ctx)
port := entClient.Port.Create().SetName("port-name").AddPoolIDs(pool.ID).SetLoadBalancer(lb).SetNumber(11).SaveX(ctx)
origin := entClient.Origin.Create().SetName("origin-name").SetPool(pool).SetTarget("127.0.0.1").SetPortNumber(12).SaveX(ctx)
provider := EntClient.Provider.Create().SetName("provider-name").SetOwnerID(ownerId).SaveX(ctx)
lb := EntClient.LoadBalancer.Create().SetName("lb-name").SetProvider(provider).SetOwnerID(ownerId).SetLocationID(gidx.MustNewID(locationPrefix)).SaveX(ctx)
pool := EntClient.Pool.Create().SetName("pool-name").SetOwnerID(ownerId).SetProtocol(pool.ProtocolTCP).SaveX(ctx)
port := EntClient.Port.Create().SetName("port-name").AddPoolIDs(pool.ID).SetLoadBalancer(lb).SetNumber(11).SaveX(ctx)
origin := EntClient.Origin.Create().SetName("origin-name").SetPool(pool).SetTarget("127.0.0.1").SetPortNumber(12).SaveX(ctx)

entClient.Pool.Use(manualhooks.PoolHooks()...)
EntClient.Pool.Use(manualhooks.PoolHooks()...)

// Act
entClient.Pool.UpdateOne(pool).SetName("other-pool-name").ExecX(ctx)
EntClient.Pool.UpdateOne(pool).SetName("other-pool-name").ExecX(ctx)

msg := testutils.ChannelReceiveWithTimeout[events.Message[events.ChangeMessage]](changesChannel, defualtTimeout)

Expand All @@ -212,22 +243,24 @@ func Test_PoolCreateUpdateHook(t *testing.T) {
assert.ElementsMatch(t, expectedAdditionalSubjectIDs, actualAdditionalSubjectIDs)
}

func Test_PoolDeleteHook(t *testing.T) {
func PoolDeleteHookTest(t *testing.T) {
// Arrange
ctx, entClient, changesChannel, teardown := setup("delete.load-balancer-pool")
defer teardown()
ctx := mockPermissions(context.Background())

changesChannel, err := EventsConn.SubscribeChanges(ctx, "delete.load-balancer-pool")
testutils.IfErrPanic("failed to subscribe to changes", err)

ownerId := gidx.MustNewID(ownerPrefix)

provider := entClient.Provider.Create().SetName("provider-name").SetOwnerID(ownerId).SaveX(ctx)
lb := entClient.LoadBalancer.Create().SetName("lb-name").SetProvider(provider).SetOwnerID(ownerId).SetLocationID(gidx.MustNewID(locationPrefix)).SaveX(ctx)
pool := entClient.Pool.Create().SetName("pool-name").SetOwnerID(ownerId).SetProtocol(pool.ProtocolTCP).SaveX(ctx)
entClient.Port.Create().AddPoolIDs(pool.ID).SetLoadBalancer(lb).SetName("port-name").SetNumber(11).SaveX(ctx)
provider := EntClient.Provider.Create().SetName("provider-name").SetOwnerID(ownerId).SaveX(ctx)
lb := EntClient.LoadBalancer.Create().SetName("lb-name").SetProvider(provider).SetOwnerID(ownerId).SetLocationID(gidx.MustNewID(locationPrefix)).SaveX(ctx)
pool := EntClient.Pool.Create().SetName("pool-name").SetOwnerID(ownerId).SetProtocol(pool.ProtocolTCP).SaveX(ctx)
EntClient.Port.Create().AddPoolIDs(pool.ID).SetLoadBalancer(lb).SetName("port-name").SetNumber(11).SaveX(ctx)

entClient.Pool.Use(manualhooks.PoolHooks()...)
EntClient.Pool.Use(manualhooks.PoolHooks()...)

// Act
entClient.Pool.DeleteOne(pool).ExecX(ctx)
EntClient.Pool.DeleteOne(pool).ExecX(ctx)

msg := testutils.ChannelReceiveWithTimeout[events.Message[events.ChangeMessage]](changesChannel, defualtTimeout)

Expand All @@ -238,22 +271,24 @@ func Test_PoolDeleteHook(t *testing.T) {
assert.ElementsMatch(t, expectedAdditionalSubjectIDs, actualAdditionalSubjectIDs)
}

func Test_PortCreateUpdateHook(t *testing.T) {
func PortCreateUpdateHookTest(t *testing.T) {
// Arrange
ctx, entClient, changesChannel, teardown := setup("update.load-balancer-port")
defer teardown()
ctx := mockPermissions(context.Background())

changesChannel, err := EventsConn.SubscribeChanges(ctx, "update.load-balancer-port")
testutils.IfErrPanic("failed to subscribe to changes", err)

ownerId := gidx.MustNewID(ownerPrefix)

provider := entClient.Provider.Create().SetName("provider-name").SetOwnerID(ownerId).SaveX(ctx)
lb := entClient.LoadBalancer.Create().SetName("lb-name").SetProvider(provider).SetOwnerID(ownerId).SetLocationID(gidx.MustNewID(locationPrefix)).SaveX(ctx)
pool := entClient.Pool.Create().SetName("pool-name").SetOwnerID(ownerId).SetProtocol(pool.ProtocolTCP).SaveX(ctx)
port := entClient.Port.Create().AddPoolIDs(pool.ID).SetLoadBalancer(lb).SetName("port-name").SetNumber(11).SaveX(ctx)
provider := EntClient.Provider.Create().SetName("provider-name").SetOwnerID(ownerId).SaveX(ctx)
lb := EntClient.LoadBalancer.Create().SetName("lb-name").SetProvider(provider).SetOwnerID(ownerId).SetLocationID(gidx.MustNewID(locationPrefix)).SaveX(ctx)
pool := EntClient.Pool.Create().SetName("pool-name").SetOwnerID(ownerId).SetProtocol(pool.ProtocolTCP).SaveX(ctx)
port := EntClient.Port.Create().AddPoolIDs(pool.ID).SetLoadBalancer(lb).SetName("port-name").SetNumber(11).SaveX(ctx)

entClient.Port.Use(manualhooks.PortHooks()...)
EntClient.Port.Use(manualhooks.PortHooks()...)

// Act
entClient.Port.UpdateOne(port).SetName("other-port-name").ExecX(ctx)
EntClient.Port.UpdateOne(port).SetName("other-port-name").ExecX(ctx)

msg := testutils.ChannelReceiveWithTimeout[events.Message[events.ChangeMessage]](changesChannel, defualtTimeout)

Expand All @@ -264,22 +299,24 @@ func Test_PortCreateUpdateHook(t *testing.T) {
assert.ElementsMatch(t, expectedAdditionalSubjectIDs, actualAdditionalSubjectIDs)
}

func Test_PortDeleteHook(t *testing.T) {
func PortDeleteHookTest(t *testing.T) {
// Arrange
ctx, entClient, changesChannel, teardown := setup("delete.load-balancer-port")
defer teardown()
ctx := mockPermissions(context.Background())

changesChannel, err := EventsConn.SubscribeChanges(ctx, "delete.load-balancer-port")
testutils.IfErrPanic("failed to subscribe to changes", err)

ownerId := gidx.MustNewID(ownerPrefix)

provider := entClient.Provider.Create().SetName("provider-name").SetOwnerID(ownerId).SaveX(ctx)
lb := entClient.LoadBalancer.Create().SetName("lb-name").SetProvider(provider).SetOwnerID(ownerId).SetLocationID(gidx.MustNewID(locationPrefix)).SaveX(ctx)
pool := entClient.Pool.Create().SetName("pool-name").SetOwnerID(ownerId).SetProtocol(pool.ProtocolTCP).SaveX(ctx)
port := entClient.Port.Create().AddPoolIDs(pool.ID).SetLoadBalancer(lb).SetName("port-name").SetNumber(11).SaveX(ctx)
provider := EntClient.Provider.Create().SetName("provider-name").SetOwnerID(ownerId).SaveX(ctx)
lb := EntClient.LoadBalancer.Create().SetName("lb-name").SetProvider(provider).SetOwnerID(ownerId).SetLocationID(gidx.MustNewID(locationPrefix)).SaveX(ctx)
pool := EntClient.Pool.Create().SetName("pool-name").SetOwnerID(ownerId).SetProtocol(pool.ProtocolTCP).SaveX(ctx)
port := EntClient.Port.Create().AddPoolIDs(pool.ID).SetLoadBalancer(lb).SetName("port-name").SetNumber(11).SaveX(ctx)

entClient.Port.Use(manualhooks.PortHooks()...)
EntClient.Port.Use(manualhooks.PortHooks()...)

// Act
entClient.Port.DeleteOne(port).ExecX(ctx)
EntClient.Port.DeleteOne(port).ExecX(ctx)

msg := testutils.ChannelReceiveWithTimeout[events.Message[events.ChangeMessage]](changesChannel, defualtTimeout)

Expand Down

0 comments on commit 5f4ae51

Please sign in to comment.