Skip to content

Commit 56a8536

Browse files
author
arpechenin
committed
introduce Testcontainers & Testcontainers/mysql to use MySQL instead of in-memory SQLite.
Signed-off-by: ntny <[email protected]> Signed-off-by: arpechenin <[email protected]>
1 parent 70d2888 commit 56a8536

File tree

8 files changed

+388
-141
lines changed

8 files changed

+388
-141
lines changed

backend/src/apiserver/model/experiment.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@
1414

1515
package model
1616

17+
func (Experiment) TableName() string {
18+
return "experiments"
19+
}
20+
1721
type Experiment struct {
1822
UUID string `gorm:"column:UUID; not null; primary_key;"`
1923
Name string `gorm:"column:Name; not null; unique_index:idx_name_namespace;"`

backend/src/apiserver/model/resource_reference.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,10 @@ type ResourceType string
146146
// The relationship between two resource objects.
147147
type Relationship string
148148

149+
func (ResourceReference) TableName() string {
150+
return "resource_references"
151+
}
152+
149153
// Resource reference table models the relationship between resources in a loosely coupled way.
150154
type ResourceReference struct {
151155
// ID of the resource object

backend/src/apiserver/model/run.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,10 @@ type RunDetails struct {
318318
TaskDetails []*Task
319319
}
320320

321+
func (RunMetric) TableName() string {
322+
return "run_metrics"
323+
}
324+
321325
type RunMetric struct {
322326
RunUUID string `gorm:"column:RunUUID; not null; primary_key;"`
323327
NodeID string `gorm:"column:NodeID; not null; primary_key;"`

backend/src/apiserver/storage/db_fake.go

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,20 +15,34 @@
1515
package storage
1616

1717
import (
18+
"database/sql"
19+
"fmt"
1820
"github.com/golang/glog"
1921
"github.com/jinzhu/gorm"
2022
"github.com/kubeflow/pipelines/backend/src/apiserver/model"
2123
"github.com/kubeflow/pipelines/backend/src/common/util"
2224
_ "github.com/mattn/go-sqlite3"
2325
)
2426

25-
func NewFakeDB() (*DB, error) {
27+
func NewInMemoryTestDB() (*DB, error) {
2628
// Initialize GORM
2729
db, err := gorm.Open("sqlite3", ":memory:")
2830
if err != nil {
2931
return nil, util.Wrap(err, "Could not create the GORM database")
3032
}
3133
// Create tables
34+
dbWithMigration, err := migrate(db)
35+
if err != nil {
36+
return nil, util.Wrap(err, "Could not migrate the database")
37+
}
38+
return NewDB(dbWithMigration, NewSQLiteDialect()), nil
39+
}
40+
41+
func migrate(db *gorm.DB) (*sql.DB, error) {
42+
if db == nil {
43+
return nil, fmt.Errorf("db cannot be nil")
44+
}
45+
// Create tables
3246
db.AutoMigrate(
3347
&model.Experiment{},
3448
&model.Job{},
@@ -41,13 +55,13 @@ func NewFakeDB() (*DB, error) {
4155
&model.DBStatus{},
4256
&model.DefaultExperiment{},
4357
)
44-
return NewDB(db.DB(), NewSQLiteDialect()), nil
58+
return db.DB(), nil
4559
}
4660

4761
func NewFakeDBOrFatal() *DB {
48-
db, err := NewFakeDB()
62+
db, err := NewInMemoryTestDB()
4963
if err != nil {
50-
glog.Fatalf("The fake DB doesn't create successfully. Fail fast")
64+
glog.Fatalf("The fake DB doesn't create successfully. Fail fast: %v", err)
5165
}
5266
return db
5367
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package storage
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"github.com/golang/glog"
7+
"github.com/jinzhu/gorm"
8+
_ "github.com/jinzhu/gorm/dialects/mysql"
9+
"github.com/kubeflow/pipelines/backend/src/common/util"
10+
mysqlcontainer "github.com/testcontainers/testcontainers-go/modules/mysql"
11+
)
12+
13+
func NewMySqlDBOrFatal(container *mysqlcontainer.MySQLContainer, ctx context.Context) *DB {
14+
gormDb, err := OpenMysqlDb(ctx, container)
15+
if err != nil {
16+
glog.Fatalf(err.Error())
17+
}
18+
db, err := ApplyMigration(gormDb)
19+
return db
20+
}
21+
22+
func LaunchMysqlContainer(ctx context.Context) (*mysqlcontainer.MySQLContainer, error) {
23+
container, err := mysqlcontainer.Run(ctx, "mysql:8.0")
24+
if err != nil {
25+
return nil, fmt.Errorf("error during creating mysql container for testing: %v", err)
26+
}
27+
return container, nil
28+
}
29+
30+
func OpenMysqlDb(ctx context.Context, container *mysqlcontainer.MySQLContainer) (*gorm.DB, error) {
31+
connString, err := container.ConnectionString(ctx)
32+
if err != nil {
33+
return nil, fmt.Errorf("error during creating mysql connection string for testing: %v", err)
34+
}
35+
db, err := gorm.Open("mysql", connString)
36+
if err != nil {
37+
return nil, util.Wrap(err, "Could not create the GORM mysql database for testing")
38+
}
39+
return db, nil
40+
}
41+
42+
func ApplyMigration(db *gorm.DB) (*DB, error) {
43+
dbWithMigration, err := migrate(db)
44+
if err != nil {
45+
return nil, util.Wrap(err, "Could not migrate the database")
46+
}
47+
return NewDB(dbWithMigration, NewMySQLDialect()), nil
48+
}

0 commit comments

Comments
 (0)