mirror of
https://github.com/go-gitea/gitea.git
synced 2026-10-19 16:18:11 +00:00
Integration test processes now get their own work directory, config file and random ports, so several runs can execute in parallel on one machine. The slowest tests also lose fixed sleeps and needlessly expensive setup, with their assertions kept. - Batch PostgreSQL sequence resets, the main per-test fixture cost there - Cheap hashing params in hash and OAuth2 fixture tests, full-cost vectors stay - Single fetch for negative Actions runner assertions instead of 1s polls - Queue flushes and backdated timestamps instead of sleeps - In-memory SQLite test journal, tmpfs for PostgreSQL in CI - E2E instance writes SSH keys into its work directory instead of `~/.ssh/authorized_keys`
130 lines
3.4 KiB
Go
130 lines
3.4 KiB
Go
// Copyright 2022 The Gitea Authors. All rights reserved.
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
package migrationtest
|
|
|
|
import (
|
|
"os"
|
|
"path/filepath"
|
|
"testing"
|
|
|
|
"gitea.dev/modelmigration/base"
|
|
"gitea.dev/models/db" //nolint:depguard // allow to access db in migration tests
|
|
"gitea.dev/models/unittest" //nolint:depguard // allow to access db in migration tests
|
|
"gitea.dev/modules/git"
|
|
"gitea.dev/modules/setting"
|
|
"gitea.dev/modules/testlogger"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
"xorm.io/xorm/schemas"
|
|
)
|
|
|
|
// PrepareTestEnv prepares the test environment and reset the database. The skip parameter should usually be 0.
|
|
// Provide models to be sync'd with the database - in particular any models you expect fixtures to be loaded from.
|
|
//
|
|
// fixtures in `modelmigration/fixtures/<TestName>` will be loaded automatically
|
|
func PrepareTestEnv(t *testing.T, skip int, syncModels ...any) (base.EngineMigration, func()) {
|
|
t.Helper()
|
|
ourSkip := 2
|
|
ourSkip += skip
|
|
deferFn := testlogger.PrintCurrentTest(t, ourSkip)
|
|
giteaRoot := setting.GetGiteaTestSourceRoot()
|
|
require.NoError(t, unittest.SyncDirs(filepath.Join(giteaRoot, "tests/gitea-repositories-meta"), setting.RepoRootPath))
|
|
|
|
cleanup, err := unittest.ResetTestDatabase()
|
|
if err != nil {
|
|
t.Fatalf("unable to reset database: %v", err)
|
|
return nil, deferFn
|
|
}
|
|
{
|
|
oldDefer := deferFn
|
|
deferFn = func() {
|
|
cleanup()
|
|
oldDefer()
|
|
}
|
|
}
|
|
|
|
err = db.InitEngine(t.Context())
|
|
if !assert.NoError(t, err) {
|
|
return nil, deferFn
|
|
}
|
|
x := unittest.GetXORMEngine()
|
|
{
|
|
oldDefer := deferFn
|
|
deferFn = func() {
|
|
_ = x.Close()
|
|
oldDefer()
|
|
}
|
|
}
|
|
|
|
db.ResetModels()
|
|
if len(syncModels) > 0 {
|
|
for _, syncModel := range syncModels {
|
|
db.RegisterModel(syncModel)
|
|
}
|
|
if err := x.Sync(syncModels...); err != nil {
|
|
t.Errorf("error during sync: %v", err)
|
|
return x, deferFn
|
|
}
|
|
}
|
|
|
|
fixturesDir := filepath.Join(giteaRoot, "modelmigration/fixtures", t.Name())
|
|
|
|
if _, err := os.Stat(fixturesDir); err == nil {
|
|
t.Logf("initializing fixtures from: %s", fixturesDir)
|
|
if err := unittest.InitFixtures(
|
|
unittest.FixturesOptions{
|
|
Dir: fixturesDir,
|
|
}); err != nil {
|
|
t.Errorf("error whilst initializing fixtures from %s: %v", fixturesDir, err)
|
|
return x, deferFn
|
|
}
|
|
if err := unittest.LoadFixtures(); err != nil {
|
|
t.Errorf("error whilst loading fixtures from %s: %v", fixturesDir, err)
|
|
return x, deferFn
|
|
}
|
|
} else if !os.IsNotExist(err) {
|
|
t.Errorf("unexpected error whilst checking for existence of fixtures: %v", err)
|
|
} else {
|
|
t.Logf("no fixtures found in: %s", fixturesDir)
|
|
}
|
|
|
|
return x, deferFn
|
|
}
|
|
|
|
func LoadTableSchemasMap(t *testing.T, x base.EngineMigration) map[string]*schemas.Table {
|
|
tables, err := x.DBMetas()
|
|
require.NoError(t, err)
|
|
tableMap := make(map[string]*schemas.Table)
|
|
for _, table := range tables {
|
|
tableMap[table.Name] = table
|
|
}
|
|
return tableMap
|
|
}
|
|
|
|
func mainTest(m *testing.M) int {
|
|
testlogger.Init()
|
|
workPath, err := setting.PrepareIntegrationTestConfig()
|
|
if err != nil {
|
|
return testlogger.MainErrorf("Unable to prepare integration test config: %v", err)
|
|
}
|
|
setting.SetupGiteaTestEnv()
|
|
|
|
if err = git.InitFull(); err != nil {
|
|
return testlogger.MainErrorf("Unable to InitFull: %v", err)
|
|
}
|
|
setting.Database.SlowQueryThreshold = 0
|
|
setting.LoadDBSetting()
|
|
setting.InitLoggersForTest()
|
|
exitCode := m.Run()
|
|
if exitCode == 0 {
|
|
_ = os.RemoveAll(workPath)
|
|
}
|
|
return exitCode
|
|
}
|
|
|
|
func MainTest(m *testing.M) {
|
|
os.Exit(mainTest(m))
|
|
}
|