Repository navigation
Conversation
|
This pull request has been marked as stale because of no activity in the last 15 days. It will be closed in the next 5 days unless it is tagged "no stalebot" or other activity occurs. |
|
Dear @EItanya, this is still needed on The branch now conflicts with |
|
This pull request has been marked as stale because of no activity in the last 15 days. It will be closed in the next 5 days unless it is tagged "no stalebot" or other activity occurs. |
…eled Start captured the caller's ctx in the cleanup closure. Callers that pass t.Context() get a context that is canceled before t.Cleanup callbacks run, so Terminate failed with "context canceled" and the container kept running. With Ryuk disabled, as in CI since kagent-dev#2554, nothing removes it. Terminate with testcontainers.TerminateContainer, which does not use the caller's context. Also terminate the container when startup fails, since postgres.Run returns the created container together with the error. Signed-off-by: thc1006 <84045975+thc1006@users.noreply.github.com>
TestMain discarded the cleanup returned by dbtest.Start, so the shared container was never terminated. Deferring it in TestMain would not help because os.Exit skips deferred calls. Run the tests from a helper that returns the exit code instead. Signed-off-by: thc1006 <84045975+thc1006@users.noreply.github.com>
c1f254e to
4c6665e
Compare
dbtest.Startkeeps the caller's context and uses it again in the cleanup it returns. A test that passest.Context()gets a context that is canceled just before thet.Cleanupcallbacks run, soTerminatefails and the container keeps running:The database package's TestMain doesn't terminate its container at all. It drops the cleanup func from
dbtest.Startand then callsos.Exit.Locally, Ryuk removes these containers after the test run, so this is easy to miss. #2554 turned Ryuk off in CI because of reaper timeouts, noting that dbtest "already terminates its containers via
t.Cleanup". For these callers it doesn't, so in CI the containers stay up until the runner goes away. On main,internal/databaseplus the controller and grpcserver tests that passt.Context()leave 23 Postgres containers running: 1 from TestMain, 14 from the controller tests, and 8 fromTestScheduledRun*andTestSessionDeletionForceThroughGeneratedClient. The sandbox and task store fixtures avoid it by passingcontext.WithoutCancel(t.Context()).This PR terminates with
testcontainers.TerminateContainer, which doesn't use the caller's context. It also terminates the container whenpostgres.Runfails, sinceRunreturns the container it created together with the error. TestMain now runs the tests from a helper that returns the exit code, so the deferred cleanup runs beforeos.Exit. Fixtures that already passcontext.WithoutCancel(t.Context())keep working as before.If a database test panics, the shared container from TestMain is still left behind, and
-timeoutskips all cleanup.Testing
With rootless podman and
TESTCONTAINERS_RYUK_DISABLED=true, I countedpodman ps -a --filter label=org.testcontainers=trueafter:Main leaves 23 running. This branch leaves none running.
go test -racealso passes for the other packages that use dbtest and forpkg/migrations, with envtest set up as in CI, andmake -C go lintis clean.TestStartTTerminatesContainerAfterTestContextIsCanceledfails on main and passes here. It adds one container start per run, about 5s. If you'd rather not have a test for a test helper, I'll drop it.pkg/migrations/runner_test.gohas its own copy of the startup code,startTestDB, which also leaks the container whenRunfails. It can't call dbtest (dbtest imports migrations), so it needs its own fix, which I left for a separate PR.