This repository has been archived by the owner on Aug 26, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 19
Speedup local tests #171
Closed
Closed
Speedup local tests #171
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
fb3c1fe
speedup mysql tests by using external db and tansactons
alovak 1d26964
create ./bin/db to manage database (at least for testing)
alovak 13317b2
run paygate client test with local paygate instance
alovak 66640d0
run watchman test with local watchman instance
alovak a90dfde
add db, paygate images into dev compose file
alovak d1969a0
remove debug statements
alovak 511d387
use dev compose for github actions
alovak 9e54fbd
try to run docker compose inside github actions
alovak d116a3a
try to run compose
alovak 89b485f
try to run compose only on linux
alovak 3f7a40e
try to use mysql as a github action service
alovak 64e82b4
fix go flow
alovak 7c0d04a
revert running containers on CI
alovak e9f7c40
rename method and test for err (to increase test cov)
alovak File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,150 @@ | ||
package main | ||
|
||
import ( | ||
"database/sql" | ||
"flag" | ||
"fmt" | ||
"os" | ||
"strings" | ||
|
||
"github.com/go-kit/kit/log" | ||
"github.com/kelseyhightower/envconfig" | ||
"github.com/moov-io/customers/internal/database" | ||
) | ||
|
||
var flagLogFormat = flag.String("log.format", "", "Format for log lines (Options: json, plain") | ||
|
||
type Config struct { | ||
RootPassword string `split_words:"true" default:"secret"` | ||
User string `default:"moov"` | ||
Password string `default:"secret"` | ||
Address string `default:"tcp(localhost:3306)"` | ||
Database string `default:"paygate_test"` | ||
} | ||
|
||
func main() { | ||
var config Config | ||
err := envconfig.Process("mysql", &config) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
err = runCmd(os.Args[1], &config) | ||
if err != nil { | ||
panic(err) | ||
} | ||
} | ||
|
||
func runCmd(cmd string, config *Config) error { | ||
switch cmd { | ||
case "setup": | ||
err := dropDB(config) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
err = createDB(config) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
err = migrateDB(config) | ||
if err != nil { | ||
return err | ||
} | ||
case "create": | ||
err := createDB(config) | ||
if err != nil { | ||
return err | ||
} | ||
case "drop": | ||
err := dropDB(config) | ||
if err != nil { | ||
return err | ||
} | ||
case "migrate": | ||
err := migrateDB(config) | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func dropDB(config *Config) error { | ||
dsn := fmt.Sprintf("root:%s@%s/", config.RootPassword, config.Address) | ||
db, err := sql.Open("mysql", dsn) | ||
if err != nil { | ||
return err | ||
} | ||
defer db.Close() | ||
|
||
_, err = db.Exec("DROP DATABASE IF EXISTS " + config.Database) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
fmt.Printf("Database %s was deleted\n", config.Database) | ||
return nil | ||
} | ||
|
||
func createDB(config *Config) error { | ||
dsn := fmt.Sprintf("root:%s@%s/", config.RootPassword, config.Address) | ||
db, err := sql.Open("mysql", dsn) | ||
if err != nil { | ||
return err | ||
} | ||
defer db.Close() | ||
|
||
_, err = db.Exec("CREATE DATABASE " + config.Database) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
_, err = db.Exec(fmt.Sprintf("CREATE USER IF NOT EXISTS '%s'@'%%' IDENTIFIED BY '%s'", config.User, config.Password)) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
_, err = db.Exec(fmt.Sprintf("GRANT ALL PRIVILEGES ON %s . * TO '%s'@'%%';", config.Database, config.User)) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
fmt.Printf("Database %s was created\n", config.Database) | ||
return nil | ||
} | ||
|
||
// TODO: having migration inside database.New (inside Connect method) makes it | ||
// ambiguous we should extract migtation method into separate public method | ||
// that we can call from here. | ||
func migrateDB(config *Config) error { | ||
var logger log.Logger | ||
|
||
// migrate database | ||
if strings.ToLower(*flagLogFormat) == "json" { | ||
logger = log.NewJSONLogger(os.Stderr) | ||
} else { | ||
logger = log.NewLogfmtLogger(os.Stderr) | ||
} | ||
logger = log.With(logger, "ts", log.DefaultTimestampUTC) | ||
logger = log.With(logger, "caller", log.DefaultCaller) | ||
|
||
dbConf := &database.MySQLConfig{ | ||
User: config.User, | ||
Password: config.Password, | ||
Address: config.Address, | ||
Database: config.Database, | ||
} | ||
|
||
db, err := database.NewMySQL(logger, dbConf) | ||
if err != nil { | ||
return err | ||
} | ||
db.Close() | ||
|
||
fmt.Printf("Database %s was migrated\n", config.Database) | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
version: '3' | ||
services: | ||
fed: | ||
image: moov/fed:v0.5.1 | ||
ports: | ||
- "8086:8086" | ||
- "9096:9096" | ||
environment: | ||
FEDACH_DATA_PATH: './data/fed/FedACHdir.txt' | ||
FEDWIRE_DATA_PATH: './data/fed/fpddir.txt' | ||
watchman: | ||
image: moov/watchman:v0.15.0 | ||
ports: | ||
- "8084:8084" | ||
- "9094:9094" | ||
paygate: | ||
image: moov/paygate:v0.8.0-dev | ||
command: -config=/conf/config.yaml | ||
volumes: | ||
- "./testdata/paygate/config.yml:/conf/config.yaml" | ||
ports: | ||
- "8082:8082" | ||
db: | ||
image: mysql:8 | ||
environment: | ||
MYSQL_ROOT_PASSWORD: secret | ||
ports: | ||
- "3306:3306" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,9 +6,11 @@ package database | |
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/go-kit/kit/log" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestMySQL__basic(t *testing.T) { | ||
|
@@ -31,6 +33,10 @@ func TestMySQL__basic(t *testing.T) { | |
if conn != nil || err == nil { | ||
t.Fatalf("conn=%#v expected error", conn) | ||
} | ||
|
||
fmt.Println("Hello") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'll remove this :) |
||
_, err = txDbConnect("user:pass@tcp(localhost:1234)/db?timeout=1s") | ||
require.Error(t, err) | ||
} | ||
|
||
func TestMySQLUniqueViolation(t *testing.T) { | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We cannot drop the database. This needs to never be an option in production in fact we will not grant a service
DROP
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For tests this may be useful. For example if you want to test some specific migrations, etc. We can add -force flag for drop and make it work only if database has
_test
.