Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix the wrong derive path #26271

Merged
merged 23 commits into from
Aug 4, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion modules/setting/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,10 +158,26 @@ func getStorage(rootCfg ConfigProvider, name, typ string, sec ConfigSection) (*S

switch targetType {
case string(LocalStorageType):
storage.Path = ConfigSectionKeyString(targetSec, "PATH", filepath.Join(AppDataPath, name))
targetPath := ConfigSectionKeyString(targetSec, "PATH", "")
if targetPath == "" {
targetPath = AppDataPath
}

extraConfigSec := sec
if extraConfigSec == nil {
extraConfigSec = storageNameSec
}

if extraConfigSec == nil {
storage.Path = filepath.Join(targetPath, name)
} else {
storage.Path = ConfigSectionKeyString(extraConfigSec, "PATH", filepath.Join(targetPath, name))
}

if !filepath.IsAbs(storage.Path) {
storage.Path = filepath.Join(AppWorkPath, storage.Path)
lunny marked this conversation as resolved.
Show resolved Hide resolved
}

case string(MinioStorageType):
storage.MinioConfig.BasePath = name + "/"

Expand Down
171 changes: 171 additions & 0 deletions modules/setting/storage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package setting

import (
"path/filepath"
"testing"

"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -90,3 +91,173 @@ STORAGE_TYPE = minio
assert.EqualValues(t, "gitea", RepoAvatar.Storage.MinioConfig.Bucket)
assert.EqualValues(t, "repo-avatars/", RepoAvatar.Storage.MinioConfig.BasePath)
}

func Test_getStorageInheritStorageTypeLocal(t *testing.T) {
iniStr := `
[storage]
STORAGE_TYPE = local
`
cfg, err := NewConfigProviderFromData(iniStr)
assert.NoError(t, err)

assert.NoError(t, loadPackagesFrom(cfg))
assert.EqualValues(t, "local", Packages.Storage.Type)
assert.EqualValues(t, filepath.Join(AppWorkPath, "packages"), Packages.Storage.Path)
lunny marked this conversation as resolved.
Show resolved Hide resolved

assert.NoError(t, loadRepoArchiveFrom(cfg))
assert.EqualValues(t, "local", RepoArchive.Storage.Type)
assert.EqualValues(t, filepath.Join(AppWorkPath, "repo-archive"), RepoArchive.Storage.Path)

assert.NoError(t, loadActionsFrom(cfg))
assert.EqualValues(t, "local", Actions.LogStorage.Type)
assert.EqualValues(t, filepath.Join(AppWorkPath, "actions_log"), Actions.LogStorage.Path)

assert.EqualValues(t, "local", Actions.ArtifactStorage.Type)
assert.EqualValues(t, filepath.Join(AppWorkPath, "actions_artifacts"), Actions.ArtifactStorage.Path)

assert.NoError(t, loadAvatarsFrom(cfg))
assert.EqualValues(t, "local", Avatar.Storage.Type)
assert.EqualValues(t, filepath.Join(AppWorkPath, "avatars"), Avatar.Storage.Path)

assert.NoError(t, loadRepoAvatarFrom(cfg))
assert.EqualValues(t, "local", RepoAvatar.Storage.Type)
assert.EqualValues(t, filepath.Join(AppWorkPath, "repo-avatars"), RepoAvatar.Storage.Path)
}

func Test_getStorageInheritStorageTypeLocalDataPath(t *testing.T) {
iniStr := `
[storage]
STORAGE_TYPE = local
`
cfg, err := NewConfigProviderFromData(iniStr)
assert.NoError(t, err)
AppDataPath = "/tmp/gitea"

assert.NoError(t, loadPackagesFrom(cfg))
assert.EqualValues(t, "local", Packages.Storage.Type)
assert.EqualValues(t, "/tmp/gitea/packages", Packages.Storage.Path)

assert.NoError(t, loadRepoArchiveFrom(cfg))
assert.EqualValues(t, "local", RepoArchive.Storage.Type)
assert.EqualValues(t, "/tmp/gitea/repo-archive", RepoArchive.Storage.Path)

assert.NoError(t, loadActionsFrom(cfg))
assert.EqualValues(t, "local", Actions.LogStorage.Type)
assert.EqualValues(t, "/tmp/gitea/actions_log", Actions.LogStorage.Path)

assert.EqualValues(t, "local", Actions.ArtifactStorage.Type)
assert.EqualValues(t, "/tmp/gitea/actions_artifacts", Actions.ArtifactStorage.Path)

assert.NoError(t, loadAvatarsFrom(cfg))
assert.EqualValues(t, "local", Avatar.Storage.Type)
assert.EqualValues(t, "/tmp/gitea/avatars", Avatar.Storage.Path)

assert.NoError(t, loadRepoAvatarFrom(cfg))
assert.EqualValues(t, "local", RepoAvatar.Storage.Type)
assert.EqualValues(t, "/tmp/gitea/repo-avatars", RepoAvatar.Storage.Path)
}

func Test_getStorageInheritStorageTypeLocalPath(t *testing.T) {
iniStr := `
[storage]
STORAGE_TYPE = local
PATH = /data/gitea
`
cfg, err := NewConfigProviderFromData(iniStr)
assert.NoError(t, err)

assert.NoError(t, loadPackagesFrom(cfg))
assert.EqualValues(t, "local", Packages.Storage.Type)
assert.EqualValues(t, "/data/gitea/packages", Packages.Storage.Path)

assert.NoError(t, loadRepoArchiveFrom(cfg))
assert.EqualValues(t, "local", RepoArchive.Storage.Type)
assert.EqualValues(t, "/data/gitea/repo-archive", RepoArchive.Storage.Path)

assert.NoError(t, loadActionsFrom(cfg))
assert.EqualValues(t, "local", Actions.LogStorage.Type)
assert.EqualValues(t, "/data/gitea/actions_log", Actions.LogStorage.Path)

assert.EqualValues(t, "local", Actions.ArtifactStorage.Type)
assert.EqualValues(t, "/data/gitea/actions_artifacts", Actions.ArtifactStorage.Path)

assert.NoError(t, loadAvatarsFrom(cfg))
assert.EqualValues(t, "local", Avatar.Storage.Type)
assert.EqualValues(t, "/data/gitea/avatars", Avatar.Storage.Path)

assert.NoError(t, loadRepoAvatarFrom(cfg))
assert.EqualValues(t, "local", RepoAvatar.Storage.Type)
assert.EqualValues(t, "/data/gitea/repo-avatars", RepoAvatar.Storage.Path)
}

func Test_getStorageInheritStorageTypeLocalPathOverride(t *testing.T) {
iniStr := `
[storage]
STORAGE_TYPE = local
PATH = /data/gitea

[repo-archive]
PATH = /data/gitea/archives
`
cfg, err := NewConfigProviderFromData(iniStr)
assert.NoError(t, err)

assert.NoError(t, loadPackagesFrom(cfg))
assert.EqualValues(t, "local", Packages.Storage.Type)
assert.EqualValues(t, "/data/gitea/packages", Packages.Storage.Path)

assert.NoError(t, loadRepoArchiveFrom(cfg))
assert.EqualValues(t, "local", RepoArchive.Storage.Type)
assert.EqualValues(t, "/data/gitea/archives", RepoArchive.Storage.Path)

assert.NoError(t, loadActionsFrom(cfg))
assert.EqualValues(t, "local", Actions.LogStorage.Type)
assert.EqualValues(t, "/data/gitea/actions_log", Actions.LogStorage.Path)

assert.EqualValues(t, "local", Actions.ArtifactStorage.Type)
assert.EqualValues(t, "/data/gitea/actions_artifacts", Actions.ArtifactStorage.Path)

assert.NoError(t, loadAvatarsFrom(cfg))
assert.EqualValues(t, "local", Avatar.Storage.Type)
assert.EqualValues(t, "/data/gitea/avatars", Avatar.Storage.Path)

assert.NoError(t, loadRepoAvatarFrom(cfg))
assert.EqualValues(t, "local", RepoAvatar.Storage.Type)
assert.EqualValues(t, "/data/gitea/repo-avatars", RepoAvatar.Storage.Path)
}

func Test_getStorageInheritStorageTypeLocalPathOverride2(t *testing.T) {
iniStr := `
[storage]
STORAGE_TYPE = local
PATH = /data/gitea

[storage.repo-archive]
PATH = /data/gitea/archives
`
cfg, err := NewConfigProviderFromData(iniStr)
assert.NoError(t, err)

assert.NoError(t, loadPackagesFrom(cfg))
assert.EqualValues(t, "local", Packages.Storage.Type)
assert.EqualValues(t, "/data/gitea/packages", Packages.Storage.Path)

assert.NoError(t, loadRepoArchiveFrom(cfg))
assert.EqualValues(t, "local", RepoArchive.Storage.Type)
assert.EqualValues(t, "/data/gitea/archives", RepoArchive.Storage.Path)

assert.NoError(t, loadActionsFrom(cfg))
assert.EqualValues(t, "local", Actions.LogStorage.Type)
assert.EqualValues(t, "/data/gitea/actions_log", Actions.LogStorage.Path)

assert.EqualValues(t, "local", Actions.ArtifactStorage.Type)
assert.EqualValues(t, "/data/gitea/actions_artifacts", Actions.ArtifactStorage.Path)

assert.NoError(t, loadAvatarsFrom(cfg))
assert.EqualValues(t, "local", Avatar.Storage.Type)
assert.EqualValues(t, "/data/gitea/avatars", Avatar.Storage.Path)

assert.NoError(t, loadRepoAvatarFrom(cfg))
assert.EqualValues(t, "local", RepoAvatar.Storage.Type)
assert.EqualValues(t, "/data/gitea/repo-avatars", RepoAvatar.Storage.Path)
}
Loading