Skip to content

Commit 72c09e3

Browse files
committed
update error handling in JiraTestManager, remove default flaky
1 parent f9311ab commit 72c09e3

File tree

1 file changed

+49
-22
lines changed

1 file changed

+49
-22
lines changed

tests/conftest.py

Lines changed: 49 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
from typing import Any, Dict
1212

1313
import pytest
14-
from flaky import flaky
1514

1615
from jira import JIRA
1716

@@ -26,7 +25,6 @@
2625
broken_test = pytest.mark.xfail
2726

2827

29-
@flaky # all have default flaki-ness
3028
class JiraTestCase(unittest.TestCase):
3129
"""Test case for all Jira tests.
3230
@@ -53,7 +51,17 @@ def setUp(self) -> None:
5351
This is called before each test. If you want to add more for your tests,
5452
Run `JiraTestCase.setUp(self) in your custom setUp() to obtain these.
5553
"""
56-
self.test_manager = JiraTestManager()
54+
55+
initialized = False
56+
try:
57+
self.test_manager = JiraTestManager()
58+
initialized = self.test_manager.initialized
59+
except Exception as e:
60+
# pytest with flaky swallows any exceptions re-raised in a try, except
61+
# so we log any exceptions for aiding debugging
62+
LOGGER.exception(e)
63+
self.assertTrue(initialized, "Test Manager setUp failed")
64+
5765
self.jira = self.test_manager.jira_admin
5866
self.jira_normal = self.test_manager.jira_normal
5967
self.user_admin = self.test_manager.user_admin
@@ -105,6 +113,7 @@ class JiraTestManager(object):
105113
CI_JIRA_ADMIN (str): Admin user account name.
106114
CI_JIRA_USER (str): Limited user account name.
107115
max_retries (int): number of retries to perform for recoverable HTTP errors.
116+
initialized (bool): if init was successful.
108117
"""
109118

110119
__shared_state: Dict[Any, Any] = {}
@@ -157,6 +166,7 @@ def set_jira_cloud_details(self):
157166
self.CI_JIRA_ADMIN_PASSWORD = os.environ["CI_JIRA_CLOUD_ADMIN_TOKEN"]
158167
self.CI_JIRA_USER = os.environ["CI_JIRA_CLOUD_USER"]
159168
self.CI_JIRA_USER_PASSWORD = os.environ["CI_JIRA_CLOUD_USER_TOKEN"]
169+
self.CI_JIRA_ISSUE = os.environ.get("CI_JIRA_ISSUE", "Bug")
160170

161171
def set_jira_server_details(self):
162172
self.CI_JIRA_URL = os.environ["CI_JIRA_URL"]
@@ -186,7 +196,15 @@ def set_basic_auth_logins(self, **jira_class_kwargs):
186196
self.jira_sysadmin = JIRA(self.CI_JIRA_URL, **jira_class_kwargs)
187197
self.jira_normal = JIRA(self.CI_JIRA_URL, **jira_class_kwargs)
188198

189-
def __remove_project(self, project_key):
199+
def _project_exists(self, project_key: str) -> bool:
200+
try:
201+
self.jira_admin.project(project_key)
202+
except Exception as e: # If the project does not exist a warning is thrown
203+
if "No project could be found" in str(e):
204+
return False
205+
return True
206+
207+
def _remove_project(self, project_key):
190208
"""Ensure if the project exists we delete it first"""
191209

192210
wait_between_checks_secs = 2
@@ -198,7 +216,8 @@ def __remove_project(self, project_key):
198216
try:
199217
self.jira_admin.project(project_key)
200218
except Exception as e: # If the project does not exist a warning is thrown
201-
LOGGER.warning(e)
219+
if "No project could be found" not in str(e):
220+
raise e
202221
else:
203222
# if no error is thrown that means the project exists, so we try to delete it
204223
try:
@@ -208,28 +227,34 @@ def __remove_project(self, project_key):
208227

209228
# wait for the project to be deleted
210229
for _ in range(1, wait_attempts):
211-
try:
212-
self.jira_admin.project(project_key)
213-
except Exception:
230+
if not self._project_exists(project_key):
214231
# If the project does not exist a warning is thrown
215232
# so once this is raised we know it is deleted successfully
216233
break
217-
print(f"Warning: Project '{project_key}' not deleted yet....")
218234
sleep(wait_between_checks_secs)
219235

220-
def __create_project(self, project_key, project_name) -> int:
221-
"""Create a project and return the id"""
236+
if self._project_exists(project_key):
237+
raise TimeoutError(
238+
" Project '{project_key}' not deleted after {time_to_wait_for_delete_secs} seconds"
239+
)
222240

223-
self.__remove_project(project_key)
241+
def _create_project(
242+
self, project_key: str, project_name: str, allow_exist: bool = False
243+
) -> int:
244+
"""Create a project and return the id"""
224245

225-
create_attempts = 6
226-
for _ in range(create_attempts):
227-
try:
228-
if self.jira_admin.create_project(project_key, project_name):
229-
break
230-
except Exception as e:
231-
if "A project with that name already exists" not in str(e):
232-
raise e
246+
if allow_exist and self._project_exists(project_key):
247+
pass
248+
else:
249+
self._remove_project(project_key)
250+
create_attempts = 6
251+
for _ in range(create_attempts):
252+
try:
253+
if self.jira_admin.create_project(project_key, project_name):
254+
break
255+
except Exception as e:
256+
if "A project with that name already exists" not in str(e):
257+
raise e
233258
return self.jira_admin.project(project_key).id
234259

235260
def create_some_data(self):
@@ -266,8 +291,10 @@ def create_some_data(self):
266291
self.project_sd,
267292
)
268293

269-
self.project_a_id = self.__create_project(self.project_a, self.project_a_name)
270-
self.project_b_id = self.__create_project(self.project_b, self.project_b_name)
294+
self.project_a_id = self._create_project(self.project_a, self.project_a_name)
295+
self.project_b_id = self._create_project(
296+
self.project_b, self.project_b_name, allow_exist=True
297+
)
271298

272299
sleep(1) # keep it here as often Jira will report the
273300
# project as missing even after is created

0 commit comments

Comments
 (0)