testcontainers / testcontainers-scala

Docker containers for testing in scala

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Can't execute command inside MySQLContainer using execInContainer

maxpoulain opened this issue · comments

Hello,

I'm not sure it's really an issue as I just started to use TestContainers but I'm trying to execute a command to load dump in MySQL container using execInContainer as following:

class MetaStoreMySQLServiceTest extends AnyFunSuite with BeforeAndAfterAll with ForAllTestContainer{
  override val container: MySQLContainer = MySQLContainer(username = "test", password = "test", databaseName = "staging")
  override def beforeAll(): Unit = {
    container.start()
    container.copyFileToContainer(MountableFile.forClasspathResource("metastore.sql"), "/")
    val res = container.execInContainer("mysql", "-utest", "-ptest", "staging", "<metastore.sql")
    println(s"stdout: ${res.getStdout}")
    println(s"sterr: ${res.getStderr}")

and It's not working, I got this, that indicates that the command is not good:

stdout: mysql  Ver 14.14 Distrib 5.7.22, for Linux (x86_64) using  EditLine wrapper
Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Usage: mysql [OPTIONS] [database]
...

I also tried a lot of different things for this row: container.execInContainer("mysql", "-utest", "-ptest", "staging", "<metastore.sql") but none of them worked..

So I tried to run the exact same command inside the container and it's working as you can see :

root@04e5686c5d95:/# ls
bin   dev			  entrypoint.sh  home  lib64  metastore.sql  opt   root  sbin  sys  usr
boot  docker-entrypoint-initdb.d  etc		 lib   media  mnt	     proc  run	 srv   tmp  var
root@04e5686c5d95:/# mysql -utest -ptest staging <metastore.sql
mysql: [Warning] Using a password on the command line interface can be insecure.
root@04e5686c5d95:/# %

So is it a problem coming from my code or is it a problem coming from how the command is "translated" by execInContainer inside the container ?

Thanks for your help,

Maxime

Thank you for your help !

It's working so this is solved but now I have another problem when the script is executed I got the following error whereas in the shell it's working well...

Exception encountered when invoking run on a nested suite - Failed to execute database script from resource: [...]
<...>ScriptUtils$ScriptStatementFailedException: Script execution failed (file:src/test/resources/metastore.sql:2): LOCK TABLES `DBS` WRITE
	at org.testcontainers.jdbc.JdbcDatabaseDelegate.execute(JdbcDatabaseDelegate.java:49)
	at org.testcontainers.delegate.AbstractDatabaseDelegate.execute(AbstractDatabaseDelegate.java:34)
	at org.testcontainers.ext.ScriptUtils.executeDatabaseScript(ScriptUtils.java:362)
	... 26 more
Caused by: java.sql.SQLSyntaxErrorException: Table 'test.DBS' doesn't exist
	at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:120)
	at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:97)
	at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:122)
	at com.mysql.cj.jdbc.StatementImpl.executeInternal(StatementImpl.java:764)
	at com.mysql.cj.jdbc.StatementImpl.execute(StatementImpl.java:648)
	at org.testcontainers.jdbc.JdbcDatabaseDelegate.execute(JdbcDatabaseDelegate.java:42)
	... 28 more

Do you think it's a problem coming from my sql script ?

Sorry if my questions are not really relevant !

Thanks for your help,

Maxime

Yes, I think the problem is here Table 'test.DBS' doesn't exist