From d6dd6d641b593c54fe1a1041c153111ce81dbc20 Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Wed, 14 Jun 2023 11:42:38 +0800 Subject: Fix all possible setting error related storages and added some tests (#23911) Follow up #22405 Fix #20703 This PR rewrites storage configuration read sequences with some breaks and tests. It becomes more strict than before and also fixed some inherit problems. - Move storage's MinioConfig struct into setting, so after the configuration loading, the values will be stored into the struct but not still on some section. - All storages configurations should be stored on one section, configuration items cannot be overrided by multiple sections. The prioioty of configuration is `[attachment]` > `[storage.attachments]` | `[storage.customized]` > `[storage]` > `default` - For extra override configuration items, currently are `SERVE_DIRECT`, `MINIO_BASE_PATH`, `MINIO_BUCKET`, which could be configured in another section. The prioioty of the override configuration is `[attachment]` > `[storage.attachments]` > `default`. - Add more tests for storages configurations. - Update the storage documentations. --------- Co-authored-by: wxiaoguang --- modules/setting/storage_test.go | 196 +++++++++------------------------------- 1 file changed, 42 insertions(+), 154 deletions(-) (limited to 'modules/setting/storage_test.go') diff --git a/modules/setting/storage_test.go b/modules/setting/storage_test.go index 5e213606e3..4eda7646e8 100644 --- a/modules/setting/storage_test.go +++ b/modules/setting/storage_test.go @@ -9,106 +9,6 @@ import ( "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 -` - cfg, err := NewConfigProviderFromData(iniStr) - assert.NoError(t, err) - - sec := cfg.Section("attachment") - storageType := sec.Key("STORAGE_TYPE").MustString("") - storage := getStorage(cfg, "attachments", storageType, sec) - - 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 -` - cfg, err := NewConfigProviderFromData(iniStr) - assert.NoError(t, err) - - sec := cfg.Section("attachment") - storageType := sec.Key("STORAGE_TYPE").MustString("") - storage := getStorage(cfg, "attachments", storageType, sec) - - 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 -` - cfg, err := NewConfigProviderFromData(iniStr) - assert.NoError(t, err) - - sec := cfg.Section("attachment") - storageType := sec.Key("STORAGE_TYPE").MustString("") - storage := getStorage(cfg, "attachments", storageType, sec) - - 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] -STORAGE_TYPE = minio -MINIO_BUCKET = gitea-attachment - -[storage.attachments] -MINIO_BUCKET = gitea - -[storage] -STORAGE_TYPE = local -` - cfg, err := NewConfigProviderFromData(iniStr) - assert.NoError(t, err) - - sec := cfg.Section("attachment") - storageType := sec.Key("STORAGE_TYPE").MustString("") - storage := getStorage(cfg, "attachments", storageType, sec) - - assert.EqualValues(t, "minio", storage.Type) - assert.EqualValues(t, "gitea-attachment", storage.Section.Key("MINIO_BUCKET").String()) -} - -func Test_getStorageGetDefaults(t *testing.T) { - cfg, err := NewConfigProviderFromData("") - assert.NoError(t, err) - - sec := cfg.Section("attachment") - storageType := sec.Key("STORAGE_TYPE").MustString("") - storage := getStorage(cfg, "attachments", storageType, sec) - - assert.EqualValues(t, "gitea", storage.Section.Key("MINIO_BUCKET").String()) -} - func Test_getStorageMultipleName(t *testing.T) { iniStr := ` [lfs] @@ -118,32 +18,20 @@ MINIO_BUCKET = gitea-lfs MINIO_BUCKET = gitea-attachment [storage] +STORAGE_TYPE = minio MINIO_BUCKET = gitea-storage ` cfg, err := NewConfigProviderFromData(iniStr) assert.NoError(t, err) - { - sec := cfg.Section("attachment") - storageType := sec.Key("STORAGE_TYPE").MustString("") - storage := getStorage(cfg, "attachments", storageType, sec) - - assert.EqualValues(t, "gitea-attachment", storage.Section.Key("MINIO_BUCKET").String()) - } - { - sec := cfg.Section("lfs") - storageType := sec.Key("STORAGE_TYPE").MustString("") - storage := getStorage(cfg, "lfs", storageType, sec) - - assert.EqualValues(t, "gitea-lfs", storage.Section.Key("MINIO_BUCKET").String()) - } - { - sec := cfg.Section("avatar") - storageType := sec.Key("STORAGE_TYPE").MustString("") - storage := getStorage(cfg, "avatars", storageType, sec) - - assert.EqualValues(t, "gitea-storage", storage.Section.Key("MINIO_BUCKET").String()) - } + assert.NoError(t, loadAttachmentFrom(cfg)) + assert.EqualValues(t, "gitea-attachment", Attachment.Storage.MinioConfig.Bucket) + + assert.NoError(t, loadLFSFrom(cfg)) + assert.EqualValues(t, "gitea-lfs", LFS.Storage.MinioConfig.Bucket) + + assert.NoError(t, loadAvatarsFrom(cfg)) + assert.EqualValues(t, "gitea-storage", Avatar.Storage.MinioConfig.Bucket) } func Test_getStorageUseOtherNameAsType(t *testing.T) { @@ -152,25 +40,17 @@ func Test_getStorageUseOtherNameAsType(t *testing.T) { STORAGE_TYPE = lfs [storage.lfs] +STORAGE_TYPE = minio MINIO_BUCKET = gitea-storage ` cfg, err := NewConfigProviderFromData(iniStr) assert.NoError(t, err) - { - sec := cfg.Section("attachment") - storageType := sec.Key("STORAGE_TYPE").MustString("") - storage := getStorage(cfg, "attachments", storageType, sec) + assert.NoError(t, loadAttachmentFrom(cfg)) + assert.EqualValues(t, "gitea-storage", Attachment.Storage.MinioConfig.Bucket) - assert.EqualValues(t, "gitea-storage", storage.Section.Key("MINIO_BUCKET").String()) - } - { - sec := cfg.Section("lfs") - storageType := sec.Key("STORAGE_TYPE").MustString("") - storage := getStorage(cfg, "lfs", storageType, sec) - - assert.EqualValues(t, "gitea-storage", storage.Section.Key("MINIO_BUCKET").String()) - } + assert.NoError(t, loadLFSFrom(cfg)) + assert.EqualValues(t, "gitea-storage", LFS.Storage.MinioConfig.Bucket) } func Test_getStorageInheritStorageType(t *testing.T) { @@ -181,24 +61,32 @@ STORAGE_TYPE = minio cfg, err := NewConfigProviderFromData(iniStr) assert.NoError(t, err) - sec := cfg.Section("attachment") - storageType := sec.Key("STORAGE_TYPE").MustString("") - storage := getStorage(cfg, "attachments", storageType, sec) - - assert.EqualValues(t, "minio", storage.Type) -} - -func Test_getStorageInheritNameSectionType(t *testing.T) { - iniStr := ` -[storage.attachments] -STORAGE_TYPE = minio -` - cfg, err := NewConfigProviderFromData(iniStr) - assert.NoError(t, err) - - sec := cfg.Section("attachment") - storageType := sec.Key("STORAGE_TYPE").MustString("") - storage := getStorage(cfg, "attachments", storageType, sec) - - assert.EqualValues(t, "minio", storage.Type) + assert.NoError(t, loadPackagesFrom(cfg)) + assert.EqualValues(t, "minio", Packages.Storage.Type) + assert.EqualValues(t, "gitea", Packages.Storage.MinioConfig.Bucket) + assert.EqualValues(t, "packages/", Packages.Storage.MinioConfig.BasePath) + + assert.NoError(t, loadRepoArchiveFrom(cfg)) + assert.EqualValues(t, "minio", RepoArchive.Storage.Type) + assert.EqualValues(t, "gitea", RepoArchive.Storage.MinioConfig.Bucket) + assert.EqualValues(t, "repo-archive/", RepoArchive.Storage.MinioConfig.BasePath) + + assert.NoError(t, loadActionsFrom(cfg)) + assert.EqualValues(t, "minio", Actions.LogStorage.Type) + assert.EqualValues(t, "gitea", Actions.LogStorage.MinioConfig.Bucket) + assert.EqualValues(t, "actions_log/", Actions.LogStorage.MinioConfig.BasePath) + + assert.EqualValues(t, "minio", Actions.ArtifactStorage.Type) + assert.EqualValues(t, "gitea", Actions.ArtifactStorage.MinioConfig.Bucket) + assert.EqualValues(t, "actions_artifacts/", Actions.ArtifactStorage.MinioConfig.BasePath) + + assert.NoError(t, loadAvatarsFrom(cfg)) + assert.EqualValues(t, "minio", Avatar.Storage.Type) + assert.EqualValues(t, "gitea", Avatar.Storage.MinioConfig.Bucket) + assert.EqualValues(t, "avatars/", Avatar.Storage.MinioConfig.BasePath) + + assert.NoError(t, loadRepoAvatarFrom(cfg)) + assert.EqualValues(t, "minio", RepoAvatar.Storage.Type) + assert.EqualValues(t, "gitea", RepoAvatar.Storage.MinioConfig.Bucket) + assert.EqualValues(t, "repo-avatars/", RepoAvatar.Storage.MinioConfig.BasePath) } -- cgit v1.2.3