Skip to content

test (backend) Added containerization support in testing and replaced SQLite with MySQL in the api-server/run_store_test #11889

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions backend/src/apiserver/model/experiment.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@

package model

func (Experiment) TableName() string {
return "experiments"
}

type Experiment struct {
UUID string `gorm:"column:UUID; not null; primary_key;"`
Name string `gorm:"column:Name; not null; unique_index:idx_name_namespace;"`
Expand Down
4 changes: 4 additions & 0 deletions backend/src/apiserver/model/resource_reference.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,10 @@ type ResourceType string
// The relationship between two resource objects.
type Relationship string

func (ResourceReference) TableName() string {
return "resource_references"
}

// Resource reference table models the relationship between resources in a loosely coupled way.
type ResourceReference struct {
// ID of the resource object
Expand Down
4 changes: 4 additions & 0 deletions backend/src/apiserver/model/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,10 @@ type RunDetails struct {
TaskDetails []*Task
}

func (RunMetric) TableName() string {
return "run_metrics"
}

type RunMetric struct {
RunUUID string `gorm:"column:RunUUID; not null; primary_key;"`
NodeID string `gorm:"column:NodeID; not null; primary_key;"`
Expand Down
2 changes: 1 addition & 1 deletion backend/src/apiserver/resource/client_manager_fake.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func NewFakeClientManager(time util.TimeInterface, uuid util.UUIDGeneratorInterf
}

// Initialize GORM
db, err := storage.NewFakeDB()
db, err := storage.NewInMemoryTestDB()
if err != nil {
return nil, err
}
Expand Down
22 changes: 18 additions & 4 deletions backend/src/apiserver/storage/db_fake.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,34 @@
package storage

import (
"database/sql"
"fmt"
"github.com/golang/glog"
"github.com/jinzhu/gorm"
"github.com/kubeflow/pipelines/backend/src/apiserver/model"
"github.com/kubeflow/pipelines/backend/src/common/util"
_ "github.com/mattn/go-sqlite3"
)

func NewFakeDB() (*DB, error) {
func NewInMemoryTestDB() (*DB, error) {
// Initialize GORM
db, err := gorm.Open("sqlite3", ":memory:")
if err != nil {
return nil, util.Wrap(err, "Could not create the GORM database")
}
// Create tables
dbWithMigration, err := migrate(db)
if err != nil {
return nil, util.Wrap(err, "Could not migrate the database")
}
return NewDB(dbWithMigration, NewSQLiteDialect()), nil
}

func migrate(db *gorm.DB) (*sql.DB, error) {
if db == nil {
return nil, fmt.Errorf("db cannot be nil")
}
// Create tables
db.AutoMigrate(
&model.Experiment{},
&model.Job{},
Expand All @@ -41,13 +55,13 @@ func NewFakeDB() (*DB, error) {
&model.DBStatus{},
&model.DefaultExperiment{},
)
return NewDB(db.DB(), NewSQLiteDialect()), nil
return db.DB(), nil
}

func NewFakeDBOrFatal() *DB {
db, err := NewFakeDB()
db, err := NewInMemoryTestDB()
if err != nil {
glog.Fatalf("The fake DB doesn't create successfully. Fail fast")
glog.Fatalf("The fake DB doesn't create successfully. Fail fast: %v", err)
}
return db
}
62 changes: 62 additions & 0 deletions backend/src/apiserver/storage/mysql_fake.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
// Copyright 2018 The Kubeflow Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package storage

import (
"context"
"fmt"
"github.com/golang/glog"
"github.com/jinzhu/gorm"
_ "github.com/jinzhu/gorm/dialects/mysql"
"github.com/kubeflow/pipelines/backend/src/common/util"
mysqlcontainer "github.com/testcontainers/testcontainers-go/modules/mysql"
)

func NewMySqlDBOrFatal(container *mysqlcontainer.MySQLContainer, ctx context.Context) *DB {
gormDb, err := OpenMysqlDb(ctx, container)
if err != nil {
glog.Fatalf(err.Error())
}
db, err := ApplyMigration(gormDb)
return db
}

func LaunchMysqlContainer(ctx context.Context) (*mysqlcontainer.MySQLContainer, error) {
container, err := mysqlcontainer.Run(ctx, "mysql:8.0")
if err != nil {
return nil, fmt.Errorf("error during creating mysql container for testing: %v", err)
}
return container, nil
}

func OpenMysqlDb(ctx context.Context, container *mysqlcontainer.MySQLContainer) (*gorm.DB, error) {
connString, err := container.ConnectionString(ctx)
if err != nil {
return nil, fmt.Errorf("error during creating mysql connection string for testing: %v", err)
}
db, err := gorm.Open("mysql", connString)
if err != nil {
return nil, util.Wrap(err, "Could not create the GORM mysql database for testing")
}
return db, nil
}

func ApplyMigration(db *gorm.DB) (*DB, error) {
dbWithMigration, err := migrate(db)
if err != nil {
return nil, util.Wrap(err, "Could not migrate the database")
}
return NewDB(dbWithMigration, NewMySQLDialect()), nil
}
Loading