diff --git a/caso/record.py b/caso/record.py index b259ea5..2f3f0f7 100644 --- a/caso/record.py +++ b/caso/record.py @@ -232,6 +232,11 @@ def ssm_message(self): # done. In order to get objects correctly serialized we need to convert to JSON, # then reload the model serialized_record = json.loads(self.model_dump_json(**opts)) + # CPU and Wall duration may be 0 as we are converting float to int when creating + # the record, here we impose at least 1 second to avoid reporting no cpu time + for f in ["CpuDuration", "WallDuration"]: + if f in serialized_record and serialized_record[f] == 0: + serialized_record[f] = 1 aux = [f"{k}: {v}" for k, v in serialized_record.items()] aux.sort() return "\n".join(aux) diff --git a/caso/tests/conftest.py b/caso/tests/conftest.py index ef59f3e..dd181eb 100644 --- a/caso/tests/conftest.py +++ b/caso/tests/conftest.py @@ -335,6 +335,15 @@ def valid_cloud_record() -> dict: return valid_cloud_records_dict[0] +@pytest.fixture() +def zero_cpu_cloud_record() -> caso.record.CloudRecord: + """Get a fixture for the CloudRecord with 0 cpu and wall time.""" + record = caso.record.CloudRecord(**valid_cloud_records_fields[0]) + record.cpu_duration = 0 + record.wall_duration = 0 + return record + + @pytest.fixture() def valid_cloud_records() -> typing.List[dict]: """Get a fixture for valid records as a dict.""" diff --git a/caso/tests/test_record.py b/caso/tests/test_record.py index f2ce9b3..711bd9b 100644 --- a/caso/tests/test_record.py +++ b/caso/tests/test_record.py @@ -88,6 +88,13 @@ def test_cloud_record_custom_cpu(cloud_record): assert cloud_record.cpu_duration == cpu +def test_cloud_record_zero_cpu(zero_cpu_cloud_record): + """Test a cloud record with 0 CPU time is correctly rendered.""" + ssm_message = zero_cpu_cloud_record.ssm_message() + assert "CpuDuration: 1" in ssm_message + assert "WallDuration: 1" in ssm_message + + def test_ip_record(ip_record): """Test that an IP record is correctly generated.""" assert isinstance(ip_record.measure_time, datetime.datetime)