diff --git a/roles/upload-logs-swift/library/test_zuul_swift_upload.py b/roles/upload-logs-swift/library/test_zuul_swift_upload.py index 39f23b40f..075fa9ec9 100644 --- a/roles/upload-logs-swift/library/test_zuul_swift_upload.py +++ b/roles/upload-logs-swift/library/test_zuul_swift_upload.py @@ -20,10 +20,12 @@ __metaclass__ = type import os import testtools +import time +import stat import fixtures from bs4 import BeautifulSoup -from .zuul_swift_upload import FileList, Indexer +from .zuul_swift_upload import FileList, Indexer, FileDetail FIXTURE_DIR = os.path.join(os.path.dirname(__file__), @@ -357,3 +359,24 @@ class TestFileList(testtools.TestCase): self.assertEqual(rows[0].find('a').get('href'), 'subdir.txt') self.assertEqual(rows[0].find('a').text, 'subdir.txt') + + +class TestFileDetail(testtools.TestCase): + + def test_get_file_detail(self): + '''Test files info''' + path = os.path.join(FIXTURE_DIR, 'logs/job-output.json') + file_detail = FileDetail(path, '') + path_stat = os.stat(path) + self.assertEqual( + time.gmtime(path_stat[stat.ST_MTIME]), + file_detail.last_modified) + self.assertEqual(16, file_detail.size) + + def test_get_file_detail_missing_file(self): + '''Test files that go missing during a walk''' + + file_detail = FileDetail('missing/file/that/we/cant/find', '') + + self.assertEqual(time.gmtime(0), file_detail.last_modified) + self.assertEqual(0, file_detail.size) diff --git a/roles/upload-logs-swift/library/zuul_swift_upload.py b/roles/upload-logs-swift/library/zuul_swift_upload.py index 1fae10896..c730abb46 100755 --- a/roles/upload-logs-swift/library/zuul_swift_upload.py +++ b/roles/upload-logs-swift/library/zuul_swift_upload.py @@ -177,6 +177,12 @@ class FileDetail(): used for links. filename (str): An optional alternate filename in links. """ + # Make FileNotFoundError exception to be compatible with python2 + try: + FileNotFoundError # noqa: F823 + except NameError: + FileNotFoundError = OSError + self.full_path = full_path if filename is None: self.filename = os.path.basename(full_path) @@ -193,11 +199,11 @@ class FileDetail(): self.mimetype = 'application/directory' self.encoding = None self.folder = True - if self.full_path: + try: st = os.stat(self.full_path) self.last_modified = time.gmtime(st[stat.ST_MTIME]) self.size = st[stat.ST_SIZE] - else: + except (FileNotFoundError, TypeError): self.last_modified = time.gmtime(0) self.size = 0