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

Replace shiplift with bollard for Docker 25+ compatibility #595

Merged
merged 7 commits into from
Apr 9, 2024
Merged
Changes from 1 commit
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
Prev Previous commit
Next Next commit
enclave_build: Add more tests
Add additional simple tests to docker.rs for better test coverage.

Signed-off-by: Erdem Meydanli <meydanli@amazon.com>
meerd committed Mar 29, 2024
commit 8624807d4ac956941f4039682f2a400b4f0d3085
73 changes: 71 additions & 2 deletions enclave_build/src/docker.rs
Original file line number Diff line number Diff line change
@@ -430,14 +430,15 @@ fn write_config(config: Vec<String>) -> Result<NamedTempFile, DockerError> {
#[cfg(test)]
mod tests {
use super::*;
use std::io::Read;
use base64::engine::general_purpose;
use std::{env, io::Read};

/// 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();

@@ -453,4 +454,72 @@ 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() {
#[cfg(target_arch = "x86_64")]
{
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");
}

#[cfg(target_arch = "aarch64")]
{
let docker = DockerUtil::new(String::from("arm64v8/alpine")).unwrap();
docker.pull_image().unwrap();
let arch = docker.architecture().unwrap();
assert_eq!(arch, "arm64");
}
}
}