Fix flaky test related to cache migration

In cache migration unit tests, I was using same cache.db file name
in the setup method which was used by all the tests, which was
causing a issue of deleting a file by other test and resulted
in random failure.

Using random filename to fix this issue permenantly.

Related blueprint centralized-cache-db

Change-Id: I321982013bdf571741a8ef2d0575f65288fff53f
This commit is contained in:
Abhishek Kekane 2024-02-29 14:56:16 +00:00 committed by Dan Smith
parent 703cf5ffdd
commit 5bf2f4e7a0

View File

@ -16,6 +16,7 @@
from contextlib import contextmanager
import os
import sqlite3
import tempfile
import time
from unittest import mock
import uuid
@ -38,8 +39,8 @@ class TestMigrate(test_utils.BaseTestCase):
super(TestMigrate, self).setUp()
self.config(worker_self_reference_url='http://worker1.example.com')
self.db = 'cache.db'
fd, self.db = tempfile.mkstemp(suffix=".db")
os.close(fd)
self.db_api = unit_test_utils.FakeDB(initialize=False)
self.migrate = sqlite_migration.Migrate(self.db, self.db_api)
self.addCleanup(self.drop_db)
@ -116,7 +117,7 @@ class TestMigrate(test_utils.BaseTestCase):
mock.call('Adding local node reference %(node)s in '
'centralized db',
{'node': 'http://worker1.example.com'}),
mock.call('Connecting to SQLite db %s', 'cache.db'),
mock.call('Connecting to SQLite db %s', self.db),
]
mock_log.debug.assert_has_calls(expected_calls)
@ -132,7 +133,7 @@ class TestMigrate(test_utils.BaseTestCase):
mock.call('Node reference %(node)s is already recorded, '
'ignoring it',
{'node': 'http://worker1.example.com'}),
mock.call('Connecting to SQLite db %s', 'cache.db'),
mock.call('Connecting to SQLite db %s', self.db),
]
mock_log.debug.assert_has_calls(expected_calls)
@ -149,7 +150,7 @@ class TestMigrate(test_utils.BaseTestCase):
mock.call('Adding local node reference %(node)s in '
'centralized db',
{'node': 'http://worker1.example.com'}),
mock.call('Connecting to SQLite db %s', 'cache.db'),
mock.call('Connecting to SQLite db %s', self.db),
mock.call('Skipping migrating image %(uuid)s from SQLite to '
'Centralized db for node %(node)s as it is present '
'in Centralized db.',
@ -169,7 +170,7 @@ class TestMigrate(test_utils.BaseTestCase):
mock.call('Adding local node reference %(node)s in '
'centralized db',
{'node': 'http://worker1.example.com'}),
mock.call('Connecting to SQLite db %s', 'cache.db'),
mock.call('Connecting to SQLite db %s', self.db),
mock.call('Migrating image %s from SQLite to Centralized db.',
FAKE_IMAGE_1),
mock.call('Image %(uuid)s is migrated to centralized db for '