From 623ac6e32e935402b988c88d28dbc9221d62f45f Mon Sep 17 00:00:00 2001 From: ryan Date: Mon, 9 Mar 2026 23:56:24 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0=20DockerExecutor=20=E7=9A=84?= =?UTF-8?q?=20Test=20=E5=92=8C=20Reload=20=E6=96=B9=E6=B3=95=EF=BC=8C?= =?UTF-8?q?=E4=BC=98=E5=8C=96=E5=AE=B9=E5=99=A8=E5=90=AF=E5=8A=A8=E9=80=BB?= =?UTF-8?q?=E8=BE=91=EF=BC=8C=E8=B0=83=E6=95=B4=E6=B5=8B=E8=AF=95=E7=94=A8?= =?UTF-8?q?=E4=BE=8B=E4=BB=A5=E5=8F=8D=E6=98=A0=E6=96=B0=E8=A1=8C=E4=B8=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- atsf_agent/internal/nginx/manager.go | 24 +++++++++++------------ atsf_agent/internal/nginx/manager_test.go | 21 +++++++++----------- 2 files changed, 21 insertions(+), 24 deletions(-) diff --git a/atsf_agent/internal/nginx/manager.go b/atsf_agent/internal/nginx/manager.go index 9641a6d2..40849329 100644 --- a/atsf_agent/internal/nginx/manager.go +++ b/atsf_agent/internal/nginx/manager.go @@ -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 { diff --git a/atsf_agent/internal/nginx/manager_test.go b/atsf_agent/internal/nginx/manager_test.go index e466847a..e853d3d7 100644 --- a/atsf_agent/internal/nginx/manager_test.go +++ b/atsf_agent/internal/nginx/manager_test.go @@ -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) {