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

DockerCommitImage task should not fail when accessing container ID property value #718

Merged
merged 4 commits into from
Dec 16, 2018

Conversation

rroques
Copy link

@rroques rroques commented Dec 13, 2018

Fixes Issue#716

@rroques
Copy link
Author

rroques commented Dec 13, 2018

Sorry, I missed my changes in the first commit. Happy to rejig the commits if the changes are accepted.

Copy link
Collaborator

@orzeh orzeh left a comment

Choose a reason for hiding this comment

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

LGTM 👍

targetContainerId createContainer.getContainerId()
}

${containerCommitImageExecutionTask}
Copy link
Owner

Choose a reason for hiding this comment

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

This test case is missing a task that stops the container. If we don't stop the container then we'll end up with stranded containers whenever the functional test is run. Could you please add it as a finalizedBy task? You will find some examples in other test classes.

Copy link
Author

Choose a reason for hiding this comment

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

Thanks for the feedback. I added image and container clean up in the test. Let me know how it looks.

@cdancy
Copy link
Collaborator

cdancy commented Dec 14, 2018

Didn't even see there was a PR out for this and I put one together :) Lets go with this one as it provides a more complete solution.

logger.quiet "Commiting image for container '${getContainerId()}'."
def commitCmd = dockerClient.commitCmd(getContainerId())
logger.quiet "Commiting image for container '${getContainerId().get()}'."
def commitCmd = dockerClient.commitCmd(getContainerId().get())
Copy link
Collaborator

Choose a reason for hiding this comment

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

Can we go with what I did for #720 here? I think it would be a good idea to start a new standard in that we need to check that the containerId was set and fail accordingly.

Copy link
Owner

Choose a reason for hiding this comment

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

The get() method will actually throw an exception if no value was set.

Copy link
Collaborator

Choose a reason for hiding this comment

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

getOrNull()?

Copy link
Collaborator

Choose a reason for hiding this comment

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

I only say to use that as it may not be exactly obvious when buried within the gradle stacktrace at least in this case we can control the message.

Copy link
Author

Choose a reason for hiding this comment

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

I updated the code with the implementation from #720

Copy link
Owner

Choose a reason for hiding this comment

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

@cdancy The method get() will throw an exception if no value was set. The method getOrNull() will not throw an exception but will return null if no value is net. I'd try to create a test and see how the error message looks like. Otherwise, we are likely just duplicating code.

Copy link
Author

Choose a reason for hiding this comment

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

I added a couple of functional tests for the cases where the container is missing, or is non existing.

In the case of the missing container, the test does not show case the getOrNull() behaviour because the task validation kicks in first, and since DockerExistingContainer#containerId input is not optional, the validation fails the build with error No value has been specified for property 'containerId'.

In the case of the non existing container, then we get the error message from the docker daemon.

@cdancy cdancy mentioned this pull request Dec 14, 2018
…nd safer retrieval of containerId in DockerCommitImage

${containerCommitImageExecutionTask}

task removeImage(type: DockerRemoveImage) {
Copy link
Owner

Choose a reason for hiding this comment

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

You won't need to remove the image as it is being reused for other functional tests. We'll only want to download that once.

Copy link
Author

Choose a reason for hiding this comment

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

Sorry I meant removing the image that has just been committed, myimage:latest. Happy to remove and only stop the container if you think that is not necessary.

Copy link
Owner

Choose a reason for hiding this comment

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

Oh, I see. Yeah, let's remove the committed image. That makes sense.

given:
String commitImage = """
task commitImage(type: DockerCommitImage) {
dependsOn startContainer
Copy link
Owner

Choose a reason for hiding this comment

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

This should say finalizedBy removeContainer.

@bmuschko bmuschko added this to the v4.2.0 milestone Dec 16, 2018
result.output.contains("Commiting image for container")
}

def "cannot commit image without container"() {
Copy link
Owner

Choose a reason for hiding this comment

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

I'd say this test case is not necessary as we are basically testing that the Gradle Provider API works as expected. You can remove this test case.

Copy link
Author

Choose a reason for hiding this comment

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

The intention was to verify that the DockerExistingContainer#containerId is properly annotated, and that the Gradle input validation kicks in.

Happy to remove though.

buildFile << containerStart(commitImage)

when:
BuildResult result = build('commitImage')
Copy link
Owner

Choose a reason for hiding this comment

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

As a side note: You'd use buildAndFail in the case that you expect the build to run and fail. In those cases you won't need to catch an exception.

Copy link
Author

Choose a reason for hiding this comment

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

Great! I did not know about that method.

buildFile << containerStart(commitImage)

when:
BuildResult result = build('commitImage')
Copy link
Owner

Choose a reason for hiding this comment

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

Same here: This should be buildAndFail.

@@ -67,8 +67,10 @@ class DockerCommitImage extends DockerExistingContainer {

@Override
void runRemoteCommand(dockerClient) {
logger.quiet "Commiting image for container '${getContainerId()}'."
def commitCmd = dockerClient.commitCmd(getContainerId())
def localId = Objects.requireNonNull(getContainerId().getOrNull(), 'containerId must be set')
Copy link
Owner

Choose a reason for hiding this comment

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

IMHO I don't think this handling is necessary. If we expect the container ID property to always container a value then we can just let Gradle fail the build with its own error message.

@bmuschko
Copy link
Owner

Thanks for the clarification. I am going to merge and clean it up in a couple of minutes so we can get you the fix.

@rroques
Copy link
Author

rroques commented Dec 16, 2018

No problem, I have committed the last changes I had prepared following your comments. Feel free to take over the PR and make all the changes you see fit.

Thanks for the help! I learned a lot playing with the function tests using testkit, they are really great!

@bmuschko bmuschko merged commit 3469466 into bmuschko:master Dec 16, 2018
@bmuschko bmuschko changed the title Issue#716 Fix DockerCommitImage task and functional test DockerCommitImage task should not fail when accessing container ID property value Dec 16, 2018
@bmuschko
Copy link
Owner

@rroques Thanks again for your work! We appreciated it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants