summaryrefslogtreecommitdiffstats
path: root/modules
diff options
context:
space:
mode:
author6543 <6543@obermui.de>2020-12-22 07:13:57 +0000
committerGitHub <noreply@github.com>2020-12-22 09:13:57 +0200
commitfae18bdac0a6a291ea6d06b77fe568bda6e87cf7 (patch)
tree7a4ee91b9b304377eefce9f3f8ed8c47c791a913 /modules
parent661e3e2bdcb6eff243c86daceed79cd4a552dd08 (diff)
downloadgitea-fae18bdac0a6a291ea6d06b77fe568bda6e87cf7.tar.gz
gitea-fae18bdac0a6a291ea6d06b77fe568bda6e87cf7.zip
more test case for STORAGE_TYPE overrides (and fixes) (#14096) (#14104)
Signed-off-by: 胡玮文 <huww98@outlook.com> Co-authored-by: 胡玮文 <huww98@outlook.com>
Diffstat (limited to 'modules')
-rw-r--r--modules/setting/storage.go32
-rw-r--r--modules/setting/storage_test.go33
2 files changed, 57 insertions, 8 deletions
diff --git a/modules/setting/storage.go b/modules/setting/storage.go
index 43890371fa..3ab08d8d2a 100644
--- a/modules/setting/storage.go
+++ b/modules/setting/storage.go
@@ -43,20 +43,36 @@ func getStorage(name, typ string, targetSec *ini.Section) Storage {
sec.Key("MINIO_LOCATION").MustString("us-east-1")
sec.Key("MINIO_USE_SSL").MustBool(false)
- nameSec := Cfg.Section(sectionName + "." + name)
- typeSec := Cfg.Section(sectionName + "." + typ)
- for _, override := range []*ini.Section{nameSec, typeSec, sec} {
+ var storage Storage
+ storage.Section = targetSec
+ 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() {
if !targetSec.HasKey(key.Name()) {
_, _ = 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)
// Specific defaults
diff --git a/modules/setting/storage_test.go b/modules/setting/storage_test.go
index 00c255a9c9..ffd8b7aa01 100644
--- a/modules/setting/storage_test.go
+++ b/modules/setting/storage_test.go
@@ -77,10 +77,14 @@ MINIO_BUCKET = gitea
func Test_getStorageSpecificOverridesStorage(t *testing.T) {
iniStr := `
[attachment]
+STORAGE_TYPE = minio
MINIO_BUCKET = gitea-attachment
[storage.attachments]
MINIO_BUCKET = gitea
+
+[storage]
+STORAGE_TYPE = local
`
Cfg, _ = ini.Load([]byte(iniStr))
@@ -88,6 +92,7 @@ MINIO_BUCKET = gitea
storageType := sec.Key("STORAGE_TYPE").MustString("")
storage := getStorage("attachments", storageType, sec)
+ assert.EqualValues(t, "minio", storage.Type)
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())
}
}
+
+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)
+}