cannot copy file into testcontainer
Asked Answered
N

1

0

I'm trying to use oracle testcontainer for java integration test. It's booting up, I can issue select 1 from dual;. But. I need to create custom table space before firing up liquibase. oracle-xe testcontainer suggest to copy init file into it, and it was also discussed here, and it is allegedly working for someone. But neither copy method is working for me.

So I have this code:

@Container
private static final OracleContainer oracleContainer = new OracleContainer("gvenzl/oracle-xe:21-slim-faststart")
    .withDatabaseName("testDB")
    .withUsername(DB_USER_NAME)
    .withPassword(DB_USER_PASSWORD)
    //            .withInitScript("initPriorToLiquibaseRun.sql") //will not work for privileged stuff
    .withCopyFileToContainer(MountableFile.forClasspathResource(    //will not copy a thing
                    "testcontainers/oracle/initPriorToLiquibaseRun.sql"),
            "/container-entrypoint-startdb.d/init1.sql")
    .withStartupTimeout(Duration.ofMinutes(10));

inside of init script I tried to create table or something non-problematic to confirm, that it's actually working, but later on I just threw Thread.sleep to have chance to look into running container and the file on given path does not exist. question 1: I failed to find any mention how to not delete container after run or even reuse it (.withReuse(true) did not work for me). Is it supported? ~ But no error message, nothing. File not created. question 2: I'd like to see logs, and IIUC this is the recommended way to use testcontainers in JUnit5 tests, but I cannot see/find any way how to attach loggers or do any other stuff requiring running container. And I'd like to do that as I saw somewhere complaint, that withCopyFileToContainer does now work, but copyFileToContainer does.

But to carry on, I went with manual testcontainer lifecycle management. So now I have code:

private static OracleContainer oracleContainer = null;

@BeforeAll
static void beforeAll() {
    Logger tcLogger = LoggerFactory.getLogger("ORACLE-TESTCONTAINER");
    Slf4jLogConsumer logConsumer = new Slf4jLogConsumer(tcLogger);

    oracleContainer = new OracleContainer("gvenzl/oracle-xe:21-slim-faststart")
            .withDatabaseName("testDB")
            .withUsername(DB_USER_NAME)
            .withPassword(DB_USER_PASSWORD);

    oracleContainer.start();

    oracleContainer.followOutput(logConsumer);
 oracleContainer.copyFileToContainer(MountableFile.forClasspathResource("testcontainers/oracle/initPriorToLiquibaseRun.sql"), "/container-entrypoint-startdb.d/init1.sql");    
}

@AfterAll
static void afterAll() {
    if (oracleContainer != null) {
        oracleContainer.stop();
    }
}

Now I see the logs, great, but file is not copied again, no complaint in logs about bad path or something (I could understand).

I saw somewhere another complain, that it can be due to privileges, but file is 644 owned by me, all directories has at least o+x, and custom created user can print it. So this cannot be it.

File is on classpath, and even if I replace it with:

oracleContainer.copyFileToContainer(MountableFile.forHostPath("/home/myActuallAbsolutePath/initPriorToLiquibaseRun.sql"), "/container-entrypoint-startdb.d/init1.sql");

copying to root path also does not work, if missing directory would be a problem.

What could it be? What am I doing wrong?

Nydia answered 20/10, 2023 at 8:59 Comment(2)
I think you need to use /container-entrypoint-initdb.d insteadWestwardly
yes, you are correct, that was another mistake I overlook when reading documentation/examples. Thanks!Nydia
N
0

I apologize, I'm just starting with all of these. Multiple errors were in this case:

  • I was looking for directory at wrong place; I expected to be in / after logging into docker container, but I wasn't
  • the init sql contained error, which I only noticed when running test container directly from bash
  • the .withDatabaseName("testDB") and ALTER SESSION SET CONTAINER=testDB; in init scripts weren't in sync.

and maybe more...

Nydia answered 20/10, 2023 at 15:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.