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.8KB

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