@ -11,7 +11,6 @@ import (
"encoding/json"
"encoding/json"
"fmt"
"fmt"
"io"
"io"
"log"
"net/http"
"net/http"
"net/http/cookiejar"
"net/http/cookiejar"
"net/http/httptest"
"net/http/httptest"
@ -27,8 +26,10 @@ import (
"code.gitea.io/gitea/models"
"code.gitea.io/gitea/models"
"code.gitea.io/gitea/modules/base"
"code.gitea.io/gitea/modules/base"
"code.gitea.io/gitea/modules/graceful"
"code.gitea.io/gitea/modules/graceful"
"code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/queue"
"code.gitea.io/gitea/modules/queue"
"code.gitea.io/gitea/modules/setting"
"code.gitea.io/gitea/modules/setting"
"code.gitea.io/gitea/modules/storage"
"code.gitea.io/gitea/modules/util"
"code.gitea.io/gitea/modules/util"
"code.gitea.io/gitea/routers"
"code.gitea.io/gitea/routers"
"code.gitea.io/gitea/routers/routes"
"code.gitea.io/gitea/routers/routes"
@ -59,6 +60,8 @@ func NewNilResponseRecorder() *NilResponseRecorder {
}
}
func TestMain ( m * testing . M ) {
func TestMain ( m * testing . M ) {
defer log . Close ( )
managerCtx , cancel := context . WithCancel ( context . Background ( ) )
managerCtx , cancel := context . WithCancel ( context . Background ( ) )
graceful . InitManager ( managerCtx )
graceful . InitManager ( managerCtx )
defer cancel ( )
defer cancel ( )
@ -142,6 +145,10 @@ func initIntegrationTest() {
util . RemoveAll ( models . LocalCopyPath ( ) )
util . RemoveAll ( models . LocalCopyPath ( ) )
setting . CheckLFSVersion ( )
setting . CheckLFSVersion ( )
setting . InitDBConfig ( )
setting . InitDBConfig ( )
if err := storage . Init ( ) ; err != nil {
fmt . Printf ( "Init storage failed: %v" , err )
os . Exit ( 1 )
}
switch {
switch {
case setting . Database . UseMySQL :
case setting . Database . UseMySQL :
@ -149,27 +156,27 @@ func initIntegrationTest() {
setting . Database . User , setting . Database . Passwd , setting . Database . Host ) )
setting . Database . User , setting . Database . Passwd , setting . Database . Host ) )
defer db . Close ( )
defer db . Close ( )
if err != nil {
if err != nil {
log . Fatalf ( "sql.Open: %v" , err )
log . Fatal ( "sql.Open: %v" , err )
}
}
if _ , err = db . Exec ( fmt . Sprintf ( "CREATE DATABASE IF NOT EXISTS %s" , setting . Database . Name ) ) ; err != nil {
if _ , err = db . Exec ( fmt . Sprintf ( "CREATE DATABASE IF NOT EXISTS %s" , setting . Database . Name ) ) ; err != nil {
log . Fatalf ( "db.Exec: %v" , err )
log . Fatal ( "db.Exec: %v" , err )
}
}
case setting . Database . UsePostgreSQL :
case setting . Database . UsePostgreSQL :
db , err := sql . Open ( "postgres" , fmt . Sprintf ( "postgres://%s:%s@%s/?sslmode=%s" ,
db , err := sql . Open ( "postgres" , fmt . Sprintf ( "postgres://%s:%s@%s/?sslmode=%s" ,
setting . Database . User , setting . Database . Passwd , setting . Database . Host , setting . Database . SSLMode ) )
setting . Database . User , setting . Database . Passwd , setting . Database . Host , setting . Database . SSLMode ) )
defer db . Close ( )
defer db . Close ( )
if err != nil {
if err != nil {
log . Fatalf ( "sql.Open: %v" , err )
log . Fatal ( "sql.Open: %v" , err )
}
}
dbrows , err := db . Query ( fmt . Sprintf ( "SELECT 1 FROM pg_database WHERE datname = '%s'" , setting . Database . Name ) )
dbrows , err := db . Query ( fmt . Sprintf ( "SELECT 1 FROM pg_database WHERE datname = '%s'" , setting . Database . Name ) )
if err != nil {
if err != nil {
log . Fatalf ( "db.Query: %v" , err )
log . Fatal ( "db.Query: %v" , err )
}
}
defer dbrows . Close ( )
defer dbrows . Close ( )
if ! dbrows . Next ( ) {
if ! dbrows . Next ( ) {
if _ , err = db . Exec ( fmt . Sprintf ( "CREATE DATABASE %s" , setting . Database . Name ) ) ; err != nil {
if _ , err = db . Exec ( fmt . Sprintf ( "CREATE DATABASE %s" , setting . Database . Name ) ) ; err != nil {
log . Fatalf ( "db.Exec: CREATE DATABASE: %v" , err )
log . Fatal ( "db.Exec: CREATE DATABASE: %v" , err )
}
}
}
}
// Check if we need to setup a specific schema
// Check if we need to setup a specific schema
@ -183,18 +190,18 @@ func initIntegrationTest() {
// This is a different db object; requires a different Close()
// This is a different db object; requires a different Close()
defer db . Close ( )
defer db . Close ( )
if err != nil {
if err != nil {
log . Fatalf ( "sql.Open: %v" , err )
log . Fatal ( "sql.Open: %v" , err )
}
}
schrows , err := db . Query ( fmt . Sprintf ( "SELECT 1 FROM information_schema.schemata WHERE schema_name = '%s'" , setting . Database . Schema ) )
schrows , err := db . Query ( fmt . Sprintf ( "SELECT 1 FROM information_schema.schemata WHERE schema_name = '%s'" , setting . Database . Schema ) )
if err != nil {
if err != nil {
log . Fatalf ( "db.Query: %v" , err )
log . Fatal ( "db.Query: %v" , err )
}
}
defer schrows . Close ( )
defer schrows . Close ( )
if ! schrows . Next ( ) {
if ! schrows . Next ( ) {
// Create and setup a DB schema
// Create and setup a DB schema
if _ , err = db . Exec ( fmt . Sprintf ( "CREATE SCHEMA %s" , setting . Database . Schema ) ) ; err != nil {
if _ , err = db . Exec ( fmt . Sprintf ( "CREATE SCHEMA %s" , setting . Database . Schema ) ) ; err != nil {
log . Fatalf ( "db.Exec: CREATE SCHEMA: %v" , err )
log . Fatal ( "db.Exec: CREATE SCHEMA: %v" , err )
}
}
}
}
@ -203,10 +210,10 @@ func initIntegrationTest() {
db , err := sql . Open ( "mssql" , fmt . Sprintf ( "server=%s; port=%s; database=%s; user id=%s; password=%s;" ,
db , err := sql . Open ( "mssql" , fmt . Sprintf ( "server=%s; port=%s; database=%s; user id=%s; password=%s;" ,
host , port , "master" , setting . Database . User , setting . Database . Passwd ) )
host , port , "master" , setting . Database . User , setting . Database . Passwd ) )
if err != nil {
if err != nil {
log . Fatalf ( "sql.Open: %v" , err )
log . Fatal ( "sql.Open: %v" , err )
}
}
if _ , err := db . Exec ( fmt . Sprintf ( "If(db_id(N'%s') IS NULL) BEGIN CREATE DATABASE %s; END;" , setting . Database . Name , setting . Database . Name ) ) ; err != nil {
if _ , err := db . Exec ( fmt . Sprintf ( "If(db_id(N'%s') IS NULL) BEGIN CREATE DATABASE %s; END;" , setting . Database . Name , setting . Database . Name ) ) ; err != nil {
log . Fatalf ( "db.Exec: %v" , err )
log . Fatal ( "db.Exec: %v" , err )
}
}
defer db . Close ( )
defer db . Close ( )
}
}