更新 DockerExecutor 的 Test 和 Reload 方法,优化容器启动逻辑,调整测试用例以反映新行为

This commit is contained in:
ryan
2026-03-09 23:56:24 +08:00
parent 29f19c5edd
commit 623ac6e32e
2 changed files with 21 additions and 24 deletions
+12 -12
View File
@@ -64,10 +64,17 @@ type DockerExecutor struct {
}
func (e *DockerExecutor) Test(ctx context.Context) error {
if err := e.EnsureRuntime(ctx, false); err != nil {
return err
}
output, err := e.Runner.Run(ctx, e.DockerBinary, "exec", e.ContainerName, "nginx", "-t")
output, err := e.Runner.Run(
ctx,
e.DockerBinary,
"run",
"--rm",
"-v",
fmt.Sprintf("%s:/etc/nginx/conf.d", e.RouteConfigDir),
e.Image,
"nginx",
"-t",
)
if err != nil {
return fmt.Errorf("docker nginx -t failed: %w: %s", err, string(output))
}
@@ -75,14 +82,7 @@ func (e *DockerExecutor) Test(ctx context.Context) error {
}
func (e *DockerExecutor) Reload(ctx context.Context) error {
if err := e.EnsureRuntime(ctx, false); err != nil {
return err
}
output, err := e.Runner.Run(ctx, e.DockerBinary, "exec", e.ContainerName, "nginx", "-s", "reload")
if err != nil {
return fmt.Errorf("docker nginx reload failed: %w: %s", err, string(output))
}
return nil
return e.EnsureRuntime(ctx, true)
}
func (e *DockerExecutor) EnsureRuntime(ctx context.Context, recreate bool) error {
+9 -12
View File
@@ -81,14 +81,11 @@ func TestDockerExecutorStartsContainerWhenMissing(t *testing.T) {
t.Fatalf("Test failed: %v", err)
}
if len(runner.calls) != 3 {
t.Fatalf("expected 3 calls, got %d", len(runner.calls))
if len(runner.calls) != 1 {
t.Fatalf("expected 1 call, got %d", len(runner.calls))
}
if runner.calls[1].args[0] != "run" {
t.Fatalf("expected docker run on second call, got %#v", runner.calls[1])
}
if runner.calls[2].args[0] != "exec" {
t.Fatalf("expected docker exec on third call, got %#v", runner.calls[2])
if runner.calls[0].args[0] != "run" || runner.calls[0].args[1] != "--rm" {
t.Fatalf("expected docker run --rm for test, got %#v", runner.calls[0])
}
}
@@ -113,8 +110,11 @@ func TestDockerExecutorStartsStoppedContainer(t *testing.T) {
t.Fatalf("Reload failed: %v", err)
}
if len(runner.calls) != 4 {
t.Fatalf("expected 4 calls, got %d", len(runner.calls))
if len(runner.calls) != 3 {
t.Fatalf("expected 3 calls, got %d", len(runner.calls))
}
if runner.calls[0].args[0] != "inspect" {
t.Fatalf("expected docker inspect on first call, got %#v", runner.calls[0])
}
if runner.calls[1].args[0] != "rm" {
t.Fatalf("expected docker rm on second call, got %#v", runner.calls[1])
@@ -122,9 +122,6 @@ func TestDockerExecutorStartsStoppedContainer(t *testing.T) {
if runner.calls[2].args[0] != "run" {
t.Fatalf("expected docker run on third call, got %#v", runner.calls[2])
}
if runner.calls[3].args[0] != "exec" {
t.Fatalf("expected docker exec on fourth call, got %#v", runner.calls[3])
}
}
func TestDockerExecutorRecreatesContainerOnStartup(t *testing.T) {