Cleanup duplicate code

This commit is contained in:
mihaelabalutoiu 2022-09-13 16:13:07 +03:00
parent 777c41e411
commit 8c910d904f
4 changed files with 49 additions and 50 deletions

View file

@ -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))
}

View file

@ -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))

View file

@ -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))

43
util/tests_util.go Normal file
View file

@ -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"),
},
}
}