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

enable copyFileToContainer feature during container startup #742

Conversation

dharanikesav
Copy link
Contributor

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.

@dharanikesav dharanikesav force-pushed the enable-copy-files-to-container-during-startup branch 2 times, most recently from 259e12d to b82cbab Compare June 11, 2018 09:35
Copy link
Member

@kiview kiview left a 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");
Copy link
Member

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);
Copy link
Member

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) {
Copy link
Member

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.

@dharanikesav dharanikesav force-pushed the enable-copy-files-to-container-during-startup branch 6 times, most recently from a12aeae to 5ea4c94 Compare June 13, 2018 12:00
@@ -0,0 +1 @@
The contents of this folder is a workaround for issues described here: https://github.com/KostyaSha/docker-java-shade/issues/1
Copy link
Member

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

@dharanikesav dharanikesav force-pushed the enable-copy-files-to-container-during-startup branch from 5ea4c94 to 0b78a2e Compare June 14, 2018 10:27
Copy link
Member

@rnorth rnorth left a 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);
Copy link
Member

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?

Copy link
Member

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();
Copy link
Member

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")
Copy link
Member

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?

Copy link
Contributor Author

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

Copy link
Member

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();
Copy link
Member

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.

Copy link
Member

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 :(

Copy link
Member

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.

Copy link
Contributor Author

@dharanikesav dharanikesav Jun 18, 2018

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

Copy link
Member

@bsideup bsideup Jun 18, 2018

Choose a reason for hiding this comment

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

Sure, see here:

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()));
}
}

Copy link
Contributor Author

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

@dharanikesav dharanikesav force-pushed the enable-copy-files-to-container-during-startup branch from 0b78a2e to 871f47c Compare June 18, 2018 08:17
@dharanikesav
Copy link
Contributor Author

@kiview @bsideup I have done the changes requested. Is it ok to merge this branch?

try(
GenericContainer container = new GenericContainer("alpine:latest")
.withCommand("sleep","3000")
.withCopyFileToContainer(MountableFile.forHostPath(folderPath), containerPath)
Copy link
Member

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")

@dharanikesav dharanikesav force-pushed the enable-copy-files-to-container-during-startup branch from 871f47c to 15ebab0 Compare June 20, 2018 07:34
@dharanikesav
Copy link
Contributor Author

@rnorth @bsideup @kiview Incorporated all the changes requested.

</configuration>
Copy link
Member

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.

@kiview
Copy link
Member

kiview commented Jun 22, 2018

@rnorth Have your remarks been addressed?

@dharanikesav
Copy link
Contributor Author

@rnorth Please verify whether the changes are done as requested by you and share your view in merging this branch

@bsideup bsideup merged commit 22cab1f into testcontainers:master Jul 10, 2018
@bsideup
Copy link
Member

bsideup commented Jul 10, 2018

@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! 👍

@bsideup bsideup added this to the next milestone Jul 10, 2018
@dharanikesav dharanikesav deleted the enable-copy-files-to-container-during-startup branch July 18, 2018 08:28
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