tests: make a consistent test env for both local and CI runs

- Only have a single database for all tests, like CI.
- Calling `PrepareDBConnectionString` drops all tables before returning
  the string.
- Tests must be run with no concurrency else they will step on each other
  due to the previous point.

This should prevent cases where local tests pass but CI fails.
This commit is contained in:
Kegan Dougal 2021-11-09 10:15:48 +00:00
parent c91e2c47b5
commit 6e55f7f608
10 changed files with 24 additions and 21 deletions

View File

@ -43,7 +43,7 @@ jobs:
run: go build ./cmd/syncv3
- name: Test
run: go test ./...
run: go test -p 1 ./...
env:
POSTGRES_HOST: localhost
POSTGRES_USER: postgres

View File

@ -15,7 +15,7 @@ import (
func TestFilters(t *testing.T) {
boolTrue := true
boolFalse := false
pqString := testutils.PrepareDBConnectionString(postgresTestDatabaseName)
pqString := testutils.PrepareDBConnectionString()
// setup code
v2 := runTestV2Server(t)
v3 := runTestServer(t, v2, pqString)

View File

@ -13,7 +13,7 @@ import (
// Test that sort operations that favour notif counts always appear at the start of the list.
func TestNotificationsOnTop(t *testing.T) {
pqString := testutils.PrepareDBConnectionString(postgresTestDatabaseName)
pqString := testutils.PrepareDBConnectionString()
// setup code
v2 := runTestV2Server(t)
v3 := runTestServer(t, v2, pqString)

View File

@ -13,7 +13,7 @@ import (
// Test that room names come through sanely. Additional testing to ensure we copy hero slices correctly.
func TestRoomNames(t *testing.T) {
pqString := testutils.PrepareDBConnectionString(postgresTestDatabaseName)
pqString := testutils.PrepareDBConnectionString()
// setup code
v2 := runTestV2Server(t)
v3 := runTestServer(t, v2, pqString)

View File

@ -10,7 +10,7 @@ import (
var postgresConnectionString = "user=xxxxx dbname=syncv3_test sslmode=disable"
func TestMain(m *testing.M) {
postgresConnectionString = testutils.PrepareDBConnectionString("syncv3_test_state")
postgresConnectionString = testutils.PrepareDBConnectionString()
exitCode := m.Run()
os.Exit(exitCode)
}

View File

@ -10,7 +10,7 @@ import (
var postgresConnectionString = "user=xxxxx dbname=syncv3_test sslmode=disable"
func TestMain(m *testing.M) {
postgresConnectionString = testutils.PrepareDBConnectionString("syncv3_test_sync2")
postgresConnectionString = testutils.PrepareDBConnectionString()
exitCode := m.Run()
os.Exit(exitCode)
}

View File

@ -16,7 +16,7 @@ func TestMain(m *testing.M) {
TimeFormat: "15:04:05",
NoColor: true,
})
postgresConnectionString = testutils.PrepareDBConnectionString("syncv3_test_sync3")
postgresConnectionString = testutils.PrepareDBConnectionString()
exitCode := m.Run()
os.Exit(exitCode)
}

View File

@ -1,6 +1,7 @@
package testutils
import (
"database/sql"
"fmt"
"os"
"os/exec"
@ -9,17 +10,10 @@ import (
func createLocalDB(dbName string) string {
fmt.Println("Note: tests require a postgres install accessible to the current user")
dropDB := exec.Command("dropdb", "-f", dbName)
dropDB.Stdout = os.Stdout
dropDB.Stderr = os.Stderr
dropDB.Run()
createDB := exec.Command("createdb", dbName)
createDB.Stdout = os.Stdout
createDB.Stderr = os.Stderr
if err := createDB.Run(); err != nil {
fmt.Println("createdb failed: ", err)
os.Exit(2)
}
createDB.Run()
return dbName
}
@ -32,7 +26,7 @@ func currentUser() string {
return user.Username
}
func PrepareDBConnectionString(wantDBName string) (connStr string) {
func PrepareDBConnectionString() (connStr string) {
// Required vars: user and db
// We'll try to infer from the local env if they are missing
user := os.Getenv("POSTGRES_USER")
@ -41,7 +35,7 @@ func PrepareDBConnectionString(wantDBName string) (connStr string) {
}
dbName := os.Getenv("POSTGRES_DB")
if dbName == "" {
dbName = createLocalDB(wantDBName)
dbName = createLocalDB("syncv3_test")
}
connStr = fmt.Sprintf(
"user=%s dbname=%s sslmode=disable",
@ -56,5 +50,16 @@ func PrepareDBConnectionString(wantDBName string) (connStr string) {
if host != "" {
connStr += fmt.Sprintf(" host=%s", host)
}
// Drop all tables on the database to get a fresh instance
db, err := sql.Open("postgres", connStr)
if err != nil {
panic(err)
}
_, err = db.Exec(`DROP SCHEMA public CASCADE;
CREATE SCHEMA public;`)
if err != nil {
panic(err)
}
return
}

View File

@ -17,7 +17,7 @@ import (
// and make sure the initial scrollback includes these new live events.
func TestTimelines(t *testing.T) {
// setup code
pqString := testutils.PrepareDBConnectionString(postgresTestDatabaseName)
pqString := testutils.PrepareDBConnectionString()
v2 := runTestV2Server(t)
v3 := runTestServer(t, v2, pqString)
defer v2.close()

View File

@ -21,8 +21,6 @@ import (
"github.com/matrix-org/sync-v3/testutils"
)
const postgresTestDatabaseName = "syncv3_test_sync3_integration"
// Integration tests for the sync-v3 server
type testV2Server struct {
@ -225,7 +223,7 @@ func (s *testV3Server) doV3Request(t *testing.T, token string, pos int64, reqBod
func runTestServer(t *testing.T, v2Server *testV2Server, postgresConnectionString string) *testV3Server {
t.Helper()
if postgresConnectionString == "" {
postgresConnectionString = testutils.PrepareDBConnectionString(postgresTestDatabaseName)
postgresConnectionString = testutils.PrepareDBConnectionString()
}
h, err := handler.NewSync3Handler(&sync2.HTTPClient{
Client: &http.Client{