Skip to content

Commit 39ef1ad

Browse files
committed
Restore original time override in test_archive_task_logs
Change I9b16a3a849937aba5b90ed1ab9a80b7f0103f673 attempted to fix a non-determinstic failure in the test_archive_task_logs functional test by adding a time override. That fix however missed that the osloutils_fixture.TimeFixture clears the time override when it exits and that does not mean a previous time override will be restored. This removes usage of the osloutils_fixture.TimeFixture and instead sets time overrides and restores previous time overrides. Restoration of the original time overrides also made an adjustment to the --before option value necessary in order to pick up the first task_log record. Closes-Bug: #1934519 Change-Id: Ic28d2e53d5f50e89635e19df08699f2b8c5cea84
1 parent 5a56ac4 commit 39ef1ad

File tree

1 file changed

+41
-32
lines changed

1 file changed

+41
-32
lines changed

nova/tests/functional/test_nova_manage.py

Lines changed: 41 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
import fixtures
1818
from neutronclient.common import exceptions as neutron_client_exc
1919
import os_resource_classes as orc
20-
from oslo_utils import fixture as osloutils_fixture
2120
from oslo_utils.fixture import uuidsentinel
2221
from oslo_utils import timeutils
2322

@@ -1696,10 +1695,10 @@ class TestDBArchiveDeletedRowsTaskLog(integrated_helpers._IntegratedTestBase):
16961695
def setUp(self):
16971696
# Override time to ensure we cross audit period boundaries in a
16981697
# predictable way.
1699-
faketoday = datetime.datetime(2021, 7, 1)
1698+
self.faketoday = datetime.datetime(2021, 7, 1)
17001699
# This needs to be done before setUp() starts services, else they will
17011700
# be considered "down" by the ComputeFilter.
1702-
self.useFixture(test.TimeOverride(override_time=faketoday))
1701+
self.useFixture(test.TimeOverride(override_time=self.faketoday))
17031702
super(TestDBArchiveDeletedRowsTaskLog, self).setUp()
17041703
self.enforce_fk_constraints()
17051704
self.cli = manage.DbCommands()
@@ -1712,7 +1711,7 @@ def test_archive_task_logs(self):
17121711
self.flags(instance_usage_audit=True)
17131712
compute = self.computes['compute']
17141713

1715-
# Create a few servers so the for the periodic task to process.
1714+
# Create a few servers for the periodic task to process.
17161715
for i in range(0, 3):
17171716
self._create_server()
17181717

@@ -1727,17 +1726,19 @@ def test_archive_task_logs(self):
17271726
# July has 31 days, August has 31 days, September has 30 days.
17281727
for days in (31, 31 + 31, 31 + 31 + 30):
17291728
future = timeutils.utcnow() + datetime.timedelta(days=days)
1730-
with osloutils_fixture.TimeFixture(future):
1731-
# task_log records are generated by the _instance_usage_audit
1732-
# periodic task.
1733-
compute.manager._instance_usage_audit(ctxt)
1734-
# Audit period defaults to 1 month, the last audit period will
1735-
# be the previous calendar month.
1736-
begin, end = nova_utils.last_completed_audit_period()
1737-
# Verify that we have 1 task_log record per audit period.
1738-
task_logs = objects.TaskLogList.get_all(
1739-
ctxt, 'instance_usage_audit', begin, end)
1740-
self.assertEqual(1, len(task_logs))
1729+
timeutils.set_time_override(future)
1730+
# task_log records are generated by the _instance_usage_audit
1731+
# periodic task.
1732+
compute.manager._instance_usage_audit(ctxt)
1733+
# Audit period defaults to 1 month, the last audit period will
1734+
# be the previous calendar month.
1735+
begin, end = nova_utils.last_completed_audit_period()
1736+
# Verify that we have 1 task_log record per audit period.
1737+
task_logs = objects.TaskLogList.get_all(
1738+
ctxt, 'instance_usage_audit', begin, end)
1739+
self.assertEqual(1, len(task_logs))
1740+
# Restore original time override.
1741+
timeutils.set_time_override(self.faketoday)
17411742

17421743
# First try archiving without --task-log. Expect no task_log entries in
17431744
# the results.
@@ -1746,7 +1747,10 @@ def test_archive_task_logs(self):
17461747
# Next try archiving with --task-log and --before.
17471748
# We'll archive records that were last updated before the second audit
17481749
# period.
1749-
before = timeutils.utcnow() + datetime.timedelta(days=31)
1750+
# The task_log records were created/updated on 2021-08-01, 2021-09-01,
1751+
# and 2021-10-01. So to archive one record, we need to use
1752+
# before > 2021-08-01. 2021-07-01 + 31 + 1 = 2021-08-02
1753+
before = timeutils.utcnow() + datetime.timedelta(days=31 + 1)
17501754
self.cli.archive_deleted_rows(
17511755
task_log=True, before=before.isoformat(), verbose=True)
17521756
# Verify that only 1 task_log record was archived.
@@ -1876,10 +1880,10 @@ class TestDBArchiveDeletedRowsMultiCellTaskLog(
18761880
def setUp(self):
18771881
# Override time to ensure we cross audit period boundaries in a
18781882
# predictable way.
1879-
faketoday = datetime.datetime(2021, 7, 1)
1883+
self.faketoday = datetime.datetime(2021, 7, 1)
18801884
# This needs to be done before setUp() starts services, else they will
18811885
# be considered "down" by the ComputeFilter.
1882-
self.useFixture(test.TimeOverride(override_time=faketoday))
1886+
self.useFixture(test.TimeOverride(override_time=self.faketoday))
18831887
super(TestDBArchiveDeletedRowsMultiCellTaskLog, self).setUp()
18841888
self.enforce_fk_constraints()
18851889
self.useFixture(nova_fixtures.NeutronFixture(self))
@@ -1910,7 +1914,7 @@ def test_archive_task_logs(self):
19101914
# nova-compute periodic task.
19111915
self.flags(instance_usage_audit=True)
19121916

1913-
# Create servers so the for the periodic task to process.
1917+
# Create servers for the periodic task to process.
19141918
# Boot a server to cell1
19151919
server = self._build_server(az='nova:host1')
19161920
created_server = self.api.post_server({'server': server})
@@ -1931,18 +1935,20 @@ def test_archive_task_logs(self):
19311935
# July has 31 days, August has 31 days, September has 30 days.
19321936
for days in (31, 31 + 31, 31 + 31 + 30):
19331937
future = timeutils.utcnow() + datetime.timedelta(days=days)
1934-
with osloutils_fixture.TimeFixture(future):
1935-
# task_log records are generated by the _instance_usage_audit
1936-
# periodic task.
1937-
with context.target_cell(
1938-
ctxt, self.cell_mappings['cell1']) as cctxt:
1939-
self.compute1.manager._instance_usage_audit(cctxt)
1940-
with context.target_cell(
1941-
ctxt, self.cell_mappings['cell2']) as cctxt:
1942-
self.compute2.manager._instance_usage_audit(ctxt)
1943-
# Audit period defaults to 1 month, the last audit period will
1944-
# be the previous calendar month.
1945-
begin, end = nova_utils.last_completed_audit_period()
1938+
timeutils.set_time_override(future)
1939+
# task_log records are generated by the _instance_usage_audit
1940+
# periodic task.
1941+
with context.target_cell(
1942+
ctxt, self.cell_mappings['cell1']) as cctxt:
1943+
self.compute1.manager._instance_usage_audit(cctxt)
1944+
with context.target_cell(
1945+
ctxt, self.cell_mappings['cell2']) as cctxt:
1946+
self.compute2.manager._instance_usage_audit(ctxt)
1947+
# Audit period defaults to 1 month, the last audit period will
1948+
# be the previous calendar month.
1949+
begin, end = nova_utils.last_completed_audit_period()
1950+
# Restore original time override.
1951+
timeutils.set_time_override(self.faketoday)
19461952

19471953
for cell_name in ('cell1', 'cell2'):
19481954
with context.target_cell(
@@ -1958,7 +1964,10 @@ def test_archive_task_logs(self):
19581964
# Next try archiving with --task-log and --before.
19591965
# We'll archive records that were last updated before the second audit
19601966
# period.
1961-
before = timeutils.utcnow() + datetime.timedelta(days=31)
1967+
# The task_log records were created/updated on 2021-08-01, 2021-09-01,
1968+
# and 2021-10-01. So to archive one record, we need to use
1969+
# before > 2021-08-01. 2021-07-01 + 31 + 1 = 2021-08-02
1970+
before = timeutils.utcnow() + datetime.timedelta(days=31 + 1)
19621971
self.cli.archive_deleted_rows(
19631972
all_cells=True, task_log=True, before=before.isoformat(),
19641973
verbose=True)

0 commit comments

Comments
 (0)