提交 cdd917b5 编写于 作者: Z zhaoke

* Add exec timeout

上级 0591a5da
......@@ -2,15 +2,16 @@ package action
import (
"fmt"
"strconv"
"strings"
"time"
commConsts "github.com/easysoft/zentaoatf/internal/pkg/consts"
commDomain "github.com/easysoft/zentaoatf/internal/pkg/domain"
scriptHelper "github.com/easysoft/zentaoatf/internal/pkg/helper/script"
i118Utils "github.com/easysoft/zentaoatf/pkg/lib/i118"
logUtils "github.com/easysoft/zentaoatf/pkg/lib/log"
"github.com/mattn/go-runewidth"
"strconv"
"strings"
"time"
)
func List(files []string, keywords string) {
......@@ -63,7 +64,7 @@ func List(files []string, keywords string) {
}
func SummaryObj(file string, keywords string) (bool, commDomain.FuncResult) {
pass, caseId, _, title := scriptHelper.GetCaseInfo(file)
pass, caseId, _, title, _ := scriptHelper.GetCaseInfo(file)
if pass {
_, err := strconv.Atoi(keywords)
......
......@@ -2,16 +2,19 @@ package execHelper
import (
"bufio"
"context"
"fmt"
"io"
"os"
"os/exec"
"strings"
"time"
commConsts "github.com/easysoft/zentaoatf/internal/pkg/consts"
commDomain "github.com/easysoft/zentaoatf/internal/pkg/domain"
configHelper "github.com/easysoft/zentaoatf/internal/pkg/helper/config"
langHelper "github.com/easysoft/zentaoatf/internal/pkg/helper/lang"
scriptHelper "github.com/easysoft/zentaoatf/internal/pkg/helper/script"
websocketHelper "github.com/easysoft/zentaoatf/internal/pkg/helper/websocket"
commonUtils "github.com/easysoft/zentaoatf/pkg/lib/common"
i118Utils "github.com/easysoft/zentaoatf/pkg/lib/i118"
......@@ -31,6 +34,13 @@ func RunFile(filePath, workspacePath string, conf commDomain.WorkspaceConf,
lang := langHelper.GetLangByFile(filePath)
var cmd *exec.Cmd
_, _, _, _, timeout := scriptHelper.GetCaseInfo(filePath)
if timeout == 0 {
timeout = 86400 * 7
}
ctxt, cancel := context.WithTimeout(context.Background(), time.Second*time.Duration(timeout))
defer cancel()
if commonUtils.IsWin() {
scriptInterpreter := ""
if strings.ToLower(lang) != "bat" {
......@@ -75,14 +85,12 @@ func RunFile(filePath, workspacePath string, conf commDomain.WorkspaceConf,
}
//logUtils.ExecFilef(msg)
cmd = exec.Command(scriptInterpreter, filePath)
cmd = exec.CommandContext(ctxt, scriptInterpreter, filePath)
} else {
cmd = exec.Command("/bin/bash", "-c", filePath)
cmd = exec.CommandContext(ctxt, "/bin/bash", "-c", filePath)
}
}
cmd.Dir = workspacePath
if cmd == nil {
msgStr := i118Utils.Sprintf("cmd_empty")
if commConsts.ExecFrom != commConsts.FromCmd {
......@@ -95,6 +103,8 @@ func RunFile(filePath, workspacePath string, conf commDomain.WorkspaceConf,
return "", msgStr
}
cmd.Dir = workspacePath
stdout, err1 := cmd.StdoutPipe()
stderr, err2 := cmd.StderrPipe()
......@@ -176,10 +186,12 @@ ExitCurrCase:
errOutputArr = append(errOutputArr, line)
}
}
if ctxt.Err() != nil {
errOutputArr = append(errOutputArr, i118Utils.Sprintf("exec_cmd_timeout"))
}
stdOutput = strings.Join(stdOutputArr, "")
errOutput = strings.Join(errOutputArr, "")
cmd.Wait()
return
}
......@@ -48,7 +48,7 @@ func ValidateCaseResult(scriptFile string, langType string,
key := stringUtils.Md5(scriptFile)
_, caseId, productId, title := scriptHelper.GetCaseInfo(scriptFile)
_, caseId, productId, title, _ := scriptHelper.GetCaseInfo(scriptFile)
stepLogs := make([]commDomain.StepLog, 0)
caseResult := commConsts.PASS
......
......@@ -167,7 +167,7 @@ func extractFromComments(file string) (stepObjs []*commDomain.ZtfStep) {
}
func prepareDesc(steps []string, file string) (desc string) {
pass, caseId, productId, title := GetCaseInfo(file)
pass, caseId, productId, title, _ := GetCaseInfo(file)
if !pass {
return
}
......
......@@ -2,12 +2,6 @@ package scriptHelper
import (
"fmt"
commConsts "github.com/easysoft/zentaoatf/internal/pkg/consts"
commDomain "github.com/easysoft/zentaoatf/internal/pkg/domain"
langHelper "github.com/easysoft/zentaoatf/internal/pkg/helper/lang"
"github.com/easysoft/zentaoatf/pkg/consts"
commonUtils "github.com/easysoft/zentaoatf/pkg/lib/common"
fileUtils "github.com/easysoft/zentaoatf/pkg/lib/file"
"html"
"io/ioutil"
"path"
......@@ -15,6 +9,13 @@ import (
"regexp"
"strconv"
"strings"
commConsts "github.com/easysoft/zentaoatf/internal/pkg/consts"
commDomain "github.com/easysoft/zentaoatf/internal/pkg/domain"
langHelper "github.com/easysoft/zentaoatf/internal/pkg/helper/lang"
"github.com/easysoft/zentaoatf/pkg/consts"
commonUtils "github.com/easysoft/zentaoatf/pkg/lib/common"
fileUtils "github.com/easysoft/zentaoatf/pkg/lib/file"
)
func ReplaceCaseDesc(desc, file string) {
......@@ -504,12 +505,12 @@ func ScriptToExpectName(file string) string {
// return runName
//}
func GetCaseInfo(file string) (pass bool, caseId, productId int, title string) {
func GetCaseInfo(file string) (pass bool, caseId, productId int, title string, timeout int64) {
content := fileUtils.ReadFile(file)
isOldFormat := strings.Index(content, "[esac]") > -1
pass = CheckFileContentIsScript(content)
if !pass {
return false, caseId, productId, title
return false, caseId, productId, title, timeout
}
caseInfo := ""
......@@ -535,6 +536,12 @@ func GetCaseInfo(file string) (pass bool, caseId, productId int, title string) {
caseId, _ = strconv.Atoi(arr[1])
}
myExp = regexp.MustCompile(`[\S\s]*timeout=\s*([^\n]*?)\s*\n`)
arr = myExp.FindStringSubmatch(caseInfo)
if len(arr) > 1 {
timeout, _ = strconv.ParseInt(arr[1], 10, 64)
}
myExp = regexp.MustCompile(`[\S\s]*pid=\s*([^\n]*?)\s*\n`)
arr = myExp.FindStringSubmatch(caseInfo)
if len(arr) > 1 {
......@@ -739,7 +746,7 @@ func GetScriptByIdsInDir(dirPth string, idMap *map[int]string) error {
}
path := dirPth + name
pass, id, _, _ := GetCaseInfo(path)
pass, id, _, _, _ := GetCaseInfo(path)
if pass {
(*idMap)[id] = path
}
......
......@@ -71,7 +71,7 @@ func SyncFromZentao(settings commDomain.SyncSettings, config commDomain.Workspac
func SyncToZentao(cases []string, config commDomain.WorkspaceConf) (count int, err error) {
for _, cs := range cases {
pass, id, _, title := scriptHelper.GetCaseInfo(cs)
pass, id, _, title, _ := scriptHelper.GetCaseInfo(cs)
if !pass {
continue
}
......
......@@ -329,7 +329,10 @@
"id": "exec_cmd_fail",
"translation": "Fail to exec cmd %s, error is %s."
},
{
"id": "exec_cmd_timeout",
"translation": "Exec cmd timeout."
},
{
"id": "success_to_conn",
"translation": "Success to connect."
......
......@@ -318,6 +318,10 @@
"id": "exec_cmd_fail",
"translation": "执行命令%s失败,错误%s。"
},
{
"id": "exec_cmd_timeout",
"translation": "执行命令超时。"
},
{
"id": "cmd_empty",
"translation": "命令为空。"
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册