A fork of Gitea (see branch `mj`) adding Majority Judgment Polls 𐄷 over Issues and Merge Requests. https://git.mieuxvoter.fr
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.

218 lines
6.5 KiB

Add a storage layer for attachments (#11387) * Add a storage layer for attachments * Fix some bug * fix test * Fix copyright head and lint * Fix bug * Add setting for minio and flags for migrate-storage * Add documents * fix lint * Add test for minio store type on attachments * fix test * fix test * Apply suggestions from code review Co-authored-by: guillep2k <18600385+guillep2k@users.noreply.github.com> * Add warning when storage migrated successfully * Fix drone * fix test * rebase * Fix test * display the error on console * Move minio test to amd64 since minio docker don't support arm64 * refactor the codes * add trace * Fix test * remove log on xorm * Fi download bug * Add a storage layer for attachments * Add setting for minio and flags for migrate-storage * fix lint * Add test for minio store type on attachments * Apply suggestions from code review Co-authored-by: guillep2k <18600385+guillep2k@users.noreply.github.com> * Fix drone * fix test * Fix test * display the error on console * Move minio test to amd64 since minio docker don't support arm64 * refactor the codes * add trace * Fix test * Add URL function to serve attachments directly from S3/Minio * Add ability to enable/disable redirection in attachment configuration * Fix typo * Add a storage layer for attachments * Add setting for minio and flags for migrate-storage * fix lint * Add test for minio store type on attachments * Apply suggestions from code review Co-authored-by: guillep2k <18600385+guillep2k@users.noreply.github.com> * Fix drone * fix test * Fix test * display the error on console * Move minio test to amd64 since minio docker don't support arm64 * don't change unrelated files * Fix lint * Fix build * update go.mod and go.sum * Use github.com/minio/minio-go/v6 * Remove unused function * Upgrade minio to v7 and some other improvements * fix lint * Fix go mod Co-authored-by: guillep2k <18600385+guillep2k@users.noreply.github.com> Co-authored-by: Tyler <tystuyfzand@gmail.com>
2 years ago
Add a storage layer for attachments (#11387) * Add a storage layer for attachments * Fix some bug * fix test * Fix copyright head and lint * Fix bug * Add setting for minio and flags for migrate-storage * Add documents * fix lint * Add test for minio store type on attachments * fix test * fix test * Apply suggestions from code review Co-authored-by: guillep2k <18600385+guillep2k@users.noreply.github.com> * Add warning when storage migrated successfully * Fix drone * fix test * rebase * Fix test * display the error on console * Move minio test to amd64 since minio docker don't support arm64 * refactor the codes * add trace * Fix test * remove log on xorm * Fi download bug * Add a storage layer for attachments * Add setting for minio and flags for migrate-storage * fix lint * Add test for minio store type on attachments * Apply suggestions from code review Co-authored-by: guillep2k <18600385+guillep2k@users.noreply.github.com> * Fix drone * fix test * Fix test * display the error on console * Move minio test to amd64 since minio docker don't support arm64 * refactor the codes * add trace * Fix test * Add URL function to serve attachments directly from S3/Minio * Add ability to enable/disable redirection in attachment configuration * Fix typo * Add a storage layer for attachments * Add setting for minio and flags for migrate-storage * fix lint * Add test for minio store type on attachments * Apply suggestions from code review Co-authored-by: guillep2k <18600385+guillep2k@users.noreply.github.com> * Fix drone * fix test * Fix test * display the error on console * Move minio test to amd64 since minio docker don't support arm64 * don't change unrelated files * Fix lint * Fix build * update go.mod and go.sum * Use github.com/minio/minio-go/v6 * Remove unused function * Upgrade minio to v7 and some other improvements * fix lint * Fix go mod Co-authored-by: guillep2k <18600385+guillep2k@users.noreply.github.com> Co-authored-by: Tyler <tystuyfzand@gmail.com>
2 years ago
  1. // Copyright 2016 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 models
  5. import (
  6. "fmt"
  7. "io/ioutil"
  8. "math"
  9. "net/url"
  10. "os"
  11. "path/filepath"
  12. "testing"
  13. "code.gitea.io/gitea/modules/base"
  14. "code.gitea.io/gitea/modules/setting"
  15. "code.gitea.io/gitea/modules/storage"
  16. "code.gitea.io/gitea/modules/util"
  17. "github.com/stretchr/testify/assert"
  18. "xorm.io/xorm"
  19. "xorm.io/xorm/names"
  20. )
  21. // NonexistentID an ID that will never exist
  22. const NonexistentID = int64(math.MaxInt64)
  23. // giteaRoot a path to the gitea root
  24. var (
  25. giteaRoot string
  26. fixturesDir string
  27. )
  28. func fatalTestError(fmtStr string, args ...interface{}) {
  29. fmt.Fprintf(os.Stderr, fmtStr, args...)
  30. os.Exit(1)
  31. }
  32. // MainTest a reusable TestMain(..) function for unit tests that need to use a
  33. // test database. Creates the test database, and sets necessary settings.
  34. func MainTest(m *testing.M, pathToGiteaRoot string) {
  35. var err error
  36. giteaRoot = pathToGiteaRoot
  37. fixturesDir = filepath.Join(pathToGiteaRoot, "models", "fixtures")
  38. if err = CreateTestEngine(fixturesDir); err != nil {
  39. fatalTestError("Error creating test engine: %v\n", err)
  40. }
  41. setting.AppURL = "https://try.gitea.io/"
  42. setting.RunUser = "runuser"
  43. setting.SSH.Port = 3000
  44. setting.SSH.Domain = "try.gitea.io"
  45. setting.Database.UseSQLite3 = true
  46. setting.RepoRootPath, err = ioutil.TempDir(os.TempDir(), "repos")
  47. if err != nil {
  48. fatalTestError("TempDir: %v\n", err)
  49. }
  50. setting.AppDataPath, err = ioutil.TempDir(os.TempDir(), "appdata")
  51. if err != nil {
  52. fatalTestError("TempDir: %v\n", err)
  53. }
  54. setting.AppWorkPath = pathToGiteaRoot
  55. setting.StaticRootPath = pathToGiteaRoot
  56. setting.GravatarSourceURL, err = url.Parse("https://secure.gravatar.com/avatar/")
  57. if err != nil {
  58. fatalTestError("url.Parse: %v\n", err)
  59. }
  60. setting.Attachment.Storage.Path = filepath.Join(setting.AppDataPath, "attachments")
  61. setting.LFS.Storage.Path = filepath.Join(setting.AppDataPath, "lfs")
  62. setting.Avatar.Storage.Path = filepath.Join(setting.AppDataPath, "avatars")
  63. setting.RepoAvatar.Storage.Path = filepath.Join(setting.AppDataPath, "repo-avatars")
  64. if err = storage.Init(); err != nil {
  65. fatalTestError("storage.Init: %v\n", err)
  66. }
  67. if err = util.RemoveAll(setting.RepoRootPath); err != nil {
  68. fatalTestError("util.RemoveAll: %v\n", err)
  69. }
  70. if err = util.CopyDir(filepath.Join(pathToGiteaRoot, "integrations", "gitea-repositories-meta"), setting.RepoRootPath); err != nil {
  71. fatalTestError("util.CopyDir: %v\n", err)
  72. }
  73. exitStatus := m.Run()
  74. if err = util.RemoveAll(setting.RepoRootPath); err != nil {
  75. fatalTestError("util.RemoveAll: %v\n", err)
  76. }
  77. if err = util.RemoveAll(setting.AppDataPath); err != nil {
  78. fatalTestError("util.RemoveAll: %v\n", err)
  79. }
  80. os.Exit(exitStatus)
  81. }
  82. // CreateTestEngine creates a memory database and loads the fixture data from fixturesDir
  83. func CreateTestEngine(fixturesDir string) error {
  84. var err error
  85. x, err = xorm.NewEngine("sqlite3", "file::memory:?cache=shared&_txlock=immediate")
  86. if err != nil {
  87. return err
  88. }
  89. x.SetMapper(names.GonicMapper{})
  90. if err = x.StoreEngine("InnoDB").Sync2(tables...); err != nil {
  91. return err
  92. }
  93. switch os.Getenv("GITEA_UNIT_TESTS_VERBOSE") {
  94. case "true", "1":
  95. x.ShowSQL(true)
  96. }
  97. return InitFixtures(fixturesDir)
  98. }
  99. // PrepareTestDatabase load test fixtures into test database
  100. func PrepareTestDatabase() error {
  101. return LoadFixtures()
  102. }
  103. // PrepareTestEnv prepares the environment for unit tests. Can only be called
  104. // by tests that use the above MainTest(..) function.
  105. func PrepareTestEnv(t testing.TB) {
  106. assert.NoError(t, PrepareTestDatabase())
  107. assert.NoError(t, util.RemoveAll(setting.RepoRootPath))
  108. metaPath := filepath.Join(giteaRoot, "integrations", "gitea-repositories-meta")
  109. assert.NoError(t, util.CopyDir(metaPath, setting.RepoRootPath))
  110. base.SetupGiteaRoot() // Makes sure GITEA_ROOT is set
  111. }
  112. type testCond struct {
  113. query interface{}
  114. args []interface{}
  115. }
  116. // Cond create a condition with arguments for a test
  117. func Cond(query interface{}, args ...interface{}) interface{} {
  118. return &testCond{query: query, args: args}
  119. }
  120. func whereConditions(sess *xorm.Session, conditions []interface{}) {
  121. for _, condition := range conditions {
  122. switch cond := condition.(type) {
  123. case *testCond:
  124. sess.Where(cond.query, cond.args...)
  125. default:
  126. sess.Where(cond)
  127. }
  128. }
  129. }
  130. func loadBeanIfExists(bean interface{}, conditions ...interface{}) (bool, error) {
  131. sess := x.NewSession()
  132. defer sess.Close()
  133. whereConditions(sess, conditions)
  134. return sess.Get(bean)
  135. }
  136. // BeanExists for testing, check if a bean exists
  137. func BeanExists(t testing.TB, bean interface{}, conditions ...interface{}) bool {
  138. exists, err := loadBeanIfExists(bean, conditions...)
  139. assert.NoError(t, err)
  140. return exists
  141. }
  142. // AssertExistsAndLoadBean assert that a bean exists and load it from the test
  143. // database
  144. func AssertExistsAndLoadBean(t testing.TB, bean interface{}, conditions ...interface{}) interface{} {
  145. exists, err := loadBeanIfExists(bean, conditions...)
  146. assert.NoError(t, err)
  147. assert.True(t, exists,
  148. "Expected to find %+v (of type %T, with conditions %+v), but did not",
  149. bean, bean, conditions)
  150. return bean
  151. }
  152. // GetCount get the count of a bean
  153. func GetCount(t testing.TB, bean interface{}, conditions ...interface{}) int {
  154. sess := x.NewSession()
  155. defer sess.Close()
  156. whereConditions(sess, conditions)
  157. count, err := sess.Count(bean)
  158. assert.NoError(t, err)
  159. return int(count)
  160. }
  161. // AssertNotExistsBean assert that a bean does not exist in the test database
  162. func AssertNotExistsBean(t testing.TB, bean interface{}, conditions ...interface{}) {
  163. exists, err := loadBeanIfExists(bean, conditions...)
  164. assert.NoError(t, err)
  165. assert.False(t, exists)
  166. }
  167. // AssertExistsIf asserts that a bean exists or does not exist, depending on
  168. // what is expected.
  169. func AssertExistsIf(t *testing.T, expected bool, bean interface{}, conditions ...interface{}) {
  170. exists, err := loadBeanIfExists(bean, conditions...)
  171. assert.NoError(t, err)
  172. assert.Equal(t, expected, exists)
  173. }
  174. // AssertSuccessfulInsert assert that beans is successfully inserted
  175. func AssertSuccessfulInsert(t testing.TB, beans ...interface{}) {
  176. _, err := x.Insert(beans...)
  177. assert.NoError(t, err)
  178. }
  179. // AssertCount assert the count of a bean
  180. func AssertCount(t testing.TB, bean, expected interface{}) {
  181. assert.EqualValues(t, expected, GetCount(t, bean))
  182. }
  183. // AssertInt64InRange assert value is in range [low, high]
  184. func AssertInt64InRange(t testing.TB, low, high, value int64) {
  185. assert.True(t, value >= low && value <= high,
  186. "Expected value in range [%d, %d], found %d", low, high, value)
  187. }