Fix database tests

Signed-off-by: Gabriel Adrian Samfira <gsamfira@cloudbasesolutions.com>
This commit is contained in:
Gabriel Adrian Samfira 2025-10-07 17:55:42 +00:00 committed by Gabriel
parent 5a93761af7
commit f66f95baff
6 changed files with 52 additions and 8 deletions

View file

@ -23,6 +23,7 @@ import (
runnerErrors "github.com/cloudbase/garm-provider-common/errors"
dbCommon "github.com/cloudbase/garm/database/common"
"github.com/cloudbase/garm/database/watcher"
garmTesting "github.com/cloudbase/garm/internal/testing" //nolint:typecheck
)
@ -32,13 +33,19 @@ type CtrlTestSuite struct {
}
func (s *CtrlTestSuite) SetupTest() {
db, err := NewSQLDatabase(context.Background(), garmTesting.GetTestSqliteDBConfig(s.T()))
ctx := context.Background()
watcher.InitWatcher(ctx)
db, err := NewSQLDatabase(ctx, garmTesting.GetTestSqliteDBConfig(s.T()))
if err != nil {
s.FailNow(fmt.Sprintf("failed to create db connection: %s", err))
}
s.Store = db
}
func (s *CtrlTestSuite) TearDownTest() {
watcher.CloseWatcher()
}
func (s *CtrlTestSuite) TestControllerInfo() {
initCtrlInfo, err := s.Store.InitController()
if err != nil {

View file

@ -30,6 +30,7 @@ import (
"github.com/cloudbase/garm/auth"
dbCommon "github.com/cloudbase/garm/database/common"
"github.com/cloudbase/garm/database/watcher"
garmTesting "github.com/cloudbase/garm/internal/testing"
"github.com/cloudbase/garm/params"
)
@ -78,15 +79,21 @@ func (s *EnterpriseTestSuite) assertSQLMockExpectations() {
}
}
func (s *EnterpriseTestSuite) TearDownTest() {
watcher.CloseWatcher()
}
func (s *EnterpriseTestSuite) SetupTest() {
ctx := context.Background()
watcher.InitWatcher(ctx)
// create testing sqlite database
db, err := NewSQLDatabase(context.Background(), garmTesting.GetTestSqliteDBConfig(s.T()))
db, err := NewSQLDatabase(ctx, garmTesting.GetTestSqliteDBConfig(s.T()))
if err != nil {
s.FailNow(fmt.Sprintf("failed to create db connection: %s", err))
}
s.Store = db
adminCtx := garmTesting.ImpersonateAdminContext(context.Background(), db, s.T())
adminCtx := garmTesting.ImpersonateAdminContext(ctx, db, s.T())
s.adminCtx = adminCtx
s.adminUserID = auth.UserID(adminCtx)
s.Require().NotEmpty(s.adminUserID)

View file

@ -25,6 +25,7 @@ import (
runnerErrors "github.com/cloudbase/garm-provider-common/errors"
"github.com/cloudbase/garm/auth"
"github.com/cloudbase/garm/database/common"
"github.com/cloudbase/garm/database/watcher"
garmTesting "github.com/cloudbase/garm/internal/testing"
"github.com/cloudbase/garm/params"
)
@ -37,7 +38,9 @@ type GiteaTestSuite struct {
}
func (s *GiteaTestSuite) SetupTest() {
db, err := NewSQLDatabase(context.Background(), garmTesting.GetTestSqliteDBConfig(s.T()))
ctx := context.Background()
watcher.InitWatcher(ctx)
db, err := NewSQLDatabase(ctx, garmTesting.GetTestSqliteDBConfig(s.T()))
if err != nil {
s.FailNow(fmt.Sprintf("failed to create db connection: %s", err))
}
@ -50,13 +53,17 @@ func (s *GiteaTestSuite) SetupTest() {
APIBaseURL: testAPIBaseURL,
BaseURL: testBaseURL,
}
endpoint, err := s.db.CreateGiteaEndpoint(context.Background(), createEpParams)
endpoint, err := s.db.CreateGiteaEndpoint(ctx, createEpParams)
s.Require().NoError(err)
s.Require().NotNil(endpoint)
s.Require().Equal(testEndpointName, endpoint.Name)
s.giteaEndpoint = endpoint
}
func (s *GiteaTestSuite) TearDownTest() {
watcher.CloseWatcher()
}
func (s *GiteaTestSuite) TestCreatingEndpoint() {
ctx := garmTesting.ImpersonateAdminContext(context.Background(), s.db, s.T())

View file

@ -28,6 +28,7 @@ import (
"github.com/cloudbase/garm/auth"
"github.com/cloudbase/garm/config"
"github.com/cloudbase/garm/database/common"
"github.com/cloudbase/garm/database/watcher"
garmTesting "github.com/cloudbase/garm/internal/testing"
"github.com/cloudbase/garm/params"
)
@ -51,6 +52,8 @@ type GithubTestSuite struct {
}
func (s *GithubTestSuite) SetupTest() {
ctx := context.Background()
watcher.InitWatcher(ctx)
db, err := NewSQLDatabase(context.Background(), garmTesting.GetTestSqliteDBConfig(s.T()))
if err != nil {
s.FailNow(fmt.Sprintf("failed to create db connection: %s", err))
@ -58,6 +61,10 @@ func (s *GithubTestSuite) SetupTest() {
s.db = db
}
func (s *GithubTestSuite) TearDownTest() {
watcher.CloseWatcher()
}
func (s *GithubTestSuite) TestDefaultEndpointGetsCreatedAutomaticallyIfNoOtherEndpointExists() {
ctx := garmTesting.ImpersonateAdminContext(context.Background(), s.db, s.T())
endpoint, err := s.db.GetGithubEndpoint(ctx, defaultGithubEndpoint)
@ -946,14 +953,16 @@ func TestCredentialsAndEndpointMigration(t *testing.T) {
// Set the config credentials in the cfg. This is what happens in the main function.
// of GARM as well.
cfg.MigrateCredentials = credentials
db, err := NewSQLDatabase(context.Background(), cfg)
ctx := context.Background()
watcher.InitWatcher(ctx)
defer watcher.CloseWatcher()
db, err := NewSQLDatabase(ctx, cfg)
if err != nil {
t.Fatalf("failed to create db connection: %s", err)
}
// We expect that 2 endpoints will exist in the migrated DB and 2 credentials.
ctx := garmTesting.ImpersonateAdminContext(context.Background(), db, t)
ctx = garmTesting.ImpersonateAdminContext(ctx, db, t)
endpoints, err := db.ListGithubEndpoints(ctx)
if err != nil {

View file

@ -31,6 +31,7 @@ import (
commonParams "github.com/cloudbase/garm-provider-common/params"
dbCommon "github.com/cloudbase/garm/database/common"
"github.com/cloudbase/garm/database/watcher"
garmTesting "github.com/cloudbase/garm/internal/testing"
"github.com/cloudbase/garm/params"
)
@ -70,7 +71,13 @@ func (s *InstancesTestSuite) assertSQLMockExpectations() {
}
}
func (s *InstancesTestSuite) TearDownTest() {
watcher.CloseWatcher()
}
func (s *InstancesTestSuite) SetupTest() {
ctx := context.Background()
watcher.InitWatcher(ctx)
// create testing sqlite database
db, err := NewSQLDatabase(context.Background(), garmTesting.GetTestSqliteDBConfig(s.T()))
if err != nil {

View file

@ -30,6 +30,7 @@ import (
"github.com/cloudbase/garm/auth"
dbCommon "github.com/cloudbase/garm/database/common"
"github.com/cloudbase/garm/database/watcher"
garmTesting "github.com/cloudbase/garm/internal/testing"
"github.com/cloudbase/garm/params"
)
@ -78,7 +79,13 @@ func (s *OrgTestSuite) assertSQLMockExpectations() {
}
}
func (s *OrgTestSuite) TearDownTest() {
watcher.CloseWatcher()
}
func (s *OrgTestSuite) SetupTest() {
ctx := context.Background()
watcher.InitWatcher(ctx)
// create testing sqlite database
dbConfig := garmTesting.GetTestSqliteDBConfig(s.T())
db, err := NewSQLDatabase(context.Background(), dbConfig)