-
Notifications
You must be signed in to change notification settings - Fork 141
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat!: support UDP and SCTP port mappings (#655)
Resolves #234 ### Migration guide A new way of defining exposed ports and port mappings is introduced to define the protocol, which could be Tcp, Udp or Sctp. Changes impact the following methods: - `GenericImage.with_exposed_port` - `GenericImage.with_mapped_port` - `Container.get_host_port_ipv4` - `Container.get_host_port_ipv6` - `ContainerAsync.get_host_port_ipv4` - `ContainerAsync.get_host_port_ipv6` #### Exposed ports Having the following container configuration, that defines a Redis container running on TCP 6379 port: ``` use testcontainers::{core::{WaitFor}, runners::AsyncRunner, GenericImage}; async fn test_redis() { let container = GenericImage::new("redis", "7.2.4") .with_exposed_port(6379) .with_wait_for(WaitFor::message_on_stdout("Ready to accept connections")) .start() .await; } ``` From now on, we should use `ExposedPort` enum instead, as follows: ``` use testcontainers::{core::{WaitFor, ExposedPort}, runners::AsyncRunner, GenericImage}; async fn test_redis() { let container = GenericImage::new("redis", "7.2.4") .with_exposed_port(ExposedPort::Tcp(6379)) .with_wait_for(WaitFor::message_on_stdout("Ready to accept connections")) .start() .await; } ``` Notice the `ExposedPort::Tcp(6379)`, where we explicitly say which protocol we want to map the port. Most of your use cases would use Tcp, like Redis container, but we recommend to check the containers documentation to be sure. #### Port mappings Having the following container configuration, that defines a Redis container running on TCP 6379 internal port, mapped to 1000 local port: ``` use testcontainers::{core::{WaitFor}, runners::AsyncRunner, GenericImage}; async fn test_redis() { let container = GenericImage::new("redis", "7.2.4") .with_mapped_port((1000, 6379)) .with_wait_for(WaitFor::message_on_stdout("Ready to accept connections")) .start() .await; } ``` From now on, we should use `ExposedPort` enum instead, as follows: ``` use testcontainers::{core::{WaitFor, ExposedPort}, runners::AsyncRunner, GenericImage}; async fn test_redis() { let container = GenericImage::new("redis", "7.2.4") .with_mapped_port((1000, ExposedPort::Tcp(6379))) .with_wait_for(WaitFor::message_on_stdout("Ready to accept connections")) .start() .await; } ``` Notice the `ExposedPort::Tcp(6379)`, where we explicitly say which protocol we want to map the port. Most of your use cases would use Tcp, like Redis container, but we recommend to check the containers documentation to be sure. If you are interested on getting the local port mapped to an internal port, before you use the following: ``` use testcontainers::{core::{WaitFor}, runners::AsyncRunner, GenericImage}; async fn test_redis() { let container = GenericImage::new("redis", "7.2.4") .with_mapped_port((1000, 6379)) .with_wait_for(WaitFor::message_on_stdout("Ready to accept connections")) .start() .await; let local_port = container.get_host_port_ipv4(6379).await?; } ``` From now on, you must specify the protocol, as follows: ``` use testcontainers::{core::{WaitFor}, runners::AsyncRunner, GenericImage}; async fn test_redis() { let container = GenericImage::new("redis", "7.2.4") .with_mapped_port((1000, ExposedPort::Tcp(6379))) .with_wait_for(WaitFor::message_on_stdout("Ready to accept connections")) .start() .await; let local_port = container.get_host_port_ipv4(ExposedPort::Tcp(6379)).await?; } ``` Notice the `ExposedPort::Tcp(6379)` in the `get_host_port_ipv4` invocation.
- Loading branch information
Showing
12 changed files
with
181 additions
and
63 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.