You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

storage_test.go 4.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. // Copyright 2020 The Gitea Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package setting
  5. import (
  6. "testing"
  7. "github.com/stretchr/testify/assert"
  8. ini "gopkg.in/ini.v1"
  9. )
  10. func Test_getStorageCustomType(t *testing.T) {
  11. iniStr := `
  12. [attachment]
  13. STORAGE_TYPE = my_minio
  14. MINIO_BUCKET = gitea-attachment
  15. [storage.my_minio]
  16. STORAGE_TYPE = minio
  17. MINIO_ENDPOINT = my_minio:9000
  18. `
  19. Cfg, _ = ini.Load([]byte(iniStr))
  20. sec := Cfg.Section("attachment")
  21. storageType := sec.Key("STORAGE_TYPE").MustString("")
  22. storage := getStorage("attachments", storageType, sec)
  23. assert.EqualValues(t, "minio", storage.Type)
  24. assert.EqualValues(t, "my_minio:9000", storage.Section.Key("MINIO_ENDPOINT").String())
  25. assert.EqualValues(t, "gitea-attachment", storage.Section.Key("MINIO_BUCKET").String())
  26. }
  27. func Test_getStorageNameSectionOverridesTypeSection(t *testing.T) {
  28. iniStr := `
  29. [attachment]
  30. STORAGE_TYPE = minio
  31. [storage.attachments]
  32. MINIO_BUCKET = gitea-attachment
  33. [storage.minio]
  34. MINIO_BUCKET = gitea
  35. `
  36. Cfg, _ = ini.Load([]byte(iniStr))
  37. sec := Cfg.Section("attachment")
  38. storageType := sec.Key("STORAGE_TYPE").MustString("")
  39. storage := getStorage("attachments", storageType, sec)
  40. assert.EqualValues(t, "minio", storage.Type)
  41. assert.EqualValues(t, "gitea-attachment", storage.Section.Key("MINIO_BUCKET").String())
  42. }
  43. func Test_getStorageTypeSectionOverridesStorageSection(t *testing.T) {
  44. iniStr := `
  45. [attachment]
  46. STORAGE_TYPE = minio
  47. [storage.minio]
  48. MINIO_BUCKET = gitea-minio
  49. [storage]
  50. MINIO_BUCKET = gitea
  51. `
  52. Cfg, _ = ini.Load([]byte(iniStr))
  53. sec := Cfg.Section("attachment")
  54. storageType := sec.Key("STORAGE_TYPE").MustString("")
  55. storage := getStorage("attachments", storageType, sec)
  56. assert.EqualValues(t, "minio", storage.Type)
  57. assert.EqualValues(t, "gitea-minio", storage.Section.Key("MINIO_BUCKET").String())
  58. }
  59. func Test_getStorageSpecificOverridesStorage(t *testing.T) {
  60. iniStr := `
  61. [attachment]
  62. STORAGE_TYPE = minio
  63. MINIO_BUCKET = gitea-attachment
  64. [storage.attachments]
  65. MINIO_BUCKET = gitea
  66. [storage]
  67. STORAGE_TYPE = local
  68. `
  69. Cfg, _ = ini.Load([]byte(iniStr))
  70. sec := Cfg.Section("attachment")
  71. storageType := sec.Key("STORAGE_TYPE").MustString("")
  72. storage := getStorage("attachments", storageType, sec)
  73. assert.EqualValues(t, "minio", storage.Type)
  74. assert.EqualValues(t, "gitea-attachment", storage.Section.Key("MINIO_BUCKET").String())
  75. }
  76. func Test_getStorageGetDefaults(t *testing.T) {
  77. Cfg, _ = ini.Load([]byte(""))
  78. sec := Cfg.Section("attachment")
  79. storageType := sec.Key("STORAGE_TYPE").MustString("")
  80. storage := getStorage("attachments", storageType, sec)
  81. assert.EqualValues(t, "gitea", storage.Section.Key("MINIO_BUCKET").String())
  82. }
  83. func Test_getStorageMultipleName(t *testing.T) {
  84. iniStr := `
  85. [lfs]
  86. MINIO_BUCKET = gitea-lfs
  87. [attachment]
  88. MINIO_BUCKET = gitea-attachment
  89. [storage]
  90. MINIO_BUCKET = gitea-storage
  91. `
  92. Cfg, _ = ini.Load([]byte(iniStr))
  93. {
  94. sec := Cfg.Section("attachment")
  95. storageType := sec.Key("STORAGE_TYPE").MustString("")
  96. storage := getStorage("attachments", storageType, sec)
  97. assert.EqualValues(t, "gitea-attachment", storage.Section.Key("MINIO_BUCKET").String())
  98. }
  99. {
  100. sec := Cfg.Section("lfs")
  101. storageType := sec.Key("STORAGE_TYPE").MustString("")
  102. storage := getStorage("lfs", storageType, sec)
  103. assert.EqualValues(t, "gitea-lfs", storage.Section.Key("MINIO_BUCKET").String())
  104. }
  105. {
  106. sec := Cfg.Section("avatar")
  107. storageType := sec.Key("STORAGE_TYPE").MustString("")
  108. storage := getStorage("avatars", storageType, sec)
  109. assert.EqualValues(t, "gitea-storage", storage.Section.Key("MINIO_BUCKET").String())
  110. }
  111. }
  112. func Test_getStorageUseOtherNameAsType(t *testing.T) {
  113. iniStr := `
  114. [attachment]
  115. STORAGE_TYPE = lfs
  116. [storage.lfs]
  117. MINIO_BUCKET = gitea-storage
  118. `
  119. Cfg, _ = ini.Load([]byte(iniStr))
  120. {
  121. sec := Cfg.Section("attachment")
  122. storageType := sec.Key("STORAGE_TYPE").MustString("")
  123. storage := getStorage("attachments", storageType, sec)
  124. assert.EqualValues(t, "gitea-storage", storage.Section.Key("MINIO_BUCKET").String())
  125. }
  126. {
  127. sec := Cfg.Section("lfs")
  128. storageType := sec.Key("STORAGE_TYPE").MustString("")
  129. storage := getStorage("lfs", storageType, sec)
  130. assert.EqualValues(t, "gitea-storage", storage.Section.Key("MINIO_BUCKET").String())
  131. }
  132. }
  133. func Test_getStorageInheritStorageType(t *testing.T) {
  134. iniStr := `
  135. [storage]
  136. STORAGE_TYPE = minio
  137. `
  138. Cfg, _ = ini.Load([]byte(iniStr))
  139. sec := Cfg.Section("attachment")
  140. storageType := sec.Key("STORAGE_TYPE").MustString("")
  141. storage := getStorage("attachments", storageType, sec)
  142. assert.EqualValues(t, "minio", storage.Type)
  143. }
  144. func Test_getStorageInheritNameSectionType(t *testing.T) {
  145. iniStr := `
  146. [storage.attachments]
  147. STORAGE_TYPE = minio
  148. `
  149. Cfg, _ = ini.Load([]byte(iniStr))
  150. sec := Cfg.Section("attachment")
  151. storageType := sec.Key("STORAGE_TYPE").MustString("")
  152. storage := getStorage("attachments", storageType, sec)
  153. assert.EqualValues(t, "minio", storage.Type)
  154. }