Skip to content

Commit 28b84b4

Browse files
test(docker): extract runImageDetached helper to fix Sonar duplication gate
Sonar flagged 17.9% duplication on new code: TestImageFirstBootAutoInit and TestImageServesHealthEndpoints share the same docker-run + cleanup + hostPort envelope. Lift it into runImageDetached and call from both.
1 parent 5777ecf commit 28b84b4

1 file changed

Lines changed: 26 additions & 26 deletions

File tree

dockertest/docker_test.go

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -199,19 +199,7 @@ func TestImageFirstBootAutoInit(t *testing.T) {
199199
}
200200

201201
containerName := fmt.Sprintf("hooks-dockertest-fb-%d", time.Now().UnixNano())
202-
out, err := exec.Command("docker", "run", "-d", "--rm",
203-
"--name", containerName,
204-
"-v", dir+":/data",
205-
"-e", "RENDER_WEBHOOK_SECRET=stub-for-tests",
206-
"-p", "0:8080",
207-
imageTag,
208-
).CombinedOutput()
209-
if err != nil {
210-
t.Fatalf("docker run: %v\n%s", err, out)
211-
}
212-
t.Cleanup(func() { cleanupContainer(t, containerName) })
213-
214-
addr := "http://127.0.0.1:" + hostPort(t, containerName, "8080/tcp")
202+
addr := runImageDetached(t, containerName, dir)
215203
if err := waitForHealthz(addr, 60*time.Second); err != nil {
216204
// Logs may contain the admin token from auto-init; never echo raw.
217205
t.Fatalf("/healthz never returned 200: %v (logs redacted: may contain admin token)", err)
@@ -237,19 +225,7 @@ func TestImageServesHealthEndpoints(t *testing.T) {
237225
dir := scaffoldDataDir(t)
238226

239227
containerName := fmt.Sprintf("hooks-dockertest-%d", time.Now().UnixNano())
240-
out, err := exec.Command("docker", "run", "-d", "--rm",
241-
"--name", containerName,
242-
"-v", dir+":/data",
243-
"-e", "RENDER_WEBHOOK_SECRET=stub-for-tests",
244-
"-p", "0:8080",
245-
imageTag,
246-
).CombinedOutput()
247-
if err != nil {
248-
t.Fatalf("docker run: %v\n%s", err, out)
249-
}
250-
t.Cleanup(func() { cleanupContainer(t, containerName) })
251-
252-
addr := "http://127.0.0.1:" + hostPort(t, containerName, "8080/tcp")
228+
addr := runImageDetached(t, containerName, dir)
253229
if err := waitForHealthz(addr, 60*time.Second); err != nil {
254230
t.Fatalf("/healthz never returned 200: %v\nlogs:\n%s", err, dockerLogs(containerName))
255231
}
@@ -544,6 +520,30 @@ func TestImageInitFailsClearlyOn0o755HostDir(t *testing.T) {
544520
}
545521
}
546522

523+
// runImageDetached starts the standard test envelope (image, /data
524+
// bind-mounted from dir, stub RENDER_WEBHOOK_SECRET, ephemeral host port
525+
// → 8080, detached + auto-remove) and registers container cleanup on t.
526+
// Returns the http://127.0.0.1:<port> base URL the test should hit.
527+
//
528+
// Tests that need different env, no --rm, no port mapping, or a non-server
529+
// invocation should call docker directly rather than thread parameters
530+
// through here — every variant added here costs more than it saves.
531+
func runImageDetached(t *testing.T, name, dir string) string {
532+
t.Helper()
533+
out, err := exec.Command("docker", "run", "-d", "--rm",
534+
"--name", name,
535+
"-v", dir+":/data",
536+
"-e", "RENDER_WEBHOOK_SECRET=stub-for-tests",
537+
"-p", "0:8080",
538+
imageTag,
539+
).CombinedOutput()
540+
if err != nil {
541+
t.Fatalf("docker run: %v\n%s", err, out)
542+
}
543+
t.Cleanup(func() { cleanupContainer(t, name) })
544+
return "http://127.0.0.1:" + hostPort(t, name, "8080/tcp")
545+
}
546+
547547
// waitForHealthz polls /healthz on the running container until it returns
548548
// 200 or the deadline expires. Server-side errors (5xx) are preserved
549549
// across iterations — if the server ever returned 500 then died, the

0 commit comments

Comments
 (0)