Skip to content

Commit

Permalink
docker.rs: add more tests
Browse files Browse the repository at this point in the history
Add simple tests to validate functionality.

Signed-off-by: Erdem Meydanli <[email protected]>
  • Loading branch information
meerd committed Mar 19, 2024
1 parent 619be52 commit 7218b4f
Showing 1 changed file with 61 additions and 23 deletions.
84 changes: 61 additions & 23 deletions enclave_build/src/docker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,27 +164,6 @@ impl DockerUtil {
}
}

async fn image_exists(&self) -> Result<bool, Error> {
/*
self.docker
.images()
.get(&self.docker_image)
.inspect()
.await
.is_ok()
*/
let result = self.docker2.inspect_image(&self.docker_image).await?;

let tag_matches = result
.repo_tags
.as_ref()
.unwrap()
.iter()
.any(|repo_tag| repo_tag == &self.docker_image);

Ok(tag_matches)
}

/// Pull the image, with the tag provided in constructor, from the Docker registry
pub fn pull_image(&self) -> Result<(), DockerError> {
let act = async {
Expand Down Expand Up @@ -451,14 +430,15 @@ fn write_config(config: Vec<String>) -> Result<NamedTempFile, DockerError> {
#[cfg(test)]
mod tests {
use super::*;
use std::io::Read;
use std::{env, io::Read};
use base64::engine::general_purpose;

/// Test extracted configuration is as expected
#[test]
fn test_config() {
let docker = DockerUtil::new(String::from("public.ecr.aws/aws-nitro-enclaves/hello:v1"));

let (cmd_file, env_file) = docker.load().unwrap();
let (cmd_file, env_file) = docker.unwrap().load().unwrap();
let mut cmd_file = File::open(cmd_file.path()).unwrap();
let mut env_file = File::open(env_file.path()).unwrap();

Expand All @@ -474,4 +454,62 @@ mod tests {
HELLO=Hello from the enclave side!\n"
);
}

#[test]
fn test_new() {
let docker = DockerUtil::new(String::from("alpine")).unwrap();
assert_eq!(docker.docker_image, "alpine:latest");
let docker = DockerUtil::new(String::from("nginx:1.19")).unwrap();
assert_eq!(docker.docker_image, "nginx:1.19");
}

#[test]
fn test_get_credentials() {
let test_user = "test_user";
let test_password = "test_password";
let auth = format!("{}:{}",test_user, test_password);
let encoded_auth = general_purpose::STANDARD.encode(auth);
let config = format!(r#"{{
"auths": {{
"https://public.ecr.aws/aws-nitro-enclaves/hello/v1/": {{
"auth": "{}"
}},
"https://registry.example.com": {{
"auth": "b3RoZXJfdXNlcjpvdGhlcl9wYXNzd29yZA=="
}}
}}
}}"#, encoded_auth);

// Create a temporary file
let mut temp_file = NamedTempFile::new().expect("Failed to create temporary file.");

// Write the config to the temporary file
write!(temp_file, "{}", config).expect("Failed to write to temporary file.");

// Set the DOCKER_CONFIG environment variable to point to the temporary file's path
let temp_file_path = temp_file.path().to_string_lossy().to_string();
env::set_var("DOCKER_CONFIG", temp_file_path);

let docker =
DockerUtil::new(String::from("public.ecr.aws/aws-nitro-enclaves/hello:v1")).unwrap();
let creds = docker.get_credentials().unwrap();
assert_eq!(creds.username, Some(test_user.to_string()));
assert_eq!(creds.password, Some(test_password.to_string()));

temp_file.close().unwrap();
}

#[test]
fn test_architecture() {
let docker =
DockerUtil::new(String::from("public.ecr.aws/aws-nitro-enclaves/hello:v1")).unwrap();
docker.pull_image().unwrap();
let arch = docker.architecture().unwrap();
assert_eq!(arch, "amd64");

let docker = DockerUtil::new(String::from("arm64v8/alpine")).unwrap();
docker.pull_image().unwrap();
let arch = docker.architecture().unwrap();
assert_eq!(arch, "arm64");
}
}

0 comments on commit 7218b4f

Please sign in to comment.