CI: Improve stability when running unit tests in parallel

Adds SQLite memory namespaces to avoid potential conflicts.
This commit is contained in:
Michael Mayer 2022-03-31 17:55:40 +02:00
parent d048038c88
commit 3698a860c6
8 changed files with 23 additions and 25 deletions

View file

@ -47,7 +47,7 @@ func AuthenticateUser(app *gin.Engine, router *gin.RouterGroup, username string,
return
}
// Performs API request with empty request body.
// Executes an API request with an empty request body.
// See https://medium.com/@craigchilds94/testing-gin-json-responses-1f258ce3b0b1
func PerformRequest(r http.Handler, method, path string) *httptest.ResponseRecorder {
req, _ := http.NewRequest(method, path, nil)
@ -65,7 +65,7 @@ func AuthenticatedRequest(r http.Handler, method, path, sess string) *httptest.R
return w
}
// Performs API request including request body as string.
// Executes an API request with the request body as a string.
func PerformRequestWithBody(r http.Handler, method, path, body string) *httptest.ResponseRecorder {
reader := strings.NewReader(body)
req, _ := http.NewRequest(method, path, reader)
@ -74,7 +74,7 @@ func PerformRequestWithBody(r http.Handler, method, path, body string) *httptest
return w
}
// Performs authenticated API request including request body as string.
// Performs an authenticated API request containing the request body as a string.
func AuthenticatedRequestWithBody(r http.Handler, method, path, body string, sessionId string) *httptest.ResponseRecorder {
reader := strings.NewReader(body)
req, _ := http.NewRequest(method, path, reader)
@ -88,7 +88,7 @@ func TestMain(m *testing.M) {
log = logrus.StandardLogger()
log.SetLevel(logrus.TraceLevel)
c := config.TestConfig()
c := config.NewTestConfig("api")
service.SetConfig(c)
code := m.Run()

View file

@ -12,7 +12,7 @@ func TestMain(m *testing.M) {
log = logrus.StandardLogger()
log.SetLevel(logrus.TraceLevel)
c := config.TestConfig()
c := config.NewTestConfig("auto")
code := m.Run()

View file

@ -85,7 +85,7 @@ func TestConfig_CreateDirectories(t *testing.T) {
defer testConfigMutex.Unlock()
c := &Config{
options: NewTestOptions(),
options: NewTestOptions("config"),
token: rnd.Token(8),
}

View file

@ -2,8 +2,10 @@ package config
import (
"flag"
"fmt"
"os"
"path/filepath"
"regexp"
"strconv"
"sync"
"testing"
@ -37,12 +39,15 @@ func testDataPath(assetsPath string) string {
return assetsPath + "/testdata"
}
var PkgNameRegexp = regexp.MustCompile("[^a-zA-Z\\-_]+")
// NewTestOptions returns valid config options for tests.
func NewTestOptions() *Options {
func NewTestOptions(pkg string) *Options {
assetsPath := fs.Abs("../../assets")
storagePath := fs.Abs("../../storage")
testDataPath := filepath.Join(storagePath, "testdata")
pkg = PkgNameRegexp.ReplaceAllString(pkg, "")
driver := os.Getenv("PHOTOPRISM_TEST_DRIVER")
dsn := os.Getenv("PHOTOPRISM_TEST_DSN")
@ -57,11 +62,12 @@ func NewTestOptions() *Options {
// Set default database DSN.
if driver == SQLite3 {
if dsn == "" {
if dsn == "" && pkg != "" {
dsn = fmt.Sprintf("file:%s?mode=memory&cache=shared", pkg)
} else if dsn == "" {
dsn = SQLiteMemoryDSN
} else if dsn != SQLiteTestDB {
// Continue.
} else if err := os.Remove(dsn); err == nil {
log.Debugf("sqlite: test file %s removed", sanitize.Log(dsn))
}
@ -119,7 +125,7 @@ func NewTestOptionsError() *Options {
}
func SetNewTestConfig() {
testConfig = NewTestConfig()
testConfig = NewTestConfig("test")
}
// TestConfig returns the existing test config instance or creates a new instance and returns it.
@ -130,14 +136,14 @@ func TestConfig() *Config {
}
// NewTestConfig returns a valid test config.
func NewTestConfig() *Config {
func NewTestConfig(pkg string) *Config {
defer log.Debug(capture.Time(time.Now(), "config: new test config created"))
testConfigMutex.Lock()
defer testConfigMutex.Unlock()
c := &Config{
options: NewTestOptions(),
options: NewTestOptions(pkg),
token: rnd.Token(8),
}
@ -174,7 +180,7 @@ func NewTestErrorConfig() *Config {
// CliTestContext returns a CLI context for testing.
func CliTestContext() *cli.Context {
config := NewTestOptions()
config := NewTestOptions("config-cli")
globalSet := flag.NewFlagSet("test", 0)
globalSet.Bool("debug", false, "doc")

View file

@ -27,7 +27,7 @@ func TestTestConfig(t *testing.T) {
}
func TestNewTestOptions(t *testing.T) {
c := NewTestOptions()
c := NewTestOptions("config")
assert.IsType(t, new(Options), c)

View file

@ -12,11 +12,7 @@ func TestMain(m *testing.M) {
log = logrus.StandardLogger()
log.SetLevel(logrus.TraceLevel)
if err := os.Remove(".test.db"); err == nil {
log.Debugln("removed .test.db")
}
c := config.TestConfig()
c := config.NewTestConfig("photoprism")
SetConfig(c)
code := m.Run()

View file

@ -17,7 +17,7 @@ import (
)
func TestMain(m *testing.M) {
c := config.TestConfig()
c := config.NewTestConfig("service")
SetConfig(c)

View file

@ -12,11 +12,7 @@ func TestMain(m *testing.M) {
log = logrus.StandardLogger()
log.SetLevel(logrus.TraceLevel)
if err := os.Remove(".test.db"); err == nil {
log.Debugln("removed .test.db")
}
c := config.TestConfig()
c := config.NewTestConfig("workers")
code := m.Run()