clean code.

pull/364/head
Otokaze 5 years ago
parent 7241f496d9
commit 4c4e0e33c2
  1. 73
      pkg/testing/lich/composer.go
  2. 14
      pkg/testing/lich/healthcheck.go
  3. 10
      tool/testcli/main.go
  4. 2
      tool/testgen/templete.go

@ -6,15 +6,17 @@ import (
"encoding/json"
"flag"
"fmt"
"log"
"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
@ -22,85 +24,78 @@ var (
func init() {
flag.StringVar(&yamlPath, "f", "docker-compose.yaml", "composer yaml path.")
flag.BoolVar(&noDown, "nodown", false, "containers are not recycled.")
}
// Setup setup UT related environment dependence for everything.
func Setup() (err error) {
func runCompose(args ...string) (output []byte, err error) {
if _, err = os.Stat(yamlPath); os.IsNotExist(err) {
log.Println("composer yaml is not exist!", yamlPath)
log.Error("os.Stat(%s) composer yaml is not exist!", yamlPath)
return
}
if yamlPath, err = filepath.Abs(yamlPath); err != nil {
log.Printf("filepath.Abs(%s) error(%v)", yamlPath, err)
log.Error("filepath.Abs(%s) error(%v)", yamlPath, err)
return
}
pathHash = fmt.Sprintf("%x", md5.Sum([]byte(yamlPath)))[:9]
var args = []string{"-f", yamlPath, "-p", pathHash, "up", "-d"}
if err = exec.Command("docker-compose", args...).Run(); err != nil {
log.Printf("exec.Command(docker-compose) args(%v) error(%v)", args, err)
Teardown()
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
}
// 拿到yaml文件中的服务名,同时通过服务名获取到启动的容器ID
if _, err = getServices(); err != nil {
Teardown()
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()
}
// 通过容器ID检测容器的状态,包括容器服务的状态
if _, err = checkServices(); err != nil {
Teardown()
}()
if _, err = getServices(); err != nil {
return
}
_, err = checkServices()
return
}
// Teardown unsetup all environment dependence.
func Teardown() (err error) {
if _, err = os.Stat(yamlPath); os.IsNotExist(err) {
log.Println("composer yaml is not exist!")
return
}
if yamlPath, err = filepath.Abs(yamlPath); err != nil {
log.Printf("filepath.Abs(%s) error(%v)", yamlPath, err)
return
}
pathHash = fmt.Sprintf("%x", md5.Sum([]byte(yamlPath)))[:9]
args := []string{"-f", yamlPath, "-p", pathHash, "down"}
if output, err := exec.Command("docker-compose", args...).CombinedOutput(); err != nil {
log.Fatalf("exec.Command(docker-compose) args(%v) stdout(%s) error(%v)", args, string(output), err)
return err
if !noDown {
_, err = runCompose("down")
}
return
}
func getServices() (output []byte, err error) {
var args = []string{"-f", yamlPath, "-p", pathHash, "config", "--services"}
if output, err = exec.Command("docker-compose", args...).CombinedOutput(); err != nil {
log.Printf("exec.Command(docker-compose) args(%v) stdout(%s) error(%v)", args, string(output), err)
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")) {
args = []string{"-f", yamlPath, "-p", pathHash, "ps", "-a", "-q", string(svr)}
if output, err = exec.Command("docker-compose", args...).CombinedOutput(); err != nil {
log.Printf("exec.Command(docker-compose) args(%v) stdout(%s) error(%v)", args, string(output), err)
if output, err = runCompose("ps", "-a", "-q", string(svr)); err != nil {
return
}
var id = string(bytes.TrimSpace(output))
var (
id = string(bytes.TrimSpace(output))
args = []string{"inspect", id, "--format", "'{{json .}}'"}
)
if output, err = exec.Command("docker", args...).CombinedOutput(); err != nil {
log.Printf("exec.Command(docker) args(%v) stdout(%s) error(%v)", args, string(output), err)
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.Printf("exec.Command(docker) args(%v) error(%v)", args, err)
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.Printf("json.Unmarshal(%s) error(%v)", string(output), err)
log.Error("json.Unmarshal(%s) error(%v)", string(output), err)
return
}
services[string(svr)] = c
@ -121,7 +116,7 @@ func checkServices() (output []byte, err error) {
}()
for svr, c := range services {
if err = c.Healthcheck(); err != nil {
log.Printf("healthcheck(%s) error(%v) retrying %d times...", svr, err, 5-retry)
log.Error("healthcheck(%s) error(%v) retrying %d times...", svr, err, 5-retry)
return
}
// TODO About container check and more...

@ -1,13 +1,13 @@
package lich
import (
"database/sql"
"fmt"
"log"
"net"
"strconv"
"strings"
"database/sql"
"github.com/bilibili/kratos/pkg/log"
// Register go-sql-driver stuff
_ "github.com/go-sql-driver/mysql"
)
@ -18,7 +18,7 @@ var healthchecks = map[string]func(*Container) error{"mysql": checkMysql, "maria
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.Printf("docker status(%s) health(%s) error(%v)", status, health, err)
log.Error("docker status(%s) health(%s) error(%v)", status, health, err)
return
}
if check, ok := healthchecks[c.GetImage()]; ok {
@ -27,7 +27,7 @@ func (c *Container) Healthcheck() (err error) {
}
for proto, ports := range c.NetworkSettings.Ports {
if id := c.GetID(); !strings.Contains(proto, "tcp") {
log.Printf("container: %s proto(%s) unsupported.", id, proto)
log.Error("container: %s proto(%s) unsupported.", id, proto)
continue
}
for _, pulish := range ports {
@ -38,7 +38,7 @@ func (c *Container) Healthcheck() (err error) {
tcpConn *net.TCPConn
)
if tcpConn, err = net.DialTCP("tcp", nil, tcpAddr); err != nil {
log.Printf("net.DialTCP(%s:%s) error(%v)", pulish.HostIP, pulish.HostPort, err)
log.Error("net.DialTCP(%s:%s) error(%v)", pulish.HostIP, pulish.HostPort, err)
return
}
tcpConn.Close()
@ -74,11 +74,11 @@ func checkMysql(c *Container) (err error) {
}
var dsn = fmt.Sprintf("%s:%s@tcp(%s:%s)/", user, passwd, ip, port)
if db, err = sql.Open("mysql", dsn); err != nil {
log.Printf("sql.Open(mysql) dsn(%s) error(%v)", dsn, err)
log.Error("sql.Open(mysql) dsn(%s) error(%v)", dsn, err)
return
}
if err = db.Ping(); err != nil {
log.Printf("ping(db) dsn(%s) error(%v)", dsn, err)
log.Error("ping(db) dsn(%s) error(%v)", dsn, err)
}
defer db.Close()
return

@ -9,14 +9,6 @@ import (
"github.com/bilibili/kratos/pkg/testing/lich"
)
var (
noDown bool
)
func init() {
flag.BoolVar(&noDown, "nodown", false, "containers are not recycled.")
}
func parseArgs() (flags map[string]string) {
flags = make(map[string]string)
for idx, arg := range os.Args {
@ -48,9 +40,7 @@ func main() {
if err := lich.Setup(); err != nil {
panic(err)
}
if !noDown {
defer lich.Teardown()
}
cmds := strings.Split(flags["run"], " ")
cmd := exec.Command(cmds[0], cmds[1:]...)
cmd.Stdout, cmd.Stderr = os.Stdout, os.Stderr

@ -37,5 +37,5 @@ var (
自己动手分田地;
你若气死谁如意?
谈笑风生活长命.
// Release 1.2.3. Powered by 主站质保团队`
// Release 1.2.3. Powered by Kratos`
)

Loading…
Cancel
Save