Skip to content

Commit

Permalink
✨ Change dal test case (#243)
Browse files Browse the repository at this point in the history
  • Loading branch information
tosone authored Nov 26, 2023
1 parent 6e554f7 commit 5732830
Show file tree
Hide file tree
Showing 11 changed files with 153 additions and 118 deletions.
6 changes: 3 additions & 3 deletions cmd/distribution.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,14 @@ var distributionCmd = &cobra.Command{
return
}

err = dal.Initialize()
config := ptr.To(configs.GetConfiguration())

err = dal.Initialize(config)
if err != nil {
log.Error().Err(err).Msg("Initialize database with error")
return
}

config := ptr.To(configs.GetConfiguration())

err = inits.Initialize(config)
if err != nil {
log.Error().Err(err).Msg("Initialize inits with error")
Expand Down
6 changes: 3 additions & 3 deletions cmd/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,14 @@ var serverCmd = &cobra.Command{
return
}

err = dal.Initialize()
config := ptr.To(configs.GetConfiguration())

err = dal.Initialize(config)
if err != nil {
log.Error().Err(err).Msg("Initialize database with error")
return
}

config := ptr.To(configs.GetConfiguration())

err = inits.Initialize(config)
if err != nil {
log.Error().Err(err).Msg("Initialize inits with error")
Expand Down
6 changes: 3 additions & 3 deletions cmd/worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,14 @@ var workerCmd = &cobra.Command{
return
}

err = dal.Initialize()
config := ptr.To(configs.GetConfiguration())

err = dal.Initialize(config)
if err != nil {
log.Error().Err(err).Msg("Initialize database with error")
return
}

config := ptr.To(configs.GetConfiguration())

err = inits.Initialize(config)
if err != nil {
log.Error().Err(err).Msg("Initialize inits with error")
Expand Down
5 changes: 4 additions & 1 deletion pkg/cronjob/builder/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"github.com/robfig/cron/v3"
"github.com/rs/zerolog/log"

"github.com/go-sigma/sigma/pkg/configs"
"github.com/go-sigma/sigma/pkg/consts"
"github.com/go-sigma/sigma/pkg/cronjob"
"github.com/go-sigma/sigma/pkg/dal/dao"
Expand Down Expand Up @@ -50,17 +51,19 @@ func builderJob() {
builderTw = timewheel.NewTimeWheel(context.Background(), cronjob.CronjobIterDuration)

runner := builderRunner{
config: ptr.To(configs.GetConfiguration()),
builderServiceFactory: dao.NewBuilderServiceFactory(),
}
builderTw.AddRunner(runner.runner)
}

type builderRunner struct {
config configs.Configuration
builderServiceFactory dao.BuilderServiceFactory
}

func (r builderRunner) runner(ctx context.Context, tw timewheel.TimeWheel) {
locker, err := locker.New()
locker, err := locker.New(r.config)
if err != nil {
log.Error().Err(err).Msg("New locker failed")
return
Expand Down
21 changes: 10 additions & 11 deletions pkg/dal/auth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,11 @@ import (
"reflect"
"testing"

"github.com/alicebob/miniredis/v2"
gonanoid "github.com/matoous/go-nanoid"
"github.com/rs/zerolog/log"
"github.com/spf13/viper"
"github.com/stretchr/testify/assert"

"github.com/go-sigma/sigma/pkg/configs"
"github.com/go-sigma/sigma/pkg/dal"
"github.com/go-sigma/sigma/pkg/dal/dao"
"github.com/go-sigma/sigma/pkg/dal/models"
Expand All @@ -39,24 +38,24 @@ func TestAuth(t *testing.T) {
logger.SetLevel("debug")

dbPath := fmt.Sprintf("%s.db", gonanoid.MustGenerate("abcdefghijklmnopqrstuvwxyz", 6))
viper.SetDefault("database.type", "sqlite3")
viper.SetDefault("database.sqlite3.path", dbPath)
miniRedis := miniredis.RunT(t)
viper.SetDefault("redis.url", "redis://"+miniRedis.Addr())

err := dal.Initialize()
assert.NoError(t, err)
assert.NoError(t, dal.Initialize(configs.Configuration{
Database: configs.ConfigurationDatabase{
Type: enums.DatabaseSqlite3,
Sqlite3: configs.ConfigurationDatabaseSqlite3{
Path: dbPath,
},
},
}))

ctx := log.Logger.WithContext(context.Background())
roleService := dao.NewNamespaceMemberServiceFactory().New()

added, _ := dal.AuthEnforcer.AddPolicy(enums.NamespaceRoleManager.String(), "library", "DS$*/**$manifests$*", "public", "(GET)|(HEAD)", "allow")
assert.True(t, added)

_, err = roleService.AddNamespaceMember(ctx, 1, models.Namespace{ID: 1, Name: "library"}, enums.NamespaceRoleManager)
_, err := roleService.AddNamespaceMember(ctx, 1, models.Namespace{ID: 1, Name: "library"}, enums.NamespaceRoleManager)
assert.NoError(t, err)
// added, _ = dal.AuthEnforcer.AddRoleForUser("1", enums.NamespaceRoleManager.String(), "library")
// assert.True(t, added)
err = dal.AuthEnforcer.LoadPolicy()
assert.NoError(t, err)

Expand Down
74 changes: 44 additions & 30 deletions pkg/dal/dal.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"gorm.io/driver/sqlite"
"gorm.io/gorm"

"github.com/go-sigma/sigma/pkg/configs"
"github.com/go-sigma/sigma/pkg/consts"
"github.com/go-sigma/sigma/pkg/dal/models"
"github.com/go-sigma/sigma/pkg/dal/query"
Expand All @@ -40,19 +41,19 @@ var (
)

// Initialize initializes the database connection
func Initialize() error {
func Initialize(config configs.Configuration) error {
var err error
var dsn string
dbType := enums.MustParseDatabase(viper.GetString("database.type"))
switch dbType {
// dbType := enums.MustParseDatabase(viper.GetString("database.type"))
switch config.Database.Type {
case enums.DatabaseMysql:
dsn, err = connectMysql()
dsn, err = connectMysql(config)
case enums.DatabasePostgresql:
dsn, err = connectPostgres()
dsn, err = connectPostgres(config)
case enums.DatabaseSqlite3:
dsn, err = connectSqlite3()
dsn, err = connectSqlite3(config)
default:
return fmt.Errorf("unknown database type: %s", dbType)
return fmt.Errorf("unknown database type: %s", config.Database.Type)
}
if err != nil {
return err
Expand All @@ -69,7 +70,7 @@ func Initialize() error {
return err
}

locker, err := locker.New()
locker, err := locker.New(config)
if err != nil {
return err
}
Expand All @@ -84,15 +85,15 @@ func Initialize() error {
}
}()

switch dbType {
switch config.Database.Type {
case enums.DatabaseMysql:
err = migrateMysql(dsn)
case enums.DatabasePostgresql:
err = migratePostgres(dsn)
case enums.DatabaseSqlite3:
err = migrateSqlite(dsn)
default:
return fmt.Errorf("unknown database type: %s", dbType)
return fmt.Errorf("unknown database type: %s", config.Database.Type)
}
if err != nil {
return err
Expand All @@ -111,14 +112,19 @@ func Initialize() error {
return nil
}

func connectMysql() (string, error) {
host := viper.GetString("database.mysql.host")
port := viper.GetString("database.mysql.port")
user := viper.GetString("database.mysql.user")
password := viper.GetString("database.mysql.password")
dbname := viper.GetString("database.mysql.dbname")

dsn := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?charset=utf8mb4&parseTime=True&loc=UTC", user, password, host, port, dbname)
func connectMysql(config configs.Configuration) (string, error) {
// host := viper.GetString("database.mysql.host")
// port := viper.GetString("database.mysql.port")
// user := viper.GetString("database.mysql.user")
// password := viper.GetString("database.mysql.password")
// dbname := viper.GetString("database.mysql.dbname")
host := config.Database.Mysql.Host
port := config.Database.Mysql.Port
user := config.Database.Mysql.User
password := config.Database.Mysql.Password
dbname := config.Database.Mysql.DBName

dsn := fmt.Sprintf("%s:%s@tcp(%s:%d)/%s?charset=utf8mb4&parseTime=True&loc=UTC", user, password, host, port, dbname)
log.Debug().Str("dsn", dsn).Msg("Connect to mysql database")

db, err := gorm.Open(mysql.Open(dsn), &gorm.Config{
Expand All @@ -136,15 +142,22 @@ func connectMysql() (string, error) {
return dsn, nil
}

func connectPostgres() (string, error) {
host := viper.GetString("database.postgres.host")
port := viper.GetString("database.postgres.port")
user := viper.GetString("database.postgres.user")
password := viper.GetString("database.postgres.password")
dbname := viper.GetString("database.postgres.dbname")
sslmode := viper.GetString("database.postgres.sslmode")

dsn := fmt.Sprintf("host=%s port=%s user=%s dbname=%s password=%s sslmode=%s", host, port, user, dbname, password, sslmode)
func connectPostgres(config configs.Configuration) (string, error) {
// host := viper.GetString("database.postgres.host")
// port := viper.GetString("database.postgres.port")
// user := viper.GetString("database.postgres.user")
// password := viper.GetString("database.postgres.password")
// dbname := viper.GetString("database.postgres.dbname")
// sslmode := viper.GetString("database.postgres.sslmode")

host := config.Database.Postgresql.Host
port := config.Database.Postgresql.Port
user := config.Database.Postgresql.User
password := config.Database.Postgresql.Password
dbname := config.Database.Postgresql.DBName
sslmode := config.Database.Postgresql.SslMode

dsn := fmt.Sprintf("host=%s port=%d user=%s dbname=%s password=%s sslmode=%s", host, port, user, dbname, password, sslmode)
db, err := gorm.Open(postgres.Open(dsn), &gorm.Config{
NowFunc: func() time.Time {
return time.Now().UTC()
Expand All @@ -157,13 +170,14 @@ func connectPostgres() (string, error) {
db = db.WithContext(log.Logger.WithContext(context.Background()))
DB = db

migrateDsn := fmt.Sprintf("%s:%s@%s:%s/%s?sslmode=disable", user, password, host, port, dbname)
migrateDsn := fmt.Sprintf("%s:%s@%s:%d/%s?sslmode=disable", user, password, host, port, dbname)

return migrateDsn, nil
}

func connectSqlite3() (string, error) {
dbname := viper.GetString("database.sqlite3.path")
func connectSqlite3(config configs.Configuration) (string, error) {
// dbname := viper.GetString("database.sqlite3.path")
dbname := config.Database.Sqlite3.Path

db, err := gorm.Open(sqlite.Open(dbname), &gorm.Config{
NowFunc: func() time.Time {
Expand Down
Loading

0 comments on commit 5732830

Please sign in to comment.