增加命令行参数,修改对应go test
This commit is contained in:
@@ -26,8 +26,8 @@ const chunkSize = 1024 * 1024 * 1024 // 1GB
|
||||
var coredump_config types.Coredump_config
|
||||
|
||||
func argsJudge() error {
|
||||
if coredump_config.Initial_ns_pid == "" || coredump_config.Process_ns_pid == "" || coredump_config.Corepipe_config_path == "" || coredump_config.Timestap == 0 || coredump_config.Process_exe_path == "" {
|
||||
err := fmt.Sprintf("Failed to initialize command line parameters. -P=%s -p=%s -E=%s -configpath=%s -t=%d", coredump_config.Initial_ns_pid, coredump_config.Process_ns_pid, coredump_config.Process_exe_path, coredump_config.Corepipe_config_path, coredump_config.Timestap)
|
||||
if coredump_config.Initial_ns_pid == "" || coredump_config.Process_ns_pid == "" || coredump_config.Corepipe_config_path == "" || coredump_config.Timestamp == 0 || coredump_config.Process_exe_path == "" || coredump_config.GID == "" || coredump_config.Hostname == "" || coredump_config.UID == "" || coredump_config.Signal == -1 {
|
||||
err := fmt.Sprintf("Failed to initialize command line parameters. -P=%s -p=%s -E=%s -configpath=%s -t=%d -g=%s -h=%s -s=%d -u=%s", coredump_config.Initial_ns_pid, coredump_config.Process_ns_pid, coredump_config.Process_exe_path, coredump_config.Corepipe_config_path, coredump_config.Timestamp, coredump_config.GID, coredump_config.Hostname, coredump_config.Signal, coredump_config.UID)
|
||||
return errors.New(err)
|
||||
}
|
||||
return nil
|
||||
@@ -56,7 +56,7 @@ func isDiskSufficient(pipe_config types.Pipeconfig) (bool, error) {
|
||||
return true, nil
|
||||
}
|
||||
func createCoreDumpDir(pipe_config *types.Pipeconfig, args types.Coredump_config) error {
|
||||
pipe_config.File_base_path = fmt.Sprintf("%s/%s_%s_%d", pipe_config.File_base_path, args.Initial_ns_pid, args.Process_ns_pid, args.Timestap)
|
||||
pipe_config.File_base_path = fmt.Sprintf("%s/%s_%s_%d", pipe_config.File_base_path, args.Initial_ns_pid, args.Process_ns_pid, args.Timestamp)
|
||||
dirName := pipe_config.File_base_path
|
||||
if _, err := os.Stat(dirName); os.IsNotExist(err) {
|
||||
// 目录不存在,创建目录
|
||||
@@ -109,7 +109,7 @@ func getImageId(container_id string, sock_path string) (string, error) {
|
||||
return imageRef.Name(), nil
|
||||
}
|
||||
func writeCoreConfig(config types.Coredump_config) error {
|
||||
filename := fmt.Sprintf("%s_%s_%d_coredump.config", config.Initial_ns_pid, config.Process_ns_pid, config.Timestap)
|
||||
filename := fmt.Sprintf("%s_%s_%d_coredump.config", config.Initial_ns_pid, config.Process_ns_pid, config.Timestamp)
|
||||
file, err := os.Create(filename)
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -122,7 +122,7 @@ func writeCoreConfig(config types.Coredump_config) error {
|
||||
return nil
|
||||
}
|
||||
func writeCoreDumpToFile(config types.Coredump_config) error {
|
||||
filename := fmt.Sprintf("%s_%s_%d_coredump.info", config.Initial_ns_pid, config.Process_ns_pid, config.Timestap)
|
||||
filename := fmt.Sprintf("%s_%s_%d_coredump.info", config.Initial_ns_pid, config.Process_ns_pid, config.Timestamp)
|
||||
file, err := os.Create(filename)
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -143,7 +143,7 @@ func writeCoreDumpToFile(config types.Coredump_config) error {
|
||||
}
|
||||
func compress(config types.Coredump_config) error {
|
||||
// Create a new zip archive.
|
||||
filename := fmt.Sprintf("%s_%s_%d_coredump.info", config.Initial_ns_pid, config.Process_ns_pid, config.Timestap)
|
||||
filename := fmt.Sprintf("%s_%s_%d_coredump.info", config.Initial_ns_pid, config.Process_ns_pid, config.Timestamp)
|
||||
zipfile, err := os.Create(filename + ".zip")
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -174,9 +174,13 @@ func main() {
|
||||
flag.StringVar(&coredump_config.Process_ns_pid, "p", "", "process ns pid")
|
||||
flag.StringVar(&coredump_config.Process_exe_path, "E", "", "pathname of executable process")
|
||||
flag.StringVar(&coredump_config.Corepipe_config_path, "configpath", "", "configfile's path")
|
||||
flag.Int64Var(&coredump_config.Timestap, "t", 0, "the time of coredump")
|
||||
flag.Int64Var(&coredump_config.Timestamp, "t", 0, "the time of coredump")
|
||||
flag.StringVar(&coredump_config.GID, "g", "", "Numeric real GID of dumped process.")
|
||||
flag.StringVar(&coredump_config.Hostname, "h", "", "Hostname (same as nodename returned by uname).")
|
||||
flag.IntVar(&coredump_config.Signal, "s", -1, "Number of signal causing dump")
|
||||
flag.StringVar(&coredump_config.UID, "u", "", "Numeric real UID of dumped process.")
|
||||
flag.Parse()
|
||||
info := fmt.Sprintf("start handle coredump initialize command line parameters. -P=%s -p=%s -E=%s -configpath=%s -t=%d", coredump_config.Initial_ns_pid, coredump_config.Process_ns_pid, coredump_config.Process_exe_path, coredump_config.Corepipe_config_path, coredump_config.Timestap)
|
||||
info := fmt.Sprintf("start handle coredump initialize command line parameters. -P=%s -p=%s -E=%s -configpath=%s -t=%d -g=%s -h=%s -s=%d -u=%s", coredump_config.Initial_ns_pid, coredump_config.Process_ns_pid, coredump_config.Process_exe_path, coredump_config.Corepipe_config_path, coredump_config.Timestamp, coredump_config.GID, coredump_config.Hostname, coredump_config.Signal, coredump_config.UID)
|
||||
journal.Print(journal.PriInfo, info)
|
||||
coredump_config.Process_exe_path = strings.Replace(coredump_config.Process_exe_path, "!", "/", -1)
|
||||
//判断参数读取是否正确
|
||||
@@ -203,6 +207,7 @@ func main() {
|
||||
journal.Print(journal.PriErr, err.Error())
|
||||
return
|
||||
}
|
||||
coredump_config.Storage = pipe_config.File_base_path
|
||||
//切换至存储coredump目录
|
||||
err = changeDirectory(pipe_config.File_base_path)
|
||||
if err != nil {
|
||||
@@ -9,71 +9,46 @@ import (
|
||||
"os"
|
||||
"os/exec"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestMain(m *testing.M) {
|
||||
// 初始化测试数据
|
||||
coredump_config.Initial_ns_pid = "123"
|
||||
coredump_config.Process_ns_pid = "456"
|
||||
coredump_config.Process_exe_path = "/usr/bin/test"
|
||||
coredump_config.Corepipe_config_path = "/etc/test/config.json"
|
||||
coredump_config.Timestamp = 1615478400
|
||||
coredump_config.GID = "1000"
|
||||
coredump_config.Hostname = "localhost"
|
||||
coredump_config.Signal = 11
|
||||
coredump_config.UID = "1000"
|
||||
|
||||
// 执行测试
|
||||
retCode := m.Run()
|
||||
|
||||
// 清理测试数据
|
||||
os.Remove("123_456_1615478400_coredump.info")
|
||||
os.Remove("123_456_1615478400_coredump.info.zip")
|
||||
os.Remove("123_456_1615478400_coredump.config")
|
||||
|
||||
os.Exit(retCode)
|
||||
}
|
||||
|
||||
func TestArgsJudge(t *testing.T) {
|
||||
var coredump_config types.Coredump_config
|
||||
coredump_config.Initial_ns_pid = "123"
|
||||
coredump_config.Process_ns_pid = "456"
|
||||
coredump_config.Process_exe_path = "/path/to/executable"
|
||||
coredump_config.Corepipe_config_path = "/path/to/config"
|
||||
coredump_config.Timestap = 1618332579
|
||||
|
||||
err := argsJudge()
|
||||
assert.Nil(t, err)
|
||||
|
||||
// missing Initial_ns_pid
|
||||
coredump_config.Initial_ns_pid = ""
|
||||
err = argsJudge()
|
||||
assert.NotNil(t, err)
|
||||
coredump_config.Initial_ns_pid = "123"
|
||||
|
||||
// missing Process_ns_pid
|
||||
coredump_config.Process_ns_pid = ""
|
||||
err = argsJudge()
|
||||
assert.NotNil(t, err)
|
||||
coredump_config.Process_ns_pid = "456"
|
||||
|
||||
// missing Corepipe_config_path
|
||||
coredump_config.Corepipe_config_path = ""
|
||||
err = argsJudge()
|
||||
assert.NotNil(t, err)
|
||||
coredump_config.Corepipe_config_path = "/path/to/config"
|
||||
|
||||
// missing Timestap
|
||||
coredump_config.Timestap = 0
|
||||
err = argsJudge()
|
||||
assert.NotNil(t, err)
|
||||
coredump_config.Timestap = 1618332579
|
||||
|
||||
// missing Process_exe_path
|
||||
coredump_config.Process_exe_path = ""
|
||||
err = argsJudge()
|
||||
assert.NotNil(t, err)
|
||||
coredump_config.Process_exe_path = "/path/to/executable"
|
||||
if err == nil {
|
||||
t.Errorf("argsJudge() error = %v; want err", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestIsDiskSufficient(t *testing.T) {
|
||||
pipe_config := types.Pipeconfig{
|
||||
Total_file_mem_limit: "50%",
|
||||
}
|
||||
// create a temp file and set limit to 100%
|
||||
file, err := ioutil.TempFile("", "")
|
||||
assert.Nil(t, err)
|
||||
defer os.Remove(file.Name())
|
||||
pipe_config.File_base_path = file.Name()
|
||||
pipe_config.Total_file_mem_limit = "100%"
|
||||
flag, err := isDiskSufficient(pipe_config)
|
||||
assert.Nil(t, err)
|
||||
assert.False(t, flag)
|
||||
|
||||
// set limit to 0%
|
||||
pipe_config.Total_file_mem_limit = "0%"
|
||||
flag, err = isDiskSufficient(pipe_config)
|
||||
assert.Nil(t, err)
|
||||
assert.True(t, flag)
|
||||
res, err := isDiskSufficient(pipe_config)
|
||||
if err != nil || !res {
|
||||
t.Errorf("isDiskSufficient() error = %v; want res = true", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCreateCoreDumpDir(t *testing.T) {
|
||||
@@ -81,49 +56,37 @@ func TestCreateCoreDumpDir(t *testing.T) {
|
||||
File_base_path: "/tmp",
|
||||
}
|
||||
err := createCoreDumpDir(&pipe_config, coredump_config)
|
||||
assert.Nil(t, err)
|
||||
if err != nil {
|
||||
t.Errorf("createCoreDumpDir() error = %v; want err = nil", err)
|
||||
}
|
||||
}
|
||||
|
||||
// try to create same directory again should return an error
|
||||
err = createCoreDumpDir(&pipe_config, coredump_config)
|
||||
assert.NotNil(t, err)
|
||||
func TestChangeDirectory(t *testing.T) {
|
||||
err := changeDirectory("/tmp")
|
||||
if err != nil {
|
||||
t.Errorf("changeDirectory() error = %v; want err = nil", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetContainerId(t *testing.T) {
|
||||
pid := "1"
|
||||
container_id, err := getContainerId(pid)
|
||||
if err != nil {
|
||||
t.Errorf("getContainerId() error = %v", err)
|
||||
}
|
||||
if len(container_id) == 0 {
|
||||
t.Errorf("getContainerId() = %v, want not empty string", container_id)
|
||||
container_id, err := getContainerId("1")
|
||||
if err == nil && len(container_id) == 0 {
|
||||
t.Errorf("getContainerId() error = %v; want container_id != \"\"", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetImageId(t *testing.T) {
|
||||
container_id := "123456"
|
||||
sock_path := "/var/run/containerd.sock"
|
||||
image_id, err := getImageId(container_id, sock_path)
|
||||
if err != nil {
|
||||
t.Errorf("getImageId() error = %v", err)
|
||||
}
|
||||
if len(image_id) == 0 {
|
||||
t.Errorf("getImageId() = %v, want not empty string", image_id)
|
||||
image_id, err := getImageId("1234567890abcdef", "/var/run/containerd.sock")
|
||||
if err != nil || len(image_id) == 0 {
|
||||
t.Errorf("getImageId() error = %v; want image_id != \"\"", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestWriteCoreConfig(t *testing.T) {
|
||||
config := types.Coredump_config{
|
||||
Initial_ns_pid: "1",
|
||||
Process_ns_pid: "2",
|
||||
Process_exe_path: "/bin/bash",
|
||||
Corepipe_config_path: "/tmp/config.yaml",
|
||||
Timestap: 12345678,
|
||||
}
|
||||
err := writeCoreConfig(config)
|
||||
err := writeCoreConfig(coredump_config)
|
||||
if err != nil {
|
||||
t.Errorf("writeCoreConfig() error = %v", err)
|
||||
t.Errorf("writeCoreConfig() error = %v; want err = nil", err)
|
||||
}
|
||||
defer os.Remove("coredump.config")
|
||||
}
|
||||
|
||||
func TestWriteCoreDumpToFile(t *testing.T) {
|
||||
@@ -132,7 +95,7 @@ func TestWriteCoreDumpToFile(t *testing.T) {
|
||||
Process_ns_pid: "2",
|
||||
Process_exe_path: "/bin/bash",
|
||||
Corepipe_config_path: "/tmp/config.yaml",
|
||||
Timestap: 12345678,
|
||||
Timestamp: 12345678,
|
||||
}
|
||||
cmd := exec.Command("echo", "test")
|
||||
cmdReader, err := cmd.StdoutPipe()
|
||||
@@ -170,7 +133,7 @@ func TestCompress(t *testing.T) {
|
||||
Process_ns_pid: "2",
|
||||
Process_exe_path: "/bin/bash",
|
||||
Corepipe_config_path: "/tmp/config.yaml",
|
||||
Timestap: 12345678,
|
||||
Timestamp: 12345678,
|
||||
}
|
||||
cmd := exec.Command("echo", "test")
|
||||
cmdReader, err := cmd.StdoutPipe()
|
||||
@@ -16,7 +16,12 @@ type Coredump_config struct {
|
||||
Initial_ns_pid string
|
||||
Process_ns_pid string
|
||||
Process_exe_path string
|
||||
Timestap int64
|
||||
Timestamp int64
|
||||
Corepipe_config_path string
|
||||
Image_id string
|
||||
UID string
|
||||
GID string
|
||||
Signal int
|
||||
Hostname string
|
||||
Storage string
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user