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.

testdb.go 8.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. // Copyright 2021 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package unittest
  4. import (
  5. "context"
  6. "fmt"
  7. "log"
  8. "os"
  9. "path/filepath"
  10. "strings"
  11. "testing"
  12. "code.gitea.io/gitea/models/db"
  13. "code.gitea.io/gitea/models/system"
  14. "code.gitea.io/gitea/modules/auth/password/hash"
  15. "code.gitea.io/gitea/modules/base"
  16. "code.gitea.io/gitea/modules/cache"
  17. "code.gitea.io/gitea/modules/git"
  18. "code.gitea.io/gitea/modules/setting"
  19. "code.gitea.io/gitea/modules/setting/config"
  20. "code.gitea.io/gitea/modules/storage"
  21. "code.gitea.io/gitea/modules/util"
  22. "github.com/stretchr/testify/assert"
  23. "xorm.io/xorm"
  24. "xorm.io/xorm/names"
  25. )
  26. // giteaRoot a path to the gitea root
  27. var (
  28. giteaRoot string
  29. fixturesDir string
  30. )
  31. // FixturesDir returns the fixture directory
  32. func FixturesDir() string {
  33. return fixturesDir
  34. }
  35. func fatalTestError(fmtStr string, args ...any) {
  36. _, _ = fmt.Fprintf(os.Stderr, fmtStr, args...)
  37. os.Exit(1)
  38. }
  39. // InitSettings initializes config provider and load common settings for tests
  40. func InitSettings() {
  41. if setting.CustomConf == "" {
  42. setting.CustomConf = filepath.Join(setting.CustomPath, "conf/app-unittest-tmp.ini")
  43. _ = os.Remove(setting.CustomConf)
  44. }
  45. setting.InitCfgProvider(setting.CustomConf)
  46. setting.LoadCommonSettings()
  47. if err := setting.PrepareAppDataPath(); err != nil {
  48. log.Fatalf("Can not prepare APP_DATA_PATH: %v", err)
  49. }
  50. // register the dummy hash algorithm function used in the test fixtures
  51. _ = hash.Register("dummy", hash.NewDummyHasher)
  52. setting.PasswordHashAlgo, _ = hash.SetDefaultPasswordHashAlgorithm("dummy")
  53. }
  54. // TestOptions represents test options
  55. type TestOptions struct {
  56. FixtureFiles []string
  57. SetUp func() error // SetUp will be executed before all tests in this package
  58. TearDown func() error // TearDown will be executed after all tests in this package
  59. }
  60. // MainTest a reusable TestMain(..) function for unit tests that need to use a
  61. // test database. Creates the test database, and sets necessary settings.
  62. func MainTest(m *testing.M, testOpts ...*TestOptions) {
  63. searchDir, _ := os.Getwd()
  64. for searchDir != "" {
  65. if _, err := os.Stat(filepath.Join(searchDir, "go.mod")); err == nil {
  66. break // The "go.mod" should be the one for Gitea repository
  67. }
  68. if dir := filepath.Dir(searchDir); dir == searchDir {
  69. searchDir = "" // reaches the root of filesystem
  70. } else {
  71. searchDir = dir
  72. }
  73. }
  74. if searchDir == "" {
  75. panic("The tests should run in a Gitea repository, there should be a 'go.mod' in the root")
  76. }
  77. giteaRoot = searchDir
  78. setting.CustomPath = filepath.Join(giteaRoot, "custom")
  79. InitSettings()
  80. fixturesDir = filepath.Join(giteaRoot, "models", "fixtures")
  81. var opts FixturesOptions
  82. if len(testOpts) == 0 || len(testOpts[0].FixtureFiles) == 0 {
  83. opts.Dir = fixturesDir
  84. } else {
  85. for _, f := range testOpts[0].FixtureFiles {
  86. if len(f) != 0 {
  87. opts.Files = append(opts.Files, filepath.Join(fixturesDir, f))
  88. }
  89. }
  90. }
  91. if err := CreateTestEngine(opts); err != nil {
  92. fatalTestError("Error creating test engine: %v\n", err)
  93. }
  94. setting.IsInTesting = true
  95. setting.AppURL = "https://try.gitea.io/"
  96. setting.RunUser = "runuser"
  97. setting.SSH.User = "sshuser"
  98. setting.SSH.BuiltinServerUser = "builtinuser"
  99. setting.SSH.Port = 3000
  100. setting.SSH.Domain = "try.gitea.io"
  101. setting.Database.Type = "sqlite3"
  102. setting.Repository.DefaultBranch = "master" // many test code still assume that default branch is called "master"
  103. repoRootPath, err := os.MkdirTemp(os.TempDir(), "repos")
  104. if err != nil {
  105. fatalTestError("TempDir: %v\n", err)
  106. }
  107. setting.RepoRootPath = repoRootPath
  108. appDataPath, err := os.MkdirTemp(os.TempDir(), "appdata")
  109. if err != nil {
  110. fatalTestError("TempDir: %v\n", err)
  111. }
  112. setting.AppDataPath = appDataPath
  113. setting.AppWorkPath = giteaRoot
  114. setting.StaticRootPath = giteaRoot
  115. setting.GravatarSource = "https://secure.gravatar.com/avatar/"
  116. setting.Attachment.Storage.Path = filepath.Join(setting.AppDataPath, "attachments")
  117. setting.LFS.Storage.Path = filepath.Join(setting.AppDataPath, "lfs")
  118. setting.Avatar.Storage.Path = filepath.Join(setting.AppDataPath, "avatars")
  119. setting.RepoAvatar.Storage.Path = filepath.Join(setting.AppDataPath, "repo-avatars")
  120. setting.RepoArchive.Storage.Path = filepath.Join(setting.AppDataPath, "repo-archive")
  121. setting.Packages.Storage.Path = filepath.Join(setting.AppDataPath, "packages")
  122. setting.Actions.LogStorage.Path = filepath.Join(setting.AppDataPath, "actions_log")
  123. setting.Git.HomePath = filepath.Join(setting.AppDataPath, "home")
  124. setting.IncomingEmail.ReplyToAddress = "incoming+%{token}@localhost"
  125. config.SetDynGetter(system.NewDatabaseDynKeyGetter())
  126. if err = cache.Init(); err != nil {
  127. fatalTestError("cache.Init: %v\n", err)
  128. }
  129. if err = storage.Init(); err != nil {
  130. fatalTestError("storage.Init: %v\n", err)
  131. }
  132. if err = util.RemoveAll(repoRootPath); err != nil {
  133. fatalTestError("util.RemoveAll: %v\n", err)
  134. }
  135. if err = CopyDir(filepath.Join(giteaRoot, "tests", "gitea-repositories-meta"), setting.RepoRootPath); err != nil {
  136. fatalTestError("util.CopyDir: %v\n", err)
  137. }
  138. if err = git.InitFull(context.Background()); err != nil {
  139. fatalTestError("git.Init: %v\n", err)
  140. }
  141. ownerDirs, err := os.ReadDir(setting.RepoRootPath)
  142. if err != nil {
  143. fatalTestError("unable to read the new repo root: %v\n", err)
  144. }
  145. for _, ownerDir := range ownerDirs {
  146. if !ownerDir.Type().IsDir() {
  147. continue
  148. }
  149. repoDirs, err := os.ReadDir(filepath.Join(setting.RepoRootPath, ownerDir.Name()))
  150. if err != nil {
  151. fatalTestError("unable to read the new repo root: %v\n", err)
  152. }
  153. for _, repoDir := range repoDirs {
  154. _ = os.MkdirAll(filepath.Join(setting.RepoRootPath, ownerDir.Name(), repoDir.Name(), "objects", "pack"), 0o755)
  155. _ = os.MkdirAll(filepath.Join(setting.RepoRootPath, ownerDir.Name(), repoDir.Name(), "objects", "info"), 0o755)
  156. _ = os.MkdirAll(filepath.Join(setting.RepoRootPath, ownerDir.Name(), repoDir.Name(), "refs", "heads"), 0o755)
  157. _ = os.MkdirAll(filepath.Join(setting.RepoRootPath, ownerDir.Name(), repoDir.Name(), "refs", "tag"), 0o755)
  158. }
  159. }
  160. if len(testOpts) > 0 && testOpts[0].SetUp != nil {
  161. if err := testOpts[0].SetUp(); err != nil {
  162. fatalTestError("set up failed: %v\n", err)
  163. }
  164. }
  165. exitStatus := m.Run()
  166. if len(testOpts) > 0 && testOpts[0].TearDown != nil {
  167. if err := testOpts[0].TearDown(); err != nil {
  168. fatalTestError("tear down failed: %v\n", err)
  169. }
  170. }
  171. if err = util.RemoveAll(repoRootPath); err != nil {
  172. fatalTestError("util.RemoveAll: %v\n", err)
  173. }
  174. if err = util.RemoveAll(appDataPath); err != nil {
  175. fatalTestError("util.RemoveAll: %v\n", err)
  176. }
  177. os.Exit(exitStatus)
  178. }
  179. // FixturesOptions fixtures needs to be loaded options
  180. type FixturesOptions struct {
  181. Dir string
  182. Files []string
  183. }
  184. // CreateTestEngine creates a memory database and loads the fixture data from fixturesDir
  185. func CreateTestEngine(opts FixturesOptions) error {
  186. x, err := xorm.NewEngine("sqlite3", "file::memory:?cache=shared&_txlock=immediate")
  187. if err != nil {
  188. if strings.Contains(err.Error(), "unknown driver") {
  189. return fmt.Errorf(`sqlite3 requires: import _ "github.com/mattn/go-sqlite3" or -tags sqlite,sqlite_unlock_notify%s%w`, "\n", err)
  190. }
  191. return err
  192. }
  193. x.SetMapper(names.GonicMapper{})
  194. db.SetDefaultEngine(context.Background(), x)
  195. if err = db.SyncAllTables(); err != nil {
  196. return err
  197. }
  198. switch os.Getenv("GITEA_UNIT_TESTS_LOG_SQL") {
  199. case "true", "1":
  200. x.ShowSQL(true)
  201. }
  202. return InitFixtures(opts)
  203. }
  204. // PrepareTestDatabase load test fixtures into test database
  205. func PrepareTestDatabase() error {
  206. return LoadFixtures()
  207. }
  208. // PrepareTestEnv prepares the environment for unit tests. Can only be called
  209. // by tests that use the above MainTest(..) function.
  210. func PrepareTestEnv(t testing.TB) {
  211. assert.NoError(t, PrepareTestDatabase())
  212. assert.NoError(t, util.RemoveAll(setting.RepoRootPath))
  213. metaPath := filepath.Join(giteaRoot, "tests", "gitea-repositories-meta")
  214. assert.NoError(t, CopyDir(metaPath, setting.RepoRootPath))
  215. ownerDirs, err := os.ReadDir(setting.RepoRootPath)
  216. assert.NoError(t, err)
  217. for _, ownerDir := range ownerDirs {
  218. if !ownerDir.Type().IsDir() {
  219. continue
  220. }
  221. repoDirs, err := os.ReadDir(filepath.Join(setting.RepoRootPath, ownerDir.Name()))
  222. assert.NoError(t, err)
  223. for _, repoDir := range repoDirs {
  224. _ = os.MkdirAll(filepath.Join(setting.RepoRootPath, ownerDir.Name(), repoDir.Name(), "objects", "pack"), 0o755)
  225. _ = os.MkdirAll(filepath.Join(setting.RepoRootPath, ownerDir.Name(), repoDir.Name(), "objects", "info"), 0o755)
  226. _ = os.MkdirAll(filepath.Join(setting.RepoRootPath, ownerDir.Name(), repoDir.Name(), "refs", "heads"), 0o755)
  227. _ = os.MkdirAll(filepath.Join(setting.RepoRootPath, ownerDir.Name(), repoDir.Name(), "refs", "tag"), 0o755)
  228. }
  229. }
  230. base.SetupGiteaRoot() // Makes sure GITEA_ROOT is set
  231. }