testutil.go 3.4 KB
Newer Older
O
ob-robot 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
package testutil

import (
	"fmt"
	"os"
	"os/exec"
	"path/filepath"
	"runtime"
	"strings"
	"time"

	"github.com/shirou/gopsutil/v3/process"
)

var (
	RunDir              string
	TmpRootDir          string
	ConfDir             string
	ConfigPropertiesDir string
	ModuleConfigDir     string
	BinDir              string
	LogDir              string
	TempDir             string
	BackupDir           string
	PkgStoreDir         string
	TaskStoreDir        string

	ProjectRoot   string
	MockAgentPath string
	AgentdPath    string
	MgrAgentPath  string
	MonAgentPath  string
	AgentctlPath  string
)

func init() {
	_, testFile, _, _ := runtime.Caller(0)
	fmt.Println(testFile)
	ProjectRoot = filepath.Dir(filepath.Dir(filepath.Dir(testFile)))
	BinDir = filepath.Join(ProjectRoot, "bin")
	TmpRootDir = filepath.Join("/tmp", fmt.Sprintf("ocp_test_%d", time.Now().UnixNano()))
	ConfDir = filepath.Join(TmpRootDir, "conf")
	ConfigPropertiesDir = filepath.Join(ConfDir, "config_properties")
	ModuleConfigDir = filepath.Join(ConfDir, "module_config")
	TempDir = filepath.Join(TmpRootDir, "tmp")
	RunDir = filepath.Join(TmpRootDir, "run")
	LogDir = filepath.Join(TmpRootDir, "log")
	BackupDir = filepath.Join(TmpRootDir, "backup")
	PkgStoreDir = filepath.Join(TmpRootDir, "pkg_store")
	TaskStoreDir = filepath.Join(TmpRootDir, "task_store")

	MockAgentPath = filepath.Join(BinDir, "mock_agent")
	AgentdPath = filepath.Join(BinDir, "ob_agentd")
	MgrAgentPath = filepath.Join(BinDir, "ob_monagent")
	MonAgentPath = filepath.Join(BinDir, "ob_mgragent")
	AgentctlPath = filepath.Join(BinDir, "ob_agentctl")
}

func MakeDirs() {
	dirsToMake := []string{
		RunDir, ConfDir, ConfigPropertiesDir, ModuleConfigDir, LogDir, BackupDir, TempDir, PkgStoreDir, TaskStoreDir,
	}
	for _, dir := range dirsToMake {
		err := os.MkdirAll(dir, 0755)
		if err != nil {
			fmt.Printf("make test dir '%s' failed %v", dir, err)
		}
	}
}

func DelTestFiles() {
	err := os.RemoveAll(TmpRootDir)
	if err != nil {
		fmt.Printf("del all test dir '%s' failed %v", TmpRootDir, err)
	}
}

func KillAll() {
	ps, err := process.Processes()
	if err != nil {
		return
	}
	for _, p := range ps {
		name, err := p.Name()
		if err != nil {
			continue
		}
		if name == "ob_agentd" || name == "ob_mgragent" || name == "ob_monagent" || name == "ob_agentctl" || name == "mock_agent" {
			exe, err := p.Exe()
			if err != nil {
				continue
			}
			if !strings.HasPrefix(exe, ProjectRoot) {
				continue
			}
			fmt.Printf("kill process %s %d\n", name, p.Pid)
			_ = p.Kill()
		}
	}
}

func FileExists(path string) bool {
	if _, err := os.Stat(path); err != nil {
		return false
	}
	return true
}

func BuildBins() error {
	if FileExists(AgentdPath) && FileExists(MonAgentPath) && FileExists(MgrAgentPath) && FileExists(AgentctlPath) {
		return nil
	}
	fmt.Printf("build agent in '%s'\n", ProjectRoot)
	_ = os.Chdir(ProjectRoot)
	out, err := exec.Command("make", "pre-build", "build").CombinedOutput()
	if err != nil {
		fmt.Printf("build agent binaries failed %v\n%s\n", err, string(out))
		return err
	}
	return nil
}

func BuildMockAgent() error {
	_, testFile, _, _ := runtime.Caller(0)
	mockAgentGo := filepath.Join(filepath.Dir(filepath.Dir(testFile)), "mockagent.go")
	if !FileExists(MockAgentPath) {
		out, err := exec.Command("go", "build", "-o", MockAgentPath, mockAgentGo).CombinedOutput()
		if err != nil {
			fmt.Printf("build mock agent failed %v\n%s\n", err, string(out))
			return err
		}
	}
	return nil
}