forked from go-kratos/kratos
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request go-kratos#364 from bilibili/go-common/testing_2019…
…9197322 この全ては既に世界の選択だ!
- Loading branch information
Showing
17 changed files
with
1,351 additions
and
48 deletions.
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
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 was deleted.
Oops, something went wrong.
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,9 @@ | ||
version: "3.7" | ||
|
||
services: | ||
mc: | ||
image: memcached:1 | ||
ports: | ||
- 11211:11211 | ||
|
||
|
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,4 @@ | ||
## testing/lich 运行环境构建 | ||
基于 docker-compose 实现跨平台跨语言环境的容器依赖管理方案,以解决运行ut场景下的 (mysql, redis, mc)容器依赖问题。 | ||
|
||
使用说明参见:https://github.com/bilibili/kratos/tree/master/tool/testcli/README.md |
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,125 @@ | ||
package lich | ||
|
||
import ( | ||
"bytes" | ||
"crypto/md5" | ||
"encoding/json" | ||
"flag" | ||
"fmt" | ||
"os" | ||
"os/exec" | ||
"path/filepath" | ||
"time" | ||
|
||
"github.com/bilibili/kratos/pkg/log" | ||
) | ||
|
||
var ( | ||
retry int | ||
noDown bool | ||
yamlPath string | ||
pathHash string | ||
services map[string]*Container | ||
) | ||
|
||
func init() { | ||
flag.StringVar(&yamlPath, "f", "docker-compose.yaml", "composer yaml path.") | ||
flag.BoolVar(&noDown, "nodown", false, "containers are not recycled.") | ||
} | ||
|
||
func runCompose(args ...string) (output []byte, err error) { | ||
if _, err = os.Stat(yamlPath); os.IsNotExist(err) { | ||
log.Error("os.Stat(%s) composer yaml is not exist!", yamlPath) | ||
return | ||
} | ||
if yamlPath, err = filepath.Abs(yamlPath); err != nil { | ||
log.Error("filepath.Abs(%s) error(%v)", yamlPath, err) | ||
return | ||
} | ||
pathHash = fmt.Sprintf("%x", md5.Sum([]byte(yamlPath)))[:9] | ||
args = append([]string{"-f", yamlPath, "-p", pathHash}, args...) | ||
if output, err = exec.Command("docker-compose", args...).CombinedOutput(); err != nil { | ||
log.Error("exec.Command(docker-compose) args(%v) stdout(%s) error(%v)", args, string(output), err) | ||
return | ||
} | ||
return | ||
} | ||
|
||
// Setup setup UT related environment dependence for everything. | ||
func Setup() (err error) { | ||
if _, err = runCompose("up", "-d"); err != nil { | ||
return | ||
} | ||
defer func() { | ||
if err != err { | ||
go Teardown() | ||
} | ||
}() | ||
if _, err = getServices(); err != nil { | ||
return | ||
} | ||
_, err = checkServices() | ||
return | ||
} | ||
|
||
// Teardown unsetup all environment dependence. | ||
func Teardown() (err error) { | ||
if !noDown { | ||
_, err = runCompose("down") | ||
} | ||
return | ||
} | ||
|
||
func getServices() (output []byte, err error) { | ||
if output, err = runCompose("config", "--services"); err != nil { | ||
return | ||
} | ||
services = make(map[string]*Container) | ||
output = bytes.TrimSpace(output) | ||
for _, svr := range bytes.Split(output, []byte("\n")) { | ||
if output, err = runCompose("ps", "-a", "-q", string(svr)); err != nil { | ||
return | ||
} | ||
var ( | ||
id = string(bytes.TrimSpace(output)) | ||
args = []string{"inspect", id, "--format", "'{{json .}}'"} | ||
) | ||
if output, err = exec.Command("docker", args...).CombinedOutput(); err != nil { | ||
log.Error("exec.Command(docker) args(%v) stdout(%s) error(%v)", args, string(output), err) | ||
return | ||
} | ||
if output = bytes.TrimSpace(output); bytes.Equal(output, []byte("")) { | ||
err = fmt.Errorf("service: %s | container: %s fails to launch", svr, id) | ||
log.Error("exec.Command(docker) args(%v) error(%v)", args, err) | ||
return | ||
} | ||
var c = &Container{} | ||
if err = json.Unmarshal(bytes.Trim(output, "'"), c); err != nil { | ||
log.Error("json.Unmarshal(%s) error(%v)", string(output), err) | ||
return | ||
} | ||
services[string(svr)] = c | ||
} | ||
return | ||
} | ||
|
||
func checkServices() (output []byte, err error) { | ||
defer func() { | ||
if err != nil && retry < 4 { | ||
retry++ | ||
getServices() | ||
time.Sleep(time.Second * 5) | ||
output, err = checkServices() | ||
return | ||
} | ||
retry = 0 | ||
}() | ||
for svr, c := range services { | ||
if err = c.Healthcheck(); err != nil { | ||
log.Error("healthcheck(%s) error(%v) retrying %d times...", svr, err, 5-retry) | ||
return | ||
} | ||
// TODO About container check and more... | ||
} | ||
return | ||
} |
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,85 @@ | ||
package lich | ||
|
||
import ( | ||
"database/sql" | ||
"fmt" | ||
"net" | ||
"strconv" | ||
"strings" | ||
|
||
"github.com/bilibili/kratos/pkg/log" | ||
// Register go-sql-driver stuff | ||
_ "github.com/go-sql-driver/mysql" | ||
) | ||
|
||
var healthchecks = map[string]func(*Container) error{"mysql": checkMysql, "mariadb": checkMysql} | ||
|
||
// Healthcheck check container health. | ||
func (c *Container) Healthcheck() (err error) { | ||
if status, health := c.State.Status, c.State.Health.Status; !c.State.Running || (health != "" && health != "healthy") { | ||
err = fmt.Errorf("service: %s | container: %s not running", c.GetImage(), c.GetID()) | ||
log.Error("docker status(%s) health(%s) error(%v)", status, health, err) | ||
return | ||
} | ||
if check, ok := healthchecks[c.GetImage()]; ok { | ||
err = check(c) | ||
return | ||
} | ||
for proto, ports := range c.NetworkSettings.Ports { | ||
if id := c.GetID(); !strings.Contains(proto, "tcp") { | ||
log.Error("container: %s proto(%s) unsupported.", id, proto) | ||
continue | ||
} | ||
for _, publish := range ports { | ||
var ( | ||
ip = net.ParseIP(publish.HostIP) | ||
port, _ = strconv.Atoi(publish.HostPort) | ||
tcpAddr = &net.TCPAddr{IP: ip, Port: port} | ||
tcpConn *net.TCPConn | ||
) | ||
if tcpConn, err = net.DialTCP("tcp", nil, tcpAddr); err != nil { | ||
log.Error("net.DialTCP(%s:%s) error(%v)", publish.HostIP, publish.HostPort, err) | ||
return | ||
} | ||
tcpConn.Close() | ||
} | ||
} | ||
return | ||
} | ||
|
||
func checkMysql(c *Container) (err error) { | ||
var ip, port, user, passwd string | ||
for _, env := range c.Config.Env { | ||
splits := strings.Split(env, "=") | ||
if strings.Contains(splits[0], "MYSQL_ROOT_PASSWORD") { | ||
user, passwd = "root", splits[1] | ||
continue | ||
} | ||
if strings.Contains(splits[0], "MYSQL_ALLOW_EMPTY_PASSWORD") { | ||
user, passwd = "root", "" | ||
continue | ||
} | ||
if strings.Contains(splits[0], "MYSQL_USER") { | ||
user = splits[1] | ||
continue | ||
} | ||
if strings.Contains(splits[0], "MYSQL_PASSWORD") { | ||
passwd = splits[1] | ||
continue | ||
} | ||
} | ||
var db *sql.DB | ||
if ports, ok := c.NetworkSettings.Ports["3306/tcp"]; ok { | ||
ip, port = ports[0].HostIP, ports[0].HostPort | ||
} | ||
var dsn = fmt.Sprintf("%s:%s@tcp(%s:%s)/", user, passwd, ip, port) | ||
if db, err = sql.Open("mysql", dsn); err != nil { | ||
log.Error("sql.Open(mysql) dsn(%s) error(%v)", dsn, err) | ||
return | ||
} | ||
if err = db.Ping(); err != nil { | ||
log.Error("ping(db) dsn(%s) error(%v)", dsn, err) | ||
} | ||
defer db.Close() | ||
return | ||
} |
Oops, something went wrong.