-
-
Notifications
You must be signed in to change notification settings - Fork 203
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
fix(testcontainers): new port-forwarder container per test file execution #678
fix(testcontainers): new port-forwarder container per test file execution #678
Conversation
✅ Deploy Preview for testcontainers-node ready!
To edit notification comments on pull requests, go to your Netlify site configuration. |
Hey, thanks @LNSD to push this suggest, we will talk about it tomorrow. |
const portForwarderId = new RandomUuid().nextUuid(); | ||
const container = await new GenericContainer(SSHD_IMAGE) | ||
.withName(`testcontainers-port-forwarder-${reaper.sessionId}`) | ||
.withName(`testcontainers-port-forwarder-${portForwarderId}`) | ||
.withExposedPorts(containerPort) | ||
.withEnvironment({ PASSWORD: password }) | ||
.withCommand([ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Alternatively, if having a container name that starts with testcontainers-port-forwarder-
is not important, removing the .withName(...)
statement would have a similar effect: When the Container engine creates the container instance, it will give it a random name that won't clash with other existing containers.
const portForwarderId = new RandomUuid().nextUuid(); | |
const container = await new GenericContainer(SSHD_IMAGE) | |
.withName(`testcontainers-port-forwarder-${reaper.sessionId}`) | |
.withName(`testcontainers-port-forwarder-${portForwarderId}`) | |
.withExposedPorts(containerPort) | |
.withEnvironment({ PASSWORD: password }) | |
.withCommand([ | |
const container = await new GenericContainer(SSHD_IMAGE) | |
.withName(`testcontainers-port-forwarder-${reaper.sessionId}`) | |
.withExposedPorts(containerPort) | |
.withEnvironment({ PASSWORD: password }) | |
.withCommand([ |
Sure. This is, yet suboptimal (due to the multiple containers), a possible solution. IIRC, testcontainers-java does something similar (random name by docker). But, please, feel free to close this PR if you find a more suitable solution, or reach me via slack if you want to discuss anything) 🙂 |
Context
From Jest documentation:
Source: https://jestjs.io/docs/configuration#resetmodules-boolean
Since each test file gets its independent module registry (i.e., independent "static" JS module state), the
PortForwarderInstance.instance
singleton field isundefined
when a test file starts the execution. This causes thePortForwarderInstance
class to believe that no other port-forwarder container is running and tries to start a new one with the same name.Proposed solution
Note
This PR fixes the limitation found in #677
A sub-optimal but working solution is to spin up a port-forwarder container per test file with a unique random name. This avoids the container name clash issue reported in #677.