mirror of
https://github.com/woodpecker-ci/woodpecker.git
synced 2024-12-23 00:46:30 +00: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:
parent
b2ce1f5da5
commit
0aefdc9978
5 changed files with 98 additions and 6 deletions
|
@ -39,7 +39,7 @@ pipeline:
|
||||||
image: golang:1.16
|
image: golang:1.16
|
||||||
group: test
|
group: test
|
||||||
environment:
|
environment:
|
||||||
- DATABASE_DRIVER=sqlite3
|
- WOODPECKER_DATABASE_DRIVER=sqlite3
|
||||||
commands:
|
commands:
|
||||||
- make test-server-datastore
|
- make test-server-datastore
|
||||||
|
|
||||||
|
@ -47,8 +47,8 @@ pipeline:
|
||||||
image: golang:1.16
|
image: golang:1.16
|
||||||
group: test
|
group: test
|
||||||
environment:
|
environment:
|
||||||
- DATABASE_DRIVER=postgres
|
- WOODPECKER_DATABASE_DRIVER=postgres
|
||||||
- DATABASE_CONFIG=host=postgres user=postgres dbname=postgres sslmode=disable
|
- WOODPECKER_DATABASE_DATASOURCE=host=service-postgres user=postgres dbname=postgres sslmode=disable
|
||||||
commands:
|
commands:
|
||||||
- make test-server-datastore
|
- make test-server-datastore
|
||||||
|
|
||||||
|
@ -56,8 +56,8 @@ pipeline:
|
||||||
image: golang:1.16
|
image: golang:1.16
|
||||||
group: test
|
group: test
|
||||||
environment:
|
environment:
|
||||||
- DATABASE_DRIVER=mysql
|
- WOODPECKER_DATABASE_DRIVER=mysql
|
||||||
- DATABASE_CONFIG=root@tcp(mysql:3306)/test?parseTime=true
|
- WOODPECKER_DATABASE_DATASOURCE=root@tcp(service-mysql:3306)/test?parseTime=true
|
||||||
commands:
|
commands:
|
||||||
- make test-server-datastore
|
- make test-server-datastore
|
||||||
|
|
||||||
|
|
|
@ -29,7 +29,7 @@ func testDriverConfig() (driver, config string) {
|
||||||
|
|
||||||
if os.Getenv("WOODPECKER_DATABASE_DRIVER") != "" {
|
if os.Getenv("WOODPECKER_DATABASE_DRIVER") != "" {
|
||||||
driver = os.Getenv("WOODPECKER_DATABASE_DRIVER")
|
driver = os.Getenv("WOODPECKER_DATABASE_DRIVER")
|
||||||
config = os.Getenv("WOODPECKER_DATABASE_CONFIG")
|
config = os.Getenv("WOODPECKER_DATABASE_DATASOURCE")
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
91
server/store/datastore/migration/migration_test.go
Normal file
91
server/store/datastore/migration/migration_test.go
Normal 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()
|
||||||
|
}
|
1
server/store/datastore/migration/testfiles/.gitignore
vendored
Normal file
1
server/store/datastore/migration/testfiles/.gitignore
vendored
Normal file
|
@ -0,0 +1 @@
|
||||||
|
tmp_*
|
BIN
server/store/datastore/migration/testfiles/sqlite.db
Normal file
BIN
server/store/datastore/migration/testfiles/sqlite.db
Normal file
Binary file not shown.
Loading…
Reference in a new issue