Skip to content

Commit 3072e53

Browse files
authored
Merge pull request #120 from KelvinLinBU/time_clamping_test
Add test for Error event time clamping
2 parents 264a45e + a56a3b1 commit 3072e53

File tree

2 files changed

+30
-7
lines changed

2 files changed

+30
-7
lines changed

src/openstack_billing_db/model.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -95,26 +95,26 @@ def run_state_machine():
9595
"""
9696
current_state = None
9797
for event in self.events:
98-
event_time = self._clamp_time(event.time, start_time, end_time)
98+
clamped_event_time = self._clamp_time(event.time, start_time, end_time)
9999

100100
# Error state can only be determined by the event message
101101
if event.message == "Error":
102102
if current_state is None:
103-
current_state = enter_state("Error", event_time)
103+
current_state = enter_state("Error", clamped_event_time)
104104
else:
105-
current_state.exit(event_time)
106-
current_state = enter_state("Error", event_time)
105+
current_state.exit(clamped_event_time)
106+
current_state = enter_state("Error", clamped_event_time)
107107
continue
108108

109109
for state in vm_states:
110110
if event.name in state.triggers:
111111
if current_state is None:
112112
current_state = state
113-
state.enter(event_time)
113+
state.enter(clamped_event_time)
114114
elif state.name != current_state.name:
115-
current_state.exit(event_time)
115+
current_state.exit(clamped_event_time)
116116
current_state = state
117-
state.enter(event_time)
117+
state.enter(clamped_event_time)
118118

119119
# Some VM instances may have a `deleted_at` time, another trigger for the `Deleted` state
120120
if self.deleted_at:

src/openstack_billing_db/tests/unit/test_instance.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -337,3 +337,26 @@ def test_instance_get_gpu_flavor():
337337
su_type, count = Database._get_gpu_flavor_info(pci_request)
338338
assert su_type == answers[i][0]
339339
assert count == answers[i][1]
340+
341+
342+
def test_error_event_outside_window():
343+
start = datetime(2000, 1, 1, 0, 0, 0)
344+
end = datetime(2000, 2, 1, 0, 0, 0)
345+
events = [
346+
InstanceEvent(time=start - timedelta(hours=1), name="create", message="Error")
347+
]
348+
# Case 1: Error BEFORE window
349+
i = Instance(
350+
uuid=uuid.uuid4().hex, name=uuid.uuid4().hex, flavor=FLAVORS[1], events=events
351+
)
352+
r_before = i.get_runtime_during(start, end)
353+
assert r_before.total_seconds_running == 0
354+
assert r_before.total_seconds_stopped == 0
355+
# Case 2: Error AFTER window
356+
i.events = [
357+
InstanceEvent(time=start, name="create", message=""),
358+
InstanceEvent(time=end + timedelta(hours=1), name="stop", message="Error"),
359+
]
360+
r_after = i.get_runtime_during(start, end)
361+
assert r_after.total_seconds_running == 1 * MONTH
362+
assert r_after.total_seconds_stopped == 0

0 commit comments

Comments
 (0)