Files
NTrace-core/cmd/trace_exit_test.go
T
tsosunchia 15d82abc04 feat(mtr): 添加快照保存、退出摘要与快捷键帮助
- 在实时和回放 TUI 中冻结快照并独占保存文本、JSON 或离线 HTML
- 恢复终端后输出可关闭的摘要,保留探测与录制退出状态
- 增加可滚动帮助及中文路径编辑,补充三平台交互验收
2026-09-12 23:35:42 +08:00

55 lines
1.7 KiB
Go

package cmd
import (
"bytes"
"context"
"errors"
"net"
"os"
"os/exec"
"testing"
"time"
"github.com/nxtrace/NTrace-core/trace"
)
func TestTraceRunErrorExitStatus(t *testing.T) {
for _, mode := range []string{"failure", "report", "canceled", "success"} {
t.Run(mode, func(t *testing.T) {
ctx, cancel := context.WithTimeout(t.Context(), 10*time.Second)
defer cancel()
process := exec.CommandContext(ctx, os.Args[0], "-test.run=^TestTraceRunErrorProcess$")
process.Env = append(os.Environ(), "NTRACE_TEST_TRACE_ERROR="+mode)
var stdout, stderr bytes.Buffer
process.Stdout, process.Stderr = &stdout, &stderr
err := process.Run()
if mode == "failure" || mode == "report" {
var exit *exec.ExitError
if !errors.As(err, &exit) || exit.ExitCode() != 1 || stdout.Len() != 0 || !bytes.Contains(stderr.Bytes(), []byte("probe setup failed")) {
t.Fatalf("error=%v stdout=%s stderr=%s", err, stdout.String(), stderr.String())
}
} else if err != nil || stderr.Len() != 0 {
t.Fatalf("error=%v stderr=%s", err, stderr.String())
}
})
}
}
func TestTraceRunErrorProcess(t *testing.T) {
switch os.Getenv("NTRACE_TEST_TRACE_ERROR") {
case "failure":
exitOnTraceRunError(errors.New("probe setup failed"))
case "report":
runMTRReportFn = func(context.Context, trace.Method, trace.Config, trace.MTROptions, trace.MTROnSnapshot) error {
return errors.New("probe setup failed")
}
maybeRunMTRMode(effectiveMTRModes{mtr: true, report: true}, trace.ICMPTrace,
trace.Config{SrcAddr: "127.0.0.1", DstIP: net.IPv4(127, 0, 0, 1)}, true, 1, false, 0,
"127.0.0.1", "disable-geoip", false, 0, mtrTUIOptions{})
case "canceled":
exitOnTraceRunError(context.Canceled)
case "success":
exitOnTraceRunError(nil)
}
}