You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
kratos/pkg/testing/lich/composer.go

130 lines
3.1 KiB

package lich
import (
"bytes"
"crypto/md5"
"encoding/json"
"flag"
"fmt"
"os"
"os/exec"
"path/filepath"
"runtime"
"time"
5 years ago
"github.com/go-kratos/kratos/pkg/log"
)
var (
retry int
5 years ago
noDown bool
yamlPath string
pathHash string
services map[string]*Container
)
func init() {
flag.StringVar(&yamlPath, "f", "docker-compose.yaml", "composer yaml path.")
flag.IntVar(&retry, "retry", 5, "number of retries on network failure.")
5 years ago
flag.BoolVar(&noDown, "nodown", false, "containers are not recycled.")
}
5 years ago
func runCompose(args ...string) (output []byte, err error) {
if _, err = os.Stat(yamlPath); os.IsNotExist(err) {
5 years ago
log.Error("os.Stat(%s) composer yaml is not exist!", yamlPath)
return
}
if yamlPath, err = filepath.Abs(yamlPath); err != nil {
5 years ago
log.Error("filepath.Abs(%s) error(%v)", yamlPath, err)
return
}
pathHash = fmt.Sprintf("%x", md5.Sum([]byte(yamlPath)))[:9]
5 years ago
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
}
5 years ago
return
}
// Setup setup UT related environment dependence for everything.
func Setup() (err error) {
if _, err = runCompose("up", "-d"); err != nil {
return
}
5 years ago
defer func() {
if err != nil {
Teardown()
5 years ago
}
}()
if _, err = getServices(); err != nil {
return
}
5 years ago
_, err = checkServices()
return
}
// Teardown unsetup all environment dependence.
func Teardown() (err error) {
5 years ago
if !noDown {
_, err = runCompose("down")
}
return
}
func getServices() (output []byte, err error) {
5 years ago
if output, err = runCompose("config", "--services"); err != nil {
return
}
var eol = []byte("\n")
if output = bytes.TrimSpace(output); runtime.GOOS == "windows" {
eol = []byte("\r\n")
}
services = make(map[string]*Container)
for _, svr := range bytes.Split(output, eol) {
if output, err = runCompose("ps", "-q", string(svr)); err != nil {
return
}
5 years ago
var (
id = string(bytes.TrimSpace(output))
args = []string{"inspect", id, "--format", "'{{json .}}'"}
)
if output, err = exec.Command("docker", args...).CombinedOutput(); err != nil {
5 years ago
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)
5 years ago
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 {
5 years ago
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 > 0 {
retry--
getServices()
time.Sleep(time.Second * 5)
output, err = checkServices()
return
}
}()
for svr, c := range services {
if err = c.Healthcheck(); err != nil {
log.Error("healthcheck(%s) error(%v) retrying %d times...", svr, err, retry)
return
}
// TODO About container check and more...
}
return
}