Skip to content

Commit

Permalink
Merge pull request #2714 from egernst/watcher-fixup-backport
Browse files Browse the repository at this point in the history
stable-2.2 | watcher: ensure we create target mount point for storage
  • Loading branch information
fidencio authored Sep 24, 2021
2 parents e58fabf + d9b41fc commit ebc23df
Showing 1 changed file with 92 additions and 0 deletions.
92 changes: 92 additions & 0 deletions src/agent/src/watcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,19 @@ impl SandboxStorages {
let entry = Storage::new(storage)
.await
.with_context(|| "Failed to add storage")?;

// If the storage source is a directory, let's create the target mount point:
if entry.source_mount_point.as_path().is_dir() {
fs::create_dir_all(&entry.target_mount_point)
.await
.with_context(|| {
format!(
"Unable to mkdir all for {}",
entry.target_mount_point.display()
)
})?;
}

self.0.push(entry);
}

Expand Down Expand Up @@ -475,6 +488,85 @@ mod tests {
Ok((storage, src_path))
}

#[tokio::test]
async fn test_empty_sourcedir_check() {
//skip_if_not_root!();
let dir = tempfile::tempdir().expect("failed to create tempdir");

let logger = slog::Logger::root(slog::Discard, o!());

let src_path = dir.path().join("src");
let dest_path = dir.path().join("dest");
let src_filename = src_path.to_str().expect("failed to create src filename");
let dest_filename = dest_path.to_str().expect("failed to create dest filename");

std::fs::create_dir_all(src_filename).expect("failed to create path");

let storage = protos::Storage {
source: src_filename.to_string(),
mount_point: dest_filename.to_string(),
..Default::default()
};

let mut entries = SandboxStorages {
..Default::default()
};

entries
.add(std::iter::once(storage), &logger)
.await
.unwrap();

assert!(entries.check(&logger).await.is_ok());
assert_eq!(entries.0.len(), 1);

assert_eq!(std::fs::read_dir(src_path).unwrap().count(), 0);
assert_eq!(std::fs::read_dir(dest_path).unwrap().count(), 0);
assert_eq!(std::fs::read_dir(dir.path()).unwrap().count(), 2);
}

#[tokio::test]
async fn test_single_file_check() {
//skip_if_not_root!();
let dir = tempfile::tempdir().expect("failed to create tempdir");

let logger = slog::Logger::root(slog::Discard, o!());

let src_file_path = dir.path().join("src.txt");
let dest_file_path = dir.path().join("dest.txt");

let src_filename = src_file_path
.to_str()
.expect("failed to create src filename");
let dest_filename = dest_file_path
.to_str()
.expect("failed to create dest filename");

let storage = protos::Storage {
source: src_filename.to_string(),
mount_point: dest_filename.to_string(),
..Default::default()
};

//create file
fs::write(src_file_path, "original").unwrap();

let mut entries = SandboxStorages::default();

entries
.add(std::iter::once(storage), &logger)
.await
.unwrap();

assert!(entries.check(&logger).await.is_ok());
assert_eq!(entries.0.len(), 1);

// there should only be 2 files
assert_eq!(std::fs::read_dir(dir.path()).unwrap().count(), 2);

assert_eq!(fs::read_to_string(dest_file_path).unwrap(), "original");
}

#[tokio::test]
async fn test_watch_entries() {
skip_if_not_root!();
Expand Down

0 comments on commit ebc23df

Please sign in to comment.