0
Fork 0
mirror of https://codeberg.org/forgejo/forgejo.git synced 2024-12-23 15:53:07 -05:00

more test case for STORAGE_TYPE overrides (and fixes) (#14096)

Signed-off-by: 胡玮文 <huww98@outlook.com>
This commit is contained in:
胡玮文 2020-12-22 07:03:18 +08:00 committed by GitHub
parent addd4248da
commit c21a399646
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 57 additions and 8 deletions

View file

@ -43,20 +43,36 @@ func getStorage(name, typ string, targetSec *ini.Section) Storage {
sec.Key("MINIO_LOCATION").MustString("us-east-1") sec.Key("MINIO_LOCATION").MustString("us-east-1")
sec.Key("MINIO_USE_SSL").MustBool(false) sec.Key("MINIO_USE_SSL").MustBool(false)
nameSec := Cfg.Section(sectionName + "." + name) var storage Storage
typeSec := Cfg.Section(sectionName + "." + typ) storage.Section = targetSec
for _, override := range []*ini.Section{nameSec, typeSec, sec} { storage.Type = typ
overrides := make([]*ini.Section, 0, 3)
nameSec, err := Cfg.GetSection(sectionName + "." + name)
if err == nil {
overrides = append(overrides, nameSec)
}
typeSec, err := Cfg.GetSection(sectionName + "." + typ)
if err == nil {
overrides = append(overrides, typeSec)
nextType := typeSec.Key("STORAGE_TYPE").String()
if len(nextType) > 0 {
storage.Type = nextType // Support custom STORAGE_TYPE
}
}
overrides = append(overrides, sec)
for _, override := range overrides {
for _, key := range override.Keys() { for _, key := range override.Keys() {
if !targetSec.HasKey(key.Name()) { if !targetSec.HasKey(key.Name()) {
_, _ = targetSec.NewKey(key.Name(), key.Value()) _, _ = targetSec.NewKey(key.Name(), key.Value())
} }
} }
if len(storage.Type) == 0 {
storage.Type = override.Key("STORAGE_TYPE").String()
}
} }
var storage Storage
storage.Section = targetSec
storage.Type = typeSec.Key("STORAGE_TYPE").MustString(typ)
storage.ServeDirect = storage.Section.Key("SERVE_DIRECT").MustBool(false) storage.ServeDirect = storage.Section.Key("SERVE_DIRECT").MustBool(false)
// Specific defaults // Specific defaults

View file

@ -77,10 +77,14 @@ MINIO_BUCKET = gitea
func Test_getStorageSpecificOverridesStorage(t *testing.T) { func Test_getStorageSpecificOverridesStorage(t *testing.T) {
iniStr := ` iniStr := `
[attachment] [attachment]
STORAGE_TYPE = minio
MINIO_BUCKET = gitea-attachment MINIO_BUCKET = gitea-attachment
[storage.attachments] [storage.attachments]
MINIO_BUCKET = gitea MINIO_BUCKET = gitea
[storage]
STORAGE_TYPE = local
` `
Cfg, _ = ini.Load([]byte(iniStr)) Cfg, _ = ini.Load([]byte(iniStr))
@ -88,6 +92,7 @@ MINIO_BUCKET = gitea
storageType := sec.Key("STORAGE_TYPE").MustString("") storageType := sec.Key("STORAGE_TYPE").MustString("")
storage := getStorage("attachments", storageType, sec) storage := getStorage("attachments", storageType, sec)
assert.EqualValues(t, "minio", storage.Type)
assert.EqualValues(t, "gitea-attachment", storage.Section.Key("MINIO_BUCKET").String()) assert.EqualValues(t, "gitea-attachment", storage.Section.Key("MINIO_BUCKET").String())
} }
@ -162,3 +167,31 @@ MINIO_BUCKET = gitea-storage
assert.EqualValues(t, "gitea-storage", storage.Section.Key("MINIO_BUCKET").String()) assert.EqualValues(t, "gitea-storage", storage.Section.Key("MINIO_BUCKET").String())
} }
} }
func Test_getStorageInheritStorageType(t *testing.T) {
iniStr := `
[storage]
STORAGE_TYPE = minio
`
Cfg, _ = ini.Load([]byte(iniStr))
sec := Cfg.Section("attachment")
storageType := sec.Key("STORAGE_TYPE").MustString("")
storage := getStorage("attachments", storageType, sec)
assert.EqualValues(t, "minio", storage.Type)
}
func Test_getStorageInheritNameSectionType(t *testing.T) {
iniStr := `
[storage.attachments]
STORAGE_TYPE = minio
`
Cfg, _ = ini.Load([]byte(iniStr))
sec := Cfg.Section("attachment")
storageType := sec.Key("STORAGE_TYPE").MustString("")
storage := getStorage("attachments", storageType, sec)
assert.EqualValues(t, "minio", storage.Type)
}