code.gitea.io/gitea@v1.21.7/tests/test_utils.go (about)

     1  // Copyright 2017 The Gitea Authors. All rights reserved.
     2  // SPDX-License-Identifier: MIT
     3  
     4  //nolint:forbidigo
     5  package tests
     6  
     7  import (
     8  	"context"
     9  	"database/sql"
    10  	"fmt"
    11  	"os"
    12  	"path"
    13  	"path/filepath"
    14  	"testing"
    15  
    16  	"code.gitea.io/gitea/models/db"
    17  	packages_model "code.gitea.io/gitea/models/packages"
    18  	"code.gitea.io/gitea/models/unittest"
    19  	"code.gitea.io/gitea/modules/base"
    20  	"code.gitea.io/gitea/modules/git"
    21  	"code.gitea.io/gitea/modules/graceful"
    22  	"code.gitea.io/gitea/modules/log"
    23  	repo_module "code.gitea.io/gitea/modules/repository"
    24  	"code.gitea.io/gitea/modules/setting"
    25  	"code.gitea.io/gitea/modules/storage"
    26  	"code.gitea.io/gitea/modules/testlogger"
    27  	"code.gitea.io/gitea/modules/util"
    28  	"code.gitea.io/gitea/routers"
    29  
    30  	"github.com/stretchr/testify/assert"
    31  )
    32  
    33  func exitf(format string, args ...any) {
    34  	fmt.Printf(format+"\n", args...)
    35  	os.Exit(1)
    36  }
    37  
    38  func InitTest(requireGitea bool) {
    39  	log.RegisterEventWriter("test", testlogger.NewTestLoggerWriter)
    40  
    41  	giteaRoot := base.SetupGiteaRoot()
    42  	if giteaRoot == "" {
    43  		exitf("Environment variable $GITEA_ROOT not set")
    44  	}
    45  
    46  	// TODO: Speedup tests that rely on the event source ticker, confirm whether there is any bug or failure.
    47  	// setting.UI.Notification.EventSourceUpdateTime = time.Second
    48  
    49  	setting.IsInTesting = true
    50  	setting.AppWorkPath = giteaRoot
    51  	setting.CustomPath = filepath.Join(setting.AppWorkPath, "custom")
    52  	if requireGitea {
    53  		giteaBinary := "gitea"
    54  		if setting.IsWindows {
    55  			giteaBinary += ".exe"
    56  		}
    57  		setting.AppPath = path.Join(giteaRoot, giteaBinary)
    58  		if _, err := os.Stat(setting.AppPath); err != nil {
    59  			exitf("Could not find gitea binary at %s", setting.AppPath)
    60  		}
    61  	}
    62  	giteaConf := os.Getenv("GITEA_CONF")
    63  	if giteaConf == "" {
    64  		// By default, use sqlite.ini for testing, then IDE like GoLand can start the test process with debugger.
    65  		// It's easier for developers to debug bugs step by step with a debugger.
    66  		// Notice: when doing "ssh push", Gitea executes sub processes, debugger won't work for the sub processes.
    67  		giteaConf = "tests/sqlite.ini"
    68  		_ = os.Setenv("GITEA_CONF", giteaConf)
    69  		fmt.Printf("Environment variable $GITEA_CONF not set, use default: %s\n", giteaConf)
    70  		if !setting.EnableSQLite3 {
    71  			exitf(`sqlite3 requires: import _ "github.com/mattn/go-sqlite3" or -tags sqlite,sqlite_unlock_notify`)
    72  		}
    73  	}
    74  	if !path.IsAbs(giteaConf) {
    75  		setting.CustomConf = filepath.Join(giteaRoot, giteaConf)
    76  	} else {
    77  		setting.CustomConf = giteaConf
    78  	}
    79  
    80  	unittest.InitSettings()
    81  	setting.Repository.DefaultBranch = "master" // many test code still assume that default branch is called "master"
    82  	_ = util.RemoveAll(repo_module.LocalCopyPath())
    83  
    84  	if err := git.InitFull(context.Background()); err != nil {
    85  		log.Fatal("git.InitOnceWithSync: %v", err)
    86  	}
    87  
    88  	setting.LoadDBSetting()
    89  	if err := storage.Init(); err != nil {
    90  		exitf("Init storage failed: %v", err)
    91  	}
    92  
    93  	switch {
    94  	case setting.Database.Type.IsMySQL():
    95  		connType := "tcp"
    96  		if len(setting.Database.Host) > 0 && setting.Database.Host[0] == '/' { // looks like a unix socket
    97  			connType = "unix"
    98  		}
    99  
   100  		db, err := sql.Open("mysql", fmt.Sprintf("%s:%s@%s(%s)/",
   101  			setting.Database.User, setting.Database.Passwd, connType, setting.Database.Host))
   102  		defer db.Close()
   103  		if err != nil {
   104  			log.Fatal("sql.Open: %v", err)
   105  		}
   106  		if _, err = db.Exec(fmt.Sprintf("CREATE DATABASE IF NOT EXISTS %s", setting.Database.Name)); err != nil {
   107  			log.Fatal("db.Exec: %v", err)
   108  		}
   109  	case setting.Database.Type.IsPostgreSQL():
   110  		var db *sql.DB
   111  		var err error
   112  		if setting.Database.Host[0] == '/' {
   113  			db, err = sql.Open("postgres", fmt.Sprintf("postgres://%s:%s@/%s?sslmode=%s&host=%s",
   114  				setting.Database.User, setting.Database.Passwd, setting.Database.Name, setting.Database.SSLMode, setting.Database.Host))
   115  		} else {
   116  			db, err = sql.Open("postgres", fmt.Sprintf("postgres://%s:%s@%s/%s?sslmode=%s",
   117  				setting.Database.User, setting.Database.Passwd, setting.Database.Host, setting.Database.Name, setting.Database.SSLMode))
   118  		}
   119  
   120  		defer db.Close()
   121  		if err != nil {
   122  			log.Fatal("sql.Open: %v", err)
   123  		}
   124  		dbrows, err := db.Query(fmt.Sprintf("SELECT 1 FROM pg_database WHERE datname = '%s'", setting.Database.Name))
   125  		if err != nil {
   126  			log.Fatal("db.Query: %v", err)
   127  		}
   128  		defer dbrows.Close()
   129  
   130  		if !dbrows.Next() {
   131  			if _, err = db.Exec(fmt.Sprintf("CREATE DATABASE %s", setting.Database.Name)); err != nil {
   132  				log.Fatal("db.Exec: CREATE DATABASE: %v", err)
   133  			}
   134  		}
   135  		// Check if we need to setup a specific schema
   136  		if len(setting.Database.Schema) == 0 {
   137  			break
   138  		}
   139  		db.Close()
   140  
   141  		if setting.Database.Host[0] == '/' {
   142  			db, err = sql.Open("postgres", fmt.Sprintf("postgres://%s:%s@/%s?sslmode=%s&host=%s",
   143  				setting.Database.User, setting.Database.Passwd, setting.Database.Name, setting.Database.SSLMode, setting.Database.Host))
   144  		} else {
   145  			db, err = sql.Open("postgres", fmt.Sprintf("postgres://%s:%s@%s/%s?sslmode=%s",
   146  				setting.Database.User, setting.Database.Passwd, setting.Database.Host, setting.Database.Name, setting.Database.SSLMode))
   147  		}
   148  		// This is a different db object; requires a different Close()
   149  		defer db.Close()
   150  		if err != nil {
   151  			log.Fatal("sql.Open: %v", err)
   152  		}
   153  		schrows, err := db.Query(fmt.Sprintf("SELECT 1 FROM information_schema.schemata WHERE schema_name = '%s'", setting.Database.Schema))
   154  		if err != nil {
   155  			log.Fatal("db.Query: %v", err)
   156  		}
   157  		defer schrows.Close()
   158  
   159  		if !schrows.Next() {
   160  			// Create and setup a DB schema
   161  			if _, err = db.Exec(fmt.Sprintf("CREATE SCHEMA %s", setting.Database.Schema)); err != nil {
   162  				log.Fatal("db.Exec: CREATE SCHEMA: %v", err)
   163  			}
   164  		}
   165  
   166  	case setting.Database.Type.IsMSSQL():
   167  		host, port := setting.ParseMSSQLHostPort(setting.Database.Host)
   168  		db, err := sql.Open("mssql", fmt.Sprintf("server=%s; port=%s; database=%s; user id=%s; password=%s;",
   169  			host, port, "master", setting.Database.User, setting.Database.Passwd))
   170  		if err != nil {
   171  			log.Fatal("sql.Open: %v", err)
   172  		}
   173  		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 {
   174  			log.Fatal("db.Exec: %v", err)
   175  		}
   176  		defer db.Close()
   177  	}
   178  
   179  	routers.InitWebInstalled(graceful.GetManager().HammerContext())
   180  }
   181  
   182  func PrepareAttachmentsStorage(t testing.TB) {
   183  	// prepare attachments directory and files
   184  	assert.NoError(t, storage.Clean(storage.Attachments))
   185  
   186  	s, err := storage.NewStorage(setting.LocalStorageType, &setting.Storage{
   187  		Path: filepath.Join(filepath.Dir(setting.AppPath), "tests", "testdata", "data", "attachments"),
   188  	})
   189  	assert.NoError(t, err)
   190  	assert.NoError(t, s.IterateObjects("", func(p string, obj storage.Object) error {
   191  		_, err = storage.Copy(storage.Attachments, p, s, p)
   192  		return err
   193  	}))
   194  }
   195  
   196  func PrepareTestEnv(t testing.TB, skip ...int) func() {
   197  	t.Helper()
   198  	ourSkip := 1
   199  	if len(skip) > 0 {
   200  		ourSkip += skip[0]
   201  	}
   202  	deferFn := PrintCurrentTest(t, ourSkip)
   203  
   204  	// load database fixtures
   205  	assert.NoError(t, unittest.LoadFixtures())
   206  
   207  	// load git repo fixtures
   208  	assert.NoError(t, util.RemoveAll(setting.RepoRootPath))
   209  	assert.NoError(t, unittest.CopyDir(path.Join(filepath.Dir(setting.AppPath), "tests/gitea-repositories-meta"), setting.RepoRootPath))
   210  	ownerDirs, err := os.ReadDir(setting.RepoRootPath)
   211  	if err != nil {
   212  		assert.NoError(t, err, "unable to read the new repo root: %v\n", err)
   213  	}
   214  	for _, ownerDir := range ownerDirs {
   215  		if !ownerDir.Type().IsDir() {
   216  			continue
   217  		}
   218  		repoDirs, err := os.ReadDir(filepath.Join(setting.RepoRootPath, ownerDir.Name()))
   219  		if err != nil {
   220  			assert.NoError(t, err, "unable to read the new repo root: %v\n", err)
   221  		}
   222  		for _, repoDir := range repoDirs {
   223  			_ = os.MkdirAll(filepath.Join(setting.RepoRootPath, ownerDir.Name(), repoDir.Name(), "objects", "pack"), 0o755)
   224  			_ = os.MkdirAll(filepath.Join(setting.RepoRootPath, ownerDir.Name(), repoDir.Name(), "objects", "info"), 0o755)
   225  			_ = os.MkdirAll(filepath.Join(setting.RepoRootPath, ownerDir.Name(), repoDir.Name(), "refs", "heads"), 0o755)
   226  			_ = os.MkdirAll(filepath.Join(setting.RepoRootPath, ownerDir.Name(), repoDir.Name(), "refs", "tag"), 0o755)
   227  		}
   228  	}
   229  
   230  	// load LFS object fixtures
   231  	// (LFS storage can be on any of several backends, including remote servers, so we init it with the storage API)
   232  	lfsFixtures, err := storage.NewStorage(setting.LocalStorageType, &setting.Storage{
   233  		Path: filepath.Join(filepath.Dir(setting.AppPath), "tests/gitea-lfs-meta"),
   234  	})
   235  	assert.NoError(t, err)
   236  	assert.NoError(t, storage.Clean(storage.LFS))
   237  	assert.NoError(t, lfsFixtures.IterateObjects("", func(path string, _ storage.Object) error {
   238  		_, err := storage.Copy(storage.LFS, path, lfsFixtures, path)
   239  		return err
   240  	}))
   241  
   242  	// clear all package data
   243  	assert.NoError(t, db.TruncateBeans(db.DefaultContext,
   244  		&packages_model.Package{},
   245  		&packages_model.PackageVersion{},
   246  		&packages_model.PackageFile{},
   247  		&packages_model.PackageBlob{},
   248  		&packages_model.PackageProperty{},
   249  		&packages_model.PackageBlobUpload{},
   250  		&packages_model.PackageCleanupRule{},
   251  	))
   252  	assert.NoError(t, storage.Clean(storage.Packages))
   253  
   254  	return deferFn
   255  }
   256  
   257  func PrintCurrentTest(t testing.TB, skip ...int) func() {
   258  	t.Helper()
   259  	actualSkip := 1
   260  	if len(skip) > 0 {
   261  		actualSkip = skip[0] + 1
   262  	}
   263  	return testlogger.PrintCurrentTest(t, actualSkip)
   264  }
   265  
   266  // Printf takes a format and args and prints the string to os.Stdout
   267  func Printf(format string, args ...any) {
   268  	testlogger.Printf(format, args...)
   269  }