diff --git a/image-rs/src/image.rs b/image-rs/src/image.rs index 809ac8184..59d009901 100644 --- a/image-rs/src/image.rs +++ b/image-rs/src/image.rs @@ -11,7 +11,7 @@ use oci_spec::image::{ImageConfiguration, Os}; use serde::Deserialize; use std::collections::{BTreeSet, HashMap}; use std::convert::TryFrom; -use std::path::Path; +use std::path::{Path, PathBuf}; use std::sync::Arc; use tokio::sync::Mutex; @@ -19,7 +19,7 @@ use tokio::sync::Mutex; use crate::bundle::{create_runtime_config, BUNDLE_ROOTFS}; use crate::config::{ImageConfig, CONFIGURATION_FILE_PATH}; use crate::decoder::Compression; -use crate::meta_store::{MetaStore, METAFILE}; +use crate::meta_store::{self, MetaStore, METAFILE}; use crate::pull::PullClient; use crate::snapshots::{SnapshotType, Snapshotter}; @@ -98,9 +98,24 @@ impl Default for ImageClient { // construct a default instance of `ImageClient` fn default() -> ImageClient { let config = ImageConfig::try_from(Path::new(CONFIGURATION_FILE_PATH)).unwrap_or_default(); - let meta_store = MetaStore::try_from(Path::new(METAFILE)).unwrap_or_default(); + let (meta_store, snapshots) = Self::init_meta_and_snapshots(&config); + + ImageClient { + config, + meta_store, + snapshots, + } + } +} - #[allow(unused_mut)] +impl ImageClient { + pub fn init_meta_and_snapshots( + config: &ImageConfig, + ) -> ( + Arc>, + HashMap>, + ) { + let meta_store = MetaStore::try_from(Path::new(METAFILE)).unwrap_or_default(); let mut snapshots = HashMap::new(); #[cfg(feature = "snapshot-overlayfs")] @@ -119,7 +134,6 @@ impl Default for ImageClient { Box::new(overlayfs) as Box, ); } - #[cfg(feature = "snapshot-unionfs")] { let occlum_unionfs_index = meta_store @@ -137,16 +151,19 @@ impl Default for ImageClient { Box::new(occlum_unionfs) as Box, ); } + (Arc::new(Mutex::new(meta_store)), snapshots) + } - ImageClient { + pub fn new(image_work_dir: PathBuf) -> Self { + let config = ImageConfig::new(image_work_dir); + let (meta_store, snapshots) = Self::init_meta_and_snapshots(&config); + Self { config, - meta_store: Arc::new(Mutex::new(meta_store)), + meta_store, snapshots, } } -} -impl ImageClient { /// pull_image pulls an image with optional auth info and decrypt config /// and store the pulled data under user defined work_dir/layers. /// It will return the image ID with prepeared bundle: a rootfs directory, @@ -512,7 +529,6 @@ mod tests { #[tokio::test] async fn test_pull_image() { let work_dir = tempfile::tempdir().unwrap(); - std::env::set_var("CC_IMAGE_WORK_DIR", work_dir.path()); // TODO test with more OCI image registries and fix broken registries. let oci_images = [ @@ -533,7 +549,7 @@ mod tests { // "releases-docker.jfrog.io/reg2/busybox:1.33.1" ]; - let mut image_client = ImageClient::default(); + let mut image_client = ImageClient::new(work_dir.path().to_path_buf()); for image in oci_images.iter() { let bundle_dir = tempfile::tempdir().unwrap(); @@ -559,7 +575,6 @@ mod tests { #[tokio::test] async fn test_nydus_image() { let work_dir = tempfile::tempdir().unwrap(); - std::env::set_var("CC_IMAGE_WORK_DIR", work_dir.path()); let nydus_images = [ "eci-nydus-registry.cn-hangzhou.cr.aliyuncs.com/v6/java:latest-test_nydus", @@ -567,7 +582,7 @@ mod tests { //"eci-nydus-registry.cn-hangzhou.cr.aliyuncs.com/test/python:latest_nydus", ]; - let mut image_client = ImageClient::default(); + let mut image_client = ImageClient::new(work_dir.path().to_path_buf()); for image in nydus_images.iter() { let bundle_dir = tempfile::tempdir().unwrap(); @@ -593,11 +608,10 @@ mod tests { #[tokio::test] async fn test_image_reuse() { let work_dir = tempfile::tempdir().unwrap(); - std::env::set_var("CC_IMAGE_WORK_DIR", work_dir.path()); let image = "mcr.microsoft.com/hello-world"; - let mut image_client = ImageClient::default(); + let mut image_client = ImageClient::new(work_dir.path().to_path_buf()); let bundle1_dir = tempfile::tempdir().unwrap(); if let Err(e) = image_client diff --git a/image-rs/tests/credential.rs b/image-rs/tests/credential.rs index e82ca6ee7..375f6e6d0 100644 --- a/image-rs/tests/credential.rs +++ b/image-rs/tests/credential.rs @@ -36,11 +36,10 @@ async fn test_use_credential(#[case] image_ref: &str, #[case] auth_file_uri: &st .expect("Delete configs failed."); let work_dir = tempfile::tempdir().unwrap(); - std::env::set_var("CC_IMAGE_WORK_DIR", work_dir.path()); // a new client for every pulling, avoid effection // of cache of old client. - let mut image_client = ImageClient::default(); + let mut image_client = ImageClient::ImageClient::new(work_dir.path().to_path_buf()); // enable container auth image_client.config.auth = true; diff --git a/image-rs/tests/image_decryption.rs b/image-rs/tests/image_decryption.rs index 51c8c1613..3c59d1a8d 100644 --- a/image-rs/tests/image_decryption.rs +++ b/image-rs/tests/image_decryption.rs @@ -43,7 +43,6 @@ async fn test_decrypt_layers(#[case] image: &str) { std::env::set_var("OCICRYPT_KEYPROVIDER_CONFIG", keyprovider_config); let work_dir = tempfile::tempdir().unwrap(); - std::env::set_var("CC_IMAGE_WORK_DIR", work_dir.path()); let bundle_dir = tempfile::tempdir().unwrap(); // clean former test files, which is needed to prevent @@ -51,7 +50,7 @@ async fn test_decrypt_layers(#[case] image: &str) { common::clean_configs() .await .expect("Delete configs failed."); - let mut image_client = ImageClient::default(); + let mut image_client = ImageClient::new(work_dir.path().to_path_buf()); if cfg!(feature = "snapshot-overlayfs") { image_client .pull_image(image, bundle_dir.path(), &None, &Some(common::AA_PARAMETER)) diff --git a/image-rs/tests/signature_verification.rs b/image-rs/tests/signature_verification.rs index 2aea36572..223c6bc51 100644 --- a/image-rs/tests/signature_verification.rs +++ b/image-rs/tests/signature_verification.rs @@ -172,11 +172,10 @@ async fn do_signature_verification_tests( // Init tempdirs let work_dir = tempfile::tempdir().unwrap(); - std::env::set_var("CC_IMAGE_WORK_DIR", work_dir.path()); // a new client for every pulling, avoid effection // of cache of old client. - let mut image_client = ImageClient::default(); + let mut image_client = ImageClient::new(work_dir.path().to_path_buf()); // enable signature verification image_client.config.security_validate = true;