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

Testcontainers for Java: Add showcase examples for different scenarios #54

Merged
merged 7 commits into from
Apr 14, 2023

Conversation

amotl
Copy link
Member

@amotl amotl commented Apr 6, 2023

About

Foundation for a potential tutorial about how to use Testcontainers for Java with CrateDB, and for re-using blueprints at, for example, crate/crate-jdbc#377.

=> README -- direct view

References

Details

The patch exercises different features of Testcontainers for Java with a basic query SELECT * FROM sys.summits LIMIT 3;.

  • TestClassScope: Class-scoped testcontainer instance with JUnit 4 @Rule/@ClassRule integration.
  • TestFunctionScope: Function-scoped testcontainer instance with JUnit 4 @Rule/@ClassRule integration.
  • TestJdbcUrlScheme: Database containers launched via Testcontainers "TC" JDBC URL scheme.
  • TestManual: Function-scoped testcontainer instance with manual setup/teardown.
  • TestSharedSingleton: Testcontainer instance shared across multiple test classes 1, implemented using the Singleton pattern.
  • TestSharedSingletonEnvironmentVersion: Testcontainer instance honoring the CRATEDB_VERSION environment variable, suitable for running a test matrix on different versions of CrateDB, shared across multiple test classes.

Backlog

Startup

Database provisioning

Other test frameworks

Testcontainers for Java also provides integrations for other test frameworks. Currently, all test cases are based on JUnit 4.

Footnotes

  1. Sometimes it might be useful to define a container that is only started once for several test classes. There is no special support for this use case provided by the Testcontainers extension. Instead, this can be implemented using the Singleton pattern. See also Manual container lifecycle control » Singleton containers.

@amotl amotl force-pushed the amo/testing-testcontainers-java branch from b3224c9 to a6287c4 Compare April 6, 2023 13:51
Copy link

@BaurzhanSakhariev BaurzhanSakhariev left a comment

Choose a reason for hiding this comment

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

Cool, thanks!

Left some minor comments but please wait for comments from others

Copy link
Contributor

@matriv matriv left a comment

Choose a reason for hiding this comment

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

Thx @amotl !
Left some first comments, but I will also checkout the branch and give it a try locally as well.

@amotl amotl force-pushed the amo/testing-testcontainers-java branch 3 times, most recently from 27a1fa4 to 67c7050 Compare April 6, 2023 16:20
@amotl amotl requested a review from matriv April 6, 2023 16:22
@amotl amotl force-pushed the amo/testing-testcontainers-java branch from 67c7050 to 5178c77 Compare April 6, 2023 16:37
amotl added 2 commits April 6, 2023 18:42
Wrap connection into the try-with-resources so that it closes Statement
and result set, and will get closed itself at the end.
@amotl amotl force-pushed the amo/testing-testcontainers-java branch from 5178c77 to 310f397 Compare April 6, 2023 16:42
@matriv
Copy link
Contributor

matriv commented Apr 7, 2023

@amotl I've pushed several changes/improvements, please take a look. (apologies, but It was easier than providing comments).

I've also added TestManualWithLegacyCrateJdbcDriver which shows how to use the legacy crate jdbc driver with testcontainers.

@matriv matriv requested a review from BaurzhanSakhariev April 7, 2023 12:25
Copy link
Member Author

@amotl amotl left a comment

Choose a reason for hiding this comment

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

@matriv: Thanks for giving this some more love. I've added a few remarks to acknowledge my learning from your adjustments, and further suggestions for improvements.


# Start CrateDB.
docker run -it --rm --publish=4200:4200 --publish=5432:5432 \
crate:latest -Cdiscovery.type=single-node
Copy link
Contributor

Choose a reason for hiding this comment

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

Did I miss something or the discovery-type option is not passed to the JdbcDatabaseContainer ?

Copy link
Member Author

@amotl amotl Apr 12, 2023

Choose a reason for hiding this comment

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

Thanks for bringing this up.

In this particular case, outlining basic examples only, CrateDB will also happily start without any options, right? So it would be acceptable to omit the -Cdiscovery.type=single-node option?

However, propagating command-line options to CrateDB will become important when aiming to use this in a multi-node scenario like what crate-java-testing provides per CrateTestCluster 1, right? Did you already take this into account, @matriv, or would this be up to a second iteration?

Footnotes

  1. See also https://github.com/crate/crate-jdbc/issues/377.

Copy link
Contributor

Choose a reason for hiding this comment

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

  1. I haven't tried testcontainers in multi-node setup, don't even know if that's supported by their existing infrastructure.
  2. This test, doesn't have to do with testcontainers, the script starts a docker container manually and then runs the tests, connecting to the bound port of the container.

Copy link
Contributor

@hlcianfagna hlcianfagna Apr 13, 2023

Choose a reason for hiding this comment

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

CrateDB will also happily start without any options, right?

That is correct, it goes ahead with a "best-effort cluster bootstrapping" and it works.

@amotl
Copy link
Member Author

amotl commented Apr 14, 2023

Hi. I've just added a final commit per 32fb516, effectively carrying over the summary from the pull request description into the "What's inside" section of the README. Can you have another look at this? Thanks already!

P.S.: The backlog items also outlined within the PR description have been converged into GH-55, so this would be ready for merging if you agree 1. Thanks.

Footnotes

  1. As you've all already acknowledged the PR, just leave a 👍 on this comment if you do.

@amotl amotl mentioned this pull request Apr 14, 2023
1 task
Copy link
Contributor

@matriv matriv left a comment

Choose a reason for hiding this comment

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

thank you!

@amotl amotl merged commit 9c79faa into main Apr 14, 2023
@amotl amotl deleted the amo/testing-testcontainers-java branch April 14, 2023 13:05
@amotl
Copy link
Member Author

amotl commented Apr 14, 2023

Merged. Thanks!

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.

4 participants