|
1 | 1 | package org.testcontainers.junit; |
2 | 2 |
|
| 3 | +import com.github.dockerjava.api.exception.NotFoundException; |
3 | 4 | import com.google.common.collect.ImmutableMap; |
| 5 | +import com.google.common.io.Files; |
4 | 6 | import com.google.common.util.concurrent.Uninterruptibles; |
5 | 7 | import com.mongodb.MongoClient; |
6 | 8 | import com.mongodb.client.MongoCollection; |
7 | 9 | import com.mongodb.client.MongoDatabase; |
8 | 10 | import com.rabbitmq.client.*; |
| 11 | +import org.apache.commons.io.FileUtils; |
9 | 12 | import org.bson.Document; |
10 | 13 | import org.junit.*; |
11 | 14 | import org.rnorth.ducttape.RetryCountExceededException; |
12 | 15 | import org.rnorth.ducttape.unreliables.Unreliables; |
13 | 16 | import org.testcontainers.containers.GenericContainer; |
14 | 17 | import org.testcontainers.utility.Base58; |
| 18 | +import org.testcontainers.utility.MountableFile; |
15 | 19 | import org.testcontainers.utility.TestEnvironment; |
16 | 20 |
|
17 | 21 | import java.io.*; |
@@ -330,6 +334,54 @@ public void createContainerCmdHookTest() { |
330 | 334 | } |
331 | 335 | } |
332 | 336 |
|
| 337 | + @Test |
| 338 | + public void copyToContainerTest() throws Exception { |
| 339 | + final File tempResultFolder = Files.createTempDir(); |
| 340 | + |
| 341 | + try (final GenericContainer alpineCopyToContainer = new GenericContainer("alpine:3.2") |
| 342 | + .withCommand("top")){ |
| 343 | + |
| 344 | + alpineCopyToContainer.start(); |
| 345 | + final MountableFile mountableFile = MountableFile.forClasspathResource("test_copy_to_container.txt"); |
| 346 | + alpineCopyToContainer.copyFileToContainer(mountableFile, "/home/"); |
| 347 | + alpineCopyToContainer.copyFileFromContainer("/home/test_copy_to_container.txt", |
| 348 | + tempResultFolder.getAbsolutePath() + "/test_copy_to_container.txt"); |
| 349 | + |
| 350 | + File expectedFile = new File(mountableFile.getResolvedPath()); |
| 351 | + File actualFile = new File(tempResultFolder.getAbsolutePath() + "/test_copy_to_container.txt"); |
| 352 | + assertTrue("Files aren't same ", FileUtils.contentEquals(expectedFile,actualFile)); |
| 353 | + } |
| 354 | + } |
| 355 | + |
| 356 | + @Test(expected = NotFoundException.class) |
| 357 | + public void copyFromContainerShouldFailBecauseNoFileTest() throws NotFoundException, IOException, InterruptedException { |
| 358 | + |
| 359 | + try (final GenericContainer alpineCopyToContainer = new GenericContainer("alpine:3.2") |
| 360 | + .withCommand("top")) { |
| 361 | + alpineCopyToContainer.start(); |
| 362 | + alpineCopyToContainer.copyFileFromContainer("/home/test.txt", "src/test/resources/copy-from/test.txt"); |
| 363 | + } |
| 364 | + } |
| 365 | + |
| 366 | + @Test |
| 367 | + public void shouldCopyFileFromContainerTest() throws IOException, InterruptedException { |
| 368 | + final File tempResultFolder = Files.createTempDir(); |
| 369 | + |
| 370 | + try (final GenericContainer alpineCopyToContainer = new GenericContainer("alpine:3.2") |
| 371 | + .withCommand("top")) { |
| 372 | + |
| 373 | + alpineCopyToContainer.start(); |
| 374 | + final MountableFile mountableFile = MountableFile.forClasspathResource("test_copy_to_container.txt"); |
| 375 | + alpineCopyToContainer.copyFileToContainer(mountableFile, "/home/"); |
| 376 | + alpineCopyToContainer.copyFileFromContainer("/home/test_copy_to_container.txt", |
| 377 | + tempResultFolder.getAbsolutePath() + "/test_copy_from_container.txt"); |
| 378 | + |
| 379 | + File expectedFile = new File(mountableFile.getResolvedPath()); |
| 380 | + File actualFile = new File(tempResultFolder.getAbsolutePath() + "/test_copy_from_container.txt"); |
| 381 | + assertTrue("Files aren't same ", FileUtils.contentEquals(expectedFile,actualFile)); |
| 382 | + } |
| 383 | + } |
| 384 | + |
333 | 385 | private BufferedReader getReaderForContainerPort80(GenericContainer container) { |
334 | 386 |
|
335 | 387 | return Unreliables.retryUntilSuccess(10, TimeUnit.SECONDS, () -> { |
|
0 commit comments