diff --git a/lib/workflows/hypervisor_downtime.py b/lib/workflows/hypervisor_downtime.py index adee8df95..ee1ab2125 100644 --- a/lib/workflows/hypervisor_downtime.py +++ b/lib/workflows/hypervisor_downtime.py @@ -31,8 +31,10 @@ def get_number_of_hours(start_dt, end_time_str, duration): :raises ValueError: if the input string is invalid or contains negative values """ - end_time_str = end_time_str.strip() - duration = duration.strip() + if end_time_str is not None: + end_time_str = end_time_str.strip() + if duration is not None: + duration = duration.strip() if not end_time_str and not duration: raise ValueError("Input strings cannot be both empty at the same time") diff --git a/tests/lib/workflows/test_hypervisor_downtimes.py b/tests/lib/workflows/test_hypervisor_downtimes.py index beb035fa0..6db0fdc57 100644 --- a/tests/lib/workflows/test_hypervisor_downtimes.py +++ b/tests/lib/workflows/test_hypervisor_downtimes.py @@ -150,6 +150,18 @@ def test_both_single_space_raise_exception(): get_number_of_hours(start_dt, " ", " ") +def test_none_types_handled_too(): + """Test that both parameters as None in either field does not raise a ValueError""" + start_dt = datetime.datetime.strptime("2024-01-01 10:00", "%Y-%m-%d %H:%M").replace( + tzinfo=pytz.utc + ) + result = get_number_of_hours(start_dt, "2024-01-01 11:00", None) + assert result == 1 + + result = get_number_of_hours(start_dt, None, "1h") + assert result == 1 + + def test_both_multiple_spaces_raise_exception(): """Test that both parameters as multiple spaces raise ValueError""" start_dt = datetime.datetime.strptime("2024-01-01 10:00", "%Y-%m-%d %H:%M").replace(