Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clean up TestContext when pack build unexpectedly succeeds #588

Closed
wants to merge 3 commits into from

Conversation

colincasey
Copy link
Contributor

Moves the creation of the TestContext to before the invocation of run_pack_command so that the context will be cleaned up regardless of the outcome.

@fixes #586

Moves the creation of the `TestContext` to before the invocation of `run_pack_command` so that the context will be cleaned up regardless of the outcome.

@fixes #586
@colincasey colincasey requested a review from a team as a code owner July 5, 2023 19:45
Moves the creation of the `TestContext` to before the invocation of `run_pack_command` so that the context will be cleaned up regardless of the outcome.

@fixes #586
Copy link
Contributor

@schneems schneems left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Your PR looks good in terms of I think it does what it says it does. I'm wanting to make it so that it doesn't regress in the future i.e. if someone were to re-order the commands in a future PR tests wouldn't fail.

That's the "changes requested": To encode the fix in the build/test logic (through tests, types, or both).

We could (brainstorm):

  • Add a test for this explicit behavior (probably hard and/or expensive)
  • Couple run_pack_command with needing a TestContext (even if it's not used) to force it's creation before it's run or possibly introduce another function
  • Move the creation of the TestContext to the same location that allocates the resource (RAII)
  • Couple the creation of the image name with the creation of something that tears it down.

Ideally I want to move the logic closest to where the resource is allocated. In this case we decouple creating an image name from allocating/generating that image (via pack). I'm proposing to couple the teardown logic of the image name along with image name creation so then there's never a scenario where one would be created and not torn down.

Sketch

  • Introduce a ImageName struct that holds, well, an image name.
  • Have util::random_docker_identifier() return this new struct instead of a string
  • Move the drop logic to this struct ImageName struct
  • Add a new field to TestContext to hold an ImageName

Now when TestContext is dropped it will drop it's contents and call drop on ImageName triggering the cleanup (existing behavior is preserved). And since ImageName is allocated before run_pack_command is ever called, if it fails then ImageName drop behavior will be called (new behavior).

This assumes that the drop logic is a no-op if an image name has not been created in docker.

CHANGELOG.md Outdated Show resolved Hide resolved
Co-authored-by: Manuel Fuchs <manuel.fuchs@salesforce.com>
@Malax
Copy link
Member

Malax commented Jul 31, 2023

@edmorley does #619 also resolve this issue? In any case, this PR here won't apply cleanly anymore after run_pack_command changed so much.:/

@edmorley
Copy link
Member

@Malax No #619 does not fix this directly, though it does give more options here as to how to fix this issue.

It seems the options here now are to:

  1. (This is now only possible after libcnb-test: Refactor run_pack_command and improve panic messages #619) Make an explicit call to remove the Docker image on the "successful but panicing" branch here:
    (PackResult::Failure, Ok(LogOutput { stdout, stderr })) => {
    panic!("The pack build was expected to fail, but did not:\n\n## stderr:\n\n{stderr}\n## stdout:\n\n{stdout}\n");
  2. (As Richard suggested above) Add a new Image (or similar) struct, which has the Drop on it itself, which can be created sooner than the TestContext - and will handle the image removal itself, rather than on TestContext drop.
  3. Move the creation of TestContext earlier (the current PR's implementation), but add a comment explaining why.

I think my preference is for option (1) or (2).

Thoughts?

@Malax
Copy link
Member

Malax commented Aug 3, 2023

Thoughts?

I would vote for option 1 here. I find Drop complicates the code and makes it hard to reason about. If we can avoid using it I think we should.

@edmorley
Copy link
Member

edmorley commented Aug 8, 2023

I would vote for option 1 here. I find Drop complicates the code and makes it hard to reason about. If we can avoid using it I think we should.

Works for me! I've opened #625.

@edmorley edmorley closed this in #625 Aug 8, 2023
edmorley added a commit that referenced this pull request Aug 8, 2023
#625)

Previously, if a test with an expected result of `PackResult::Failure`
unexpectedly succeeded, the built app image was left behind since
cleanup was only performed in `TestContext::Drop` (and in this
scenario, a `TestContext` is never created in the first place).

Now the app image is correctly cleaned up.

Fixes #586.
Closes #588.
GUS-W-13903181.
@edmorley edmorley deleted the fix_586 branch August 8, 2023 09:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working libcnb-test
Projects
None yet
Development

Successfully merging this pull request may close these issues.

libcnb-test: Images not cleaned up when pack build unexpectedly suceeeds
4 participants