Make tests work on PostgreSQL
This commit is contained in:
parent
860ad168a6
commit
edda8f21cf
21
README.md
21
README.md
@ -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 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
|
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
|
||||||
To create a migration called `create_users_table`, run:
|
echo "alter schema public owner to flamenco;" | sudo -u postgres psql flamenco-test
|
||||||
|
|
||||||
```
|
|
||||||
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
|
|
||||||
```
|
```
|
||||||
|
@ -43,14 +43,23 @@ type DB struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func OpenDB(ctx context.Context) (*DB, error) {
|
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) {
|
func openDB(ctx context.Context, uri string) (*DB, error) {
|
||||||
// TODO: don't log the password.
|
// 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 {
|
if err != nil {
|
||||||
log.Panic().Err(err).Msg("failed to connect database")
|
log.Panic().Err(err).Msg("failed to connect database")
|
||||||
}
|
}
|
||||||
@ -58,11 +67,7 @@ func openDB(ctx context.Context, uri string) (*DB, error) {
|
|||||||
db := DB{
|
db := DB{
|
||||||
gormDB: gormDB,
|
gormDB: gormDB,
|
||||||
}
|
}
|
||||||
if err := db.migrate(); err != nil {
|
return &db, nil
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
return &db, err
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (db *DB) StoreJob(ctx context.Context, authoredJob job_compilers.AuthoredJob) error {
|
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,
|
JobType: authoredJob.JobType,
|
||||||
Priority: int8(authoredJob.Priority),
|
Priority: int8(authoredJob.Priority),
|
||||||
Settings: JobSettings(authoredJob.Settings),
|
Settings: JobSettings(authoredJob.Settings),
|
||||||
Metadata: JobMetadata(authoredJob.Metadata),
|
Metadata: StringStringMap(authoredJob.Metadata),
|
||||||
}
|
}
|
||||||
|
|
||||||
tx := db.gormDB.Create(&dbJob)
|
tx := db.gormDB.Create(&dbJob)
|
||||||
|
@ -22,34 +22,47 @@ package persistence
|
|||||||
* ***** END GPL LICENSE BLOCK ***** */
|
* ***** END GPL LICENSE BLOCK ***** */
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"os"
|
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
"gitlab.com/blender/flamenco-ng-poc/internal/manager/job_compilers"
|
"gitlab.com/blender/flamenco-ng-poc/internal/manager/job_compilers"
|
||||||
"golang.org/x/net/context"
|
"golang.org/x/net/context"
|
||||||
|
"gorm.io/gorm"
|
||||||
_ "modernc.org/sqlite"
|
_ "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.
|
// Creating a new database should be fast.
|
||||||
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second)
|
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second)
|
||||||
defer cancel()
|
defer cancel()
|
||||||
|
|
||||||
db, err := openDB(ctx, testURI)
|
db, err := openDB(ctx, testURI)
|
||||||
assert.Nil(t, err)
|
assert.NoError(t, err)
|
||||||
|
|
||||||
return db, func() {
|
// Erase everything in the database.
|
||||||
os.Remove(testURI)
|
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) {
|
func TestStoreAuthoredJob(t *testing.T) {
|
||||||
db, cleanup := createTestDB(t)
|
db := createTestDB(t)
|
||||||
defer cleanup()
|
|
||||||
|
|
||||||
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second)
|
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second)
|
||||||
defer cancel()
|
defer cancel()
|
||||||
|
Loading…
x
Reference in New Issue
Block a user