Skip to content

Commit fae18bd

Browse files
6543huww98
andauthored
more test case for STORAGE_TYPE overrides (and fixes) (#14096) (#14104)
Signed-off-by: 胡玮文 <[email protected]> Co-authored-by: 胡玮文 <[email protected]>
1 parent 661e3e2 commit fae18bd

File tree

2 files changed

+57
-8
lines changed

2 files changed

+57
-8
lines changed

modules/setting/storage.go

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -43,20 +43,36 @@ func getStorage(name, typ string, targetSec *ini.Section) Storage {
4343
sec.Key("MINIO_LOCATION").MustString("us-east-1")
4444
sec.Key("MINIO_USE_SSL").MustBool(false)
4545

46-
nameSec := Cfg.Section(sectionName + "." + name)
47-
typeSec := Cfg.Section(sectionName + "." + typ)
48-
for _, override := range []*ini.Section{nameSec, typeSec, sec} {
46+
var storage Storage
47+
storage.Section = targetSec
48+
storage.Type = typ
49+
50+
overrides := make([]*ini.Section, 0, 3)
51+
nameSec, err := Cfg.GetSection(sectionName + "." + name)
52+
if err == nil {
53+
overrides = append(overrides, nameSec)
54+
}
55+
56+
typeSec, err := Cfg.GetSection(sectionName + "." + typ)
57+
if err == nil {
58+
overrides = append(overrides, typeSec)
59+
nextType := typeSec.Key("STORAGE_TYPE").String()
60+
if len(nextType) > 0 {
61+
storage.Type = nextType // Support custom STORAGE_TYPE
62+
}
63+
}
64+
overrides = append(overrides, sec)
65+
66+
for _, override := range overrides {
4967
for _, key := range override.Keys() {
5068
if !targetSec.HasKey(key.Name()) {
5169
_, _ = targetSec.NewKey(key.Name(), key.Value())
5270
}
5371
}
72+
if len(storage.Type) == 0 {
73+
storage.Type = override.Key("STORAGE_TYPE").String()
74+
}
5475
}
55-
56-
var storage Storage
57-
storage.Section = targetSec
58-
59-
storage.Type = typeSec.Key("STORAGE_TYPE").MustString(typ)
6076
storage.ServeDirect = storage.Section.Key("SERVE_DIRECT").MustBool(false)
6177

6278
// Specific defaults

modules/setting/storage_test.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,17 +77,22 @@ MINIO_BUCKET = gitea
7777
func Test_getStorageSpecificOverridesStorage(t *testing.T) {
7878
iniStr := `
7979
[attachment]
80+
STORAGE_TYPE = minio
8081
MINIO_BUCKET = gitea-attachment
8182
8283
[storage.attachments]
8384
MINIO_BUCKET = gitea
85+
86+
[storage]
87+
STORAGE_TYPE = local
8488
`
8589
Cfg, _ = ini.Load([]byte(iniStr))
8690

8791
sec := Cfg.Section("attachment")
8892
storageType := sec.Key("STORAGE_TYPE").MustString("")
8993
storage := getStorage("attachments", storageType, sec)
9094

95+
assert.EqualValues(t, "minio", storage.Type)
9196
assert.EqualValues(t, "gitea-attachment", storage.Section.Key("MINIO_BUCKET").String())
9297
}
9398

@@ -162,3 +167,31 @@ MINIO_BUCKET = gitea-storage
162167
assert.EqualValues(t, "gitea-storage", storage.Section.Key("MINIO_BUCKET").String())
163168
}
164169
}
170+
171+
func Test_getStorageInheritStorageType(t *testing.T) {
172+
iniStr := `
173+
[storage]
174+
STORAGE_TYPE = minio
175+
`
176+
Cfg, _ = ini.Load([]byte(iniStr))
177+
178+
sec := Cfg.Section("attachment")
179+
storageType := sec.Key("STORAGE_TYPE").MustString("")
180+
storage := getStorage("attachments", storageType, sec)
181+
182+
assert.EqualValues(t, "minio", storage.Type)
183+
}
184+
185+
func Test_getStorageInheritNameSectionType(t *testing.T) {
186+
iniStr := `
187+
[storage.attachments]
188+
STORAGE_TYPE = minio
189+
`
190+
Cfg, _ = ini.Load([]byte(iniStr))
191+
192+
sec := Cfg.Section("attachment")
193+
storageType := sec.Key("STORAGE_TYPE").MustString("")
194+
storage := getStorage("attachments", storageType, sec)
195+
196+
assert.EqualValues(t, "minio", storage.Type)
197+
}

0 commit comments

Comments
 (0)