Skip to content

Commit

Permalink
improve: standalone integration test, require nothing (arana-db#159)
Browse files Browse the repository at this point in the history
  • Loading branch information
jjeffcaii authored May 4, 2022
1 parent 481acdc commit 4acdaa7
Show file tree
Hide file tree
Showing 6 changed files with 295 additions and 187 deletions.
7 changes: 1 addition & 6 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -35,18 +35,13 @@ build dist/arana dist/arana-sha-256:
docker-build:
docker build -t aranadb/arana:latest .

integration-test: docker-build
docker-compose up -d
@sleep 30
integration-test:
@go clean -testcache
go test -tags integration -v ./test/...

clean:
docker-compose -f docker/docker-compose.yaml down
@rm -rf coverage.txt
@rm -rf dist
@rm -rf docker/data
@rm -rf docker/mysqld

prepareLic:
echo 'The makefile is for ci test and has dependencies. Do not run it locally. If you want to run the unit tests, run command `go test ./...` directly.'
Expand Down
41 changes: 22 additions & 19 deletions cmd/start/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ func init() {
Use: "start",
Short: "start arana",
Example: "arana start -c bootstrap.yaml",
Run: Run,
Run: run,
}
cmd.PersistentFlags().
StringP(constants.ConfigPathKey, "c", os.Getenv(constants.EnvBootstrapPath), "bootstrap configuration file path")
Expand All @@ -69,24 +69,7 @@ func init() {
})
}

func Run(cmd *cobra.Command, args []string) {
_ = args

bootstrapConfigPath, _ := cmd.PersistentFlags().GetString(constants.ConfigPathKey)
if len(bootstrapConfigPath) < 1 {
// search bootstrap yaml
for _, path := range constants.GetConfigSearchPathList() {
bootstrapConfigPath = filepath.Join(path, "bootstrap.yaml")
if _, err := os.Stat(bootstrapConfigPath); err == nil {
break
}
bootstrapConfigPath = filepath.Join(path, "bootstrap.yml")
if _, err := os.Stat(bootstrapConfigPath); err == nil {
break
}
}
}

func Run(bootstrapConfigPath string) {
// print slogan
fmt.Printf("\033[92m%s\033[0m\n", slogan) // 92m: light green

Expand Down Expand Up @@ -147,3 +130,23 @@ func Run(cmd *cobra.Command, args []string) {
return
}
}

func run(cmd *cobra.Command, args []string) {
_ = args
bootstrapConfigPath, _ := cmd.PersistentFlags().GetString(constants.ConfigPathKey)
if len(bootstrapConfigPath) < 1 {
// search bootstrap yaml
for _, path := range constants.GetConfigSearchPathList() {
bootstrapConfigPath = filepath.Join(path, "bootstrap.yaml")
if _, err := os.Stat(bootstrapConfigPath); err == nil {
break
}
bootstrapConfigPath = filepath.Join(path, "bootstrap.yml")
if _, err := os.Stat(bootstrapConfigPath); err == nil {
break
}
}
}

Run(bootstrapConfigPath)
}
103 changes: 58 additions & 45 deletions test/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,35 +18,36 @@
package test

import (
"database/sql"
"fmt"
"testing"
"time"
)

import (
_ "github.com/go-sql-driver/mysql" // register mysql

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/suite"
)

import (
"github.com/arana-db/arana/pkg/util/rand2"
utils "github.com/arana-db/arana/pkg/util/tableprint"
)

const (
driverName string = "mysql"

// user:password@tcp(127.0.0.1:3306)/dbName?
dataSourceName string = "dksl:123456@tcp(127.0.0.1:13306)/employees?timeout=1s&readTimeout=1s&writeTimeout=1s&parseTime=true&loc=Local&charset=utf8mb4,utf8"
)
type IntegrationSuite struct {
*MySuite
}

func TestBasicTx(t *testing.T) {
db, err := sql.Open(driverName, dataSourceName)
assert.NoErrorf(t, err, "connection error: %v", err)
defer db.Close()
func TestSuite(t *testing.T) {
su := NewMySuite(WithMySQLServerAuth("root", "123456"), WithMySQLDatabase("employees"))
suite.Run(t, &IntegrationSuite{su})
}

func (s *IntegrationSuite) TestBasicTx() {
var (
db = s.DB()
t = s.T()
)
tx, err := db.Begin()
assert.NoError(t, err, "should begin a new tx")

Expand Down Expand Up @@ -99,10 +100,11 @@ func TestBasicTx(t *testing.T) {
_, _ = db.Exec("delete from sequence where name = ?", name)
}

func TestSimpleSharding(t *testing.T) {
db, err := sql.Open(driverName, dataSourceName)
assert.NoErrorf(t, err, "connection error: %v", err)
defer db.Close()
func (s *IntegrationSuite) TestSimpleSharding() {
var (
db = s.DB()
t = s.T()
)

const total = 100

Expand Down Expand Up @@ -171,11 +173,11 @@ func TestSimpleSharding(t *testing.T) {
})
}

func TestInsert(t *testing.T) {
db, err := sql.Open(driverName, dataSourceName)
assert.NoErrorf(t, err, "connection error: %v", err)
defer db.Close()

func (s *IntegrationSuite) TestInsert() {
var (
db = s.DB()
t = s.T()
)
result, err := db.Exec(`INSERT INTO employees ( emp_no, birth_date, first_name, last_name, gender, hire_date )
VALUES (?, ?, ?, ?, ?, ?)`, 100001, "1992-01-07", "scott", "lewis", "M", "2014-09-01")
assert.NoErrorf(t, err, "insert row error: %v", err)
Expand All @@ -184,10 +186,11 @@ func TestInsert(t *testing.T) {
assert.Equal(t, int64(1), affected)
}

func TestSelect(t *testing.T) {
db, err := sql.Open(driverName, dataSourceName)
assert.NoErrorf(t, err, "connection error: %v", err)
defer db.Close()
func (s *IntegrationSuite) TestSelect() {
var (
db = s.DB()
t = s.T()
)

rows, err := db.Query(`SELECT emp_no, birth_date, first_name, last_name, gender, hire_date FROM employees
WHERE emp_no = ?`, 100001)
Expand All @@ -208,10 +211,11 @@ func TestSelect(t *testing.T) {
assert.Equal(t, "scott", firstName)
}

func TestSelectLimit1(t *testing.T) {
db, err := sql.Open(driverName, dataSourceName)
assert.NoErrorf(t, err, "connection error: %v", err)
defer db.Close()
func (s *IntegrationSuite) TestSelectLimit1() {
var (
db = s.DB()
t = s.T()
)

rows, err := db.Query(`SELECT emp_no, birth_date, first_name, last_name, gender, hire_date FROM employees LIMIT 1`)
assert.NoErrorf(t, err, "select row error: %v", err)
Expand All @@ -231,10 +235,11 @@ func TestSelectLimit1(t *testing.T) {
assert.Equal(t, "scott", firstName)
}

func TestUpdate(t *testing.T) {
db, err := sql.Open(driverName, dataSourceName)
assert.NoErrorf(t, err, "connection error: %v", err)
defer db.Close()
func (s *IntegrationSuite) TestUpdate() {
var (
db = s.DB()
t = s.T()
)

result, err := db.Exec(`UPDATE employees set last_name = ? where emp_no = ?`, "louis", 100001)
assert.NoErrorf(t, err, "update row error: %v", err)
Expand All @@ -244,10 +249,15 @@ func TestUpdate(t *testing.T) {
assert.Equal(t, int64(1), affected)
}

func TestDelete(t *testing.T) {
db, err := sql.Open(driverName, dataSourceName)
assert.NoErrorf(t, err, "connection error: %v", err)
defer db.Close()
func (s *IntegrationSuite) TestDelete() {
var (
db = s.DB()
t = s.T()
)

// prepare test records
_, _ = db.Exec(`INSERT IGNORE INTO employees ( emp_no, birth_date, first_name, last_name, gender, hire_date )
VALUES (?, ?, ?, ?, ?, ?)`, 100001, "1992-01-07", "scott", "lewis", "M", "2014-09-01")

result, err := db.Exec(`DELETE FROM employees WHERE emp_no = ?`, 100001)
assert.NoErrorf(t, err, "delete row error: %v", err)
Expand All @@ -256,10 +266,11 @@ func TestDelete(t *testing.T) {
assert.Equal(t, int64(1), affected)
}

func TestShowDatabases(t *testing.T) {
db, err := sql.Open(driverName, dataSourceName)
assert.NoErrorf(t, err, "connection error: %v", err)
defer db.Close()
func (s *IntegrationSuite) TestShowDatabases() {
var (
db = s.DB()
t = s.T()
)

result, err := db.Query("show databases")
assert.NoErrorf(t, err, "show databases error: %v", err)
Expand All @@ -268,11 +279,13 @@ func TestShowDatabases(t *testing.T) {
assert.Equal(t, affected[0].DatabaseTypeName(), "VARCHAR")
}

func TestDropTable(t *testing.T) {
func (s *IntegrationSuite) TestDropTable() {
var (
db = s.DB()
t = s.T()
)

t.Skip()
db, err := sql.Open(driverName, dataSourceName)
assert.NoErrorf(t, err, "connection error: %v", err)
defer db.Close()

//drop table physical name != logical name and physical name = logical name
result, err := db.Exec(`DROP TABLE student,salaries`)
Expand Down
Loading

0 comments on commit 4acdaa7

Please sign in to comment.