From 343a080833a184083127092eb6bc255584333fe4 Mon Sep 17 00:00:00 2001 From: Joseph Schorr Date: Thu, 5 May 2016 13:55:24 -0400 Subject: [PATCH] Make security scan testing much faster --- endpoints/verbs.py | 2 +- storage/fakestorage.py | 3 +++ test/test_secscan.py | 12 +++++++++--- util/secscan/api.py | 1 - 4 files changed, 13 insertions(+), 5 deletions(-) diff --git a/endpoints/verbs.py b/endpoints/verbs.py index 1d7a8210b..518cb38be 100644 --- a/endpoints/verbs.py +++ b/endpoints/verbs.py @@ -170,7 +170,7 @@ def _torrent_repo_verb(repo_image, tag, verb, **kwargs): abort(406) # Lookup an *existing* derived storage for the verb. If the verb's image storage doesn't exist, - # we cannot create it here, so we 404. + # we cannot create it here, so we 406. derived = model.image.find_derived_storage_for_image(repo_image, verb) if not derived: abort(406) diff --git a/storage/fakestorage.py b/storage/fakestorage.py index 50bb5720e..806911095 100644 --- a/storage/fakestorage.py +++ b/storage/fakestorage.py @@ -56,6 +56,9 @@ class FakeStorage(BaseStorageV2): _FAKE_STORAGE_MAP.pop(path, None) def exists(self, path): + if _FAKE_STORAGE_MAP.get('all_files_exist', None): + return True + return path in _FAKE_STORAGE_MAP def get_checksum(self, path): diff --git a/test/test_secscan.py b/test/test_secscan.py index acdc6febc..b9c6fce58 100644 --- a/test/test_secscan.py +++ b/test/test_secscan.py @@ -10,6 +10,7 @@ from util.secscan.api import SecurityScannerAPI, AnalyzeLayerException from util.secscan.analyzer import LayerAnalyzer from util.secscan.notifier import process_notification_data from data import model +from storage.basestorage import StoragePaths from workers.security_notification_worker import SecurityNotificationWorker from endpoints.v2 import v2_bp @@ -116,9 +117,11 @@ class TestSecurityScanner(unittest.TestCase): # Enable direct download in fake storage. storage.put_content(['local_us'], 'supports_direct_download', 'true') + # Have fake storage say all files exist for the duration of the test. + storage.put_content(['local_us'], 'all_files_exist', 'true') + # Setup the database with fake storage. - force_rebuild = os.environ.get('SKIP_REBUILD') != 'true' - setup_database_for_testing(self, with_storage=True, force_rebuild=force_rebuild) + setup_database_for_testing(self) self.app = app.test_client() self.ctx = app.test_request_context() self.ctx.__enter__() @@ -126,7 +129,9 @@ class TestSecurityScanner(unittest.TestCase): self.api = SecurityScannerAPI(app, app.config, storage) def tearDown(self): - storage.put_content(['local_us'], 'supports_direct_download', 'false') + storage.remove(['local_us'], 'supports_direct_download') + storage.remove(['local_us'], 'all_files_exist') + finished_database_for_testing(self) self.ctx.__exit__(True, None, None) @@ -256,6 +261,7 @@ class TestSecurityScanner(unittest.TestCase): path = model.storage.get_layer_path(layer.storage) locations = app.config['DISTRIBUTED_STORAGE_PREFERENCE'] storage.remove(locations, path) + storage.remove(locations, 'all_files_exist') with HTTMock(analyze_layer_success_mock, response_content): analyzer = LayerAnalyzer(app.config, self.api) diff --git a/util/secscan/api.py b/util/secscan/api.py index a06c346b9..7558298f0 100644 --- a/util/secscan/api.py +++ b/util/secscan/api.py @@ -59,7 +59,6 @@ class SecurityScannerAPI(object): if not self._storage.exists(locations, path): locations = get_storage_locations(image.storage.uuid) - if not locations or not self._storage.exists(locations, path): logger.warning('Could not find a valid location to download layer %s.%s out of %s', image.docker_image_id, image.storage.uuid, locations)