def test_get_types(self): # Setup type_def_1 = TypeDefinition('type-1', 'Type 1', 'Type 1', [], [], []) type_def_2 = TypeDefinition('type-2', 'Type 2', 'Type 2', [], [], []) types_db._create_or_update_type(type_def_1) types_db._create_or_update_type(type_def_2) # Test status, body = self.get('/v2/plugins/types/') # Verify self.assertEqual(200, status) self.assertEqual(2, len(body)) # the order isn't guaranteed, so we adjust here if necessary if body[0]['id'] == 'type-2': body.reverse() self.assertEqual(body[0]['_href'], '/v2/plugins/types/type-1/') self.assertEqual(body[0]['id'], 'type-1') self.assertEqual(body[0]['display_name'], 'Type 1') self.assertEqual(body[1]['_href'], '/v2/plugins/types/type-2/') self.assertEqual(body[1]['id'], 'type-2') self.assertEqual(body[1]['display_name'], 'Type 2')
def test_drop_indexes(self): """ Tests updating indexes on an existing collection with different indexes correctly changes them. """ # Setup old_key = ['compound_1', 'compound_2'] type_def = TypeDefinition('rpm', 'RPM', 'RPM Packages', old_key, None, []) types_db._update_unit_key(type_def) # Test new_key = ['new_1'] type_def.unit_key = new_key types_db._drop_indexes(type_def) types_db._update_unit_key(type_def) # Verify collection_name = types_db.unit_collection_name(type_def.id) collection = pulp_db.get_collection(collection_name) index_dict = collection.index_information() self.assertEqual(2, len(index_dict)) # default (_id) + new one
def test_create_or_update_existing_type_collection(self): """ Tests calling create_or_update with a change to an existing type collection is successful. """ # Setup type_def = TypeDefinition('rpm', 'RPM', 'RPM Packages', ['name'], ['name'], []) types_db._create_or_update_type(type_def) # Test type_def.display_name = 'new-name' type_def.description = 'new-description' type_def.unit_key = 'new-key' type_def.search_indexes = None types_db._create_or_update_type(type_def) # Verify # Present in types collection all_types = list(ContentType.get_collection().find()) self.assertEqual(1, len(all_types)) found = all_types[0] self.assertEqual(type_def.id, found['id']) self.assertEqual(type_def.display_name, found['display_name']) self.assertEqual(type_def.description, found['description']) self.assertEqual(type_def.unit_key, found['unit_key']) self.assertEqual(type_def.search_indexes, found['search_indexes']) # Type collection exists collection_name = types_db.unit_collection_name(type_def.id) self.assertTrue(collection_name in pulp_db.get_database().collection_names())
def test_types(self): """ Tests retrieving all types in the database. """ # Setup type_def_1 = TypeDefinition('type-1', 'Type 1', 'Type 1', [], [], []) type_def_2 = TypeDefinition('type-2', 'Type 2', 'Type 2', [], [], []) types_db._create_or_update_type(type_def_1) types_db._create_or_update_type(type_def_2) # Test found_defs = self.manager.types() # Verify self.assertEqual(2, len(found_defs)) for type_def in [type_def_1, type_def_2]: found_def = [t for t in found_defs if t['id'] == type_def.id][0] self.assertEqual(found_def['id'], type_def.id) self.assertEqual(found_def['display_name'], type_def.display_name) self.assertEqual(found_def['description'], type_def.description) self.assertEqual(found_def['unit_key'], type_def.unit_key) self.assertEqual(found_def['search_indexes'], type_def.search_indexes) self.assertEqual(found_def['referenced_types'], type_def.referenced_types)
def test_create_or_update_existing_type_collection(self): """ Tests calling create_or_update with a change to an existing type collection is successful. """ # Setup type_def = TypeDefinition("rpm", "RPM", "RPM Packages", ["name"], ["name"], []) types_db._create_or_update_type(type_def) # Test type_def.display_name = "new-name" type_def.description = "new-description" type_def.unit_key = "new-key" type_def.search_indexes = None types_db._create_or_update_type(type_def) # Verify # Present in types collection all_types = list(ContentType.get_collection().find()) self.assertEqual(1, len(all_types)) found = all_types[0] self.assertEqual(type_def.id, found["id"]) self.assertEqual(type_def.display_name, found["display_name"]) self.assertEqual(type_def.description, found["description"]) self.assertEqual(type_def.unit_key, found["unit_key"]) self.assertEqual(type_def.search_indexes, found["search_indexes"]) # Type collection exists collection_name = types_db.unit_collection_name(type_def.id) self.assertTrue(collection_name in pulp_db.get_database().collection_names())
def test_update_unit_key_multiple_fields(self): """ Tests that a multiple field unit key is built as a single, compound index """ # Setup unit_key = ['compound_1', 'compound_2'] type_def = TypeDefinition('rpm', 'RPM', 'RPM Packages', unit_key, None, []) # Test types_db._update_unit_key(type_def) # Verify collection_name = types_db.unit_collection_name(type_def.id) collection = pulp_db.get_collection(collection_name) index_dict = collection.index_information() self.assertEqual(2, len(index_dict)) # default (_id) + unit key index = index_dict['compound_1_1_compound_2_1'] self.assertTrue(index['unique']) keys = index['key'] self.assertEqual(2, len(keys)) self.assertEqual('compound_1', keys[0][0]) self.assertEqual(types_db.ASCENDING, keys[0][1]) self.assertEqual('compound_2', keys[1][0]) self.assertEqual(types_db.ASCENDING, keys[1][1])
def test_all_type_ids(self): """ Tests listing all type IDs. """ # Setup types_db._create_or_update_type(TypeDefinition('a', 'A', 'A', [], [], [])) types_db._create_or_update_type(TypeDefinition('b', 'B', 'B', [], [], [])) # Test type_ids = types_db.all_type_ids() # Verify self.assertEqual(2, len(type_ids)) self.assertTrue('a' in type_ids) self.assertTrue('b' in type_ids)
def test_create_or_update_type_collection(self): """ Tests the call to create a new type collection works. """ # Setup type_def = TypeDefinition('rpm', 'RPM', 'RPM Packages', ['name'], ['name'], []) # Test types_db._create_or_update_type(type_def) # Verify # Present in types collection all_types = list(ContentType.get_collection().find()) self.assertEqual(1, len(all_types)) found = all_types[0] self.assertEqual(type_def.id, found['id']) self.assertEqual(type_def.display_name, found['display_name']) self.assertEqual(type_def.description, found['description']) self.assertEqual(type_def.unit_key, found['unit_key']) self.assertEqual(type_def.search_indexes, found['search_indexes']) # Type collection exists collection_name = types_db.unit_collection_name(type_def.id) self.assertTrue( collection_name in pulp_db.database().collection_names())
def test_update_unit_key_single_field(self): """ Tests a single field unit key is handled correctly. """ # Setup unit_key = 'individual_1', type_def = TypeDefinition('rpm', 'RPM', 'RPM Packages', unit_key, None, []) # Test types_db._update_unit_key(type_def) # Verify collection_name = types_db.unit_collection_name(type_def.id) collection = pulp_db.get_collection(collection_name) index_dict = collection.index_information() self.assertEqual(2, len(index_dict)) # default (_id) + unit key index = index_dict['individual_1_1'] self.assertTrue(index['unique']) keys = index['key'] self.assertEqual(1, len(keys)) self.assertEqual('individual_1', keys[0][0]) self.assertEqual(types_db.ASCENDING, keys[0][1])
def test_create_or_update_existing_type_collection(self): """ Tests calling create_or_update with a change to an existing type collection is successful. """ # Setup type_def = TypeDefinition('rpm', 'RPM', 'RPM Packages', ['name'], ['name'], []) types_db._create_or_update_type(type_def) # Test type_def.display_name = 'new-name' type_def.description = 'new-description' type_def.unit_key = 'new-key' type_def.search_indexes = None types_db._create_or_update_type(type_def) # Verify # Present in types collection all_types = list(ContentType.get_collection().find()) self.assertEqual(1, len(all_types)) found = all_types[0] self.assertEqual(type_def.id, found['id']) self.assertEqual(type_def.display_name, found['display_name']) self.assertEqual(type_def.description, found['description']) self.assertEqual(type_def.unit_key, found['unit_key']) self.assertEqual(type_def.search_indexes, found['search_indexes']) # Type collection exists collection_name = types_db.unit_collection_name(type_def.id) self.assertTrue( collection_name in pulp_db.database().collection_names())
def test_get_type(self): # Setup type_def_1 = TypeDefinition('type-1', 'Type 1', 'Type 1', [], [], []) types_db._create_or_update_type(type_def_1) # Test status, body = self.get('/v2/plugins/types/type-1/') # Verify self.assertEqual(200, status) self.assertEqual(body['_href'], '/v2/plugins/types/type-1/') self.assertEqual(body['id'], 'type-1') self.assertEqual(body['display_name'], 'Type 1')
def test_type_units_unit_key(self): """ Tests the syntactic sugar method for retrieving unit key on a type. """ # Setup type_def = TypeDefinition('rpm', 'RPM', 'RPM Packages', ['unique_1', 'unique_2'], ['name'], []) types_db._create_or_update_type(type_def) # Test unit_key = types_db.type_units_unit_key('rpm') # Verify self.assertEqual(type_def.unit_key, unit_key)
def test_all_type_collection_names(self): """ Tests listing all type collections. """ # Setup type_def = TypeDefinition('rpm', 'RPM', 'RPM Packages', ['name'], ['name'], []) types_db._create_or_update_type(type_def) # Test all_names = types_db.all_type_collection_names() # Verify self.assertEqual(1, len(all_names)) self.assertEqual(types_db.unit_collection_name(type_def.id), all_names[0])
def test_update_failed_create(self): """ Simulates a failure to create a collection by passing in a bad ID for the definition. """ # Setup busted = TypeDefinition('!@#$%^&*()', 'Busted', 'Busted', None, None, []) defs = [DEF_1, busted] # Tests try: types_db.update_database(defs) self.fail('Update with a failed create did not raise exception') except types_db.UpdateFailed, e: self.assertEqual(1, len(e.type_definitions)) self.assertEqual(busted, e.type_definitions[0]) print(e)
def test_check_content_definitions_nothing_old(self, mock_type_definition, mock_parser): """ Test that when the content type from the database matches the TypeDefinition, an empty list is returned. """ fake_type = { 'id': 'steve_holt', 'display_name': 'STEVE HOLT!', 'description': 'STEVE HOLT!', 'unit_key': ['STEVE HOLT!'], 'search_indexes': ['STEVE HOLT!'], 'referenced_types': ['STEVE HOLT!'], } mock_type_definition.return_value = fake_type type_definition = TypeDefinition('steve_holt', 'STEVE HOLT!', 'STEVE HOLT!', 'STEVE HOLT!', 'STEVE HOLT!', 'STEVE HOLT!') result = api._check_content_definitions([type_definition]) self.assertEquals(0, len(result))
def test_update_search_indexes(self): """ Tests that the unique index creation on a new collection is successful. This will test both single key and compound indexes to ensure mongo handles them successfully. """ # Setup search_indexes = [ ['compound_1', 'compound_2'], 'individual_1' ] type_def = TypeDefinition('rpm', 'RPM', 'RPM Packages', None, search_indexes, []) # Test types_db._update_search_indexes(type_def) # Verify collection_name = types_db.unit_collection_name(type_def.id) collection = pulp_db.get_collection(collection_name) index_dict = collection.index_information() self.assertEqual(3, len(index_dict)) # default (_id) + definition ones # Verify individual index index = index_dict['individual_1_1'] keys = index['key'] self.assertEqual(1, len(keys)) self.assertEqual('individual_1', keys[0][0]) self.assertEqual(types_db.ASCENDING, keys[0][1]) # Verify compound index index = index_dict['compound_1_1_compound_2_1'] keys = index['key'] self.assertEqual(2, len(keys)) self.assertEqual('compound_1', keys[0][0]) self.assertEqual(types_db.ASCENDING, keys[0][1]) self.assertEqual('compound_2', keys[1][0]) self.assertEqual(types_db.ASCENDING, keys[1][1])
def _generate_plugin_definitions(): """ Use entry points to get the information about available content unit types :return: A list of content unit types :rtype: list of TypeDefinition """ definitions = [] plugin_manager = PluginManager() for unit_type, model_class in plugin_manager.unit_models.items(): content_type_id = unit_type display_name = getattr(model_class, 'unit_display_name', unit_type) description = getattr(model_class, 'unit_description', '') referenced_types = getattr(model_class, 'unit_referenced_types', []) unit_key = list(getattr(model_class, 'unit_key_fields', [])) search_indexes = list(model_class._meta.get('indexes', [])) definition = TypeDefinition(content_type_id, display_name, description, unit_key, search_indexes, referenced_types) definitions.append(definition) return definitions
def test_check_content_definitions_old(self, mock_type_definition): """ Test that when the content type from the database doesn't match the TypeDefinition, the list contains that content type. """ fake_type = { 'id': 'gob', 'display_name': 'Trickster', 'description': 'Trickster', 'unit_key': ['Trickster'], 'search_indexes': ['Trickster'], 'referenced_types': ['Trickster'], } mock_type_definition.return_value = fake_type type_definition = TypeDefinition('gob', 'STEVE HOLT!', 'STEVE HOLT!', 'STEVE HOLT!', 'STEVE HOLT!', 'STEVE HOLT!') result = api._check_content_definitions([type_definition]) self.assertEquals(1, len(result)) self.assertEquals(result[0], type_definition)
import mock from pulp.plugins.types import database as types_db from pulp.plugins.types.model import TypeDefinition from pulp.server.db import model from pulp.server.db.connection import get_collection from pulp.server.db.model.repository import RepoContentUnit from pulp.server.db.migrate.models import _import_all_the_way from pulp.server.managers import factory from pulp_rpm.common import ids from pulp_rpm.devel import rpm_support_base from pulp.plugins.loader import api # Trimmed down versions of the type defs TYPE_DEF_GROUP = TypeDefinition('package_group', '', '', ['id', 'repo_id'], [], []) TYPE_DEF_CATEGORY = TypeDefinition('package_category', '', '', ['id', 'repo_id'], [], []) class Migration0004Tests(rpm_support_base.PulpRPMTests): def setUp(self): super(Migration0004Tests, self).setUp() # Special way to import modules that start with a number self.migration = _import_all_the_way( 'pulp_rpm.plugins.migrations.0004_pkg_group_category_repoid') factory.initialize() api.initialize(False) types_db.update_database([TYPE_DEF_GROUP, TYPE_DEF_CATEGORY])
import tempfile import traceback from pprint import pformat import base from pulp.server import exceptions as pulp_exceptions from pulp.plugins.types import database as content_type_db from pulp.plugins.types.model import TypeDefinition from pulp.server.db.model.repository import RepoContentUnit from pulp.server.managers import factory as manager_factory from pulp.server.managers.content.orphan import OrphanManager # globals and constants -------------------------------------------------------- PHONY_TYPE_1 = TypeDefinition('phony_type_1', 'Phony Type 1', None, 'name', [], []) PHONY_TYPE_2 = TypeDefinition('phony_type_2', 'Phony Type 2', None, 'name', [], []) PHONY_REPO_ID = 'phony_repo' PHONY_USER_ID = 'orphan_manager_unittests' # content generation ----------------------------------------------------------- def gen_content_unit(content_type_id, content_root, name=None): name = name or ''.join(random.sample(string.ascii_letters, 5)) path = os.path.join(content_root, name) file = open(path, mode='w') file.write('') file.close()
class BaseProfilerConduitTests(base.PulpServerTests): CONSUMER_ID = 'test-consumer' REPO_ID = 'test-repo' DISTRIBUTOR_ID = 'test-distributor' NOTIFY_AGENT = True BINDING_CONFIG = {'x': 'x'} TYPE_1_DEF = TypeDefinition('type-1', 'Type 1', 'One', ['key-1'], [], []) TYPE_2_DEF = TypeDefinition('type-2', 'Type 2', 'Two', ['key-2'], [], []) PROFILE = {'name': 'zsh', 'version': '1.0'} UNIT_ID = 0 def setUp(self): super(BaseProfilerConduitTests, self).setUp() Consumer.get_collection().remove() Repo.get_collection().remove() RepoDistributor.get_collection().remove() Bind.get_collection().remove() RepoContentUnit.get_collection().remove() UnitProfile.get_collection().remove() plugin_api._create_manager() typedb.update_database([self.TYPE_1_DEF, self.TYPE_2_DEF]) mock_plugins.install() def tearDown(self): super(BaseProfilerConduitTests, self).tearDown() Consumer.get_collection().remove() Repo.get_collection().remove() RepoDistributor.get_collection().remove() Bind.get_collection().remove() RepoContentUnit.get_collection().remove() UnitProfile.get_collection().remove() typedb.clean() factory.reset() mock_plugins.reset() def populate(self, additional_key=None): self.populate_consumer() self.populate_repository() self.populate_bindings() self.populate_units('key-1', self.TYPE_1_DEF, additional_key) self.populate_units('key-2', self.TYPE_2_DEF, additional_key) self.populate_profile() def populate_consumer(self): manager = factory.consumer_manager() manager.register(self.CONSUMER_ID) def populate_repository(self): config = {'key1': 'value1', 'key2': None} manager = factory.repo_manager() manager.create_repo(self.REPO_ID) manager = factory.repo_distributor_manager() manager.add_distributor(self.REPO_ID, 'mock-distributor', config, True, distributor_id=self.DISTRIBUTOR_ID) def populate_bindings(self): manager = factory.consumer_bind_manager() manager.bind(self.CONSUMER_ID, self.REPO_ID, self.DISTRIBUTOR_ID, self.NOTIFY_AGENT, self.BINDING_CONFIG) def populate_units(self, key, typedef, additional_key=None): for i in range(1, 10): unit_id = 'unit-%s' % self.UNIT_ID md = {key: str(i)} if additional_key: md[additional_key] = str(i) manager = factory.content_manager() manager.add_content_unit(typedef.id, unit_id, md) manager = factory.repo_unit_association_manager() manager.associate_unit_by_id(self.REPO_ID, typedef.id, unit_id) self.UNIT_ID += 1 def populate_profile(self): manager = factory.consumer_profile_manager() manager.update(self.CONSUMER_ID, self.TYPE_1_DEF.id, self.PROFILE) def test_get_bindings(self): # Setup self.populate() # Test conduit = ProfilerConduit() binds = conduit.get_bindings(self.CONSUMER_ID) # Verify self.assertEquals(1, len(binds)) self.assertTrue(binds[0], self.REPO_ID) def test_get_units(self): # Setup self.populate() # Test conduit = ProfilerConduit() criteria = UnitAssociationCriteria(type_ids=[self.TYPE_1_DEF.id]) units = conduit.get_units(self.REPO_ID, criteria) # Verify self.assertEquals(len(units), 9) def test_get_repo_units(self): # Setup self.populate() # Test conduit = ProfilerConduit() units1 = conduit.get_repo_units(self.REPO_ID, content_type_id=self.TYPE_1_DEF.id, additional_unit_fields=[]) units2 = conduit.get_repo_units(self.REPO_ID, content_type_id=self.TYPE_2_DEF.id, additional_unit_fields=[]) # Verify that all the units in the repo with given type are returned along with unit_key self.assertEquals(len(units1), 9) for u in units1: self.assertTrue('key-1' in u.unit_key) self.assertFalse('key-2' in u.unit_key) self.assertEquals(len(units2), 9) for u in units2: self.assertTrue('key-2' in u.unit_key) self.assertFalse('key-1' in u.unit_key) def test_get_repo_units_additional_field(self): # Setup self.populate(additional_key='extra_field') # Test conduit = ProfilerConduit() units = conduit.get_repo_units(self.REPO_ID, content_type_id=self.TYPE_1_DEF.id, additional_unit_fields=['extra_field']) # Verify that all the units in the repo with given type are returned along with unit_key and # extra field self.assertEquals(len(units), 9) for u in units: self.assertTrue('key-1' in u.unit_key) self.assertTrue('extra_field' in u.metadata)
# There is NO WARRANTY for this software, express or implied, # including the implied warranties of MERCHANTABILITY, # NON-INFRINGEMENT, or FITNESS FOR A PARTICULAR PURPOSE. You should # have received a copy of GPLv2 along with this software; if not, see # http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt. import base import pulp.plugins.types.database as types_db from pulp.plugins.types.model import TypeDefinition from pulp.server.db.model.content import ContentType import pulp.server.db.connection as pulp_db # -- constants ----------------------------------------------------------------- DEF_1 = TypeDefinition('def_1', 'Definition 1', 'Test definition', 'single_1', ['search_1'], []) DEF_2 = TypeDefinition('def_2', 'Definition 2', 'Test definition', ['single_1'], ['search_1'], []) DEF_3 = TypeDefinition('def_3', 'Definition 3', 'Test definition', ['compound_1', 'compound_2'], ['search_1'], []) DEF_4 = TypeDefinition('def_4', 'Definition 4', 'Test definition', 'single_1', ['search_1'], []) # -- test cases ---------------------------------------------------------------- class TypesDatabaseTests(base.PulpServerTests): def clean(self): super(TypesDatabaseTests, self).clean() types_db.clean()