Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make the test suite connectivity-agnostic. #12095

Conversation

jayaddison
Copy link
Contributor

@jayaddison jayaddison commented Mar 14, 2024

Feature or Bugfix

  • Bugfix

Purpose

  • Intended to allow the test suite to run equally well whether the test host is online.

Detail

  • Use local test HTTP server instances instead of fetching remote resources.
  • Remove a remote image that causes epubcheck to fail when checking an epub file that contains remote references (because the image couldn't be retrieved during the Sphinx build).

Relates

@jayaddison jayaddison changed the title Tests: make the test suite connectivity-agnostic. [tests] Make the test suite connectivity-agnostic. Mar 14, 2024
@jayaddison
Copy link
Contributor Author

The Windows test output for this change appears to have uncovered an unrelated problem: handling http:https://localhost:7777/sphinx.png raises a WinError exception somewhere on that platform while it does not on Linux.

WARNING: Could not fetch remote image: http:https://localhost:7777/sphinx.png [[WinError 267] The directory name is invalid: 'C:\\Users\\runneradmin\\AppData\\Local\\Temp\\...\\images\\_build\\doctrees\\images\\http/localhost:7777']

My best guess is around this area of code (it has quite a large try...catch block):

try:
basename = os.path.basename(node['uri'])
if '?' in basename:
basename = basename.split('?')[0]
if basename == '' or len(basename) > MAX_FILENAME_LEN:
filename, ext = os.path.splitext(node['uri'])
basename = sha1(filename.encode(), usedforsecurity=False).hexdigest() + ext
basename = CRITICAL_PATH_CHAR_RE.sub("_", basename)
dirname = node['uri'].replace(':https://', '/').translate({ord("?"): "/",
ord("&"): "/"})
if len(dirname) > MAX_FILENAME_LEN:
dirname = sha1(dirname.encode(), usedforsecurity=False).hexdigest()
ensuredir(os.path.join(self.imagedir, dirname))
path = os.path.join(self.imagedir, dirname, basename)
headers = {}
if os.path.exists(path):
timestamp: float = ceil(os.stat(path).st_mtime)
headers['If-Modified-Since'] = epoch_to_rfc1123(timestamp)
config = self.app.config
r = requests.get(
node['uri'], headers=headers,
_user_agent=config.user_agent,
_tls_info=(config.tls_verify, config.tls_cacerts),
)
if r.status_code >= 400:
logger.warning(__('Could not fetch remote image: %s [%d]') %
(node['uri'], r.status_code))
else:
self.app.env.original_image_uri[path] = node['uri']
if r.status_code == 200:
with open(path, 'wb') as f:
f.write(r.content)
last_modified = r.headers.get('last-modified')
if last_modified:
timestamp = rfc1123_to_epoch(last_modified)
os.utime(path, (timestamp, timestamp))
mimetype = guess_mimetype(path, default='*')
if mimetype != '*' and os.path.splitext(basename)[1] == '':
# append a suffix if URI does not contain suffix
ext = get_image_extension(mimetype)
newpath = os.path.join(self.imagedir, dirname, basename + ext)
os.replace(path, newpath)
self.app.env.original_image_uri.pop(path)
self.app.env.original_image_uri[newpath] = node['uri']
path = newpath
node['candidates'].pop('?')
node['candidates'][mimetype] = path
node['uri'] = path
self.app.env.images.add_file(self.env.docname, path)
except Exception as exc:
logger.warning(__('Could not fetch remote image: %s [%s]') % (node['uri'], exc))

These lines initially seem like a possibility:

ensuredir(os.path.join(self.imagedir, dirname))
path = os.path.join(self.imagedir, dirname, basename)

@jayaddison jayaddison force-pushed the issue-12094/test-suite-connectivity-agnostic branch from 8e38511 to 9ca2670 Compare April 9, 2024 22:55
@jayaddison
Copy link
Contributor Author

The Windows test output for this change appears to have uncovered an unrelated problem: handling http:https://localhost:7777/sphinx.png raises a WinError exception somewhere on that platform while it does not on Linux.

WARNING: Could not fetch remote image: http:https://localhost:7777/sphinx.png [[WinError 267] The directory name is invalid: 'C:\\Users\\runneradmin\\AppData\\Local\\Temp\\...\\images\\_build\\doctrees\\images\\http/localhost:7777']

See #12100 (comment) for more details about the cause of this. In short: the colon character, as used to indicate the network port number within URIs, is not considered a valid character within the components of directory paths on Windows, and therefore remote image URIs such as this one were caused a problem for the existing ImageDownloader transform.

@jayaddison jayaddison mentioned this pull request Apr 9, 2024
@jayaddison jayaddison marked this pull request as ready for review April 9, 2024 23:27
@jayaddison
Copy link
Contributor Author

No further changes planned. Please note that this branch depends upon / includes #12253.

@AA-Turner AA-Turner changed the title [tests] Make the test suite connectivity-agnostic. Make the test suite connectivity-agnostic. Apr 12, 2024
@AA-Turner AA-Turner merged commit 6730392 into sphinx-doc:master Apr 12, 2024
23 checks passed
@jayaddison
Copy link
Contributor Author

Thank you!

@jayaddison jayaddison deleted the issue-12094/test-suite-connectivity-agnostic branch April 12, 2024 22:37
@github-actions github-actions bot locked as resolved and limited conversation to collaborators May 13, 2024
@AA-Turner AA-Turner added this to the 7.3.0 milestone Jul 13, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

tests: network state (online/offline) affects epubcheck unit test outcome.
2 participants