From 8c910d904f16d385b69edfb4375ca0f0d11103dd Mon Sep 17 00:00:00 2001 From: mihaelabalutoiu Date: Tue, 13 Sep 2022 16:13:07 +0300 Subject: [PATCH] Cleanup duplicate code --- database/sql/controller_test.go | 27 ++------------------- runner/organizations_test.go | 26 ++------------------ runner/repositories_test.go | 3 ++- util/tests_util.go | 43 +++++++++++++++++++++++++++++++++ 4 files changed, 49 insertions(+), 50 deletions(-) create mode 100644 util/tests_util.go diff --git a/database/sql/controller_test.go b/database/sql/controller_test.go index 3542dc82..274150e7 100644 --- a/database/sql/controller_test.go +++ b/database/sql/controller_test.go @@ -17,44 +17,21 @@ package sql import ( "context" "fmt" - "garm/config" dbCommon "garm/database/common" runnerErrors "garm/errors" - "os" - "path/filepath" + "garm/util" "testing" "github.com/stretchr/testify/suite" ) -var ( - encryptionPassphrase = "bocyasicgatEtenOubwonIbsudNutDom" -) - -func getTestSqliteDBConfig(t *testing.T) config.Database { - dir, err := os.MkdirTemp("", "garm-config-test") - if err != nil { - t.Fatalf("failed to create temporary directory: %s", err) - } - t.Cleanup(func() { os.RemoveAll(dir) }) - - return config.Database{ - Debug: false, - DbBackend: config.SQLiteBackend, - Passphrase: encryptionPassphrase, - SQLite: config.SQLite{ - DBFile: filepath.Join(dir, "garm.db"), - }, - } -} - type CtrlTestSuite struct { suite.Suite Store dbCommon.Store } func (s *CtrlTestSuite) SetupTest() { - db, err := NewSQLDatabase(context.Background(), getTestSqliteDBConfig(s.T())) + db, err := NewSQLDatabase(context.Background(), util.GetTestSqliteDBConfig(s.T())) if err != nil { s.FailNow(fmt.Sprintf("failed to create db connection: %s", err)) } diff --git a/runner/organizations_test.go b/runner/organizations_test.go index cce6f043..6270211a 100644 --- a/runner/organizations_test.go +++ b/runner/organizations_test.go @@ -26,8 +26,7 @@ import ( "garm/runner/common" runnerCommonMocks "garm/runner/common/mocks" runnerMocks "garm/runner/mocks" - "os" - "path/filepath" + "garm/util" "sort" "testing" @@ -35,10 +34,6 @@ import ( "github.com/stretchr/testify/suite" ) -var ( - EncryptionPassphrase = "bocyasicgatEtenOubwonIbsudNutDom" -) - type OrgTestFixtures struct { AdminContext context.Context DBFile string @@ -64,23 +59,6 @@ type OrgTestSuite struct { Runner *Runner } -func getTestSqliteDBConfig(t *testing.T) config.Database { - dir, err := os.MkdirTemp("", "garm-config-test") - if err != nil { - t.Fatalf("failed to create temporary directory: %s", err) - } - t.Cleanup(func() { os.RemoveAll(dir) }) - - return config.Database{ - Debug: false, - DbBackend: config.SQLiteBackend, - Passphrase: EncryptionPassphrase, - SQLite: config.SQLite{ - DBFile: filepath.Join(dir, "garm.db"), - }, - } -} - func (s *OrgTestSuite) orgsMapValues(orgs map[string]params.Organization) []params.Organization { orgsSlice := []params.Organization{} for _, value := range orgs { @@ -126,7 +104,7 @@ func (s *OrgTestSuite) SetupTest() { adminCtx := auth.GetAdminContext() // create testing sqlite database - dbCfg := getTestSqliteDBConfig(s.T()) + dbCfg := util.GetTestSqliteDBConfig(s.T()) db, err := database.NewDatabase(adminCtx, dbCfg) if err != nil { s.FailNow(fmt.Sprintf("failed to create db connection: %s", err)) diff --git a/runner/repositories_test.go b/runner/repositories_test.go index 047e2309..0cfb4dc2 100644 --- a/runner/repositories_test.go +++ b/runner/repositories_test.go @@ -26,6 +26,7 @@ import ( "garm/runner/common" runnerCommonMocks "garm/runner/common/mocks" runnerMocks "garm/runner/mocks" + "garm/util" "sort" "testing" @@ -102,7 +103,7 @@ func (s *RepoTestSuite) SetupTest() { adminCtx := auth.GetAdminContext() // create testing sqlite database - dbCfg := getTestSqliteDBConfig(s.T()) + dbCfg := util.GetTestSqliteDBConfig(s.T()) db, err := database.NewDatabase(adminCtx, dbCfg) if err != nil { s.FailNow(fmt.Sprintf("failed to create db connection: %s", err)) diff --git a/util/tests_util.go b/util/tests_util.go new file mode 100644 index 00000000..84fb9a14 --- /dev/null +++ b/util/tests_util.go @@ -0,0 +1,43 @@ +// Copyright 2022 Cloudbase Solutions SRL +// +// Licensed under the Apache License, Version 2.0 (the "License"); you may +// not use this file except in compliance with the License. You may obtain +// a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +// License for the specific language governing permissions and limitations +// under the License. + +package util + +import ( + "garm/config" + "os" + "path/filepath" + "testing" +) + +var ( + encryptionPassphrase = "bocyasicgatEtenOubwonIbsudNutDom" +) + +func GetTestSqliteDBConfig(t *testing.T) config.Database { + dir, err := os.MkdirTemp("", "garm-config-test") + if err != nil { + t.Fatalf("failed to create temporary directory: %s", err) + } + t.Cleanup(func() { os.RemoveAll(dir) }) + + return config.Database{ + Debug: false, + DbBackend: config.SQLiteBackend, + Passphrase: encryptionPassphrase, + SQLite: config.SQLite{ + DBFile: filepath.Join(dir, "garm.db"), + }, + } +}