From 45390c47a95c6f56026681ea20fbd1f3de056008 Mon Sep 17 00:00:00 2001 From: David Rochow Date: Mon, 28 Oct 2024 17:21:51 +0100 Subject: [PATCH] fix(test): added additonal readiness checks for DB (#334) Co-authored-by: Michael Reimsbach --- internal/database/mariadb/database.go | 8 ++++++-- internal/database/mariadb/issue_test.go | 4 ---- internal/database/mariadb/test/database_manager.go | 6 ++++++ internal/e2e/activity_query_test.go | 10 ++++------ internal/e2e/component_instance_query_test.go | 6 +----- internal/e2e/component_query_test.go | 6 +----- internal/e2e/component_version_query_test.go | 6 +----- internal/e2e/evidence_query_test.go | 2 -- internal/e2e/issue_match_change_test.go | 6 +----- internal/e2e/issue_match_query_test.go | 2 -- internal/e2e/service_query_test.go | 6 +----- 11 files changed, 21 insertions(+), 41 deletions(-) diff --git a/internal/database/mariadb/database.go b/internal/database/mariadb/database.go index b5a3592b..55dd60a8 100644 --- a/internal/database/mariadb/database.go +++ b/internal/database/mariadb/database.go @@ -43,14 +43,18 @@ func TestConnection(cfg util.Config, backOff int) error { } //before each try wait 1 Second - time.Sleep(time.Millisecond * 1000) + time.Sleep(1 * time.Second) connectionString := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?multiStatements=true&parseTime=true", cfg.DBUser, cfg.DBPassword, cfg.DBAddress, cfg.DBPort, cfg.DBName) - logrus.Info("Testing Connection to Database...") db, err := sqlx.Connect("mysql", connectionString) if err != nil { return TestConnection(cfg, backOff-1) } defer db.Close() + //do an actual ping to check if not only the handshake works but the db schema is as well ready to operate on + err = db.Ping() + if err != nil { + return TestConnection(cfg, backOff-1) + } return nil } diff --git a/internal/database/mariadb/issue_test.go b/internal/database/mariadb/issue_test.go index 1b18eaed..3ab3852f 100644 --- a/internal/database/mariadb/issue_test.go +++ b/internal/database/mariadb/issue_test.go @@ -4,8 +4,6 @@ package mariadb_test import ( - "time" - "github.com/cloudoperators/heureka/internal/database/mariadb" "github.com/cloudoperators/heureka/internal/database/mariadb/test" "github.com/cloudoperators/heureka/internal/entity" @@ -23,8 +21,6 @@ var _ = Describe("Issue", Label("database", "Issue"), func() { var db *mariadb.SqlDatabase var seeder *test.DatabaseSeeder BeforeEach(func() { - // This sleep suppresses a potential racing condition which triggers test failures. - time.Sleep(3 * time.Second) var err error db = dbm.NewTestSchema() diff --git a/internal/database/mariadb/test/database_manager.go b/internal/database/mariadb/test/database_manager.go index 830eab4f..d1e30765 100644 --- a/internal/database/mariadb/test/database_manager.go +++ b/internal/database/mariadb/test/database_manager.go @@ -164,6 +164,12 @@ func (dbm *LocalTestDataBaseManager) NewTestSchema() *mariadb.SqlDatabase { if err != nil { ginkgo.GinkgoLogr.WithCallDepth(5).Error(err, "Failure while loading DB Client for new Schema") } + + err = mariadb.TestConnection(dbm.Config.Config, 10) + if err != nil { + ginkgo.GinkgoLogr.WithCallDepth(5).Error(err, "Failure while testing connection for new Schema") + } + return dbClient } diff --git a/internal/e2e/activity_query_test.go b/internal/e2e/activity_query_test.go index ce9df42b..aa42f507 100644 --- a/internal/e2e/activity_query_test.go +++ b/internal/e2e/activity_query_test.go @@ -6,13 +6,11 @@ package e2e_test import ( "context" "fmt" - "os" - "time" - "github.com/cloudoperators/heureka/internal/entity" testentity "github.com/cloudoperators/heureka/internal/entity/test" "github.com/cloudoperators/heureka/internal/util" util2 "github.com/cloudoperators/heureka/pkg/util" + "os" "github.com/cloudoperators/heureka/internal/api/graphql/graph/model" "github.com/cloudoperators/heureka/internal/database/mariadb" @@ -32,8 +30,6 @@ var _ = Describe("Getting Activities via API", Label("e2e", "Activity"), func() var cfg util.Config BeforeEach(func() { - // This sleep suppresses a potential racing condition which triggers test failures. - time.Sleep(3 * time.Second) var err error _ = dbm.NewTestSchema() @@ -141,7 +137,9 @@ var _ = Describe("Getting Activities via API", Label("e2e", "Activity"), func() ctx := context.Background() err = client.Run(ctx, req, &respData) - + if err != nil { + logrus.WithError(err).WithField("request", req).Info("Error while unmarshaling") + } Expect(err).To(BeNil(), "Error while unmarshaling") }) diff --git a/internal/e2e/component_instance_query_test.go b/internal/e2e/component_instance_query_test.go index ea51c2f0..b7344aa3 100644 --- a/internal/e2e/component_instance_query_test.go +++ b/internal/e2e/component_instance_query_test.go @@ -6,13 +6,11 @@ package e2e_test import ( "context" "fmt" - "os" - "time" - "github.com/cloudoperators/heureka/internal/entity" testentity "github.com/cloudoperators/heureka/internal/entity/test" "github.com/cloudoperators/heureka/internal/util" util2 "github.com/cloudoperators/heureka/pkg/util" + "os" "github.com/cloudoperators/heureka/internal/server" @@ -32,8 +30,6 @@ var _ = Describe("Getting ComponentInstances via API", Label("e2e", "ComponentIn var cfg util.Config BeforeEach(func() { - // This sleep suppresses a potential racing condition which triggers test failures. - time.Sleep(3 * time.Second) var err error _ = dbm.NewTestSchema() diff --git a/internal/e2e/component_query_test.go b/internal/e2e/component_query_test.go index 16f63429..431fab88 100644 --- a/internal/e2e/component_query_test.go +++ b/internal/e2e/component_query_test.go @@ -6,11 +6,9 @@ package e2e_test import ( "context" "fmt" - "os" - "time" - "github.com/cloudoperators/heureka/internal/entity" testentity "github.com/cloudoperators/heureka/internal/entity/test" + "os" "github.com/cloudoperators/heureka/internal/util" util2 "github.com/cloudoperators/heureka/pkg/util" @@ -33,8 +31,6 @@ var _ = Describe("Getting Components via API", Label("e2e", "Components"), func( var cfg util.Config BeforeEach(func() { - // This sleep suppresses a potential racing condition which triggers test failures. - time.Sleep(3 * time.Second) var err error _ = dbm.NewTestSchema() diff --git a/internal/e2e/component_version_query_test.go b/internal/e2e/component_version_query_test.go index cf29d47b..44577805 100644 --- a/internal/e2e/component_version_query_test.go +++ b/internal/e2e/component_version_query_test.go @@ -6,13 +6,11 @@ package e2e_test import ( "context" "fmt" - "os" - "time" - "github.com/cloudoperators/heureka/internal/entity" testentity "github.com/cloudoperators/heureka/internal/entity/test" "github.com/cloudoperators/heureka/internal/util" util2 "github.com/cloudoperators/heureka/pkg/util" + "os" "github.com/cloudoperators/heureka/internal/server" @@ -32,8 +30,6 @@ var _ = Describe("Getting ComponentVersions via API", Label("e2e", "ComponentVer var cfg util.Config BeforeEach(func() { - // This sleep suppresses a potential racing condition which triggers test failures. - time.Sleep(3 * time.Second) var err error _ = dbm.NewTestSchema() diff --git a/internal/e2e/evidence_query_test.go b/internal/e2e/evidence_query_test.go index afdce61d..a2586b3e 100644 --- a/internal/e2e/evidence_query_test.go +++ b/internal/e2e/evidence_query_test.go @@ -32,8 +32,6 @@ var _ = Describe("Getting Evidences via API", Label("e2e", "Evidences"), func() var cfg util.Config BeforeEach(func() { - // This sleep suppresses a potential racing condition which triggers test failures. - time.Sleep(3 * time.Second) var err error _ = dbm.NewTestSchema() diff --git a/internal/e2e/issue_match_change_test.go b/internal/e2e/issue_match_change_test.go index eea4eaba..4a43359d 100644 --- a/internal/e2e/issue_match_change_test.go +++ b/internal/e2e/issue_match_change_test.go @@ -6,13 +6,11 @@ package e2e_test import ( "context" "fmt" - "os" - "time" - "github.com/cloudoperators/heureka/internal/entity" testentity "github.com/cloudoperators/heureka/internal/entity/test" "github.com/cloudoperators/heureka/internal/util" util2 "github.com/cloudoperators/heureka/pkg/util" + "os" "github.com/cloudoperators/heureka/internal/api/graphql/graph/model" "github.com/cloudoperators/heureka/internal/database/mariadb/test" @@ -29,8 +27,6 @@ var _ = Describe("Getting IssueMatchChanges via API", Label("e2e", "IssueMatchCh var s *server.Server var cfg util.Config BeforeEach(func() { - // This sleep suppresses a potential racing condition which triggers test failures. - time.Sleep(3 * time.Second) var err error _ = dbm.NewTestSchema() diff --git a/internal/e2e/issue_match_query_test.go b/internal/e2e/issue_match_query_test.go index 284d3801..a83c8e86 100644 --- a/internal/e2e/issue_match_query_test.go +++ b/internal/e2e/issue_match_query_test.go @@ -31,8 +31,6 @@ var _ = Describe("Getting IssueMatches via API", Label("e2e", "IssueMatches"), f var s *server.Server var cfg util.Config BeforeEach(func() { - // This sleep suppresses a potential racing condition which triggers test failures. - time.Sleep(3 * time.Second) var err error _ = dbm.NewTestSchema() diff --git a/internal/e2e/service_query_test.go b/internal/e2e/service_query_test.go index 04ef7ab2..2af44899 100644 --- a/internal/e2e/service_query_test.go +++ b/internal/e2e/service_query_test.go @@ -6,13 +6,11 @@ package e2e_test import ( "context" "fmt" - "os" - "time" - "github.com/cloudoperators/heureka/internal/entity" testentity "github.com/cloudoperators/heureka/internal/entity/test" "github.com/cloudoperators/heureka/internal/util" util2 "github.com/cloudoperators/heureka/pkg/util" + "os" "github.com/cloudoperators/heureka/internal/server" @@ -32,8 +30,6 @@ var _ = Describe("Getting Services via API", Label("e2e", "Services"), func() { var cfg util.Config BeforeEach(func() { - // This sleep suppresses a potential racing condition which triggers test failures. - time.Sleep(3 * time.Second) var err error _ = dbm.NewTestSchema()