Changeset View
Changeset View
Standalone View
Standalone View
swh/indexer/tests/test_fossology_license.py
# Copyright (C) 2017-2018 The Software Heritage developers | # Copyright (C) 2017-2018 The Software Heritage developers | ||||
# See the AUTHORS file at the top-level directory of this distribution | # See the AUTHORS file at the top-level directory of this distribution | ||||
# License: GNU General Public License version 3, or any later version | # License: GNU General Public License version 3, or any later version | ||||
# See top-level LICENSE file for more information | # See top-level LICENSE file for more information | ||||
import unittest | import unittest | ||||
from unittest.mock import patch | from unittest.mock import patch | ||||
from swh.indexer import fossology_license | |||||
from swh.indexer.fossology_license import ( | from swh.indexer.fossology_license import ( | ||||
FossologyLicenseIndexer, FossologyLicenseRangeIndexer, | FossologyLicenseIndexer, FossologyLicenseRangeIndexer, | ||||
compute_license | compute_license | ||||
) | ) | ||||
from swh.indexer.tests.utils import ( | from swh.indexer.tests.utils import ( | ||||
SHA1_TO_LICENSES, CommonContentIndexerTest, CommonContentIndexerRangeTest, | SHA1_TO_LICENSES, CommonContentIndexerTest, CommonContentIndexerRangeTest, | ||||
CommonIndexerWithErrorsTest, CommonIndexerNoTool, NoDiskIndexer, | CommonIndexerWithErrorsTest, CommonIndexerNoTool, NoDiskIndexer, | ||||
Show All 19 Lines | def test_compute_license(self, mock_subprocess): | ||||
actual_result = compute_license(path, log=None) | actual_result = compute_license(path, log=None) | ||||
self.assertEqual(actual_result, { | self.assertEqual(actual_result, { | ||||
'licenses': output, | 'licenses': output, | ||||
'path': path, | 'path': path, | ||||
}) | }) | ||||
class InjectLicenseIndexer: | def mock_compute_license(path, log=None): | ||||
"""Override license computations. | |||||
""" | |||||
def compute_license(self, path, log=None): | |||||
"""path is the content identifier | """path is the content identifier | ||||
""" | """ | ||||
if isinstance(id, bytes): | if isinstance(id, bytes): | ||||
path = path.decode('utf-8') | path = path.decode('utf-8') | ||||
return { | return { | ||||
'licenses': SHA1_TO_LICENSES.get(path) | 'licenses': SHA1_TO_LICENSES.get(path) | ||||
} | } | ||||
class FossologyLicenseTestIndexer( | class FossologyLicenseTestIndexer( | ||||
NoDiskIndexer, InjectLicenseIndexer, FossologyLicenseIndexer): | NoDiskIndexer, FossologyLicenseIndexer): | ||||
"""Specific fossology license whose configuration is enough to satisfy | """Specific fossology license whose configuration is enough to satisfy | ||||
the indexing checks. | the indexing checks. | ||||
""" | """ | ||||
def parse_config_file(self, *args, **kwargs): | def parse_config_file(self, *args, **kwargs): | ||||
return { | return { | ||||
**BASE_TEST_CONFIG, | **BASE_TEST_CONFIG, | ||||
'workdir': '/nowhere', | 'workdir': '/nowhere', | ||||
Show All 15 Lines | class TestFossologyLicenseIndexer(CommonContentIndexerTest, unittest.TestCase): | ||||
""" | """ | ||||
def get_indexer_results(self, ids): | def get_indexer_results(self, ids): | ||||
yield from self.idx_storage.content_fossology_license_get(ids) | yield from self.idx_storage.content_fossology_license_get(ids) | ||||
def setUp(self): | def setUp(self): | ||||
super().setUp() | super().setUp() | ||||
# replace actual license computation with a mock | |||||
self.orig_compute_license = fossology_license.compute_license | |||||
fossology_license.compute_license = mock_compute_license | |||||
self.indexer = FossologyLicenseTestIndexer() | self.indexer = FossologyLicenseTestIndexer() | ||||
self.idx_storage = self.indexer.idx_storage | self.idx_storage = self.indexer.idx_storage | ||||
fill_storage(self.indexer.storage) | fill_storage(self.indexer.storage) | ||||
fill_obj_storage(self.indexer.objstorage) | fill_obj_storage(self.indexer.objstorage) | ||||
self.id0 = '01c9379dfc33803963d07c1ccc748d3fe4c96bb5' | self.id0 = '01c9379dfc33803963d07c1ccc748d3fe4c96bb5' | ||||
self.id1 = '688a5ef812c53907562fe379d4b3851e69c7cb15' | self.id1 = '688a5ef812c53907562fe379d4b3851e69c7cb15' | ||||
self.id2 = 'da39a3ee5e6b4b0d3255bfef95601890afd80709' # empty content | self.id2 = 'da39a3ee5e6b4b0d3255bfef95601890afd80709' # empty content | ||||
Show All 11 Lines | def setUp(self): | ||||
'licenses': SHA1_TO_LICENSES[self.id1], | 'licenses': SHA1_TO_LICENSES[self.id1], | ||||
}, | }, | ||||
self.id2: { | self.id2: { | ||||
'tool': tool, | 'tool': tool, | ||||
'licenses': SHA1_TO_LICENSES[self.id2], | 'licenses': SHA1_TO_LICENSES[self.id2], | ||||
} | } | ||||
} | } | ||||
def tearDown(self): | |||||
super().tearDown() | |||||
fossology_license.compute_license = self.orig_compute_license | |||||
class FossologyLicenseRangeIndexerTest( | class FossologyLicenseRangeIndexerTest( | ||||
NoDiskIndexer, InjectLicenseIndexer, FossologyLicenseRangeIndexer): | NoDiskIndexer, FossologyLicenseRangeIndexer): | ||||
"""Testing the range indexer on fossology license. | """Testing the range indexer on fossology license. | ||||
""" | """ | ||||
def parse_config_file(self, *args, **kwargs): | def parse_config_file(self, *args, **kwargs): | ||||
return { | return { | ||||
**BASE_TEST_CONFIG, | **BASE_TEST_CONFIG, | ||||
'workdir': '/nowhere', | 'workdir': '/nowhere', | ||||
'tools': { | 'tools': { | ||||
Show All 14 Lines | class TestFossologyLicenseRangeIndexer( | ||||
- new data within range are indexed | - new data within range are indexed | ||||
- no data outside a range are indexed | - no data outside a range are indexed | ||||
- with filtering existing indexed data prior to compute new index | - with filtering existing indexed data prior to compute new index | ||||
- without filtering existing indexed data prior to compute new index | - without filtering existing indexed data prior to compute new index | ||||
""" | """ | ||||
def setUp(self): | def setUp(self): | ||||
super().setUp() | super().setUp() | ||||
# replace actual license computation with a mock | |||||
self.orig_compute_license = fossology_license.compute_license | |||||
fossology_license.compute_license = mock_compute_license | |||||
self.indexer = FossologyLicenseRangeIndexerTest() | self.indexer = FossologyLicenseRangeIndexerTest() | ||||
fill_storage(self.indexer.storage) | fill_storage(self.indexer.storage) | ||||
fill_obj_storage(self.indexer.objstorage) | fill_obj_storage(self.indexer.objstorage) | ||||
self.id0 = '01c9379dfc33803963d07c1ccc748d3fe4c96bb5' | self.id0 = '01c9379dfc33803963d07c1ccc748d3fe4c96bb5' | ||||
self.id1 = '02fb2c89e14f7fab46701478c83779c7beb7b069' | self.id1 = '02fb2c89e14f7fab46701478c83779c7beb7b069' | ||||
self.id2 = '103bc087db1d26afc3a0283f38663d081e9b01e6' | self.id2 = '103bc087db1d26afc3a0283f38663d081e9b01e6' | ||||
tool_id = self.indexer.tool['id'] | tool_id = self.indexer.tool['id'] | ||||
Show All 10 Lines | def setUp(self): | ||||
}, | }, | ||||
self.id2: { | self.id2: { | ||||
'id': self.id2, | 'id': self.id2, | ||||
'indexer_configuration_id': tool_id, | 'indexer_configuration_id': tool_id, | ||||
'licenses': SHA1_TO_LICENSES[self.id2] | 'licenses': SHA1_TO_LICENSES[self.id2] | ||||
} | } | ||||
} | } | ||||
def tearDown(self): | |||||
super().tearDown() | |||||
fossology_license.compute_license = self.orig_compute_license | |||||
class FossologyLicenseIndexerUnknownToolTestStorage( | class FossologyLicenseIndexerUnknownToolTestStorage( | ||||
CommonIndexerNoTool, FossologyLicenseTestIndexer): | CommonIndexerNoTool, FossologyLicenseTestIndexer): | ||||
"""Fossology license indexer with wrong configuration""" | """Fossology license indexer with wrong configuration""" | ||||
class FossologyLicenseRangeIndexerUnknownToolTestStorage( | class FossologyLicenseRangeIndexerUnknownToolTestStorage( | ||||
CommonIndexerNoTool, FossologyLicenseRangeIndexerTest): | CommonIndexerNoTool, FossologyLicenseRangeIndexerTest): | ||||
"""Fossology license range indexer with wrong configuration""" | """Fossology license range indexer with wrong configuration""" | ||||
class TestFossologyLicenseIndexersErrors( | class TestFossologyLicenseIndexersErrors( | ||||
CommonIndexerWithErrorsTest, unittest.TestCase): | CommonIndexerWithErrorsTest, unittest.TestCase): | ||||
"""Test the indexer raise the right errors when wrongly initialized""" | """Test the indexer raise the right errors when wrongly initialized""" | ||||
Indexer = FossologyLicenseIndexerUnknownToolTestStorage | Indexer = FossologyLicenseIndexerUnknownToolTestStorage | ||||
RangeIndexer = FossologyLicenseRangeIndexerUnknownToolTestStorage | RangeIndexer = FossologyLicenseRangeIndexerUnknownToolTestStorage |