2020-12-21 23:29:18 +05:30
|
|
|
// Copyright 2020 The Gitea Authors. All rights reserved.
|
2022-11-27 23:50:29 +05:30
|
|
|
// SPDX-License-Identifier: MIT
|
2020-12-21 23:29:18 +05:30
|
|
|
|
|
|
|
package setting
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
func Test_getStorageCustomType(t *testing.T) {
|
|
|
|
iniStr := `
|
|
|
|
[attachment]
|
|
|
|
STORAGE_TYPE = my_minio
|
|
|
|
MINIO_BUCKET = gitea-attachment
|
|
|
|
|
|
|
|
[storage.my_minio]
|
|
|
|
STORAGE_TYPE = minio
|
|
|
|
MINIO_ENDPOINT = my_minio:9000
|
|
|
|
`
|
2023-04-25 20:36:39 +05:30
|
|
|
cfg, err := newConfigProviderFromData(iniStr)
|
2023-02-19 21:42:01 +05:30
|
|
|
assert.NoError(t, err)
|
2020-12-21 23:29:18 +05:30
|
|
|
|
2023-02-19 21:42:01 +05:30
|
|
|
sec := cfg.Section("attachment")
|
2020-12-21 23:29:18 +05:30
|
|
|
storageType := sec.Key("STORAGE_TYPE").MustString("")
|
2023-02-19 21:42:01 +05:30
|
|
|
storage := getStorage(cfg, "attachments", storageType, sec)
|
2020-12-21 23:29:18 +05:30
|
|
|
|
|
|
|
assert.EqualValues(t, "minio", storage.Type)
|
|
|
|
assert.EqualValues(t, "my_minio:9000", storage.Section.Key("MINIO_ENDPOINT").String())
|
|
|
|
assert.EqualValues(t, "gitea-attachment", storage.Section.Key("MINIO_BUCKET").String())
|
|
|
|
}
|
|
|
|
|
|
|
|
func Test_getStorageNameSectionOverridesTypeSection(t *testing.T) {
|
|
|
|
iniStr := `
|
|
|
|
[attachment]
|
|
|
|
STORAGE_TYPE = minio
|
|
|
|
|
|
|
|
[storage.attachments]
|
|
|
|
MINIO_BUCKET = gitea-attachment
|
|
|
|
|
|
|
|
[storage.minio]
|
|
|
|
MINIO_BUCKET = gitea
|
|
|
|
`
|
2023-04-25 20:36:39 +05:30
|
|
|
cfg, err := newConfigProviderFromData(iniStr)
|
2023-02-19 21:42:01 +05:30
|
|
|
assert.NoError(t, err)
|
2020-12-21 23:29:18 +05:30
|
|
|
|
2023-02-19 21:42:01 +05:30
|
|
|
sec := cfg.Section("attachment")
|
2020-12-21 23:29:18 +05:30
|
|
|
storageType := sec.Key("STORAGE_TYPE").MustString("")
|
2023-02-19 21:42:01 +05:30
|
|
|
storage := getStorage(cfg, "attachments", storageType, sec)
|
2020-12-21 23:29:18 +05:30
|
|
|
|
|
|
|
assert.EqualValues(t, "minio", storage.Type)
|
|
|
|
assert.EqualValues(t, "gitea-attachment", storage.Section.Key("MINIO_BUCKET").String())
|
|
|
|
}
|
|
|
|
|
|
|
|
func Test_getStorageTypeSectionOverridesStorageSection(t *testing.T) {
|
|
|
|
iniStr := `
|
|
|
|
[attachment]
|
|
|
|
STORAGE_TYPE = minio
|
|
|
|
|
|
|
|
[storage.minio]
|
|
|
|
MINIO_BUCKET = gitea-minio
|
|
|
|
|
|
|
|
[storage]
|
|
|
|
MINIO_BUCKET = gitea
|
|
|
|
`
|
2023-04-25 20:36:39 +05:30
|
|
|
cfg, err := newConfigProviderFromData(iniStr)
|
2023-02-19 21:42:01 +05:30
|
|
|
assert.NoError(t, err)
|
2020-12-21 23:29:18 +05:30
|
|
|
|
2023-02-19 21:42:01 +05:30
|
|
|
sec := cfg.Section("attachment")
|
2020-12-21 23:29:18 +05:30
|
|
|
storageType := sec.Key("STORAGE_TYPE").MustString("")
|
2023-02-19 21:42:01 +05:30
|
|
|
storage := getStorage(cfg, "attachments", storageType, sec)
|
2020-12-21 23:29:18 +05:30
|
|
|
|
|
|
|
assert.EqualValues(t, "minio", storage.Type)
|
|
|
|
assert.EqualValues(t, "gitea-minio", storage.Section.Key("MINIO_BUCKET").String())
|
|
|
|
}
|
|
|
|
|
|
|
|
func Test_getStorageSpecificOverridesStorage(t *testing.T) {
|
|
|
|
iniStr := `
|
|
|
|
[attachment]
|
2020-12-22 04:33:18 +05:30
|
|
|
STORAGE_TYPE = minio
|
2020-12-21 23:29:18 +05:30
|
|
|
MINIO_BUCKET = gitea-attachment
|
|
|
|
|
|
|
|
[storage.attachments]
|
|
|
|
MINIO_BUCKET = gitea
|
2020-12-22 04:33:18 +05:30
|
|
|
|
|
|
|
[storage]
|
|
|
|
STORAGE_TYPE = local
|
2020-12-21 23:29:18 +05:30
|
|
|
`
|
2023-04-25 20:36:39 +05:30
|
|
|
cfg, err := newConfigProviderFromData(iniStr)
|
2023-02-19 21:42:01 +05:30
|
|
|
assert.NoError(t, err)
|
2020-12-21 23:29:18 +05:30
|
|
|
|
2023-02-19 21:42:01 +05:30
|
|
|
sec := cfg.Section("attachment")
|
2020-12-21 23:29:18 +05:30
|
|
|
storageType := sec.Key("STORAGE_TYPE").MustString("")
|
2023-02-19 21:42:01 +05:30
|
|
|
storage := getStorage(cfg, "attachments", storageType, sec)
|
2020-12-21 23:29:18 +05:30
|
|
|
|
2020-12-22 04:33:18 +05:30
|
|
|
assert.EqualValues(t, "minio", storage.Type)
|
2020-12-21 23:29:18 +05:30
|
|
|
assert.EqualValues(t, "gitea-attachment", storage.Section.Key("MINIO_BUCKET").String())
|
|
|
|
}
|
|
|
|
|
|
|
|
func Test_getStorageGetDefaults(t *testing.T) {
|
2023-04-25 20:36:39 +05:30
|
|
|
cfg, err := newConfigProviderFromData("")
|
2023-02-19 21:42:01 +05:30
|
|
|
assert.NoError(t, err)
|
2020-12-21 23:29:18 +05:30
|
|
|
|
2023-02-19 21:42:01 +05:30
|
|
|
sec := cfg.Section("attachment")
|
2020-12-21 23:29:18 +05:30
|
|
|
storageType := sec.Key("STORAGE_TYPE").MustString("")
|
2023-02-19 21:42:01 +05:30
|
|
|
storage := getStorage(cfg, "attachments", storageType, sec)
|
2020-12-21 23:29:18 +05:30
|
|
|
|
|
|
|
assert.EqualValues(t, "gitea", storage.Section.Key("MINIO_BUCKET").String())
|
|
|
|
}
|
|
|
|
|
|
|
|
func Test_getStorageMultipleName(t *testing.T) {
|
|
|
|
iniStr := `
|
|
|
|
[lfs]
|
|
|
|
MINIO_BUCKET = gitea-lfs
|
|
|
|
|
|
|
|
[attachment]
|
|
|
|
MINIO_BUCKET = gitea-attachment
|
|
|
|
|
|
|
|
[storage]
|
|
|
|
MINIO_BUCKET = gitea-storage
|
|
|
|
`
|
2023-04-25 20:36:39 +05:30
|
|
|
cfg, err := newConfigProviderFromData(iniStr)
|
2023-02-19 21:42:01 +05:30
|
|
|
assert.NoError(t, err)
|
2020-12-21 23:29:18 +05:30
|
|
|
|
|
|
|
{
|
2023-02-19 21:42:01 +05:30
|
|
|
sec := cfg.Section("attachment")
|
2020-12-21 23:29:18 +05:30
|
|
|
storageType := sec.Key("STORAGE_TYPE").MustString("")
|
2023-02-19 21:42:01 +05:30
|
|
|
storage := getStorage(cfg, "attachments", storageType, sec)
|
2020-12-21 23:29:18 +05:30
|
|
|
|
|
|
|
assert.EqualValues(t, "gitea-attachment", storage.Section.Key("MINIO_BUCKET").String())
|
|
|
|
}
|
|
|
|
{
|
2023-02-19 21:42:01 +05:30
|
|
|
sec := cfg.Section("lfs")
|
2020-12-21 23:29:18 +05:30
|
|
|
storageType := sec.Key("STORAGE_TYPE").MustString("")
|
2023-02-19 21:42:01 +05:30
|
|
|
storage := getStorage(cfg, "lfs", storageType, sec)
|
2020-12-21 23:29:18 +05:30
|
|
|
|
|
|
|
assert.EqualValues(t, "gitea-lfs", storage.Section.Key("MINIO_BUCKET").String())
|
|
|
|
}
|
|
|
|
{
|
2023-02-19 21:42:01 +05:30
|
|
|
sec := cfg.Section("avatar")
|
2020-12-21 23:29:18 +05:30
|
|
|
storageType := sec.Key("STORAGE_TYPE").MustString("")
|
2023-02-19 21:42:01 +05:30
|
|
|
storage := getStorage(cfg, "avatars", storageType, sec)
|
2020-12-21 23:29:18 +05:30
|
|
|
|
|
|
|
assert.EqualValues(t, "gitea-storage", storage.Section.Key("MINIO_BUCKET").String())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func Test_getStorageUseOtherNameAsType(t *testing.T) {
|
|
|
|
iniStr := `
|
|
|
|
[attachment]
|
|
|
|
STORAGE_TYPE = lfs
|
|
|
|
|
|
|
|
[storage.lfs]
|
|
|
|
MINIO_BUCKET = gitea-storage
|
|
|
|
`
|
2023-04-25 20:36:39 +05:30
|
|
|
cfg, err := newConfigProviderFromData(iniStr)
|
2023-02-19 21:42:01 +05:30
|
|
|
assert.NoError(t, err)
|
2020-12-21 23:29:18 +05:30
|
|
|
|
|
|
|
{
|
2023-02-19 21:42:01 +05:30
|
|
|
sec := cfg.Section("attachment")
|
2020-12-21 23:29:18 +05:30
|
|
|
storageType := sec.Key("STORAGE_TYPE").MustString("")
|
2023-02-19 21:42:01 +05:30
|
|
|
storage := getStorage(cfg, "attachments", storageType, sec)
|
2020-12-21 23:29:18 +05:30
|
|
|
|
|
|
|
assert.EqualValues(t, "gitea-storage", storage.Section.Key("MINIO_BUCKET").String())
|
|
|
|
}
|
|
|
|
{
|
2023-02-19 21:42:01 +05:30
|
|
|
sec := cfg.Section("lfs")
|
2020-12-21 23:29:18 +05:30
|
|
|
storageType := sec.Key("STORAGE_TYPE").MustString("")
|
2023-02-19 21:42:01 +05:30
|
|
|
storage := getStorage(cfg, "lfs", storageType, sec)
|
2020-12-21 23:29:18 +05:30
|
|
|
|
|
|
|
assert.EqualValues(t, "gitea-storage", storage.Section.Key("MINIO_BUCKET").String())
|
|
|
|
}
|
|
|
|
}
|
2020-12-22 04:33:18 +05:30
|
|
|
|
|
|
|
func Test_getStorageInheritStorageType(t *testing.T) {
|
|
|
|
iniStr := `
|
|
|
|
[storage]
|
|
|
|
STORAGE_TYPE = minio
|
|
|
|
`
|
2023-04-25 20:36:39 +05:30
|
|
|
cfg, err := newConfigProviderFromData(iniStr)
|
2023-02-19 21:42:01 +05:30
|
|
|
assert.NoError(t, err)
|
2020-12-22 04:33:18 +05:30
|
|
|
|
2023-02-19 21:42:01 +05:30
|
|
|
sec := cfg.Section("attachment")
|
2020-12-22 04:33:18 +05:30
|
|
|
storageType := sec.Key("STORAGE_TYPE").MustString("")
|
2023-02-19 21:42:01 +05:30
|
|
|
storage := getStorage(cfg, "attachments", storageType, sec)
|
2020-12-22 04:33:18 +05:30
|
|
|
|
|
|
|
assert.EqualValues(t, "minio", storage.Type)
|
|
|
|
}
|
|
|
|
|
|
|
|
func Test_getStorageInheritNameSectionType(t *testing.T) {
|
|
|
|
iniStr := `
|
|
|
|
[storage.attachments]
|
|
|
|
STORAGE_TYPE = minio
|
|
|
|
`
|
2023-04-25 20:36:39 +05:30
|
|
|
cfg, err := newConfigProviderFromData(iniStr)
|
2023-02-19 21:42:01 +05:30
|
|
|
assert.NoError(t, err)
|
2020-12-22 04:33:18 +05:30
|
|
|
|
2023-02-19 21:42:01 +05:30
|
|
|
sec := cfg.Section("attachment")
|
2020-12-22 04:33:18 +05:30
|
|
|
storageType := sec.Key("STORAGE_TYPE").MustString("")
|
2023-02-19 21:42:01 +05:30
|
|
|
storage := getStorage(cfg, "attachments", storageType, sec)
|
2020-12-22 04:33:18 +05:30
|
|
|
|
|
|
|
assert.EqualValues(t, "minio", storage.Type)
|
|
|
|
}
|