Skip to content

Commit 5d09c4d

Browse files
author
arpechenin
committed
- enable container tests by introducing Testcontainers and Testcontainers/mysql.
- replace in-memory SQLite with MySQL 8.0 using Testcontainers in run_store_test - ignored false-positive test Signed-off-by: ntny <[email protected]> Signed-off-by: arpechenin <[email protected]>
1 parent 4f09f01 commit 5d09c4d

File tree

10 files changed

+466
-150
lines changed

10 files changed

+466
-150
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/resource/client_manager_fake.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ func NewFakeClientManager(time util.TimeInterface, uuid util.UUIDGeneratorInterf
5757
}
5858

5959
// Initialize GORM
60-
db, err := storage.NewFakeDB()
60+
db, err := storage.NewInMemoryTestDB()
6161
if err != nil {
6262
return nil, err
6363
}

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: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
// Copyright 2018 The Kubeflow Authors
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// https://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package storage
16+
17+
import (
18+
"context"
19+
"fmt"
20+
"github.com/golang/glog"
21+
"github.com/jinzhu/gorm"
22+
_ "github.com/jinzhu/gorm/dialects/mysql"
23+
"github.com/kubeflow/pipelines/backend/src/common/util"
24+
mysqlcontainer "github.com/testcontainers/testcontainers-go/modules/mysql"
25+
)
26+
27+
func NewMySqlDBOrFatal(container *mysqlcontainer.MySQLContainer, ctx context.Context) *DB {
28+
gormDb, err := OpenMysqlDb(ctx, container)
29+
if err != nil {
30+
glog.Fatalf(err.Error())
31+
}
32+
db, err := ApplyMigration(gormDb)
33+
return db
34+
}
35+
36+
func LaunchMysqlContainer(ctx context.Context) (*mysqlcontainer.MySQLContainer, error) {
37+
container, err := mysqlcontainer.Run(ctx, "mysql:8.0")
38+
if err != nil {
39+
return nil, fmt.Errorf("error during creating mysql container for testing: %v", err)
40+
}
41+
return container, nil
42+
}
43+
44+
func OpenMysqlDb(ctx context.Context, container *mysqlcontainer.MySQLContainer) (*gorm.DB, error) {
45+
connString, err := container.ConnectionString(ctx)
46+
if err != nil {
47+
return nil, fmt.Errorf("error during creating mysql connection string for testing: %v", err)
48+
}
49+
db, err := gorm.Open("mysql", connString)
50+
if err != nil {
51+
return nil, util.Wrap(err, "Could not create the GORM mysql database for testing")
52+
}
53+
return db, nil
54+
}
55+
56+
func ApplyMigration(db *gorm.DB) (*DB, error) {
57+
dbWithMigration, err := migrate(db)
58+
if err != nil {
59+
return nil, util.Wrap(err, "Could not migrate the database")
60+
}
61+
return NewDB(dbWithMigration, NewMySQLDialect()), nil
62+
}

0 commit comments

Comments
 (0)