1
0
mirror of https://github.com/woodpecker-ci/woodpecker.git synced 2024-12-24 10:07:21 +02:00

Add tests framework for storage migration (#630)

* add test sqlite db

* migration tests for sqlite

* fix pipeline config

* fix tests to other dbms

* fix postgres & mysql
This commit is contained in:
6543 2021-12-20 16:15:21 +01:00 committed by GitHub
parent b2ce1f5da5
commit 0aefdc9978
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 98 additions and 6 deletions

View File

@ -39,7 +39,7 @@ pipeline:
image: golang:1.16
group: test
environment:
- DATABASE_DRIVER=sqlite3
- WOODPECKER_DATABASE_DRIVER=sqlite3
commands:
- make test-server-datastore
@ -47,8 +47,8 @@ pipeline:
image: golang:1.16
group: test
environment:
- DATABASE_DRIVER=postgres
- DATABASE_CONFIG=host=postgres user=postgres dbname=postgres sslmode=disable
- WOODPECKER_DATABASE_DRIVER=postgres
- WOODPECKER_DATABASE_DATASOURCE=host=service-postgres user=postgres dbname=postgres sslmode=disable
commands:
- make test-server-datastore
@ -56,8 +56,8 @@ pipeline:
image: golang:1.16
group: test
environment:
- DATABASE_DRIVER=mysql
- DATABASE_CONFIG=root@tcp(mysql:3306)/test?parseTime=true
- WOODPECKER_DATABASE_DRIVER=mysql
- WOODPECKER_DATABASE_DATASOURCE=root@tcp(service-mysql:3306)/test?parseTime=true
commands:
- make test-server-datastore

View File

@ -29,7 +29,7 @@ func testDriverConfig() (driver, config string) {
if os.Getenv("WOODPECKER_DATABASE_DRIVER") != "" {
driver = os.Getenv("WOODPECKER_DATABASE_DRIVER")
config = os.Getenv("WOODPECKER_DATABASE_CONFIG")
config = os.Getenv("WOODPECKER_DATABASE_DATASOURCE")
}
return
}

View File

@ -0,0 +1,91 @@
package migration
import (
"io/ioutil"
"os"
"testing"
"github.com/stretchr/testify/assert"
"xorm.io/xorm"
// blank imports to register the sql drivers
_ "github.com/go-sql-driver/mysql"
_ "github.com/lib/pq"
_ "github.com/mattn/go-sqlite3"
)
const (
sqliteDB = "./testfiles/sqlite.db"
)
func testDriver() string {
driver := os.Getenv("WOODPECKER_DATABASE_DRIVER")
if len(driver) == 0 {
return "sqlite3"
}
return driver
}
func createSQLiteDB(t *testing.T) string {
tmpF, err := ioutil.TempFile("./testfiles", "tmp_")
if !assert.NoError(t, err) {
t.FailNow()
}
dbF, err := ioutil.ReadFile(sqliteDB)
if !assert.NoError(t, err) {
t.FailNow()
}
if !assert.NoError(t, ioutil.WriteFile(tmpF.Name(), dbF, 0644)) {
t.FailNow()
}
return tmpF.Name()
}
func testDB(t *testing.T, new bool) (engine *xorm.Engine, close func()) {
driver := testDriver()
var err error
close = func() {}
switch driver {
case "sqlite3":
config := ":memory:"
if !new {
config = createSQLiteDB(t)
close = func() {
_ = os.Remove(config)
}
}
engine, err = xorm.NewEngine(driver, config)
if !assert.NoError(t, err) {
t.FailNow()
}
return
case "mysql", "postgres":
config := os.Getenv("WOODPECKER_DATABASE_DATASOURCE")
if !new {
t.Logf("do not have dump to test against")
t.SkipNow()
}
engine, err = xorm.NewEngine(driver, config)
if !assert.NoError(t, err) {
t.FailNow()
}
return
default:
t.Errorf("unsupported driver: %s", driver)
t.FailNow()
}
return
}
func TestMigrate(t *testing.T) {
// init new db
engine, close := testDB(t, true)
assert.NoError(t, Migrate(engine))
close()
// migrate old db
engine, close = testDB(t, false)
assert.NoError(t, Migrate(engine))
close()
}

View File

@ -0,0 +1 @@
tmp_*

Binary file not shown.