Files
NTrace-core/cmd/trace_exit_test.go
T
tsosunchia 205bdaa7f1 feat(mtr): 支持实时 NDJSON 与最终 JSON 报告 (#230)
- 为 full、tiny 和 ntr 提供 MTR JSON 输出并保留传统模式
- 传播初始化和监听错误,完善取消、资源清理及非零退出
- 补充事件顺序、信号处理和跨平台回归测试及文档
2026-09-07 14:35:59 +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)
case "canceled":
exitOnTraceRunError(context.Canceled)
case "success":
exitOnTraceRunError(nil)
}
}