-
Notifications
You must be signed in to change notification settings - Fork 362
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
Conversation
Sorry, I missed my changes in the first commit. Happy to rejig the commits if the changes are accepted. |
There was a problem hiding this 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} |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
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()) |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
getOrNull()
?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
…nd safer retrieval of containerId in DockerCommitImage
|
||
${containerCommitImageExecutionTask} | ||
|
||
task removeImage(type: DockerRemoveImage) { |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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
.
result.output.contains("Commiting image for container") | ||
} | ||
|
||
def "cannot commit image without container"() { |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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') |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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') |
There was a problem hiding this comment.
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') |
There was a problem hiding this comment.
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.
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. |
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! |
@rroques Thanks again for your work! We appreciated it. |
Fixes Issue#716