Setup test env

This commit is contained in:
Jinzhu 2018-02-10 11:06:43 +08:00
parent ae509ab237
commit e0f9087c8d
3 changed files with 56 additions and 17 deletions

30
docker-compose.yml Normal file
View File

@ -0,0 +1,30 @@
version: '3'
services:
mysql:
image: 'mysql:latest'
ports:
- 9910:3306
environment:
- MYSQL_DATABASE=gorm
- MYSQL_USER=gorm
- MYSQL_PASSWORD=gorm
- MYSQL_RANDOM_ROOT_PASSWORD="yes"
postgres:
image: 'postgres:latest'
ports:
- 9920:5432
environment:
- POSTGRES_USER=gorm
- POSTGRES_DB=gorm
- POSTGRES_PASSWORD=gorm
mssql:
image: 'mcmoe/mssqldocker:latest'
ports:
- 9930:1433
environment:
- ACCEPT_EULA=Y
- SA_PASSWORD=LoremIpsum86
- MSSQL_DB=gorm
- MSSQL_USER=gorm
- MSSQL_PASSWORD=LoremIpsum86

View File

@ -36,27 +36,20 @@ func init() {
} }
func OpenTestConnection() (db *gorm.DB, err error) { func OpenTestConnection() (db *gorm.DB, err error) {
dbDSN := os.Getenv("GORM_DSN")
switch os.Getenv("GORM_DIALECT") { switch os.Getenv("GORM_DIALECT") {
case "mysql": case "mysql":
// CREATE USER 'gorm'@'localhost' IDENTIFIED BY 'gorm';
// CREATE DATABASE gorm;
// GRANT ALL ON gorm.* TO 'gorm'@'localhost';
fmt.Println("testing mysql...") fmt.Println("testing mysql...")
dbhost := os.Getenv("GORM_DBADDRESS") if dbDSN == "" {
if dbhost != "" { dbDSN = "gorm:gorm@tcp(localhost:9910)/gorm?charset=utf8&parseTime=True"
dbhost = fmt.Sprintf("tcp(%v)", dbhost)
} }
db, err = gorm.Open("mysql", fmt.Sprintf("gorm:gorm@%v/gorm?charset=utf8&parseTime=True", dbhost)) db, err = gorm.Open("mysql", dbDSN)
case "postgres": case "postgres":
fmt.Println("testing postgres...") fmt.Println("testing postgres...")
dbhost := os.Getenv("GORM_DBHOST") if dbDSN == "" {
if dbhost != "" { dbDSN = "user=gorm password=gorm DB.name=gorm port=9920 sslmode=disable"
dbhost = fmt.Sprintf("host=%v ", dbhost)
} }
db, err = gorm.Open("postgres", fmt.Sprintf("%vuser=gorm password=gorm DB.name=gorm sslmode=disable", dbhost)) db, err = gorm.Open("postgres", dbDSN)
case "foundation":
fmt.Println("testing foundation...")
db, err = gorm.Open("foundation", "dbname=gorm port=15432 sslmode=disable")
case "mssql": case "mssql":
// CREATE LOGIN gorm WITH PASSWORD = 'LoremIpsum86'; // CREATE LOGIN gorm WITH PASSWORD = 'LoremIpsum86';
// CREATE DATABASE gorm; // CREATE DATABASE gorm;
@ -64,7 +57,10 @@ func OpenTestConnection() (db *gorm.DB, err error) {
// CREATE USER gorm FROM LOGIN gorm; // CREATE USER gorm FROM LOGIN gorm;
// sp_changedbowner 'gorm'; // sp_changedbowner 'gorm';
fmt.Println("testing mssql...") fmt.Println("testing mssql...")
db, err = gorm.Open("mssql", "sqlserver://gorm:LoremIpsum86@localhost:1433?database=gorm") if dbDSN == "" {
dbDSN = "sqlserver://gorm:LoremIpsum86@localhost:9930?database=gorm"
}
db, err = gorm.Open("mssql", dbDSN)
default: default:
fmt.Println("testing sqlite3...") fmt.Println("testing sqlite3...")
db, err = gorm.Open("sqlite3", filepath.Join(os.TempDir(), "gorm.db")) db, err = gorm.Open("sqlite3", filepath.Join(os.TempDir(), "gorm.db"))

View File

@ -13,6 +13,14 @@ services:
POSTGRES_USER: gorm POSTGRES_USER: gorm
POSTGRES_PASSWORD: gorm POSTGRES_PASSWORD: gorm
POSTGRES_DB: gorm POSTGRES_DB: gorm
- name: mssql
id: mcmoe/mssqldocker:
env:
ACCEPT_EULA: Y
SA_PASSWORD: LoremIpsum86
MSSQL_DB: gorm
MSSQL_USER: gorm
MSSQL_PASSWORD: LoremIpsum86
# The steps that will be executed in the build pipeline # The steps that will be executed in the build pipeline
build: build:
@ -45,9 +53,14 @@ build:
- script: - script:
name: test mysql name: test mysql
code: | code: |
GORM_DIALECT=mysql GORM_DBADDRESS=mariadb:3306 go test ./... GORM_DIALECT=mysql GORM_DSN=gorm:gorm@tcp(mariadb:3306)/gorm?charset=utf8&parseTime=True go test ./...
- script: - script:
name: test postgres name: test postgres
code: | code: |
GORM_DIALECT=postgres GORM_DBHOST=postgres go test ./... GORM_DIALECT=postgres GORM_DSN="host=postgres user=gorm password=gorm DB.name=gorm port=5432 sslmode=disable" go test ./...
- script:
name: test mssql
code: |
GORM_DIALECT=mssql GORM_DSN="sqlserver://gorm:LoremIpsum86@mssql:1433?database=gorm" go test ./...