diff --git a/src/tape/inventory.rs b/src/tape/inventory.rs index 31e3626a..49bd2985 100644 --- a/src/tape/inventory.rs +++ b/src/tape/inventory.rs @@ -505,20 +505,27 @@ impl Inventory { Some(time) => time, }; - let max_use_time = match media_set_policy { - MediaSetPolicy::ContinueCurrent => { - match self.media_set_next_start_time(&set.uuid) { - Some(next_start_time) => next_start_time, - None => return i64::MAX, - } + let max_use_time = match self.media_set_next_start_time(&set.uuid) { + Some(next_start_time) => { + match media_set_policy { + MediaSetPolicy::AlwaysCreate => set_start_time, + _ => next_start_time, + } } - MediaSetPolicy::AlwaysCreate => { - set_start_time - } - MediaSetPolicy::CreateAt(ref event) => { - match compute_next_event(event, set_start_time, false) { - Ok(Some(next)) => next, - Ok(None) | Err(_) => return i64::MAX, + None => { + match media_set_policy { + MediaSetPolicy::ContinueCurrent => { + return i64::MAX; + } + MediaSetPolicy::AlwaysCreate => { + set_start_time + } + MediaSetPolicy::CreateAt(ref event) => { + match compute_next_event(event, set_start_time, false) { + Ok(Some(next)) => next, + Ok(None) | Err(_) => return i64::MAX, + } + } } } }; diff --git a/src/tape/test/compute_media_state.rs b/src/tape/test/compute_media_state.rs index 641448b1..7c6a5f86 100644 --- a/src/tape/test/compute_media_state.rs +++ b/src/tape/test/compute_media_state.rs @@ -84,3 +84,55 @@ fn test_compute_media_state() -> Result<(), Error> { Ok(()) } + +#[test] +fn test_media_expire_time() -> Result<(), Error> { + + let testdir = create_testdir("test_media_expire_time")?; + + let ctime = 0; + + let mut inventory = Inventory::load(&testdir)?; + + // tape0: single tape media set + let sl0 = MediaSetLabel::with_data("p1", Uuid::generate(), 0, ctime, None); + let tape0_uuid = inventory.generate_used_tape("tape0", sl0, 0); + + // tape1: single tape media set + let sl1 = MediaSetLabel::with_data("p1", Uuid::generate(), 0, ctime + 60, None); + let tape1_uuid = inventory.generate_used_tape("tape1", sl1, 0); + + // tape2: single tape media set + let sl2= MediaSetLabel::with_data("p1", Uuid::generate(), 0, ctime + 120, None); + let tape2_uuid = inventory.generate_used_tape("tape2", sl2, 0); + + let event = crate::tools::systemd::time::parse_calendar_event("*:0/2")?; + let span = crate::tools::systemd::time::parse_time_span("120 seconds")?; + + let pool = MediaPool::new( + "p1", + &testdir , + MediaSetPolicy::CreateAt(event), + RetentionPolicy::ProtectFor(span), + None, + None, + )?; + + assert_eq!(pool.lookup_media(&tape0_uuid)?.status(), &MediaStatus::Full); + assert_eq!(pool.lookup_media(&tape1_uuid)?.status(), &MediaStatus::Full); + assert_eq!(pool.lookup_media(&tape2_uuid)?.status(), &MediaStatus::Writable); + + assert_eq!(pool.media_is_expired(&pool.lookup_media(&tape0_uuid)?, 0), false); + assert_eq!(pool.media_is_expired(&pool.lookup_media(&tape0_uuid)?, 60), false); + assert_eq!(pool.media_is_expired(&pool.lookup_media(&tape0_uuid)?, 120), false); + assert_eq!(pool.media_is_expired(&pool.lookup_media(&tape0_uuid)?, 180), true); + + assert_eq!(pool.media_is_expired(&pool.lookup_media(&tape1_uuid)?, 0), false); + assert_eq!(pool.media_is_expired(&pool.lookup_media(&tape1_uuid)?, 60), false); + assert_eq!(pool.media_is_expired(&pool.lookup_media(&tape1_uuid)?, 120), false); + assert_eq!(pool.media_is_expired(&pool.lookup_media(&tape1_uuid)?, 180), false); + assert_eq!(pool.media_is_expired(&pool.lookup_media(&tape1_uuid)?, 190), false); + assert_eq!(pool.media_is_expired(&pool.lookup_media(&tape1_uuid)?, 240), true); + + Ok(()) +}