Skip to content

Commit

Permalink
test
Browse files Browse the repository at this point in the history
Signed-off-by: ChengyuZhu6 <[email protected]>
  • Loading branch information
ChengyuZhu6 committed Dec 22, 2023
1 parent e849dc8 commit 7cef732
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 20 deletions.
43 changes: 24 additions & 19 deletions image-rs/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use std::fs::File;
use std::path::{Path, PathBuf};

use crate::snapshots::SnapshotType;
use crate::CC_IMAGE_WORK_DIR;
use crate::IMAGE_PULL_WORK_DIR;

const DEFAULT_WORK_DIR: &str = "/var/lib/image-rs/";

Expand Down Expand Up @@ -85,24 +85,10 @@ impl Default for ImageConfig {
// Construct a default instance of `ImageConfig`
fn default() -> ImageConfig {
let work_dir = PathBuf::from(
std::env::var(CC_IMAGE_WORK_DIR).unwrap_or_else(|_| DEFAULT_WORK_DIR.to_string()),
std::env::var(IMAGE_PULL_WORK_DIR).unwrap_or_else(|_| DEFAULT_WORK_DIR.to_string()),
);

ImageConfig {
work_dir,
#[cfg(feature = "snapshot-overlayfs")]
default_snapshot: SnapshotType::Overlay,
#[cfg(not(feature = "snapshot-overlayfs"))]
default_snapshot: SnapshotType::Unknown,
security_validate: false,
auth: false,
file_paths: Paths::default(),
max_concurrent_download: DEFAULT_MAX_CONCURRENT_DOWNLOAD,
#[cfg(feature = "nydus")]
nydus_config: Some(NydusConfig::default()),
#[cfg(not(feature = "nydus"))]
nydus_config: None,
}
ImageConfig:: with_work_dir(work_dir)
}
}

Expand Down Expand Up @@ -131,6 +117,25 @@ impl TryFrom<&Path> for ImageConfig {
}

impl ImageConfig {

// Create an instance of `ImageConfig` with a custom work directory
pub fn with_work_dir(work_dir: PathBuf) -> ImageConfig {
ImageConfig {
work_dir,
#[cfg(feature = "snapshot-overlayfs")]
default_snapshot: SnapshotType::Overlay,
#[cfg(not(feature = "snapshot-overlayfs"))]
default_snapshot: SnapshotType::Unknown,
security_validate: false,
auth: false,
file_paths: Paths::default(),
max_concurrent_download: DEFAULT_MAX_CONCURRENT_DOWNLOAD,
#[cfg(feature = "nydus")]
nydus_config: Some(NydusConfig::default()),
#[cfg(not(feature = "nydus"))]
nydus_config: None,
}
}
/// Validate the configuration object.
pub fn validate(&self) -> bool {
if let Some(nydus_cfg) = self.nydus_config.as_ref() {
Expand Down Expand Up @@ -307,7 +312,7 @@ mod tests {

#[test]
fn test_image_config() {
std::env::remove_var(CC_IMAGE_WORK_DIR);
std::env::remove_var(IMAGE_PULL_WORK_DIR);
let config = ImageConfig::default();
let work_dir = PathBuf::from(DEFAULT_WORK_DIR);

Expand All @@ -319,7 +324,7 @@ mod tests {
);

let env_work_dir = "/tmp";
std::env::set_var(CC_IMAGE_WORK_DIR, env_work_dir);
std::env::set_var(IMAGE_PULL_WORK_DIR, env_work_dir);
let config = ImageConfig::default();
let work_dir = PathBuf::from(env_work_dir);
assert_eq!(config.work_dir, work_dir);
Expand Down
2 changes: 1 addition & 1 deletion image-rs/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// SPDX-License-Identifier: Apache-2.0

/// Environment macro for `image-rs` work dir.
pub const CC_IMAGE_WORK_DIR: &str = "CC_IMAGE_WORK_DIR";
pub const IMAGE_PULL_WORK_DIR: &str = "IMAGE_PULL_WORK_DIR";

pub const ERR_BAD_UNCOMPRESSED_DIGEST: &str = "unsupported uncompressed digest format";

Expand Down

0 comments on commit 7cef732

Please sign in to comment.