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

chore: get rid of outdated variables naming #679

Merged
merged 6 commits into from
Jul 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 34 additions & 34 deletions testcontainers/src/core/image/image_ext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ pub trait ImageExt<I: Image> {
///
/// assert!(overridden_cmd.cmd().eq(cmd));
///
/// let another_runnable_image = image.with_cmd(cmd);
/// let another_container_req = image.with_cmd(cmd);
///
/// assert!(another_runnable_image.cmd().eq(overridden_cmd.cmd()));
/// assert!(another_container_req.cmd().eq(overridden_cmd.cmd()));
/// ```
fn with_cmd(self, cmd: impl IntoIterator<Item = impl Into<String>>) -> ContainerRequest<I>;

Expand Down Expand Up @@ -86,43 +86,43 @@ pub trait ImageExt<I: Image> {
/// Implements the [`ImageExt`] trait for the every type that can be converted into a [`ContainerRequest`].
impl<RI: Into<ContainerRequest<I>>, I: Image> ImageExt<I> for RI {
fn with_cmd(self, cmd: impl IntoIterator<Item = impl Into<String>>) -> ContainerRequest<I> {
let runnable = self.into();
let container_req = self.into();
ContainerRequest {
overridden_cmd: cmd.into_iter().map(Into::into).collect(),
..runnable
..container_req
}
}

fn with_name(self, name: impl Into<String>) -> ContainerRequest<I> {
let runnable = self.into();
let container_req = self.into();
ContainerRequest {
image_name: Some(name.into()),
..runnable
..container_req
}
}

fn with_tag(self, tag: impl Into<String>) -> ContainerRequest<I> {
let runnable = self.into();
let container_req = self.into();
ContainerRequest {
image_tag: Some(tag.into()),
..runnable
..container_req
}
}

fn with_container_name(self, name: impl Into<String>) -> ContainerRequest<I> {
let runnable = self.into();
let container_req = self.into();

ContainerRequest {
container_name: Some(name.into()),
..runnable
..container_req
}
}

fn with_network(self, network: impl Into<String>) -> ContainerRequest<I> {
let runnable = self.into();
let container_req = self.into();
ContainerRequest {
network: Some(network.into()),
..runnable
..container_req
}
}

Expand All @@ -131,75 +131,75 @@ impl<RI: Into<ContainerRequest<I>>, I: Image> ImageExt<I> for RI {
name: impl Into<String>,
value: impl Into<String>,
) -> ContainerRequest<I> {
let mut runnable = self.into();
runnable.env_vars.insert(name.into(), value.into());
runnable
let mut container_req = self.into();
container_req.env_vars.insert(name.into(), value.into());
container_req
}

fn with_host(self, key: impl Into<String>, value: impl Into<Host>) -> ContainerRequest<I> {
let mut runnable = self.into();
runnable.hosts.insert(key.into(), value.into());
runnable
let mut container_req = self.into();
container_req.hosts.insert(key.into(), value.into());
container_req
}

fn with_mount(self, mount: impl Into<Mount>) -> ContainerRequest<I> {
let mut runnable = self.into();
runnable.mounts.push(mount.into());
runnable
let mut container_req = self.into();
container_req.mounts.push(mount.into());
container_req
}

fn with_mapped_port(
self,
host_port: u16,
container_port: ContainerPort,
) -> ContainerRequest<I> {
let runnable = self.into();
let mut ports = runnable.ports.unwrap_or_default();
let container_req = self.into();
let mut ports = container_req.ports.unwrap_or_default();
ports.push(PortMapping::new(host_port, container_port));

ContainerRequest {
ports: Some(ports),
..runnable
..container_req
}
}

fn with_privileged(self, privileged: bool) -> ContainerRequest<I> {
let runnable = self.into();
let container_req = self.into();
ContainerRequest {
privileged,
..runnable
..container_req
}
}

fn with_cgroupns_mode(self, cgroupns_mode: CgroupnsMode) -> ContainerRequest<I> {
let runnable = self.into();
let container_req = self.into();
ContainerRequest {
cgroupns_mode: Some(cgroupns_mode),
..runnable
..container_req
}
}

fn with_userns_mode(self, userns_mode: &str) -> ContainerRequest<I> {
let runnable = self.into();
let container_req = self.into();
ContainerRequest {
userns_mode: Some(String::from(userns_mode)),
..runnable
..container_req
}
}

fn with_shm_size(self, bytes: u64) -> ContainerRequest<I> {
let runnable = self.into();
let container_req = self.into();
ContainerRequest {
shm_size: Some(bytes),
..runnable
..container_req
}
}

fn with_startup_timeout(self, timeout: Duration) -> ContainerRequest<I> {
let runnable = self.into();
let container_req = self.into();
ContainerRequest {
startup_timeout: Some(timeout),
..runnable
..container_req
}
}
}
48 changes: 24 additions & 24 deletions testcontainers/src/runners/async_runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,42 +52,42 @@ where
I: Image,
{
async fn start(self) -> Result<ContainerAsync<I>> {
let runnable_image = self.into();
let startup_timeout = runnable_image
let container_req = self.into();
let startup_timeout = container_req
.startup_timeout()
.unwrap_or(DEFAULT_STARTUP_TIMEOUT);

tokio::time::timeout(startup_timeout, async {
let client = Client::lazy_client().await?;
let mut create_options: Option<CreateContainerOptions<String>> = None;

let extra_hosts: Vec<_> = runnable_image
let extra_hosts: Vec<_> = container_req
.hosts()
.map(|(key, value)| format!("{key}:{value}"))
.collect();

let mut config: Config<String> = Config {
image: Some(runnable_image.descriptor()),
image: Some(container_req.descriptor()),
host_config: Some(HostConfig {
privileged: Some(runnable_image.privileged()),
privileged: Some(container_req.privileged()),
extra_hosts: Some(extra_hosts),
cgroupns_mode: runnable_image.cgroupns_mode().map(|mode| mode.into()),
userns_mode: runnable_image.userns_mode().map(|v| v.to_string()),
cgroupns_mode: container_req.cgroupns_mode().map(|mode| mode.into()),
userns_mode: container_req.userns_mode().map(|v| v.to_string()),
..Default::default()
}),
..Default::default()
};

// shared memory
if let Some(bytes) = runnable_image.shm_size() {
if let Some(bytes) = container_req.shm_size() {
config.host_config = config.host_config.map(|mut host_config| {
host_config.shm_size = Some(bytes as i64);
host_config
});
}

// create network and add it to container creation
let network = if let Some(network) = runnable_image.network() {
let network = if let Some(network) = container_req.network() {
config.host_config = config.host_config.map(|mut host_config| {
host_config.network_mode = Some(network.to_string());
host_config
Expand All @@ -98,22 +98,22 @@ where
};

// name of the container
if let Some(name) = runnable_image.container_name() {
if let Some(name) = container_req.container_name() {
create_options = Some(CreateContainerOptions {
name: name.to_owned(),
platform: None,
})
}

// handle environment variables
let envs: Vec<String> = runnable_image
let envs: Vec<String> = container_req
.env_vars()
.map(|(k, v)| format!("{k}={v}"))
.collect();
config.env = Some(envs);

// mounts and volumes
let mounts: Vec<_> = runnable_image.mounts().map(Into::into).collect();
let mounts: Vec<_> = container_req.mounts().map(Into::into).collect();

if !mounts.is_empty() {
config.host_config = config.host_config.map(|mut host_config| {
Expand All @@ -123,24 +123,24 @@ where
}

// entrypoint
if let Some(entrypoint) = runnable_image.entrypoint() {
if let Some(entrypoint) = container_req.entrypoint() {
config.entrypoint = Some(vec![entrypoint.to_string()]);
}

let is_container_networked = runnable_image
let is_container_networked = container_req
.network()
.as_ref()
.map(|network| network.starts_with("container:"))
.unwrap_or(false);

// expose ports
if !is_container_networked {
let mapped_ports = runnable_image
let mapped_ports = container_req
.ports()
.map(|ports| ports.iter().map(|p| p.container_port).collect::<Vec<_>>())
.unwrap_or_default();

let ports_to_expose = runnable_image
let ports_to_expose = container_req
.expose_ports()
.iter()
.copied()
Expand All @@ -153,9 +153,9 @@ where
}

// ports
if runnable_image.ports().is_some() {
if container_req.ports().is_some() {
let empty: Vec<_> = Vec::new();
let bindings = runnable_image.ports().unwrap_or(&empty).iter().map(|p| {
let bindings = container_req.ports().unwrap_or(&empty).iter().map(|p| {
(
format!("{}", p.container_port),
Some(vec![PortBinding {
Expand All @@ -176,7 +176,7 @@ where
});
}

let cmd: Vec<_> = runnable_image.cmd().map(|v| v.to_string()).collect();
let cmd: Vec<_> = container_req.cmd().map(|v| v.to_string()).collect();
if !cmd.is_empty() {
config.cmd = Some(cmd);
}
Expand All @@ -192,7 +192,7 @@ where
status_code: 404, ..
},
)) => {
client.pull_image(&runnable_image.descriptor()).await?;
client.pull_image(&container_req.descriptor()).await?;
client.create_container(create_options, config).await
}
res => res,
Expand All @@ -206,7 +206,7 @@ where
client.start_container(&container_id).await?;

let container =
ContainerAsync::new(container_id, client.clone(), runnable_image, network).await?;
ContainerAsync::new(container_id, client.clone(), container_req, network).await?;

let state = ContainerState::new(container.id(), container.ports().await?);
for cmd in container.image().exec_after_start(state)? {
Expand All @@ -220,11 +220,11 @@ where
}

async fn pull_image(self) -> Result<ContainerRequest<I>> {
let runnable_image = self.into();
let container_req = self.into();
let client = Client::lazy_client().await?;
client.pull_image(&runnable_image.descriptor()).await?;
client.pull_image(&container_req.descriptor()).await?;

Ok(runnable_image)
Ok(container_req)
}
}

Expand Down
Loading