summaryrefslogtreecommitdiffstats
path: root/modules/setting/forgejo_storage_test.go
blob: d91bff59e92d7ff5b59ea4e856a9db13ef6b7592 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
// SPDX-License-Identifier: MIT

//
// Tests verifying the Forgejo documentation on storage settings is correct
//
// https://forgejo.org/docs/v1.20/admin/storage/
//

package setting

import (
	"fmt"
	"path/filepath"
	"testing"

	"github.com/stretchr/testify/assert"
	"github.com/stretchr/testify/require"
)

func TestForgejoDocs_StorageTypes(t *testing.T) {
	iniStr := `
[server]
APP_DATA_PATH = /
`
	testStorageTypesDefaultAndSpecificStorage(t, iniStr)
}

func testStorageGetPath(storage *Storage) string {
	if storage.Type == MinioStorageType {
		return storage.MinioConfig.BasePath
	}
	return storage.Path
}

var testSectionToBasePath = map[string]string{
	"attachment":          "attachments",
	"lfs":                 "lfs",
	"avatar":              "avatars",
	"repo-avatar":         "repo-avatars",
	"repo-archive":        "repo-archive",
	"packages":            "packages",
	"storage.actions_log": "actions_log",
	"actions.artifacts":   "actions_artifacts",
}

type testSectionToPathFun func(StorageType, string) string

func testBuildPath(t StorageType, path string) string {
	if t == LocalStorageType {
		return "/" + path
	}
	return path + "/"
}

func testSectionToPath(t StorageType, section string) string {
	return testBuildPath(t, testSectionToBasePath[section])
}

func testSpecificPath(t StorageType, section string) string {
	if t == LocalStorageType {
		return "/specific_local_path"
	}
	return "specific_s3_base_path/"
}

func testDefaultDir(t StorageType) string {
	if t == LocalStorageType {
		return "default_local_path"
	}
	return "default_s3_base_path"
}

func testDefaultPath(t StorageType) string {
	return testBuildPath(t, testDefaultDir(t))
}

func testSectionToDefaultPath(t StorageType, section string) string {
	return testBuildPath(t, filepath.Join(testDefaultDir(t), testSectionToPath(t, section)))
}

func testLegacyPath(t StorageType, section string) string {
	return testBuildPath(t, fmt.Sprintf("legacy_%s_path", section))
}

func testStorageTypeToSetting(t StorageType) string {
	if t == LocalStorageType {
		return "PATH"
	}
	return "MINIO_BASE_PATH"
}

var testSectionToLegacy = map[string]string{
	"lfs": fmt.Sprintf(`
[server]
APP_DATA_PATH = /
LFS_CONTENT_PATH = %s
`, testLegacyPath(LocalStorageType, "lfs")),
	"avatar": fmt.Sprintf(`
[picture]
AVATAR_UPLOAD_PATH = %s
`, testLegacyPath(LocalStorageType, "avatar")),
	"repo-avatar": fmt.Sprintf(`
[picture]
REPOSITORY_AVATAR_UPLOAD_PATH = %s
`, testLegacyPath(LocalStorageType, "repo-avatar")),
}

func testStorageTypesDefaultAndSpecificStorage(t *testing.T, iniStr string) {
	storageType := MinioStorageType
	t.Run(string(storageType), func(t *testing.T) {
		t.Run("override type minio", func(t *testing.T) {
			storageSection := `
[storage]
STORAGE_TYPE = minio
`
			testStorageTypesSpecificStorages(t, iniStr+storageSection, storageType, testSectionToPath, testSectionToPath)
		})
	})

	storageType = LocalStorageType

	t.Run(string(storageType), func(t *testing.T) {
		storageSection := ""
		testStorageTypesSpecificStorages(t, iniStr+storageSection, storageType, testSectionToPath, testSectionToPath)

		t.Run("override type local", func(t *testing.T) {
			storageSection := `
[storage]
STORAGE_TYPE = local
`
			testStorageTypesSpecificStorages(t, iniStr+storageSection, storageType, testSectionToPath, testSectionToPath)

			storageSection = fmt.Sprintf(`
[storage]
STORAGE_TYPE = local
PATH = %s
`, testDefaultPath(LocalStorageType))
			testStorageTypesSpecificStorageSections(t, iniStr+storageSection, storageType, testSectionToDefaultPath, testSectionToPath)
		})
	})
}

func testStorageTypesSpecificStorageSections(t *testing.T, iniStr string, defaultStorageType StorageType, defaultStorageTypePath, testSectionToPath testSectionToPathFun) {
	testSectionsMap := map[string]**Storage{
		"attachment":   &Attachment.Storage,
		"lfs":          &LFS.Storage,
		"avatar":       &Avatar.Storage,
		"repo-avatar":  &RepoAvatar.Storage,
		"repo-archive": &RepoArchive.Storage,
		"packages":     &Packages.Storage,
		// there are inconsistencies in how actions storage is determined in v1.20
		// it is still alpha and undocumented and is ignored for now
		//"storage.actions_log": &Actions.LogStorage,
		//"actions.artifacts":   &Actions.ArtifactStorage,
	}

	for sectionName, storage := range testSectionsMap {
		t.Run(sectionName, func(t *testing.T) {
			testStorageTypesSpecificStorage(t, iniStr, defaultStorageType, defaultStorageTypePath, testSectionToPath, sectionName, storage)
		})
	}
}

func testStorageTypesSpecificStorages(t *testing.T, iniStr string, defaultStorageType StorageType, defaultStorageTypePath, testSectionToPath testSectionToPathFun) {
	testSectionsMap := map[string]**Storage{
		"attachment":          &Attachment.Storage,
		"lfs":                 &LFS.Storage,
		"avatar":              &Avatar.Storage,
		"repo-avatar":         &RepoAvatar.Storage,
		"repo-archive":        &RepoArchive.Storage,
		"packages":            &Packages.Storage,
		"storage.actions_log": &Actions.LogStorage,
		"actions.artifacts":   &Actions.ArtifactStorage,
	}

	for sectionName, storage := range testSectionsMap {
		t.Run(sectionName, func(t *testing.T) {
			if legacy, ok := testSectionToLegacy[sectionName]; ok {
				if defaultStorageType == LocalStorageType {
					t.Run("legacy local", func(t *testing.T) {
						testStorageTypesSpecificStorage(t, iniStr+legacy, LocalStorageType, testLegacyPath, testSectionToPath, sectionName, storage)
						testStorageTypesSpecificStorageTypeOverride(t, iniStr+legacy, LocalStorageType, testLegacyPath, testSectionToPath, sectionName, storage)
					})
				} else {
					t.Run("legacy minio", func(t *testing.T) {
						testStorageTypesSpecificStorage(t, iniStr+legacy, MinioStorageType, defaultStorageTypePath, testSectionToPath, sectionName, storage)
						testStorageTypesSpecificStorageTypeOverride(t, iniStr+legacy, LocalStorageType, testLegacyPath, testSectionToPath, sectionName, storage)
					})
				}
			}
			for _, specificStorageType := range storageTypes {
				testStorageTypesSpecificStorageTypeOverride(t, iniStr, specificStorageType, defaultStorageTypePath, testSectionToPath, sectionName, storage)
			}
		})
	}
}

func testStorageTypesSpecificStorage(t *testing.T, iniStr string, defaultStorageType StorageType, defaultStorageTypePath, testSectionToPath testSectionToPathFun, sectionName string, storage **Storage) {
	var section string

	//
	// Specific section is absent
	//
	testStoragePathMatch(t, iniStr, defaultStorageType, defaultStorageTypePath, sectionName, storage)

	//
	// Specific section is empty
	//
	section = fmt.Sprintf(`
[%s]
`,
		sectionName)
	testStoragePathMatch(t, iniStr+section, defaultStorageType, defaultStorageTypePath, sectionName, storage)

	//
	// Specific section with a path override
	//
	section = fmt.Sprintf(`
[%s]
%s = %s
`,
		sectionName,
		testStorageTypeToSetting(defaultStorageType),
		testSpecificPath(defaultStorageType, ""))
	testStoragePathMatch(t, iniStr+section, defaultStorageType, testSpecificPath, sectionName, storage)
}

func testStorageTypesSpecificStorageTypeOverride(t *testing.T, iniStr string, overrideStorageType StorageType, defaultStorageTypePath, testSectionToPath testSectionToPathFun, sectionName string, storage **Storage) {
	var section string
	t.Run("specific-"+string(overrideStorageType), func(t *testing.T) {
		//
		// Specific section with a path and storage type override
		//
		section = fmt.Sprintf(`
[%s]
STORAGE_TYPE = %s
%s = %s
`,
			sectionName,
			overrideStorageType,
			testStorageTypeToSetting(overrideStorageType),
			testSpecificPath(overrideStorageType, ""))
		testStoragePathMatch(t, iniStr+section, overrideStorageType, testSpecificPath, sectionName, storage)

		//
		// Specific section with type override
		//
		section = fmt.Sprintf(`
[%s]
STORAGE_TYPE = %s
`,
			sectionName,
			overrideStorageType)
		testStoragePathMatch(t, iniStr+section, overrideStorageType, defaultStorageTypePath, sectionName, storage)
	})
}

func testStoragePathMatch(t *testing.T, iniStr string, storageType StorageType, testSectionToPath testSectionToPathFun, section string, storage **Storage) {
	cfg, err := NewConfigProviderFromData(iniStr)
	require.NoError(t, err, iniStr)
	require.NoError(t, loadCommonSettingsFrom(cfg), iniStr)
	assert.EqualValues(t, testSectionToPath(storageType, section), testStorageGetPath(*storage), iniStr)
	assert.EqualValues(t, storageType, (*storage).Type, iniStr)
}