优化结构
This commit is contained in:
@@ -8,7 +8,7 @@ import (
|
||||
"testing"
|
||||
|
||||
integrationbiz "kra/internal/biz/integration"
|
||||
"kra/internal/conf"
|
||||
"kra/internal/config"
|
||||
dataintegration "kra/internal/data/integration"
|
||||
"kra/internal/integration/storage"
|
||||
|
||||
@@ -54,21 +54,21 @@ func TestMigrateAllCreatesIntegrationConfigTable(t *testing.T) {
|
||||
|
||||
func TestStorageIntegrationConfigRoundTrip(t *testing.T) {
|
||||
db := openIntegrationConfigTestDB(t)
|
||||
storage := &conf.AdminBackend_Storage{
|
||||
storage := &config.Storage{
|
||||
Type: "aliyun-oss",
|
||||
Qiniu: &conf.AdminBackend_Qiniu{
|
||||
Qiniu: &config.Qiniu{
|
||||
Zone: "ZoneHuadong", Bucket: "qiniu-bucket", AccessKey: "qiniu-key", SecretKey: "qiniu-secret",
|
||||
},
|
||||
AliyunOss: &conf.AdminBackend_ObjectStore{
|
||||
AliyunOss: &config.ObjectStore{
|
||||
Endpoint: "oss-cn-hangzhou.aliyuncs.com", Region: "cn-hangzhou", Bucket: "assets",
|
||||
AccessKey: "aliyun-key", SecretKey: "aliyun-secret", BaseUrl: "https://cdn.example.com", PathPrefix: "uploads",
|
||||
},
|
||||
Minio: &conf.AdminBackend_ObjectStore{Endpoint: "127.0.0.1:9000", Bucket: "local", ForcePathStyle: true},
|
||||
Minio: &config.ObjectStore{Endpoint: "127.0.0.1:9000", Bucket: "local", ForcePathStyle: true},
|
||||
}
|
||||
if err := saveStorageIntegrationConfig(db, storage); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
email := &conf.AdminBackend_Email{
|
||||
email := &config.Email{
|
||||
To: "ops@example.com", From: "mailer@example.com", Host: "smtp.example.com",
|
||||
Secret: "smtp-secret", Nickname: "Kra", Port: 465, IsSsl: true,
|
||||
}
|
||||
@@ -89,8 +89,8 @@ func TestStorageIntegrationConfigRoundTrip(t *testing.T) {
|
||||
if loaded.Type != "aliyun-oss" {
|
||||
t.Fatalf("storage type = %q, want aliyun-oss", loaded.Type)
|
||||
}
|
||||
if loaded.AliyunOss == nil || loaded.AliyunOss.SecretKey != "aliyun-secret" || loaded.AliyunOss.PathPrefix != "uploads" {
|
||||
t.Fatalf("aliyun configuration = %#v", loaded.AliyunOss)
|
||||
if loaded.AliyunOSS == nil || loaded.AliyunOSS.SecretKey != "aliyun-secret" || loaded.AliyunOSS.PathPrefix != "uploads" {
|
||||
t.Fatalf("aliyun configuration = %#v", loaded.AliyunOSS)
|
||||
}
|
||||
if loaded.Qiniu == nil || loaded.Qiniu.SecretKey != "qiniu-secret" {
|
||||
t.Fatalf("qiniu configuration = %#v", loaded.Qiniu)
|
||||
@@ -127,9 +127,9 @@ func TestStorageIntegrationConfigRoundTrip(t *testing.T) {
|
||||
|
||||
func TestResolveStorageIntegrationConfigMigratesLegacyOnlyOnce(t *testing.T) {
|
||||
db := openIntegrationConfigTestDB(t)
|
||||
legacy := &conf.AdminBackend_Storage{
|
||||
legacy := &config.Storage{
|
||||
Type: "qiniu",
|
||||
Qiniu: &conf.AdminBackend_Qiniu{Bucket: "legacy", SecretKey: "legacy-secret"},
|
||||
Qiniu: &config.Qiniu{Bucket: "legacy", SecretKey: "legacy-secret"},
|
||||
}
|
||||
loaded, err := resolveStorageIntegrationConfig(db, legacy)
|
||||
if err != nil {
|
||||
@@ -139,9 +139,9 @@ func TestResolveStorageIntegrationConfigMigratesLegacyOnlyOnce(t *testing.T) {
|
||||
t.Fatalf("migrated storage = %#v", loaded)
|
||||
}
|
||||
|
||||
other := &conf.AdminBackend_Storage{
|
||||
other := &config.Storage{
|
||||
Type: "minio",
|
||||
Minio: &conf.AdminBackend_ObjectStore{Bucket: "must-not-replace-database"},
|
||||
Minio: &config.ObjectStore{Bucket: "must-not-replace-database"},
|
||||
}
|
||||
loaded, err = resolveStorageIntegrationConfig(db, other)
|
||||
if err != nil {
|
||||
@@ -153,23 +153,23 @@ func TestResolveStorageIntegrationConfigMigratesLegacyOnlyOnce(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestMaskStorageSecretsLeavesUnconfiguredProvidersEmpty(t *testing.T) {
|
||||
storage := &conf.AdminBackend_Storage{
|
||||
Qiniu: &conf.AdminBackend_Qiniu{},
|
||||
AliyunOss: &conf.AdminBackend_ObjectStore{SecretKey: "configured-secret"},
|
||||
Minio: &conf.AdminBackend_ObjectStore{},
|
||||
storage := &config.Storage{
|
||||
Qiniu: &config.Qiniu{},
|
||||
AliyunOss: &config.ObjectStore{SecretKey: "configured-secret"},
|
||||
Minio: &config.ObjectStore{},
|
||||
}
|
||||
maskStorageSecrets(storage)
|
||||
if storage.Qiniu.SecretKey != "" || storage.Minio.SecretKey != "" {
|
||||
t.Fatalf("empty provider secrets were masked: %#v", storage)
|
||||
}
|
||||
if storage.AliyunOss.SecretKey != "******" {
|
||||
t.Fatalf("configured secret was not masked: %q", storage.AliyunOss.SecretKey)
|
||||
if storage.AliyunOSS.SecretKey != "******" {
|
||||
t.Fatalf("configured secret was not masked: %q", storage.AliyunOSS.SecretKey)
|
||||
}
|
||||
}
|
||||
|
||||
func TestResolveEmailIntegrationConfigMigratesLegacyOnlyOnce(t *testing.T) {
|
||||
db := openIntegrationConfigTestDB(t)
|
||||
legacy := &conf.AdminBackend_Email{To: "ops@example.com", From: "old@example.com", Host: "smtp.old.example.com", Secret: "old-secret", Port: 465, IsSsl: true}
|
||||
legacy := &config.Email{To: "ops@example.com", From: "old@example.com", Host: "smtp.old.example.com", Secret: "old-secret", Port: 465, IsSsl: true}
|
||||
loaded, err := resolveEmailIntegrationConfig(db, legacy)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
@@ -177,7 +177,7 @@ func TestResolveEmailIntegrationConfigMigratesLegacyOnlyOnce(t *testing.T) {
|
||||
if loaded.Host != legacy.Host || loaded.Secret != legacy.Secret {
|
||||
t.Fatalf("migrated email = %#v", loaded)
|
||||
}
|
||||
loaded, err = resolveEmailIntegrationConfig(db, &conf.AdminBackend_Email{Host: "must-not-replace.example.com"})
|
||||
loaded, err = resolveEmailIntegrationConfig(db, &config.Email{Host: "must-not-replace.example.com"})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
@@ -193,16 +193,16 @@ func TestPersistConfigValuesRemovesStorageFromYAML(t *testing.T) {
|
||||
t.Fatal(err)
|
||||
}
|
||||
d := &Data{}
|
||||
admin := &conf.AdminBackend{
|
||||
admin := &config.Admin{
|
||||
ConfigPath: path,
|
||||
RouterPrefix: "/api",
|
||||
Storage: &conf.AdminBackend_Storage{
|
||||
Storage: &config.Storage{
|
||||
Type: "qiniu",
|
||||
Qiniu: &conf.AdminBackend_Qiniu{SecretKey: "database-only-secret"},
|
||||
Qiniu: &config.Qiniu{SecretKey: "database-only-secret"},
|
||||
},
|
||||
Email: &conf.AdminBackend_Email{Host: "smtp.database.example.com", Secret: "database-only-email-secret"},
|
||||
Email: &config.Email{Host: "smtp.database.example.com", Secret: "database-only-email-secret"},
|
||||
}
|
||||
if err := d.persistConfigValues(&conf.Data{}, admin); err != nil {
|
||||
if err := d.persistConfigValues(&config.Data{}, admin); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
@@ -237,10 +237,10 @@ func TestPersistRuntimeConfigReplacesActiveStorage(t *testing.T) {
|
||||
}
|
||||
oldRoot := filepath.Join(t.TempDir(), "old")
|
||||
newRoot := filepath.Join(t.TempDir(), "new")
|
||||
currentAdmin := &conf.AdminBackend{
|
||||
currentAdmin := &config.Admin{
|
||||
ConfigPath: configPath,
|
||||
Local: &conf.AdminBackend_Local{StorePath: oldRoot, PathPrefix: "old-files"},
|
||||
Storage: &conf.AdminBackend_Storage{Type: "local"},
|
||||
Local: &config.Local{StorePath: oldRoot, PathPrefix: "old-files"},
|
||||
Storage: &config.Storage{Type: "local"},
|
||||
}
|
||||
currentStorage, err := storage.NewReloadable(currentAdmin)
|
||||
if err != nil {
|
||||
@@ -249,19 +249,19 @@ func TestPersistRuntimeConfigReplacesActiveStorage(t *testing.T) {
|
||||
reloadableDB := &reloadableDB{}
|
||||
reloadableDB.current.Store(db)
|
||||
d := &Data{
|
||||
runtime: conf.NewRuntime(&conf.Data{}, currentAdmin),
|
||||
runtime: config.NewStore(&config.Config{Data: &config.Data{}, Admin: currentAdmin}),
|
||||
gormDB: reloadableDB,
|
||||
storage: currentStorage,
|
||||
}
|
||||
d.databaseReady.Store(true)
|
||||
|
||||
nextAdmin := cloneAdminConfig(currentAdmin)
|
||||
nextAdmin.Local = &conf.AdminBackend_Local{StorePath: newRoot, PathPrefix: "new-files"}
|
||||
nextAdmin.Email = &conf.AdminBackend_Email{
|
||||
nextAdmin.Local = &config.Local{StorePath: newRoot, PathPrefix: "new-files"}
|
||||
nextAdmin.Email = &config.Email{
|
||||
To: "ops@example.com", From: "mailer@example.com", Host: "smtp.example.com",
|
||||
Secret: "runtime-secret", Port: 465, IsSsl: true,
|
||||
}
|
||||
dataRaw, err := protojson.Marshal(&conf.Data{})
|
||||
dataRaw, err := protojson.Marshal(&config.Data{})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user