test(rabbitmq): 集成测试的故障模拟命令兼容 Windows

两个集成用例原先用 exec.Command("sh", "-c", …) 执行 RABBITMQ_TEST_RESTART_CMD 与
RABBITMQ_TEST_DELETE_QUEUE_CMD,但 Windows 上 sh 不在 PATH,用例必然失败——而这套
故障模拟正是 P0-2 的核心验收手段,必须在开发者机器上也能跑。

改为按 runtime.GOOS 选择 shell(Windows 用 cmd /c,其余用 sh -c)。
This commit is contained in:
2026-10-14 14:37:27 +08:00
parent 4b7f707860
commit ea134d6d71

View File

@@ -11,6 +11,7 @@ import (
"net/url"
"os"
"os/exec"
"runtime"
"strconv"
"testing"
"time"
@@ -71,6 +72,16 @@ func integrationTestManager(t *testing.T) (*RabbitMQ, config.RabbitMQConfig) {
return mgr, cfg
}
// runShell 执行由环境变量提供的"模拟故障"命令(重启 broker / 删除队列)。
// 这些命令在 Windows 开发机与 Unix CI 上都要能跑,因此按 GOOS 选择 shell——
// 硬编码 "sh -c" 会让本用例在 Windows 上直接失败。
func runShell(cmdline string) ([]byte, error) {
if runtime.GOOS == "windows" {
return exec.Command("cmd", "/c", cmdline).CombinedOutput()
}
return exec.Command("sh", "-c", cmdline).CombinedOutput()
}
func randSuffix(t *testing.T) string {
t.Helper()
b := make([]byte, 6)
@@ -134,7 +145,7 @@ func TestIntegration_ConsumerRecoversAfterBrokerRestart(t *testing.T) {
// —— 制造故障:重启 broker ——
genBefore := mgr.Stats().Generation
t.Logf("restarting broker: %s", restartCmd)
if out, err := exec.Command("sh", "-c", restartCmd).CombinedOutput(); err != nil {
if out, err := runShell(restartCmd); err != nil {
t.Fatalf("restart command failed: %v\n%s", err, out)
}
@@ -200,7 +211,7 @@ func TestIntegration_TopologyIsRedeclaredAfterQueueDeleted(t *testing.T) {
genBefore := mgr.Stats().Generation
cmd := fmt.Sprintf(deleteCmd, topo.Queue)
t.Logf("deleting queue: %s", cmd)
if out, err := exec.Command("sh", "-c", cmd).CombinedOutput(); err != nil {
if out, err := runShell(cmd); err != nil {
t.Fatalf("delete command failed: %v\n%s", err, out)
}