Add tests for manifest generation for missing files

This is a patch that adds test to make sure that we handle FileNotFound
exceptions during walks.

Change-Id: I6d3513ddb4a705a28043065b1398b75018ee3115
This commit is contained in:
Mohammed Naser 2019-08-26 10:18:45 -04:00 committed by Andreas Jaeger
parent dd40f5ed9f
commit f3615abc01
2 changed files with 33 additions and 12 deletions

View File

@ -37,6 +37,15 @@ def path_in_tree(root, path):
return True return True
def _get_file_info(path):
try:
st = os.stat(path)
except OSError:
return 0, 0
return st[stat.ST_MTIME], st[stat.ST_SIZE]
def walk(root, original_root=None): def walk(root, original_root=None):
if original_root is None: if original_root is None:
original_root = root original_root = root
@ -67,19 +76,14 @@ def walk(root, original_root=None):
mime_guess, encoding = mimetypes.guess_type(path) mime_guess, encoding = mimetypes.guess_type(path)
if not mime_guess: if not mime_guess:
mime_guess = 'text/plain' mime_guess = 'text/plain'
# This may fail e.g. for dangling symlinks, just ignore those last_modified, size = _get_file_info(path)
try: if not last_modified and not size:
st = os.stat(path)
last_modified = st[stat.ST_MTIME]
size = st[stat.ST_SIZE]
data.append(dict(name=f,
mimetype=mime_guess,
encoding=encoding,
last_modified=last_modified,
size=size))
except FileNotFoundError:
continue continue
data.append(dict(name=f,
mimetype=mime_guess,
encoding=encoding,
last_modified=last_modified,
size=size))
return data return data

View File

@ -19,9 +19,11 @@ from __future__ import (absolute_import, division, print_function)
__metaclass__ = type __metaclass__ = type
import os import os
import stat
import testtools import testtools
import fixtures import fixtures
from .generate_manifest import _get_file_info
from .generate_manifest import walk from .generate_manifest import walk
@ -126,3 +128,18 @@ class TestFileList(testtools.TestCase):
('controller/service_log.txt', 'text/plain', None), ('controller/service_log.txt', 'text/plain', None),
('symlink_loop/placeholder', 'text/plain', None), ('symlink_loop/placeholder', 'text/plain', None),
]) ])
def test_get_file_info(self):
'''Test files info'''
path = os.path.join(FIXTURE_DIR, 'logs', 'job-output.json')
last_modified, size = _get_file_info(path)
self.assertEqual(os.stat(path)[stat.ST_MTIME], last_modified)
self.assertEqual(16, size)
def test_get_file_info_missing_file(self):
'''Test files that go missing during a walk'''
last_modified, size = _get_file_info('missing/file/that/we/cant/find')
self.assertEqual(0, last_modified)
self.assertEqual(0, size)