From fa2cbeaae25dd9268be763b4c588cd88272f094d Mon Sep 17 00:00:00 2001 From: Dmitriy Rabotyagov Date: Mon, 26 Aug 2019 19:57:57 +0300 Subject: [PATCH] Fix handling of dangling symlink There may be broken symlinks within the log directories, those fail with an error when os.stat() is executed on them. So if/else is replaced with try/except while TypeError used to catch when self.full_path is None Change-Id: Iffee97760a39fa4f7760bd67fb63c5f0905064bd --- .../library/test_zuul_swift_upload.py | 25 ++++++++++++++++++- .../library/zuul_swift_upload.py | 10 ++++++-- 2 files changed, 32 insertions(+), 3 deletions(-) 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