diff --git a/contrib/buildbot/server.py b/contrib/buildbot/server.py --- a/contrib/buildbot/server.py +++ b/contrib/buildbot/server.py @@ -71,19 +71,18 @@ # If db_file_no_ext is not None, attempt to restore old database state if db_file_no_ext: - db_file = db_file_no_ext + '.db' - app.logger.info("Loading persisted state file '{}'...".format(db_file)) - if os.path.exists(db_file): + app.logger.info( + "Loading persisted state database with base name '{}'...".format(db_file_no_ext)) + try: with shelve.open(db_file_no_ext, flag='r') as db: for key in create_server.db.keys(): if key in db: create_server.db[key] = db[key] app.logger.info( - "Restored key '{}' from persisted state in file '{}'".format( - key, db_file)) - else: + "Restored key '{}' from persisted state".format(key)) + except BaseException: app.logger.info( - "Persisted state file '{}' does not exist. It will be created when written to.".format(db_file)) + "Persisted state database with base name '{}' could not be opened. A new one will be created when written to.".format(db_file_no_ext)) app.logger.info("Done") else: app.logger.warning( diff --git a/contrib/buildbot/test/.gitignore b/contrib/buildbot/test/.gitignore new file mode 100644 --- /dev/null +++ b/contrib/buildbot/test/.gitignore @@ -0,0 +1 @@ +test_output/ diff --git a/contrib/buildbot/test/abcbot_fixture.py b/contrib/buildbot/test/abcbot_fixture.py --- a/contrib/buildbot/test/abcbot_fixture.py +++ b/contrib/buildbot/test/abcbot_fixture.py @@ -11,6 +11,7 @@ import os from pathlib import Path import server +import shutil import unittest import test.mocks.fixture @@ -38,9 +39,13 @@ TEST_USER, TEST_PASSWORD).encode()).decode('utf-8') self.headers = {'Authorization': 'Basic ' + self.credentials} + self.test_output_dir = os.path.join( + os.path.dirname(__file__), "test_output") self.db_file_no_ext = None def setUp(self): + shutil.rmtree(self.test_output_dir, ignore_errors=True) + os.makedirs(self.test_output_dir, exist_ok=True) self.phab = test.mocks.phabricator.instance() self.slackbot = test.mocks.slackbot.instance() self.teamcity = test.mocks.teamcity.instance() diff --git a/contrib/buildbot/test/test_persist_database.py b/contrib/buildbot/test/test_persist_database.py --- a/contrib/buildbot/test/test_persist_database.py +++ b/contrib/buildbot/test/test_persist_database.py @@ -20,9 +20,6 @@ from test.test_endpoint_status import statusRequestData -DB_FILE_NO_EXT = "test_database" -DB_FILE = DB_FILE_NO_EXT + '.db' - BUILD_NAME = 'build-name' BUILD_TYPE_ID = 'build-type-id' BUILD_TARGET_PHID = 'build-target-PHID' @@ -30,7 +27,8 @@ class PersistDataTestCase(ABCBotFixture): def setUp(self): - self.db_file_no_ext = DB_FILE_NO_EXT + self.db_file_no_ext = os.path.join( + self.test_output_dir, "test_database") super().setUp() self.phab.get_file_content_from_master = mock.Mock() @@ -49,10 +47,6 @@ self.travis.get_branch_status = mock.Mock() self.travis.get_branch_status.return_value = BuildStatus.Success - def tearDown(self): - if os.path.exists(DB_FILE): - os.remove(DB_FILE) - def test_persist_diff_targets(self): queryData = buildRequestQuery() queryData.abcBuildName = BUILD_NAME @@ -69,8 +63,7 @@ self.assertEqual(response.status_code, 200) # Check the diff target state was persisted - self.assertTrue(os.path.exists(DB_FILE)) - with shelve.open(DB_FILE_NO_EXT, flag='r') as db: + with shelve.open(self.db_file_no_ext, flag='r') as db: self.assertIn('diff_targets', db) self.assertIn(BUILD_TARGET_PHID, db['diff_targets']) self.assertIn( @@ -93,7 +86,7 @@ self.phab, self.slackbot, self.travis, - db_file_no_ext=DB_FILE_NO_EXT, + db_file_no_ext=self.db_file_no_ext, jsonEncoder=test.mocks.fixture.MockJSONEncoder).test_client() data = statusRequestData() @@ -123,7 +116,7 @@ ) # Check the diff target was cleared from persisted state - with shelve.open(DB_FILE_NO_EXT, flag='r') as db: + with shelve.open(self.db_file_no_ext, flag='r') as db: self.assertNotIn(BUILD_TARGET_PHID, db['diff_targets'])