Skip to content

Commit

Permalink
Add test for the docker_client
Browse files Browse the repository at this point in the history
  • Loading branch information
rafaelfranca committed Mar 28, 2024
1 parent 8235904 commit 06a20cc
Show file tree
Hide file tree
Showing 3 changed files with 134 additions and 6 deletions.
128 changes: 128 additions & 0 deletions src/docker_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,3 +65,131 @@ impl DockerClient {
command
}
}

#[cfg(test)]
mod tests {
use super::*;
use std::{env::current_dir, ffi::OsStr};

#[test]
fn build_image() {
let command = DockerClient::build_image("3.2.3", "7.1.3", None, None);

assert_eq!(command.get_program(), "docker");

let args: Vec<&OsStr> = command.get_args().collect();

assert_eq!(
args,
&[
"build",
"--build-arg",
"RUBY_VERSION=3.2.3",
"--build-arg",
"RAILS_VERSION=7.1.3",
"-t",
"rails-new-3.2.3-7.1.3",
"-",
]
);
}

#[test]
fn build_image_with_user_id() {
let command = DockerClient::build_image("3.2.3", "7.1.3", Some(1000), None);

assert_eq!(command.get_program(), "docker");

let args: Vec<&OsStr> = command.get_args().collect();

assert_eq!(
args,
&[
"build",
"--build-arg",
"RUBY_VERSION=3.2.3",
"--build-arg",
"RAILS_VERSION=7.1.3",
"--build-arg",
"USER_ID=1000",
"-t",
"rails-new-3.2.3-7.1.3",
"-",
]
);
}

#[test]
fn build_image_with_group_id() {
let command = DockerClient::build_image("3.2.3", "7.1.3", None, Some(1000));

assert_eq!(command.get_program(), "docker");

let args: Vec<&OsStr> = command.get_args().collect();

assert_eq!(
args,
&[
"build",
"--build-arg",
"RUBY_VERSION=3.2.3",
"--build-arg",
"RAILS_VERSION=7.1.3",
"--build-arg",
"GROUP_ID=1000",
"-t",
"rails-new-3.2.3-7.1.3",
"-",
]
);
}

#[test]
fn run_image() {
let command = DockerClient::run_image("3.2.3", "7.1.3", vec!["my_app".to_string()]);

assert_eq!(command.get_program(), "docker");

let binding = current_dir().unwrap();
let current_dir = binding.to_str().unwrap();

let args: Vec<&OsStr> = command.get_args().collect();

assert_eq!(
args,
&[
"run",
"--rm",
"-v",
&format!("{}:{}", current_dir, current_dir),
"-w",
current_dir,
"rails-new-3.2.3-7.1.3",
"rails",
"new",
"my_app",
]
);
}

#[test]
fn get_help() {
let command = DockerClient::get_help("3.2.3", "7.1.3");

assert_eq!(command.get_program(), "docker");

let args: Vec<&OsStr> = command.get_args().collect();

assert_eq!(
args,
&[
"run",
"--rm",
"rails-new-3.2.3-7.1.3",
"rails",
"new",
"--help",
]
);
}
}
6 changes: 3 additions & 3 deletions src/unix.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
pub fn dockerfile_content() -> &'static [u8] {
include_bytes!("../Dockerfile")
include_bytes!("../Dockerfile")
}

pub fn get_user_id() -> Option<u32> {
Some(users::get_current_uid())
Some(users::get_current_uid())
}

pub fn get_group_id() -> Option<u32> {
Some(users::get_current_gid())
Some(users::get_current_gid())
}
6 changes: 3 additions & 3 deletions src/windows.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
pub fn dockerfile_content() -> &'static [u8] {
include_bytes!("../Dockerfile.windows")
include_bytes!("../Dockerfile.windows")
}

pub fn get_user_id() -> Option<u32> {
None
None
}

pub fn get_group_id() -> Option<u32> {
None
None
}

0 comments on commit 06a20cc

Please sign in to comment.