From 6f4aea46cb318e42df545bfdfb618aff9207d211 Mon Sep 17 00:00:00 2001 From: Rune Soerensen Date: Tue, 22 Oct 2024 00:02:53 -0400 Subject: [PATCH] Prefer bind mount semantics --- libcnb-test/src/container_config.rs | 17 ++++++++--------- libcnb-test/src/docker.rs | 28 ++++++++++++++-------------- libcnb-test/src/test_context.rs | 4 ++-- 3 files changed, 24 insertions(+), 25 deletions(-) diff --git a/libcnb-test/src/container_config.rs b/libcnb-test/src/container_config.rs index 712114c4..1b3ad169 100644 --- a/libcnb-test/src/container_config.rs +++ b/libcnb-test/src/container_config.rs @@ -32,7 +32,7 @@ pub struct ContainerConfig { pub(crate) command: Option>, pub(crate) env: HashMap, pub(crate) exposed_ports: HashSet, - pub(crate) volumes: HashMap, + pub(crate) bind_mounts: HashMap, } impl ContainerConfig { @@ -171,22 +171,21 @@ impl ContainerConfig { self } - /// Mounts a named volume `source` into the container `destination`. Useful for integration - /// tests that depend on persistent storage shared between container executions. + /// Mount a `source` file or directory on the host machine into the container `target`. Useful + /// for integration tests that depend on persistent storage shared between container executions. /// - /// See: [Docker CLI, Mount Volume](https://docs.docker.com/reference/cli/docker/container/run/#volume) + /// See: [Docker Engine: Bind Mounts](https://docs.docker.com/engine/storage/bind-mounts/) /// /// # Example /// ```no_run /// use libcnb_test::{BuildConfig, ContainerConfig, TestRunner}; - /// use std::path::PathBuf; /// /// TestRunner::default().build( /// BuildConfig::new("heroku/builder:22", "tests/fixtures/app"), /// |context| { /// // ... /// context.start_container( - /// ContainerConfig::new().volume(PathBuf::from("/shared/cache"), PathBuf::from("/workspace/cache")), + /// ContainerConfig::new().bind_mount("/shared/cache", "/workspace/cache"), /// |container| { /// // ... /// }, @@ -194,12 +193,12 @@ impl ContainerConfig { /// }, /// ); /// ``` - pub fn volume( + pub fn bind_mount( &mut self, source: impl Into, - destination: impl Into, + target: impl Into, ) -> &mut Self { - self.volumes.insert(source.into(), destination.into()); + self.bind_mounts.insert(source.into(), target.into()); self } diff --git a/libcnb-test/src/docker.rs b/libcnb-test/src/docker.rs index 992c3775..d1edf075 100644 --- a/libcnb-test/src/docker.rs +++ b/libcnb-test/src/docker.rs @@ -14,7 +14,7 @@ pub(crate) struct DockerRunCommand { image_name: String, platform: Option, remove: bool, - volumes: BTreeMap, + bind_mounts: BTreeMap, } impl DockerRunCommand { @@ -29,7 +29,7 @@ impl DockerRunCommand { image_name: image_name.into(), platform: None, remove: false, - volumes: BTreeMap::new(), + bind_mounts: BTreeMap::new(), } } @@ -71,8 +71,8 @@ impl DockerRunCommand { self } - pub(crate) fn volume>(&mut self, source: P, destination: P) -> &mut Self { - self.volumes.insert(source.into(), destination.into()); + pub(crate) fn bind_mount>(&mut self, source: P, target: P) -> &mut Self { + self.bind_mounts.insert(source.into(), target.into()); self } } @@ -106,13 +106,13 @@ impl From for Command { command.args(["--publish", &format!("127.0.0.1::{port}")]); } - for (source, destination) in &docker_run_command.volumes { + for (source, target) in &docker_run_command.bind_mounts { command.args([ - "--volume", + "--mount", &format!( - "{}:{}", + "type=bind,source={},target={}", source.to_string_lossy(), - destination.to_string_lossy() + target.to_string_lossy() ), ]); } @@ -334,8 +334,8 @@ mod tests { docker_run_command.expose_port(55555); docker_run_command.platform("linux/amd64"); docker_run_command.remove(true); - docker_run_command.volume(PathBuf::from("./test-cache"), PathBuf::from("/cache")); - docker_run_command.volume("foo", "/bar"); + docker_run_command.bind_mount(PathBuf::from("./test-cache"), PathBuf::from("/cache")); + docker_run_command.bind_mount("foo", "/bar"); let command: Command = docker_run_command.clone().into(); assert_eq!( @@ -358,10 +358,10 @@ mod tests { "127.0.0.1::12345", "--publish", "127.0.0.1::55555", - "--volume", - "./test-cache:/cache", - "--volume", - "foo:/bar", + "--mount", + "type=bind,source=./test-cache,target=/cache", + "--mount", + "type=bind,source=foo,target=/bar", "my-image", "echo", "hello", diff --git a/libcnb-test/src/test_context.rs b/libcnb-test/src/test_context.rs index 83d42615..2b981371 100644 --- a/libcnb-test/src/test_context.rs +++ b/libcnb-test/src/test_context.rs @@ -112,8 +112,8 @@ impl<'a> TestContext<'a> { docker_run_command.expose_port(*port); }); - config.volumes.iter().for_each(|(source, destination)| { - docker_run_command.volume(source, destination); + config.bind_mounts.iter().for_each(|(source, target)| { + docker_run_command.bind_mount(source, target); }); // We create the ContainerContext early to ensure the cleanup in ContainerContext::drop