Skip to content

Commit

Permalink
Refactor sync integration test with test containers. (#343)
Browse files Browse the repository at this point in the history
Refactor legacy integration test using testsuite and testcontainer.
  • Loading branch information
sudiptob2 authored Nov 26, 2024
1 parent f8078ce commit a944ebf
Show file tree
Hide file tree
Showing 17 changed files with 1,114 additions and 958 deletions.
10 changes: 5 additions & 5 deletions internal/database/repository/application_int_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@ import (
"github.com/G-Research/unicorn-history-server/internal/util"
)

type ApplicationTestSuite struct {
type ApplicationIntTest struct {
suite.Suite
pool *pgxpool.Pool
repo *PostgresRepository
}

func (as *ApplicationTestSuite) SetupSuite() {
func (as *ApplicationIntTest) SetupSuite() {
ctx := context.Background()
require.NotNil(as.T(), as.pool)
repo, err := NewPostgresRepository(as.pool)
Expand All @@ -32,11 +32,11 @@ func (as *ApplicationTestSuite) SetupSuite() {
seedApplications(ctx, as.T(), as.repo)
}

func (as *ApplicationTestSuite) TearDownSuite() {
func (as *ApplicationIntTest) TearDownSuite() {
as.pool.Close()
}

func (as *ApplicationTestSuite) TestGetAllApplications() {
func (as *ApplicationIntTest) TestGetAllApplications() {
ctx := context.Background()
tests := []struct {
name string
Expand Down Expand Up @@ -106,7 +106,7 @@ func (as *ApplicationTestSuite) TestGetAllApplications() {
}
}

func (as *ApplicationTestSuite) TestGetAppsPerPartitionPerQueue() {
func (as *ApplicationIntTest) TestGetAppsPerPartitionPerQueue() {
ctx := context.Background()
tests := []struct {
name string
Expand Down
10 changes: 5 additions & 5 deletions internal/database/repository/history_int_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@ import (
"github.com/G-Research/unicorn-history-server/internal/util"
)

type HistoryTestSuite struct {
type HistoryIntTest struct {
suite.Suite
pool *pgxpool.Pool
repo *PostgresRepository
}

func (hs *HistoryTestSuite) SetupSuite() {
func (hs *HistoryIntTest) SetupSuite() {
ctx := context.Background()
require.NotNil(hs.T(), hs.pool)
repo, err := NewPostgresRepository(hs.pool)
Expand All @@ -31,11 +31,11 @@ func (hs *HistoryTestSuite) SetupSuite() {
seedHistory(ctx, hs.T(), hs.repo)
}

func (hs *HistoryTestSuite) TearDownSuite() {
func (hs *HistoryIntTest) TearDownSuite() {
hs.pool.Close()
}

func (hs *HistoryTestSuite) TestGetApplicationsHistory() {
func (hs *HistoryIntTest) TestGetApplicationsHistory() {
ctx := context.Background()
tests := []struct {
name string
Expand Down Expand Up @@ -76,7 +76,7 @@ func (hs *HistoryTestSuite) TestGetApplicationsHistory() {
}
}

func (hs *HistoryTestSuite) TestGetContainersHistory() {
func (hs *HistoryIntTest) TestGetContainersHistory() {
ctx := context.Background()
tests := []struct {
name string
Expand Down
8 changes: 4 additions & 4 deletions internal/database/repository/node_int_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@ import (
"github.com/G-Research/unicorn-history-server/internal/util"
)

type NodeTestSuite struct {
type NodeIntTest struct {
suite.Suite
pool *pgxpool.Pool
repo *PostgresRepository
}

var partitionID = ulid.Make().String()

func (ns *NodeTestSuite) SetupSuite() {
func (ns *NodeIntTest) SetupSuite() {
ctx := context.Background()
require.NotNil(ns.T(), ns.pool)
repo, err := NewPostgresRepository(ns.pool)
Expand All @@ -34,11 +34,11 @@ func (ns *NodeTestSuite) SetupSuite() {
seedNodes(ctx, ns.T(), ns.repo)
}

func (ns *NodeTestSuite) TearDownSuite() {
func (ns *NodeIntTest) TearDownSuite() {
ns.pool.Close()
}

func (ns *NodeTestSuite) TestGetNodesPerPartition() {
func (ns *NodeIntTest) TestGetNodesPerPartition() {
ctx := context.Background()
tests := []struct {
name string
Expand Down
8 changes: 4 additions & 4 deletions internal/database/repository/partitions_init_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@ import (
"github.com/G-Research/unicorn-history-server/internal/util"
)

type PartitionTestSuite struct {
type PartitionIntTest struct {
suite.Suite
pool *pgxpool.Pool
repo *PostgresRepository
}

func (ps *PartitionTestSuite) SetupSuite() {
func (ps *PartitionIntTest) SetupSuite() {
ctx := context.Background()
require.NotNil(ps.T(), ps.pool)
repo, err := NewPostgresRepository(ps.pool)
Expand All @@ -30,11 +30,11 @@ func (ps *PartitionTestSuite) SetupSuite() {
seedPartitions(ctx, ps.T(), ps.repo)
}

func (ps *PartitionTestSuite) TearDownSuite() {
func (ps *PartitionIntTest) TearDownSuite() {
ps.pool.Close()
}

func (ps *PartitionTestSuite) TestGetAllPartitions() {
func (ps *PartitionIntTest) TestGetAllPartitions() {
ctx := context.Background()
tests := []struct {
name string
Expand Down
16 changes: 8 additions & 8 deletions internal/database/repository/queue_int_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@ import (
"github.com/stretchr/testify/suite"
)

type QueueTestSuite struct {
type QueueIntTest struct {
suite.Suite
pool *pgxpool.Pool
repo *PostgresRepository
}

func (qs *QueueTestSuite) SetupSuite() {
func (qs *QueueIntTest) SetupSuite() {
require.NotNil(qs.T(), qs.pool)
repo, err := NewPostgresRepository(qs.pool)
require.NoError(qs.T(), err)
Expand All @@ -28,11 +28,11 @@ func (qs *QueueTestSuite) SetupSuite() {
seedQueues(qs.T(), qs.repo)
}

func (qs *QueueTestSuite) TearDownSuite() {
func (qs *QueueIntTest) TearDownSuite() {
qs.pool.Close()
}

func (qs *QueueTestSuite) TestGetAllQueues() {
func (qs *QueueIntTest) TestGetAllQueues() {
ctx := context.Background()
tests := []struct {
name string
Expand All @@ -53,7 +53,7 @@ func (qs *QueueTestSuite) TestGetAllQueues() {
}
}

func (qs *QueueTestSuite) TestGetQueuesInPartition() {
func (qs *QueueIntTest) TestGetQueuesInPartition() {
ctx := context.Background()
tests := []struct {
name string
Expand Down Expand Up @@ -81,7 +81,7 @@ func (qs *QueueTestSuite) TestGetQueuesInPartition() {
}
}

func (qs *QueueTestSuite) TestGetQueue() {
func (qs *QueueIntTest) TestGetQueue() {
ctx := context.Background()
tests := []struct {
name string
Expand Down Expand Up @@ -119,7 +119,7 @@ func (qs *QueueTestSuite) TestGetQueue() {
}
}

func (qs *QueueTestSuite) TestDeleteQueues() {
func (qs *QueueIntTest) TestDeleteQueues() {
ctx := context.Background()
tests := []struct {
name string
Expand Down Expand Up @@ -163,7 +163,7 @@ func (qs *QueueTestSuite) TestDeleteQueues() {
}
}

func (qs *QueueTestSuite) TestUpdateQueue() {
func (qs *QueueIntTest) TestUpdateQueue() {
ctx := context.Background()
now := time.Now()
tests := []struct {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@ import (
"github.com/G-Research/unicorn-history-server/test/database"
)

type RepositoryTestSuite struct {
type RepositorySuite struct {
suite.Suite
tp *database.TestPostgresContainer
pool *pgxpool.Pool
}

func (ts *RepositoryTestSuite) SetupSuite() {
func (ts *RepositorySuite) SetupSuite() {
ctx := context.Background()
cfg := database.InstanceConfig{
User: "test",
Expand All @@ -36,35 +36,38 @@ func (ts *RepositoryTestSuite) SetupSuite() {
ts.pool = tp.Pool(ctx, ts.T(), &cfg)
}

func (ts *RepositoryTestSuite) TearDownSuite() {
func (ts *RepositorySuite) TearDownSuite() {
err := ts.tp.Container.Terminate(context.Background())
require.NoError(ts.T(), err)
}

func (ts *RepositoryTestSuite) TestSubSuites() {
ts.T().Run("ApplicationTestSuite", func(t *testing.T) {
func (ts *RepositorySuite) TestSubSuites() {
ts.T().Run("ApplicationIntTest", func(t *testing.T) {
pool := database.CloneDB(t, ts.tp, ts.pool)
suite.Run(t, &ApplicationTestSuite{pool: pool})
suite.Run(t, &ApplicationIntTest{pool: pool})
})
ts.T().Run("HistoryTestSuite", func(t *testing.T) {
ts.T().Run("HistoryIntTest", func(t *testing.T) {
pool := database.CloneDB(t, ts.tp, ts.pool)
suite.Run(t, &HistoryTestSuite{pool: pool})
suite.Run(t, &HistoryIntTest{pool: pool})
})
ts.T().Run("NodeTestSuite", func(t *testing.T) {
ts.T().Run("NodeIntTest", func(t *testing.T) {
pool := database.CloneDB(t, ts.tp, ts.pool)
suite.Run(t, &NodeTestSuite{pool: pool})
suite.Run(t, &NodeIntTest{pool: pool})
})
ts.T().Run("QueueTestSuite", func(t *testing.T) {
ts.T().Run("QueueIntTest", func(t *testing.T) {
pool := database.CloneDB(t, ts.tp, ts.pool)
suite.Run(t, &QueueTestSuite{pool: pool})
suite.Run(t, &QueueIntTest{pool: pool})
})
ts.T().Run("PartitionTestSuite", func(t *testing.T) {
ts.T().Run("PartitionIntTest", func(t *testing.T) {
pool := database.CloneDB(t, ts.tp, ts.pool)
suite.Run(t, &PartitionTestSuite{pool: pool})
suite.Run(t, &PartitionIntTest{pool: pool})
})
}

func TestRepositoryIntegrationTestSuite(t *testing.T) {
topSuite := new(RepositoryTestSuite)
func TestRepositoryIntegration(t *testing.T) {
if testing.Short() {
t.Skip("skipping integration test in short mode.")
}
topSuite := new(RepositorySuite)
suite.Run(t, topSuite)
}
2 changes: 1 addition & 1 deletion internal/database/repository/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ type Repository interface {
UpdateNode(ctx context.Context, node *model.Node) error
GetNodeByID(ctx context.Context, id string) (*model.Node, error)
DeleteNodesNotInIDs(ctx context.Context, ids []string, deletedAtNano int64) error
GetNodesPerPartition(ctx context.Context, partition string, filters NodeFilters) ([]*model.Node, error)
GetNodesPerPartition(ctx context.Context, partitionID string, filters NodeFilters) ([]*model.Node, error)
InsertPartition(ctx context.Context, partition *model.Partition) error
UpdatePartition(ctx context.Context, partition *model.Partition) error
GetAllPartitions(ctx context.Context, filters PartitionFilters) ([]*model.Partition, error)
Expand Down
41 changes: 22 additions & 19 deletions internal/health/components_int_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,30 @@ package health

import (
"context"
"testing"

"github.com/stretchr/testify/assert"

"github.com/G-Research/unicorn-history-server/internal/database/postgres"
"github.com/G-Research/unicorn-history-server/internal/yunikorn"
"github.com/G-Research/unicorn-history-server/test/config"
"github.com/jackc/pgx/v5/pgxpool"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/suite"
)

func TestNewComponent_Integration(t *testing.T) {
if testing.Short() {
t.Skip("skipping integration test in short mode.")
}
type ComponentsIntTest struct {
suite.Suite
pool *pgxpool.Pool
yunikornClient *yunikorn.RESTClient
}

ctx := context.Background()
func (ts *ComponentsIntTest) SetupSuite() {
ts.yunikornClient = yunikorn.NewRESTClient(config.GetTestYunikornConfig())
}

yunikornClient := yunikorn.NewRESTClient(config.GetTestYunikornConfig())
postgresPool, err := postgres.NewConnectionPool(ctx, config.GetTestPostgresConfig())
if err != nil {
t.Fatalf("error creating postgres connection pool: %v", err)
}
func (ts *ComponentsIntTest) TearDownSuite() {
ts.pool.Close()
}

func (ts *ComponentsIntTest) TestNewComponents() {
ctx := context.Background()

tests := []struct {
name string
Expand All @@ -32,23 +35,23 @@ func TestNewComponent_Integration(t *testing.T) {
}{
{
name: "should return a valid ComponentStatus when Yunikorn is reachable",
component: NewYunikornComponent(yunikornClient),
component: NewYunikornComponent(ts.yunikornClient),
expectedIdentifier: "yunikorn",
expectedHealthy: true,
},
{
name: "should return a valid ComponentStatus when Postgres is reachable",
component: NewPostgresComponent(postgresPool),
component: NewPostgresComponent(ts.pool),
expectedIdentifier: "postgres",
expectedHealthy: true,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
ts.Run(tt.name, func() {
status := tt.component.Check(ctx)
assert.Equal(t, tt.expectedIdentifier, status.Identifier)
assert.Equal(t, tt.expectedHealthy, status.Healthy)
assert.Equal(ts.T(), tt.expectedIdentifier, status.Identifier)
assert.Equal(ts.T(), tt.expectedHealthy, status.Healthy)
})
}
}
Loading

0 comments on commit a944ebf

Please sign in to comment.