Skip to content

OBPIH-6387 Run MySQL in a testcontainer for integration tests#4644

Merged
ewaterman merged 2 commits intodevelopfrom
feature/OBPIH-6387-testcontainers-in-api-tests
Jun 1, 2024
Merged

OBPIH-6387 Run MySQL in a testcontainer for integration tests#4644
ewaterman merged 2 commits intodevelopfrom
feature/OBPIH-6387-testcontainers-in-api-tests

Conversation

@ewaterman
Copy link
Member

The bulk of the leg work to get testcontainers set up was done by Justin, this is just to actually hook it up.

There's two methods of wiring up the test database to use testcontainers. You can do it directly via jdbc url, or via annotations. I chose via url because it's the smallest change, it automatically picks up the db config we have set in application.yml, and it immediately applies to all integration tests without issue.

More info here: https://java.testcontainers.org/modules/databases/jdbc/

If we wanted to do it the annotation way, it'd look something like this:

@Testcontainers
class Test extends Specification {

    @Shared // Marking it shared allows us to preserve the database across tests.
    MySQLContainer mySQLContainer = new MySQLContainer(DockerImageName.parse("mysql:5.7"))
            .withLogConsumer(new Slf4jLogConsumer(logger))
            .withDatabaseName('openboxes')
            .withUsername('openboxes')
            .withPassword('openboxes')

    void setupSpec() {
        mySQLContainer.start()
        assert mySQLContainer.isCreated()
        assert mySQLContainer.isRunning()
    }

    void cleanupSpec() {
        mySQLContainer.stop()
    }
}

@ewaterman ewaterman requested a review from jmiranda May 29, 2024 22:17
@ewaterman
Copy link
Member Author

Once this is merged I'll update our documentation on test running to make it clear that by default this will run against a container and not your local db

@ewaterman ewaterman self-assigned this May 30, 2024
Copy link
Member

@jmiranda jmiranda left a comment

Choose a reason for hiding this comment

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

LGTM

username: "openboxes"
password: "openboxes"
url: jdbc:mysql://localhost:3306/openboxes?serverTimezone=UTC&useSSL=false
url: jdbc:tc:mysql:///openboxes?serverTimezone=UTC&useSSL=false # https://java.testcontainers.org/modules/databases/jdbc/
Copy link
Member

Choose a reason for hiding this comment

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

Copy link
Member

Choose a reason for hiding this comment

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

Copy link
Member

Choose a reason for hiding this comment

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

With the config I want to

  1. make it work out of the box without any changes
  2. use passthrough in build.gradle
  3. do something like this in application.yml
config:
    property: System.get("config.property", defaultValue)

I would prefer 1 or 2, and would like to stay away from 3 like the plague because it would require a code change to application.yml every time we add a new property.

@ewaterman ewaterman changed the title Run MySQL in a testcontainer for integration tests OBPIH-6387 Run MySQL in a testcontainer for integration tests May 30, 2024
@ewaterman
Copy link
Member Author

@jmiranda you were right the settings weren't getting applied. I added an override my.cnf file like you mentioned and that worked.

The default version is MySQL 57 so I changed it to use a specific version for clarity.

I think I'll be changing this shortly to use the annotation based config instead of just the jdbc connection url since that is far easier to make customizable for the scenario where we want to be able to swap around the db that we use, but I'll make a separate task for that one.

I'll be updating our testing docs to mention all this.

@ewaterman ewaterman merged commit d2f19ae into develop Jun 1, 2024
@ewaterman ewaterman deleted the feature/OBPIH-6387-testcontainers-in-api-tests branch June 1, 2024 15:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants