55import argparse
66
77import os
8+ import pprint
9+ import sys
810from typing import TYPE_CHECKING
911
1012import pytest
1113import logging
1214
15+ from ._api import get_test_suite_manifest
1316from ._git import get_current_git_commit , get_current_git_branch
14- from ._plugin import UnflakablePlugin , QuarantineMode
17+ from ._plugin import UnflakablePlugin , QuarantineMode , UnflakableXdistHooks
1518
1619if TYPE_CHECKING :
1720 Config = pytest .Config
@@ -130,7 +133,7 @@ def pytest_configure(config: Config) -> None:
130133 if config .getoption ('unflakable_suite_id' ) is None :
131134 raise pytest .UsageError ('missing required argument --test-suite-id' )
132135
133- # pytest-xdist workers don't make API calls and amy not have the API key available.
136+ # pytest-xdist workers don't make API calls and may not have the API key available.
134137 if is_xdist_worker :
135138 api_key = ''
136139 elif config .option .unflakable_api_key_path is not None :
@@ -143,6 +146,7 @@ def pytest_configure(config: Config) -> None:
143146
144147 branch = config .option .unflakable_branch
145148 commit = config .option .unflakable_commit
149+ test_suite_id = config .option .unflakable_suite_id
146150 git_auto_detect = not config .getoption ('unflakable_no_git_auto_detect' , False )
147151 if git_auto_detect and not is_xdist_worker :
148152 if commit is None :
@@ -153,24 +157,47 @@ def pytest_configure(config: Config) -> None:
153157 branch = get_current_git_branch (commit , logger )
154158 logger .debug ('auto-detected branch `%s`' , branch )
155159
160+ insecure_disable_tls_validation = config .getoption (
161+ 'unflakable_insecure_disable_tls_validation' , False )
162+ manifest = None
156163 if is_xdist_worker and 'unflakable_manifest' in config .workerinput : # type: ignore
157- worker_manifest = config .workerinput ['unflakable_manifest' ] # type: ignore
164+ manifest = config .workerinput ['unflakable_manifest' ] # type: ignore
165+ logger .debug (
166+ f'xdist worker received manifest for test suite { test_suite_id } : '
167+ f'{ pprint .pformat (manifest )} '
168+ )
158169 else :
159- worker_manifest = None
170+ try :
171+ manifest = get_test_suite_manifest (
172+ test_suite_id = test_suite_id ,
173+ api_key = api_key ,
174+ base_url = config .option .unflakable_base_url ,
175+ insecure_disable_tls_validation = insecure_disable_tls_validation ,
176+ logger = logger ,
177+ )
178+ # IOError is the base class for `requests.RequestException`.
179+ except IOError as e :
180+ sys .stderr .write (
181+ ('ERROR: Failed to get Unflakable manifest: %s\n Test failures will NOT be'
182+ ' quarantined.\n ' ) % (repr (e ))),
183+
184+ if config .pluginmanager .hasplugin ('xdist' ):
185+ config .pluginmanager .register (
186+ UnflakableXdistHooks (logger = logger , worker_manifest = manifest )
187+ )
160188
161189 config .pluginmanager .register (UnflakablePlugin (
162190 api_key = api_key ,
163191 base_url = config .option .unflakable_base_url ,
164192 branch = branch ,
165193 commit = commit ,
166194 failure_retries = config .option .unflakable_failure_retries ,
167- insecure_disable_tls_validation = config .getoption (
168- 'unflakable_insecure_disable_tls_validation' , False ),
195+ insecure_disable_tls_validation = insecure_disable_tls_validation ,
169196 quarantine_mode = quarantine_mode ,
170- test_suite_id = config . option . unflakable_suite_id ,
197+ test_suite_id = test_suite_id ,
171198 upload_results = not is_xdist_worker and (
172199 not config .getoption ('unflakable_no_upload_results' , False )),
173200 logger = logger ,
174- worker_manifest = worker_manifest ,
201+ manifest = manifest ,
175202 is_xdist_worker = is_xdist_worker ,
176203 ))
0 commit comments