-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
enable copyFileToContainer feature during container startup #742
enable copyFileToContainer feature during container startup #742
Conversation
259e12d
to
b82cbab
Compare
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 you please add some test cases as well?
if (!isRunning()) { | ||
throw new IllegalStateException("copyFileToContainer can only be used while the Container is running"); | ||
if (!isRunning() && !isCreated()) { | ||
throw new IllegalStateException("opyFileToContainer can only be used with created / running 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.
Typo in exception message
* @param mountableFile a Mountable file with path of source file / folder on host machine | ||
* @param containerPath a destination path on conatiner to which the files / folders to be copied | ||
*/ | ||
void addCopyFileToContainer(MountableFile mountableFile, String containerPath); |
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.
We've decided to deprecate the non-fluent setters, so you can omit this method.
@@ -835,6 +838,21 @@ public SELF withWorkingDirectory(String workDir) { | |||
return self(); | |||
} | |||
|
|||
|
|||
@Override | |||
public void addCopyFileToContainer(MountableFile mountableFile, String containerPath) { |
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.
See above, you can remove this method.
a12aeae
to
5ea4c94
Compare
@@ -0,0 +1 @@ | |||
The contents of this folder is a workaround for issues described here: https://github.com/KostyaSha/docker-java-shade/issues/1 |
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 file (as well as other /out
files) should not be commited
5ea4c94
to
0b78a2e
Compare
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.
A few minor comments from me, but this looks like a good change to be making - thank you very much!
|
||
try { | ||
Boolean created = getCurrentContainerInfo().getState().getStatus().equalsIgnoreCase("Created"); | ||
return Boolean.TRUE.equals(created); |
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.
In the isRunning
method the result of getRunning()
is a nullable Boolean
, which is why we do the TRUE.equals
call in that method.
Here, equalsIgnoreCase
just returns a primitive boolean, and further, getStatus()
may return null.
As such, I think it would actually be safer to just replace these two lines with:
return "created".equalsIgnoreCase(getCurrentContainerInfo().getState().getStatus());
Please could we do that instead?
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.
Also, this method will return false
if the container is not just created but also started
|
||
Assert.assertTrue(filesList.contains(fileName)); | ||
} catch (Exception e) { | ||
e.printStackTrace(); |
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.
Please let the exception be thrown from the test method, as it could be important.
|
||
@Before | ||
public void before() { | ||
container = new GenericContainer("couchbase:latest") |
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 think we can get away with using a smaller image here (and save some startup time). Would an alpine
image be OK?
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.
we need container to be up and running to perform this test. Alpine container is exiting immediately after start. Because of this, i am unable to use alpine 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.
Use the alpine
image and provide a long running command (like sleep
), using withCommand()
on the container.
|
||
@Test | ||
public void checkFileCopied() { | ||
container.start(); |
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.
Do we need to split the instantiation/start of the container like this, or could the container just be a field with an @Rule
annotation for this test? I think I'd prefer the latter, as it's the more idiomatic way.
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.
actually, didn't we agree to use try-with-resources
style? Otherwise, if you have 5 rules in your test and run a single test all of them will start - that sucks :(
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.
Maybe it depends? This class has only one test method (and would have only one Rule), which is why I suggested this way. I'm very relaxed about it though; try-with-resources
would also be fine.
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.
What is this try-with-resources? could you please provide a reference to understand this
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.
Sure, see here:
testcontainers-java/core/src/test/java/org/testcontainers/junit/GenericContainerRuleTest.java
Lines 346 to 363 in 609f9a1
public void createContainerCmdHookTest() { | |
// Use random name to avoid the conflicts between the tests | |
String randomName = Base58.randomString(5); | |
try( | |
GenericContainer container = new GenericContainer<>("redis:3.0.2") | |
.withCommand("redis-server", "--help") | |
.withCreateContainerCmdModifier(cmd -> cmd.withName("overrideMe")) | |
// Preserves the order | |
.withCreateContainerCmdModifier(cmd -> cmd.withName(randomName)) | |
// Allows to override pre-configured values by GenericContainer | |
.withCreateContainerCmdModifier(cmd -> cmd.withCmd("redis-server", "--port", "6379")) | |
) { | |
container.start(); | |
assertEquals("Name is configured", "/" + randomName, container.getContainerInfo().getName()); | |
assertEquals("Command is configured", "[redis-server, --port, 6379]", Arrays.toString(container.getContainerInfo().getConfig().getCmd())); | |
} | |
} |
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.
@bsideup Thank you.. that helps
0b78a2e
to
871f47c
Compare
try( | ||
GenericContainer container = new GenericContainer("alpine:latest") | ||
.withCommand("sleep","3000") | ||
.withCopyFileToContainer(MountableFile.forHostPath(folderPath), containerPath) |
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.
please use MountableFile.forClasspathResource("/mappable-resource/test-resource.txt")
871f47c
to
15ebab0
Compare
</configuration> |
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 is of course an unnecessary change, but rest LGTM.
@rnorth Have your remarks been addressed? |
@rnorth Please verify whether the changes are done as requested by you and share your view in merging this branch |
@dharanikesav sorry for taking so long for the review! Richard is a bit busy, so I took an action to merge it :) Thanks for your contribution! A really good one! 👍 |
Currently, testcontainer is capable of copying files only after starting a container. But, we have a requirement to copy files to container after creation and then start the container. This code change is to enable copy files to container immediately after creation.