Skip to content

Commit

Permalink
Fix format
Browse files Browse the repository at this point in the history
  • Loading branch information
estigma88 committed Jun 14, 2024
1 parent 1a235be commit f63c77e
Show file tree
Hide file tree
Showing 9 changed files with 48 additions and 37 deletions.
2 changes: 1 addition & 1 deletion testcontainers/src/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ pub use self::{
containers::*,
image::{CmdWaitFor, ContainerState, ExecCommand, Image, ImageExt, WaitFor},
mounts::{AccessMode, Mount, MountType},
ports::{ExposedPort}
ports::ExposedPort,
};

mod image;
Expand Down
2 changes: 1 addition & 1 deletion testcontainers/src/core/containers/async_container.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use crate::{
macros,
network::Network,
ports::Ports,
ContainerState, ExecCommand, WaitFor, ExposedPort
ContainerState, ExecCommand, ExposedPort, WaitFor,
},
ContainerRequest, Image,
};
Expand Down
4 changes: 2 additions & 2 deletions testcontainers/src/core/containers/request.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use std::{borrow::Cow, collections::BTreeMap, net::IpAddr, time::Duration};

use crate::core::ports::ExposedPort;
use crate::{
core::{mounts::Mount, ContainerState, ExecCommand, WaitFor},
Image, TestcontainersError,
};
use crate::core::ports::{ExposedPort};

/// Represents a request to start a container, allowing customization of the container.
#[must_use]
Expand All @@ -31,7 +31,7 @@ pub struct ContainerRequest<I: Image> {
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct PortMapping {
pub local: u16,
pub internal: ExposedPort
pub internal: ExposedPort,
}

#[derive(parse_display::Display, Debug, Clone)]
Expand Down
29 changes: 20 additions & 9 deletions testcontainers/src/core/ports.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,16 @@ use std::{collections::HashMap, net::IpAddr, num::ParseIntError};

use bollard_stubs::models::{PortBinding, PortMap};

#[derive(parse_display::Display, parse_display::FromStr, Debug, Clone, Copy, Eq, PartialEq, Hash)]
#[derive(
parse_display::Display, parse_display::FromStr, Debug, Clone, Copy, Eq, PartialEq, Hash,
)]
pub enum ExposedPort {
#[display("{0}/tcp")]
Tcp(u16),
#[display("{0}/udp")]
Udp(u16),
#[display("{0}/sctp")]
Sctp(u16)
Sctp(u16),
}

#[derive(Debug, thiserror::Error)]
Expand Down Expand Up @@ -69,8 +71,7 @@ impl TryFrom<PortMap> for Ports {
let mut ipv6_mapping = HashMap::new();
for (internal, external) in ports {
// internal is of the form '8332/tcp', split off the protocol ...
let internal_port = internal.parse::<ExposedPort>()
.expect("Internal port");
let internal_port = internal.parse::<ExposedPort>().expect("Internal port");

// get the `HostPort` of each external port binding
for binding in external.into_iter().flatten() {
Expand Down Expand Up @@ -359,11 +360,21 @@ mod tests {
.unwrap_or_default();

let mut expected_ports = Ports::default();
expected_ports.ipv6_mapping.insert(ExposedPort::Tcp(8333), 49718);
expected_ports.ipv4_mapping.insert(ExposedPort::Sctp(8332), 33078);
expected_ports.ipv4_mapping.insert(ExposedPort::Tcp(18332), 33076);
expected_ports.ipv4_mapping.insert(ExposedPort::Tcp(8333), 33077);
expected_ports.ipv4_mapping.insert(ExposedPort::Udp(18333), 33075);
expected_ports
.ipv6_mapping
.insert(ExposedPort::Tcp(8333), 49718);
expected_ports
.ipv4_mapping
.insert(ExposedPort::Sctp(8332), 33078);
expected_ports
.ipv4_mapping
.insert(ExposedPort::Tcp(18332), 33076);
expected_ports
.ipv4_mapping
.insert(ExposedPort::Tcp(8333), 33077);
expected_ports
.ipv4_mapping
.insert(ExposedPort::Udp(18333), 33075);

assert_eq!(parsed_ports, expected_ports)
}
Expand Down
2 changes: 1 addition & 1 deletion testcontainers/src/images/generic.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::core::ports::ExposedPort;
use crate::{core::WaitFor, Image};
use crate::core::ports::{ExposedPort};

#[must_use]
#[derive(Debug, Clone)]
Expand Down
36 changes: 16 additions & 20 deletions testcontainers/src/runners/async_runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,9 +137,7 @@ where
if !is_container_networked {
let mapped_ports = runnable_image
.ports()
.map(|ports| ports.iter()
.map(|p| p.internal)
.collect::<Vec<_>>())
.map(|ports| ports.iter().map(|p| p.internal).collect::<Vec<_>>())
.unwrap_or_default();

let ports_to_expose = runnable_image
Expand Down Expand Up @@ -262,7 +260,11 @@ impl From<CgroupnsMode> for HostConfigCgroupnsModeEnum {
#[cfg(test)]
mod tests {
use super::*;
use crate::{core::{WaitFor, ExposedPort}, images::generic::GenericImage, ImageExt};
use crate::{
core::{ExposedPort, WaitFor},
images::generic::GenericImage,
ImageExt,
};

#[tokio::test]
async fn async_run_command_should_expose_all_ports_if_no_explicit_mapping_requested(
Expand Down Expand Up @@ -294,8 +296,6 @@ mod tests {
Ok(())
}



#[tokio::test]
async fn async_run_command_should_map_exposed_port_udp_sctp() -> anyhow::Result<()> {
let client = Client::lazy_client().await?;
Expand All @@ -311,8 +311,12 @@ mod tests {
.with_exposed_port(ExposedPort::Sctp(sctp_port));

let container = generic_server.start().await?;
container.get_host_port_ipv4(ExposedPort::Udp(udp_port)).await?;
container.get_host_port_ipv4(ExposedPort::Sctp(sctp_port)).await?;
container
.get_host_port_ipv4(ExposedPort::Udp(udp_port))
.await?;
container
.get_host_port_ipv4(ExposedPort::Sctp(sctp_port))
.await?;

let container_details = client.inspect(container.id()).await?;

Expand All @@ -322,8 +326,7 @@ mod tests {
.ports
.expect("ports");

let mut current_ports = current_ports_map.keys()
.collect::<Vec<&String>>();
let mut current_ports = current_ports_map.keys().collect::<Vec<&String>>();

current_ports.sort();

Expand All @@ -337,10 +340,7 @@ mod tests {
expected_ports.push(sctp_expected_port);
expected_ports.push(tcp_expected_port);

assert_eq!(
current_ports,
expected_ports
);
assert_eq!(current_ports, expected_ports);

Ok(())
}
Expand Down Expand Up @@ -396,8 +396,7 @@ mod tests {
.port_bindings
.expect("ports");

let mut current_ports = current_ports_map.keys()
.collect::<Vec<&String>>();
let mut current_ports = current_ports_map.keys().collect::<Vec<&String>>();

current_ports.sort();

Expand All @@ -409,10 +408,7 @@ mod tests {
expected_ports.push(udp_expected_port);
expected_ports.push(sctp_expected_port);

assert_eq!(
current_ports,
expected_ports
);
assert_eq!(current_ports, expected_ports);

Ok(())
}
Expand Down
2 changes: 1 addition & 1 deletion testcontainers/src/runners/sync_runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ mod tests {

use super::*;
use crate::{
core::{client::Client, mounts::Mount, WaitFor, ExposedPort},
core::{client::Client, mounts::Mount, ExposedPort, WaitFor},
images::generic::GenericImage,
ImageExt,
};
Expand Down
6 changes: 5 additions & 1 deletion testcontainers/tests/dual_stack_host_ports.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@

use std::net::{Ipv6Addr, TcpListener};

use testcontainers::{core::{WaitFor, ExposedPort}, runners::SyncRunner, GenericImage};
use testcontainers::{
core::{ExposedPort, WaitFor},
runners::SyncRunner,
GenericImage,
};

/// Test the functionality of exposing container ports over both IPv4 and IPv6.
#[test]
Expand Down
2 changes: 1 addition & 1 deletion testcontainers/tests/sync_runner.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#![cfg(feature = "blocking")]

use testcontainers::{
core::{CmdWaitFor, ExecCommand, Host, WaitFor, ExposedPort},
core::{CmdWaitFor, ExecCommand, ExposedPort, Host, WaitFor},
runners::SyncRunner,
*,
};
Expand Down

0 comments on commit f63c77e

Please sign in to comment.