Fork to maintain patches against the official gitea for https://code.ceondo.com https://github.com/go-gitea/gitea

integration_test.go 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. // Copyright 2017 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 integrations
  5. import (
  6. "bytes"
  7. "database/sql"
  8. "fmt"
  9. "io"
  10. "log"
  11. "net/http"
  12. "os"
  13. "testing"
  14. "code.gitea.io/gitea/models"
  15. "code.gitea.io/gitea/modules/setting"
  16. "code.gitea.io/gitea/routers"
  17. "code.gitea.io/gitea/routers/routes"
  18. "github.com/Unknwon/com"
  19. "github.com/stretchr/testify/assert"
  20. "gopkg.in/macaron.v1"
  21. "gopkg.in/testfixtures.v2"
  22. )
  23. var mac *macaron.Macaron
  24. func TestMain(m *testing.M) {
  25. initIntegrationTest()
  26. mac = routes.NewMacaron()
  27. routes.RegisterRoutes(mac)
  28. var helper testfixtures.Helper
  29. if setting.UseMySQL {
  30. helper = &testfixtures.MySQL{}
  31. } else if setting.UsePostgreSQL {
  32. helper = &testfixtures.PostgreSQL{}
  33. } else if setting.UseSQLite3 {
  34. helper = &testfixtures.SQLite{}
  35. } else {
  36. fmt.Println("Unsupported RDBMS for integration tests")
  37. os.Exit(1)
  38. }
  39. err := models.InitFixtures(
  40. helper,
  41. "models/fixtures/",
  42. )
  43. if err != nil {
  44. fmt.Printf("Error initializing test database: %v\n", err)
  45. os.Exit(1)
  46. }
  47. os.Exit(m.Run())
  48. }
  49. func initIntegrationTest() {
  50. if setting.CustomConf = os.Getenv("GITEA_CONF"); setting.CustomConf == "" {
  51. fmt.Println("Environment variable $GITEA_CONF not set")
  52. os.Exit(1)
  53. }
  54. setting.NewContext()
  55. models.LoadConfigs()
  56. switch {
  57. case setting.UseMySQL:
  58. db, err := sql.Open("mysql", fmt.Sprintf("%s:%s@tcp(%s)/",
  59. models.DbCfg.User, models.DbCfg.Passwd, models.DbCfg.Host))
  60. defer db.Close()
  61. if err != nil {
  62. log.Fatalf("sql.Open: %v", err)
  63. }
  64. if _, err = db.Exec("CREATE DATABASE IF NOT EXISTS testgitea"); err != nil {
  65. log.Fatalf("db.Exec: %v", err)
  66. }
  67. case setting.UsePostgreSQL:
  68. db, err := sql.Open("postgres", fmt.Sprintf("postgres://%s:%s@%s/?sslmode=%s",
  69. models.DbCfg.User, models.DbCfg.Passwd, models.DbCfg.Host, models.DbCfg.SSLMode))
  70. defer db.Close()
  71. if err != nil {
  72. log.Fatalf("sql.Open: %v", err)
  73. }
  74. rows, err := db.Query(fmt.Sprintf("SELECT 1 FROM pg_database WHERE datname = '%s'",
  75. models.DbCfg.Name))
  76. if err != nil {
  77. log.Fatalf("db.Query: %v", err)
  78. }
  79. if rows.Next() {
  80. break // database already exists
  81. }
  82. if _, err = db.Exec("CREATE DATABASE testgitea"); err != nil {
  83. log.Fatalf("db.Exec: %v", err)
  84. }
  85. }
  86. routers.GlobalInit()
  87. }
  88. func prepareTestEnv(t *testing.T) {
  89. assert.NoError(t, models.LoadFixtures())
  90. assert.NoError(t, os.RemoveAll("integrations/gitea-integration"))
  91. assert.NoError(t, com.CopyDir("integrations/gitea-integration-meta", "integrations/gitea-integration"))
  92. }
  93. type TestResponseWriter struct {
  94. HeaderCode int
  95. Writer io.Writer
  96. }
  97. func (w *TestResponseWriter) Header() http.Header {
  98. return make(map[string][]string)
  99. }
  100. func (w *TestResponseWriter) Write(b []byte) (int, error) {
  101. return w.Writer.Write(b)
  102. }
  103. func (w *TestResponseWriter) WriteHeader(n int) {
  104. w.HeaderCode = n
  105. }
  106. type TestResponse struct {
  107. HeaderCode int
  108. Body []byte
  109. }
  110. func MakeRequest(req *http.Request) *TestResponse {
  111. buffer := bytes.NewBuffer(nil)
  112. respWriter := &TestResponseWriter{
  113. Writer: buffer,
  114. }
  115. mac.ServeHTTP(respWriter, req)
  116. return &TestResponse{
  117. HeaderCode: respWriter.HeaderCode,
  118. Body: buffer.Bytes(),
  119. }
  120. }