Skip to content

Commit f66f95b

Browse files
Fix database tests
Signed-off-by: Gabriel Adrian Samfira <gsamfira@cloudbasesolutions.com>
1 parent 5a93761 commit f66f95b

File tree

6 files changed

+52
-8
lines changed

6 files changed

+52
-8
lines changed

database/sql/controller_test.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323

2424
runnerErrors "github.com/cloudbase/garm-provider-common/errors"
2525
dbCommon "github.com/cloudbase/garm/database/common"
26+
"github.com/cloudbase/garm/database/watcher"
2627
garmTesting "github.com/cloudbase/garm/internal/testing" //nolint:typecheck
2728
)
2829

@@ -32,13 +33,19 @@ type CtrlTestSuite struct {
3233
}
3334

3435
func (s *CtrlTestSuite) SetupTest() {
35-
db, err := NewSQLDatabase(context.Background(), garmTesting.GetTestSqliteDBConfig(s.T()))
36+
ctx := context.Background()
37+
watcher.InitWatcher(ctx)
38+
db, err := NewSQLDatabase(ctx, garmTesting.GetTestSqliteDBConfig(s.T()))
3639
if err != nil {
3740
s.FailNow(fmt.Sprintf("failed to create db connection: %s", err))
3841
}
3942
s.Store = db
4043
}
4144

45+
func (s *CtrlTestSuite) TearDownTest() {
46+
watcher.CloseWatcher()
47+
}
48+
4249
func (s *CtrlTestSuite) TestControllerInfo() {
4350
initCtrlInfo, err := s.Store.InitController()
4451
if err != nil {

database/sql/enterprise_test.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import (
3030

3131
"github.com/cloudbase/garm/auth"
3232
dbCommon "github.com/cloudbase/garm/database/common"
33+
"github.com/cloudbase/garm/database/watcher"
3334
garmTesting "github.com/cloudbase/garm/internal/testing"
3435
"github.com/cloudbase/garm/params"
3536
)
@@ -78,15 +79,21 @@ func (s *EnterpriseTestSuite) assertSQLMockExpectations() {
7879
}
7980
}
8081

82+
func (s *EnterpriseTestSuite) TearDownTest() {
83+
watcher.CloseWatcher()
84+
}
85+
8186
func (s *EnterpriseTestSuite) SetupTest() {
87+
ctx := context.Background()
88+
watcher.InitWatcher(ctx)
8289
// create testing sqlite database
83-
db, err := NewSQLDatabase(context.Background(), garmTesting.GetTestSqliteDBConfig(s.T()))
90+
db, err := NewSQLDatabase(ctx, garmTesting.GetTestSqliteDBConfig(s.T()))
8491
if err != nil {
8592
s.FailNow(fmt.Sprintf("failed to create db connection: %s", err))
8693
}
8794
s.Store = db
8895

89-
adminCtx := garmTesting.ImpersonateAdminContext(context.Background(), db, s.T())
96+
adminCtx := garmTesting.ImpersonateAdminContext(ctx, db, s.T())
9097
s.adminCtx = adminCtx
9198
s.adminUserID = auth.UserID(adminCtx)
9299
s.Require().NotEmpty(s.adminUserID)

database/sql/gitea_test.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import (
2525
runnerErrors "github.com/cloudbase/garm-provider-common/errors"
2626
"github.com/cloudbase/garm/auth"
2727
"github.com/cloudbase/garm/database/common"
28+
"github.com/cloudbase/garm/database/watcher"
2829
garmTesting "github.com/cloudbase/garm/internal/testing"
2930
"github.com/cloudbase/garm/params"
3031
)
@@ -37,7 +38,9 @@ type GiteaTestSuite struct {
3738
}
3839

3940
func (s *GiteaTestSuite) SetupTest() {
40-
db, err := NewSQLDatabase(context.Background(), garmTesting.GetTestSqliteDBConfig(s.T()))
41+
ctx := context.Background()
42+
watcher.InitWatcher(ctx)
43+
db, err := NewSQLDatabase(ctx, garmTesting.GetTestSqliteDBConfig(s.T()))
4144
if err != nil {
4245
s.FailNow(fmt.Sprintf("failed to create db connection: %s", err))
4346
}
@@ -50,13 +53,17 @@ func (s *GiteaTestSuite) SetupTest() {
5053
APIBaseURL: testAPIBaseURL,
5154
BaseURL: testBaseURL,
5255
}
53-
endpoint, err := s.db.CreateGiteaEndpoint(context.Background(), createEpParams)
56+
endpoint, err := s.db.CreateGiteaEndpoint(ctx, createEpParams)
5457
s.Require().NoError(err)
5558
s.Require().NotNil(endpoint)
5659
s.Require().Equal(testEndpointName, endpoint.Name)
5760
s.giteaEndpoint = endpoint
5861
}
5962

63+
func (s *GiteaTestSuite) TearDownTest() {
64+
watcher.CloseWatcher()
65+
}
66+
6067
func (s *GiteaTestSuite) TestCreatingEndpoint() {
6168
ctx := garmTesting.ImpersonateAdminContext(context.Background(), s.db, s.T())
6269

database/sql/github_test.go

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import (
2828
"github.com/cloudbase/garm/auth"
2929
"github.com/cloudbase/garm/config"
3030
"github.com/cloudbase/garm/database/common"
31+
"github.com/cloudbase/garm/database/watcher"
3132
garmTesting "github.com/cloudbase/garm/internal/testing"
3233
"github.com/cloudbase/garm/params"
3334
)
@@ -51,13 +52,19 @@ type GithubTestSuite struct {
5152
}
5253

5354
func (s *GithubTestSuite) SetupTest() {
55+
ctx := context.Background()
56+
watcher.InitWatcher(ctx)
5457
db, err := NewSQLDatabase(context.Background(), garmTesting.GetTestSqliteDBConfig(s.T()))
5558
if err != nil {
5659
s.FailNow(fmt.Sprintf("failed to create db connection: %s", err))
5760
}
5861
s.db = db
5962
}
6063

64+
func (s *GithubTestSuite) TearDownTest() {
65+
watcher.CloseWatcher()
66+
}
67+
6168
func (s *GithubTestSuite) TestDefaultEndpointGetsCreatedAutomaticallyIfNoOtherEndpointExists() {
6269
ctx := garmTesting.ImpersonateAdminContext(context.Background(), s.db, s.T())
6370
endpoint, err := s.db.GetGithubEndpoint(ctx, defaultGithubEndpoint)
@@ -946,14 +953,16 @@ func TestCredentialsAndEndpointMigration(t *testing.T) {
946953
// Set the config credentials in the cfg. This is what happens in the main function.
947954
// of GARM as well.
948955
cfg.MigrateCredentials = credentials
949-
950-
db, err := NewSQLDatabase(context.Background(), cfg)
956+
ctx := context.Background()
957+
watcher.InitWatcher(ctx)
958+
defer watcher.CloseWatcher()
959+
db, err := NewSQLDatabase(ctx, cfg)
951960
if err != nil {
952961
t.Fatalf("failed to create db connection: %s", err)
953962
}
954963

955964
// We expect that 2 endpoints will exist in the migrated DB and 2 credentials.
956-
ctx := garmTesting.ImpersonateAdminContext(context.Background(), db, t)
965+
ctx = garmTesting.ImpersonateAdminContext(ctx, db, t)
957966

958967
endpoints, err := db.ListGithubEndpoints(ctx)
959968
if err != nil {

database/sql/instances_test.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import (
3131

3232
commonParams "github.com/cloudbase/garm-provider-common/params"
3333
dbCommon "github.com/cloudbase/garm/database/common"
34+
"github.com/cloudbase/garm/database/watcher"
3435
garmTesting "github.com/cloudbase/garm/internal/testing"
3536
"github.com/cloudbase/garm/params"
3637
)
@@ -70,7 +71,13 @@ func (s *InstancesTestSuite) assertSQLMockExpectations() {
7071
}
7172
}
7273

74+
func (s *InstancesTestSuite) TearDownTest() {
75+
watcher.CloseWatcher()
76+
}
77+
7378
func (s *InstancesTestSuite) SetupTest() {
79+
ctx := context.Background()
80+
watcher.InitWatcher(ctx)
7481
// create testing sqlite database
7582
db, err := NewSQLDatabase(context.Background(), garmTesting.GetTestSqliteDBConfig(s.T()))
7683
if err != nil {

database/sql/organizations_test.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import (
3030

3131
"github.com/cloudbase/garm/auth"
3232
dbCommon "github.com/cloudbase/garm/database/common"
33+
"github.com/cloudbase/garm/database/watcher"
3334
garmTesting "github.com/cloudbase/garm/internal/testing"
3435
"github.com/cloudbase/garm/params"
3536
)
@@ -78,7 +79,13 @@ func (s *OrgTestSuite) assertSQLMockExpectations() {
7879
}
7980
}
8081

82+
func (s *OrgTestSuite) TearDownTest() {
83+
watcher.CloseWatcher()
84+
}
85+
8186
func (s *OrgTestSuite) SetupTest() {
87+
ctx := context.Background()
88+
watcher.InitWatcher(ctx)
8289
// create testing sqlite database
8390
dbConfig := garmTesting.GetTestSqliteDBConfig(s.T())
8491
db, err := NewSQLDatabase(context.Background(), dbConfig)

0 commit comments

Comments
 (0)