Make tests work on PostgreSQL

This commit is contained in:
Sybren A. Stüvel 2022-01-25 17:28:12 +01:00
parent 860ad168a6
commit edda8f21cf
3 changed files with 42 additions and 33 deletions

View File

@ -15,22 +15,13 @@ You should now have two executables: `flamenco-manager-poc` and `flamenco-worker
Flamenco Manager has a SwaggerUI interface at http://localhost:8080/api/swagger-ui/
## Flamenco Manager DB migrations
## Flamenco Manager DB development machine setup.
First install the `migrate` tool:
Install PostgreSQL, then run:
```
go install -tags sqlite github.com/golang-migrate/migrate/v4/cmd/migrate
```
To create a migration called `create_users_table`, run:
```
migrate create -dir internal/manager/persistence/migrations -ext sql -seq create_users_table
```
Migrations are **automatically run when Flamenco Manager starts**. To run them manually, use:
```
migrate -database sqlite://flamenco-manager.sqlite -path internal/manager/persistence/migrations up
sudo -u postgres createuser -D -P flamenco # give it the password 'flamenco'
sudo -u postgres createdb flamenco -O flamenco -E utf8
sudo -u postgres createdb flamenco-test -O flamenco -E utf8
echo "alter schema public owner to flamenco;" | sudo -u postgres psql flamenco-test
```

View File

@ -43,14 +43,23 @@ type DB struct {
}
func OpenDB(ctx context.Context) (*DB, error) {
return openDB(ctx, dbDSN)
db, err := openDB(ctx, dbDSN)
if err != nil {
return nil, err
}
if err := db.migrate(); err != nil {
return nil, err
}
return db, nil
}
func openDB(ctx context.Context, uri string) (*DB, error) {
// TODO: don't log the password.
log.Info().Str("dsn", dbDSN).Msg("opening database")
log.Info().Str("dsn", uri).Msg("opening database")
gormDB, err := gorm.Open(postgres.Open(dbDSN), &gorm.Config{})
gormDB, err := gorm.Open(postgres.Open(uri), &gorm.Config{})
if err != nil {
log.Panic().Err(err).Msg("failed to connect database")
}
@ -58,11 +67,7 @@ func openDB(ctx context.Context, uri string) (*DB, error) {
db := DB{
gormDB: gormDB,
}
if err := db.migrate(); err != nil {
return nil, err
}
return &db, err
return &db, nil
}
func (db *DB) StoreJob(ctx context.Context, authoredJob job_compilers.AuthoredJob) error {
@ -73,7 +78,7 @@ func (db *DB) StoreJob(ctx context.Context, authoredJob job_compilers.AuthoredJo
JobType: authoredJob.JobType,
Priority: int8(authoredJob.Priority),
Settings: JobSettings(authoredJob.Settings),
Metadata: JobMetadata(authoredJob.Metadata),
Metadata: StringStringMap(authoredJob.Metadata),
}
tx := db.gormDB.Create(&dbJob)

View File

@ -22,34 +22,47 @@ package persistence
* ***** END GPL LICENSE BLOCK ***** */
import (
"os"
"testing"
"time"
"github.com/stretchr/testify/assert"
"gitlab.com/blender/flamenco-ng-poc/internal/manager/job_compilers"
"golang.org/x/net/context"
"gorm.io/gorm"
_ "modernc.org/sqlite"
)
const testURI = "testing.sqlite"
const testURI = "host=localhost user=flamenco password=flamenco dbname=flamenco-test TimeZone=Europe/Amsterdam"
func createTestDB(t *testing.T) (*DB, func()) {
func createTestDB(t *testing.T) *DB {
// Creating a new database should be fast.
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second)
defer cancel()
db, err := openDB(ctx, testURI)
assert.Nil(t, err)
assert.NoError(t, err)
return db, func() {
os.Remove(testURI)
}
// Erase everything in the database.
var tx *gorm.DB
tx = db.gormDB.Exec("DROP SCHEMA public CASCADE")
assert.NoError(t, tx.Error)
tx = db.gormDB.Exec("CREATE SCHEMA public")
assert.NoError(t, tx.Error)
// Restore default grants (source: https://stackoverflow.com/questions/3327312/how-can-i-drop-all-the-tables-in-a-postgresql-database)
tx = db.gormDB.Exec("GRANT ALL ON SCHEMA public TO postgres")
assert.NoError(t, tx.Error)
tx = db.gormDB.Exec("GRANT ALL ON SCHEMA public TO public")
assert.NoError(t, tx.Error)
err = db.migrate()
assert.NoError(t, err)
return db
}
func TestStoreAuthoredJob(t *testing.T) {
db, cleanup := createTestDB(t)
defer cleanup()
db := createTestDB(t)
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second)
defer cancel()