def test_move_directory_contents(self):
        test_old_publish_dir = tempfile.mkdtemp(prefix='test_0002_migration_old')
        old_http_publish_dir = os.path.join(test_old_publish_dir, 'http', 'repos')
        old_https_publish_dir = os.path.join(test_old_publish_dir, 'https', 'repos')
        os.makedirs(old_http_publish_dir)
        os.makedirs(old_https_publish_dir)

        test_new_publish_dir = tempfile.mkdtemp(prefix='test_0002_migration_new')
        test_new_publish_puppet_dir = os.path.join(test_new_publish_dir, 'puppet')
        # on a real system, this dir is created by the rpm
        os.mkdir(test_new_publish_puppet_dir)

        new_http_publish_dir = os.path.join(test_new_publish_puppet_dir, 'http', 'repos')
        new_https_publish_dir = os.path.join(test_new_publish_puppet_dir, 'https', 'repos')
        # put a file in the top-level dir to ensure it gets copied over too.
        # It is not typical to have a file there but we should move it over
        # just in case.
        open(os.path.join(test_old_publish_dir, 'some_file'), 'w').close()

        migration = _import_all_the_way('pulp_puppet.plugins.migrations.0002_puppet_'
                                        'publishing_directory_change')

        migration.move_directory_contents(test_old_publish_dir, test_new_publish_puppet_dir)

        self.assertTrue(os.path.exists(new_http_publish_dir))
        self.assertTrue(os.path.exists(new_https_publish_dir))
        self.assertTrue(os.path.exists(os.path.join(test_new_publish_puppet_dir, 'some_file')))
        # bz 1153072 - user needs to clear this dir manually
        self.assertTrue(os.path.exists(test_old_publish_dir))
        for (root, files, dirs) in os.walk(test_old_publish_dir):
            self.assertTrue(files == [])
            self.assertTrue(dirs == [])
Exemplo n.º 2
0
    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()
        types_db.update_database([TYPE_DEF_GROUP, TYPE_DEF_CATEGORY])

        # Create the repositories necessary for the tests
        self.source_repo_id = 'source-repo'  # where units were copied from with the bad code
        self.dest_repo_id = 'dest-repo'  # where bad units were copied to

        source_repo = Repo(self.source_repo_id, '')
        Repo.get_collection().insert(source_repo, safe=True)

        dest_repo = Repo(self.dest_repo_id, '')
        Repo.get_collection().insert(dest_repo, safe=True)

        source_importer = RepoImporter(self.source_repo_id, 'yum_importer', 'yum_importer', {})
        RepoImporter.get_collection().insert(source_importer, safe=True)

        dest_importer = RepoImporter(self.dest_repo_id, 'yum_importer', 'yum_importer', {})
        RepoImporter.get_collection().insert(dest_importer, safe=True)
    def test_migrate(self):
        # Let's set up some package groups, some with the new way, and some with the old way
        # We'll only put the name and conditional_package_names attributes since the
        # migration only touches those fields
        package_groups = [
            {"name": "v1_style_1", "conditional_package_names" : {'a': 1, 'b': 2}},
            {"name": "v1_style_2", "conditional_package_names" : {'b': 1, 'c': 3}},
            {"name": "v2_style", "conditional_package_names" : [['d', 4], ['e', 5]]}]
        for package_group in package_groups:
            self.package_group_collection.insert(package_group)
        migration = _import_all_the_way(
            'pulp_rpm.migrations.0012_conditional_package_names_v1_v2_upgrade')

        # Run the migration
        migration.migrate()

        # Inspect the package groups
        expected_package_groups = [
            {"name": "v1_style_1", "conditional_package_names" : [['a', 1], ['b', 2]]},
            {"name": "v1_style_2", "conditional_package_names" : [['b', 1], ['c', 3]]},
            {"name": "v2_style", "conditional_package_names" : [['d', 4], ['e', 5]]}]
        for expected_package_group in expected_package_groups:
            package_group = self.package_group_collection.find_one(
                {'name': expected_package_group['name']})
            self.assertTrue(isinstance(package_group['conditional_package_names'], list))
            self.assertEqual(len(package_group['conditional_package_names']),
                             len(expected_package_group['conditional_package_names']))
            # Since dictionaries don't have ordering, we cannot assert that the expected
            # list is the same as the actual list. Instead, we assert that the lengths are
            # the same, and that all the expected items appear in the actual
            for pair in expected_package_group['conditional_package_names']:
                self.assertTrue(pair in package_group['conditional_package_names'])
    def test_migrate(self):
        migration = _import_all_the_way('pulp_rpm.migrations.0009_iso_importer_config_keys')

        # Run the migration
        migration.migrate()

        # Verify the proxy repo
        proxy = self.repo_importers.find_one({'repo_id': 'proxy'})
        self.assertEqual(proxy['importer_type_id'], 'iso_importer')
        self.assertEqual(proxy['last_sync'], '2013-04-09T16:57:06-04:00')
        self.assertEqual(proxy['scheduled_syncs'], [])
        self.assertEqual(proxy['scratchpad'], None)
        self.assertEqual(dict(proxy['config']), {
            u'proxy_username': u'rbarlow',
            u'feed': u'http://pkilambi.fedorapeople.org/test_file_repo/',
            u'proxy_host': u'localhost', u'proxy_password': u'password',
            u'proxy_port': 3128, u'id': u'proxy'})
        self.assertEqual(proxy['id'], 'iso_importer')

        # Verify the test repo
        test = self.repo_importers.find_one({'repo_id': 'test'})
        self.assertEqual(test['importer_type_id'], 'iso_importer')
        self.assertEqual(dict(test['config']), {
            u'max_downloads': 42,
            u'feed': u'http://feed.com/isos',
            u'proxy_host': u'proxy.com', u'proxy_username': u'jeeves',
            u'remove_missing': False, u'validate': True})
        self.assertEqual(test['id'], 'iso_importer')

        # verify that the yum repo wasn't touched
        a_yum_repo = self.repo_importers.find_one({'repo_id': 'a_yum_repo'})
        self.assertEqual(a_yum_repo['importer_type_id'], 'yum_importer')
        self.assertEqual(dict(a_yum_repo['config']),
                         {u'feed_url': u'This should not change.'})
Exemplo n.º 5
0
    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])

        # Create the repositories necessary for the tests
        self.source_repo_id = 'source-repo'  # where units were copied from with the bad code
        self.dest_repo_id = 'dest-repo'  # where bad units were copied to

        source_repo = model.Repository(repo_id=self.source_repo_id)
        source_repo.save()
        dest_repo = model.Repository(repo_id=self.dest_repo_id)
        dest_repo.save()

        source_importer = model.Importer(self.source_repo_id, 'yum_importer', {})
        source_importer.save()

        dest_importer = model.Importer(self.dest_repo_id, 'yum_importer', {})
        dest_importer.save()
    def test_fix_distribution_units(self):
        migration = _import_all_the_way('pulp_rpm.plugins.migrations.0015_fix_distributor_units')

        mock_collection = mock.MagicMock()
        mock_collection.find.return_value = FAKE_DIST_UNITS
        migration._fix_distribution_units(mock_collection)
        mock_collection.find.assert_called_once_with({'files': {'$exists': True}})
        mock_collection.update.assert_called_once_with({'_id': u'6ec94809-6d4f-48cf-9077-88d003eb284e'},
                                                       {'$set': {'files': []}}, safe=True)
    def setUp(self):
        super(BaseMigrationTests, self).setUp()

        self.distributors_collection = RepoDistributor.get_collection()

        self.root_test_dir = tempfile.mkdtemp(prefix='test_0016_migration_')
        self.http_publish_dir = os.path.join(self.root_test_dir, 'http', 'repos')
        self.https_publish_dir = os.path.join(self.root_test_dir, 'https', 'repos')

        self.migration_module = _import_all_the_way(MIGRATION_MODULE)
    def test_migrate(self):
        migration = _import_all_the_way('pulp_rpm.migrations.0013_errata_from_str')

        # Run the migration
        migration.migrate()

        # Verify that this one's "from_str" got changed to "from"
        old = self.collection.find_one({'id': 'RHEA-2012:0003'})
        self.assertEqual(old.get('from', None), '*****@*****.**')
        self.assertFalse('from_str' in old)

        # Verify that this one's "from" is still "from"
        new = self.collection.find_one({'id': 'RHEA-2012:0004'})
        self.assertEqual(new.get('from', None), '*****@*****.**')
        self.assertFalse('from_str' in new)
 def test_migration(self, mock_query_manager, mock_calc_checksum):
     migration = _import_all_the_way('pulp_puppet.plugins.migrations.0001_puppet_'
                                     'module_unit_checksum')
     storage_path = '/foo/storage'
     mock_calc_checksum.return_value = "foo_checksum"
     unit = {'_storage_path': storage_path}
     mock_query_manager.return_value.get_content_unit_collection.return_value.find.return_value \
         = MockCursor([unit])
     migration.migrate()
     mock_calc_checksum.assert_called_once_with(storage_path)
     mock_query_manager.return_value.get_content_unit_collection.return_value.\
         save.assert_called_once()
     target_unit = mock_query_manager.return_value.get_content_unit_collection.return_value. \
         save.call_args[0][0]
     self.assertEquals(target_unit['checksum'], 'foo_checksum')
     self.assertEquals(target_unit['checksum_type'], constants.DEFAULT_HASHLIB)
    def test_fix_distribution_units_multifile(self):
        """
        verify that we don't remove files that are OK
        """
        migration = _import_all_the_way('pulp_rpm.plugins.migrations.0015_fix_distributor_units')

        mock_collection = mock.MagicMock()
        mock_collection.find.return_value = FAKE_DIST_UNITS_MULTIFILE
        migration._fix_distribution_units(mock_collection)
        mock_collection.find.assert_called_once_with({'files': {'$exists': True}})
        mock_collection.update.assert_called_once_with({'_id': u'6ec94809-6d4f-48cf-9077-88d003eb284e'},
                                                       {'$set':
                                                        {'files': [{'downloadurl': 'http ://fake-url/os/another_file',
                                                                    'item_type': 'distribution',
                                                                    'fileName': 'another_file'}]}},
                                                       safe=True)
    def test_migrate(self):
        migration = _import_all_the_way("pulp_rpm.plugins.migrations.0009_iso_importer_config_keys")

        # Run the migration
        migration.migrate()

        # Verify the proxy repo
        proxy = self.repo_importers.find_one({"repo_id": "proxy"})
        self.assertEqual(proxy["importer_type_id"], "iso_importer")
        self.assertEqual(proxy["last_sync"], "2013-04-09T16:57:06-04:00")
        self.assertEqual(proxy["scheduled_syncs"], [])
        self.assertEqual(proxy["scratchpad"], None)
        self.assertEqual(
            dict(proxy["config"]),
            {
                u"proxy_username": u"rbarlow",
                u"feed": u"http://pkilambi.fedorapeople.org/test_file_repo/",
                u"proxy_host": u"localhost",
                u"proxy_password": u"password",
                u"proxy_port": 3128,
                u"id": u"proxy",
            },
        )
        self.assertEqual(proxy["id"], "iso_importer")

        # Verify the test repo
        test = self.repo_importers.find_one({"repo_id": "test"})
        self.assertEqual(test["importer_type_id"], "iso_importer")
        self.assertEqual(
            dict(test["config"]),
            {
                u"max_downloads": 42,
                u"feed": u"http://feed.com/isos",
                u"proxy_host": u"proxy.com",
                u"proxy_username": u"jeeves",
                u"remove_missing": False,
                u"validate": True,
            },
        )
        self.assertEqual(test["id"], "iso_importer")

        # verify that the yum repo wasn't touched
        a_yum_repo = self.repo_importers.find_one({"repo_id": "a_yum_repo"})
        self.assertEqual(a_yum_repo["importer_type_id"], "yum_importer")
        self.assertEqual(dict(a_yum_repo["config"]), {u"feed_url": u"This should not change."})
Exemplo n.º 12
0
    def test_migrate(self):
        # Test
        migration = _import_all_the_way('pulp_rpm.plugins.migrations.0010_yum_importer_config_keys')
        migration.migrate()

        # Verify
        proxy = self.repo_importers.find_one({'repo_id': 'proxy'})
        self.assertTrue('proxy_url' not in proxy['config'])
        self.assertTrue('proxy_user' not in proxy['config'])
        self.assertTrue('proxy_pass' not in proxy['config'])
        self.assertEqual(proxy['config']['proxy_host'], 'localhost')
        self.assertEqual(proxy['config']['proxy_username'], 'user-1')
        self.assertEqual(proxy['config']['proxy_password'], 'pass-1')

        mixed = self.repo_importers.find_one({'repo_id': 'mixed'})
        self.assertTrue('feed_url' not in mixed['config'])
        self.assertTrue('ssl_verify' not in mixed['config'])
        self.assertTrue('num_threads' not in mixed['config'])
        self.assertTrue('verify_checksum' not in mixed['config'])
        self.assertTrue('remove_old' not in mixed['config'])
        self.assertTrue('num_old_packages' not in mixed['config'])
        self.assertEqual(mixed['config']['feed'], 'http://localhost/repo')
        self.assertEqual(mixed['config']['ssl_validation'], True)
        self.assertEqual(mixed['config']['max_downloads'], 42)
        self.assertEqual(mixed['config']['validate'], True)
        self.assertEqual(mixed['config']['remove_missing'], False)
        self.assertEqual(mixed['config']['retain_old_count'], 3)
        self.assertEqual(mixed['config']['skip'], ['rpm'])
        self.assertEqual(mixed['config']['max_speed'], 1024)

        remove = self.repo_importers.find_one({'repo_id': 'remove'})
        self.assertTrue('newest' not in remove['config'])
        self.assertTrue('verify_size' not in remove['config'])
        self.assertTrue('purge_orphaned' not in remove['config'])
        self.assertEqual(remove['config']['feed'], 'localhost')

        no_touch = self.repo_importers.find_one({'repo_id': 'no-touch'})
        self.assertEqual(no_touch['config']['feed'], 'localhost')
        self.assertEqual(no_touch['config']['newest'], True)
        self.assertEqual(no_touch['config']['verify_size'], True)
        self.assertEqual(no_touch['config']['purge_orphaned'], True)
Exemplo n.º 13
0
    def test_migrate(self):
        # Test
        migration = _import_all_the_way("pulp_rpm.plugins.migrations.0010_yum_importer_config_keys")
        migration.migrate()

        # Verify
        proxy = self.repo_importers.find_one({"repo_id": "proxy"})
        self.assertTrue("proxy_url" not in proxy["config"])
        self.assertTrue("proxy_user" not in proxy["config"])
        self.assertTrue("proxy_pass" not in proxy["config"])
        self.assertEqual(proxy["config"]["proxy_host"], "localhost")
        self.assertEqual(proxy["config"]["proxy_username"], "user-1")
        self.assertEqual(proxy["config"]["proxy_password"], "pass-1")

        mixed = self.repo_importers.find_one({"repo_id": "mixed"})
        self.assertTrue("feed_url" not in mixed["config"])
        self.assertTrue("ssl_verify" not in mixed["config"])
        self.assertTrue("num_threads" not in mixed["config"])
        self.assertTrue("verify_checksum" not in mixed["config"])
        self.assertTrue("remove_old" not in mixed["config"])
        self.assertTrue("num_old_packages" not in mixed["config"])
        self.assertEqual(mixed["config"]["feed"], "http://localhost/repo")
        self.assertEqual(mixed["config"]["ssl_validation"], True)
        self.assertEqual(mixed["config"]["max_downloads"], 42)
        self.assertEqual(mixed["config"]["validate"], True)
        self.assertEqual(mixed["config"]["remove_missing"], False)
        self.assertEqual(mixed["config"]["retain_old_count"], 3)
        self.assertEqual(mixed["config"]["skip"], ["rpm"])
        self.assertEqual(mixed["config"]["max_speed"], 1024)

        remove = self.repo_importers.find_one({"repo_id": "remove"})
        self.assertTrue("newest" not in remove["config"])
        self.assertTrue("verify_size" not in remove["config"])
        self.assertTrue("purge_orphaned" not in remove["config"])
        self.assertEqual(remove["config"]["feed"], "localhost")

        no_touch = self.repo_importers.find_one({"repo_id": "no-touch"})
        self.assertEqual(no_touch["config"]["feed"], "localhost")
        self.assertEqual(no_touch["config"]["newest"], True)
        self.assertEqual(no_touch["config"]["verify_size"], True)
        self.assertEqual(no_touch["config"]["purge_orphaned"], True)
    def test_move_directory_contents_and_rename(self):
        test_old_publish_dir = tempfile.mkdtemp(prefix='test_0002_migration_old')
        old_http_publish_dir = os.path.join(test_old_publish_dir, 'http', 'repos')
        old_https_publish_dir = os.path.join(test_old_publish_dir, 'https', 'repos')
        os.makedirs(old_http_publish_dir)
        os.makedirs(old_https_publish_dir)

        test_new_publish_dir = tempfile.mkdtemp(prefix='test_0002_migration_new')
        new_http_publish_dir = os.path.join(test_new_publish_dir, 'puppet', 'http', 'repos')
        new_https_publish_dir = os.path.join(test_new_publish_dir, 'puppet', 'https', 'repos')

        migration = _import_all_the_way('pulp_puppet.plugins.migrations.0002_puppet_'
                                        'publishing_directory_change')
        migration.move_directory_contents_and_rename(test_old_publish_dir,
                                                     test_new_publish_dir,
                                                     os.path.basename(test_old_publish_dir),
                                                     'puppet')

        self.assertTrue(os.path.exists(new_http_publish_dir))
        self.assertTrue(os.path.exists(new_https_publish_dir))
        self.assertFalse(os.path.exists(test_old_publish_dir))
    def test_migrate(self):
        # Setup
        for type_id in (TYPE_ID_RPM, TYPE_ID_SRPM, TYPE_ID_DRPM):
            self.add_sample_data(type_id)

        # Test
        migration = _import_all_the_way('pulp_rpm.plugins.migrations.0008_version_sort_index')
        migration.migrate()

        # Verify

        # The migration should cover these three types, so make sure they were all included
        for type_id in (TYPE_ID_RPM, TYPE_ID_SRPM, TYPE_ID_DRPM):
            collection = types_db.type_units_collection(type_id)

            test_me = collection.find_one({'version': '1.1'})
            self.assertEqual(test_me['version_sort_index'], version_utils.encode('1.1'))
            self.assertEqual(test_me['release_sort_index'], version_utils.encode('1.1'))

            # Make sure the script didn't run on units that already have the indexes
            test_me = collection.find_one({'version': '3.1'})
            self.assertEqual(test_me['version_sort_index'], 'fake')
            self.assertEqual(test_me['release_sort_index'], 'fake')
Exemplo n.º 16
0
from unittest import TestCase

from mock import patch

from pulp.server.db.migrate.models import _import_all_the_way

MODULE = 'pulp_rpm.plugins.migrations.0032_ensure_variant_field'

migration = _import_all_the_way(MODULE)


class TestMigrate(TestCase):
    """
    Test migration 0032.
    """
    @patch(MODULE + '.connection.get_collection')
    def test_migration(self, get_collection):
        # test
        migration.migrate()

        # validation
        get_collection.assert_called_once_with('units_distribution')
        get_collection.return_value.update.assert_called_once_with(
            {'$or': [{
                'variant': None
            }, {
                'variant': {
                    '$exists': False
                }
            }]}, {'$set': {
                'variant': ''
Exemplo n.º 17
0
 def test__import_all_the_way(self):
     """
     Make sure that models._import_all_the_way() gives back the most specific module.
     """
     module = models._import_all_the_way('unit.server.db.migration_packages.z.0001_test')
     self.assertEqual(module.__name__, 'unit.server.db.migration_packages.z.0001_test')
 def test_migration(self, mock_listdir, mock_path_exists, mock_move_directory):
     migration = _import_all_the_way('pulp_puppet.plugins.migrations.0002_puppet_'
                                     'publishing_directory_change')
     migration.migrate()
     mock_listdir.assert_called_once_with('/var/www/pulp_puppet')
     mock_path_exists.assert_called_once_with('/var/www/pulp_puppet')
# -*- coding: utf-8 -*-

import copy
import json
import unittest

import mock
from pulp.server.db.migrate.models import _import_all_the_way

from pulp_rpm.plugins.db.models import RPM, SRPM


migration = _import_all_the_way("pulp_rpm.plugins.migrations.0011_new_importer")


class TestMigrateNewImporter(unittest.TestCase):
    def setUp(self):
        self.rpm_unit = copy.deepcopy(RPM_UNIT)
        self.srpm_unit = copy.deepcopy(SRPM_UNIT)

    @mock.patch.object(migration, "_migrate_collection")
    def test_types(self, mock_add):
        migration.migrate()
        self.assertEqual(mock_add.call_count, 2)
        mock_add.assert_any_call(RPM.TYPE)
        mock_add.assert_any_call(SRPM.TYPE)

    @mock.patch("pulp.plugins.types.database.type_units_collection")
    def test_adds_size(self, mock_collection):
        mock_collection.return_value.find.return_value = [self.rpm_unit]
        self.assertFalse("size" in self.rpm_unit)
Exemplo n.º 20
0
    handle = MagicMock(spec=file_spec)
    handle.write.return_value = None
    fake_file = StringIO(read_data)
    if read_data is None:
        if hasattr(handle, '__enter__'):
            handle.__enter__.return_value = handle
    else:
        if hasattr(handle, '__enter__'):
            handle.__enter__.return_value = fake_file
        handle.read = fake_file.read
    mock.return_value = handle
    return mock


migration = _import_all_the_way('pulp.server.db.migrations.0015_load_content_types')


class TestMigrate(unittest.TestCase):
    @patch('pulp.plugins.types.database._drop_indexes')
    @patch('pulp.plugins.loader.api._generate_plugin_definitions', return_value=[])
    @patch('__builtin__.open', mock_open(read_data=_test_type_json))
    @patch('os.listdir', return_value=['test_type.json'])
    @patch('sys.argv', ["pulp-manage-db"])
    @patch('sys.stdout', MagicMock())
    @patch('pulp.server.db.manage._start_logging')
    def test_migrate(self, start_logging_mock, listdir_mock, mock_plugin_definitions,
                     mock_drop_indices):
        """
        Ensure that migrate() imports types on a clean types database.
        """
 def test_treeinfo_fix_dot_treeinfo(self, mock_strip_treeinfo, mock_walk):
     mock_walk.return_value = [('/some/path/', [], ['file-A', '.treeinfo'])]
     migration = _import_all_the_way(
         'pulp_rpm.plugins.migrations.0015_fix_distributor_units')
     migration._fix_treeinfo_files('/some/path')
     mock_strip_treeinfo.assert_called_once_with('/some/path/.treeinfo')
 def test_treeinfo_fix_dot_treeinfo(self, mock_strip_treeinfo, mock_walk):
     mock_walk.return_value = [('/some/path/', [], ['file-A', '.treeinfo'])]
     migration = _import_all_the_way('pulp_rpm.plugins.migrations.0015_fix_distributor_units')
     migration._fix_treeinfo_files('/some/path')
     mock_strip_treeinfo.assert_called_once_with('/some/path/.treeinfo')
Exemplo n.º 23
0
from pulp.common.compat import unittest

from pulp.server.db.migrate.models import _import_all_the_way, MigrationRemovedError

migration = _import_all_the_way(
    'pulp_rpm.plugins.migrations.0014_migrations_removed')


class TestMigrate(unittest.TestCase):
    def test_raises_exception(self):
        with self.assertRaises(MigrationRemovedError) as assertion:
            migration.migrate()
        self.assertEqual(assertion.exception.migration_version, '0014')
        self.assertEqual(assertion.exception.component_version, '2.8.0')
        self.assertEqual(assertion.exception.min_component_version, '2.4.0')
        self.assertEqual(assertion.exception.component, 'pulp_rpm')
Exemplo n.º 24
0
 def test__import_all_the_way(self):
     """
     Make sure that models._import_all_the_way() gives back the most specific module.
     """
     module = models._import_all_the_way('unit.server.db.migration_packages.z.0001_test')
     self.assertEqual(module.__name__, 'unit.server.db.migration_packages.z.0001_test')
import tempfile
import os
import shutil
import unittest

from pulp.devel.unit import util
from pulp.server.db.migrate.models import _import_all_the_way
import mock

migration = _import_all_the_way(
    'pulp_rpm.plugins.migrations.0020_nested_drpm_directory')
DATA_DIR = os.path.join(os.path.dirname(__file__), '../../../data/')


class TestMigrate(unittest.TestCase):
    def setUp(self):
        # Create the sample environment
        self.working_dir = tempfile.mkdtemp()

        # Create the nested drpm directory
        drpm_foo = os.path.join(self.working_dir, 'drpms', 'drpms', 'foo.drpm')
        util.touch(os.path.join(drpm_foo))

        # create the repomd files & prestodelta files
        self.repodata_dir = os.path.join(self.working_dir, 'repodata')
        os.makedirs(self.repodata_dir)
        self.good_presto = os.path.join(self.working_dir, 'repodata',
                                        'good-prestodelta.xml.gz')
        util.touch(self.good_presto)
        self.bad_presto = os.path.join(self.working_dir, 'repodata',
                                       'bad-prestodelta.xml.gz')
Exemplo n.º 26
0
"""
This module contains tests for pulp.server.db.migrations.0014_pulp_user_metadata.
"""
import unittest

import mock

from pulp.server import constants
from pulp.server.db.migrate.models import _import_all_the_way

migration = _import_all_the_way(
    'pulp.server.db.migrations.0014_pulp_user_metadata')


class TestMigrate(unittest.TestCase):
    """
    Test the migrate() function.
    """
    @mock.patch(
        'pulp.server.db.migrations.0014_pulp_user_metadata.database.type_units_collection'
    )
    @mock.patch(
        'pulp.server.db.migrations.0014_pulp_user_metadata.factory.plugin_manager'
    )
    def test_migrate(self, plugin_manager, type_units_collection):
        """
        Ensure that migrate() runs the correct queries on the correct collections.
        """
        types = [{'id': 'type_a'}, {'id': 'type_2'}]

        class MockPluginManager(object):
Exemplo n.º 27
0
"""
This module contains unit tests for pulp.server.db.migrations.0012_reserved_resources_schema_change.
"""
import unittest

from mock import call, patch

from pulp.server.db.migrate.models import _import_all_the_way

migration = _import_all_the_way(
    'pulp.server.db.migrations.0012_reserved_resources_schema_change')


class TestMigrate(unittest.TestCase):
    """
    Test the migrate() function.
    """
    @patch(
        'pulp.server.db.migrations.0012_reserved_resources_schema_change._migrate_task_status',
        side_effect=migration._migrate_task_status)
    @patch('pulp.server.db.migrations.0012_reserved_resources_schema_change.'
           '_migrate_reserved_resources',
           side_effect=migration._migrate_reserved_resources)
    @patch(
        'pulp.server.db.migrations.0012_reserved_resources_schema_change.connection.get_collection',
        autospec=True)
    def test_calls_correct_functions(self, get_collection,
                                     _migrate_reserved_resources,
                                     _migrate_task_status):
        """
        Assert that migrate() calls the correct other functions that do the real work.
Exemplo n.º 28
0
"""
This module contains tests for pulp.server.db.migrations.0018_remove_archived_calls.
"""
import unittest

from mock import patch

from pulp.server.db.migrate.models import _import_all_the_way

migration = _import_all_the_way('pulp.server.db.migrations.0018_remove_archived_calls')


class TestMigrate(unittest.TestCase):
    """
    Test the migrate() function.
    """

    @patch.object(migration.connection, 'get_database')
    def test_migrate_no_collection_archived_calls(self, mock_get_database):
        """
        Assert that drop is not called when archived_calls is not present in the db
        """

        mock_get_database.return_value.collection_names.return_value = []
        collection = mock_get_database.return_value['archived_calls']
        migration.migrate()
        self.assertFalse(collection.drop.called)

    @patch.object(migration.connection, 'get_database')
    def test_migrate_collection_present_archived_calls(self, mock_get_database):
        """
"""
This module contains tests for pulp.server.db.migrations.0020_drop_celery_taskmeta.py
"""
import unittest

from mock import patch

from pulp.server.db.migrate.models import _import_all_the_way

migration = _import_all_the_way(
    'pulp.server.db.migrations.0020_drop_celery_taskmeta')


class TestMigrate(unittest.TestCase):
    """
    Test the migrate() function.
    """
    @patch.object(migration.connection, 'get_database')
    def test_celery_taskmeta_collection_dropped(self, mock_get_database):
        mock_get_database.return_value.collection_names.return_value = []
        collection = mock_get_database.return_value['celery_taskmeta']
        migration.migrate()
        collection.drop.assert_called_once()
Exemplo n.º 30
0
"""
This module contains tests for pulp.server.db.migrations.0019_repo_collection_id.py
"""
import unittest

from mock import patch

from pulp.server.db.migrate.models import _import_all_the_way

migration = _import_all_the_way('pulp.server.db.migrations.0019_repo_collection_id')


class TestMigrate(unittest.TestCase):
    """
    Test the migrate() function.
    """

    @patch.object(migration.connection, 'get_database')
    def test_repos_collection_id_renamed(self, mock_get_database):
        mock_get_database.return_value.collection_names.return_value = []
        collection = mock_get_database.return_value['archived_calls']
        migration.migrate()
        collection.update.assert_called_once_with({}, {"$rename": {"id": "repo_id"}})
        collection.drop_index.assert_called_once_with("id_-1")
Exemplo n.º 31
0
import os.path
import unittest

from pulp.server.db.migrate.models import _import_all_the_way
import mock

migration = _import_all_the_way(
    'pulp_rpm.plugins.migrations.0019_add_timestamp_'
    'to_distribution')


class TestMigrate(unittest.TestCase):
    """
    Test the migrate() function.
    """
    @mock.patch.object(migration, 'get_collection')
    def test_calls_correct_functions(self, mock_get_collection):
        """
        """
        mock_get_collection.return_value.find.return_value = [
            fake_distribution1,
        ]

        migration.migrate()

        self.assertEqual(mock_get_collection.call_count, 1)
        mock_get_collection.return_value.update.assert_called_once_with(
            {'_id': fake_distribution1['_id']},
            {'$set': {
                'timestamp': 1354213090.94
            }},
import os
import shutil
import tempfile
import unittest

import mock

from pulp.devel.unit import util
from pulp.server.db.migrate.models import _import_all_the_way

migration = _import_all_the_way(
    'pulp_rpm.plugins.migrations.0021_clean_http_directories')


class TestMigrate(unittest.TestCase):
    """
    Test migration 0021.
    """
    @mock.patch('pulp_rpm.plugins.migrations.0021_clean_http_directories.'
                'configuration')
    @mock.patch('pulp_rpm.plugins.migrations.0021_clean_http_directories.'
                'walk_and_clean_directories')
    def test_walk_correct_directories(self, mock_walk, mock_conf):
        """Migration should check http and https simple serve directories"""
        mock_conf.get_http_publish_dir.return_value = 'http'
        mock_conf.get_https_publish_dir.return_value = 'https'
        migration.migrate()
        mock_walk.assert_has_calls([mock.call('https'), mock.call('http')])

    def setUp(self):
        """Setup a publish base"""
Exemplo n.º 33
0
from pulp.common.compat import unittest

import mock

from pymongo.errors import DuplicateKeyError

from pulp.server.db.migrate.models import _import_all_the_way

PATH_TO_MODULE = 'pulp_rpm.plugins.migrations.0040_errata_pkglist_collection'
migration = _import_all_the_way(PATH_TO_MODULE)


@mock.patch('pulp.server.db.connection.get_database')
@mock.patch.object(migration, 'migrate_erratum_pkglist')
class TestMigrate(unittest.TestCase):
    def test_calls_migrate_erratum_pkglist(self, mock_migrate_pkglist,
                                           mock_get_db):
        mock_db = mock_get_db.return_value
        mock_unit = mock.MagicMock()
        mock_erratum_collection = mock_db['units_erratum']
        mock_pkglist_collection = mock_db['erratum_pkglists']
        mock_erratum_collection.find.return_value.batch_size.return_value = [
            mock_unit
        ]

        migration.migrate()

        self.assertEqual(mock_migrate_pkglist.call_count, 1)
        mock_migrate_pkglist.assert_called_with(mock_erratum_collection,
                                                mock_pkglist_collection,
                                                mock_unit)
Exemplo n.º 34
0
"""
This module contains tests for pulp.server.db.migrations.0021_remove_extra_importer_fields.py
"""
import unittest

import mock

from pulp.server.db.migrate.models import _import_all_the_way

migration = _import_all_the_way(
    'pulp.server.db.migrations.0021_remove_extra_importer_fields')


class TestMigrate(unittest.TestCase):
    """
    Test the migrate() function.
    """
    @classmethod
    def setUpClass(cls):
        """
        Run the migration
        """
        with mock.patch.object(migration.connection,
                               'get_database') as mock_db:
            cls.collection = mock_db.return_value['archived_calls']
            migration.migrate()

    def test_index_removed(self):
        """
        Assert that the old index is removed.
        """
    def test_migration(self):
        """
        Assert that the migration adds the appropriate hashes to the three consumers.
        """
        consumer_unit_profiles = [
            {
                "consumer_id": "consumer_1",
                "content_type": "rpm",
                "profile": [
                    {
                        "name": "Package A",
                        "epoch": 0,
                        "version": "1.0.1",
                        "release": "1.el6",
                        "arch": "x86_64",
                        "vendor": "Red Hat, Inc.",
                    },
                    {
                        "name": "Package B",
                        "epoch": 0,
                        "version": "2.0.3",
                        "release": "3.el6",
                        "arch": "x86_64",
                        "vendor": "Red Hat, Inc.",
                    },
                    {
                        "name": "Package C",
                        "epoch": 0,
                        "version": "1.3.6",
                        "release": "2.el6",
                        "arch": "x86_64",
                        "vendor": "Red Hat, Inc.",
                    },
                ],
            },
            {
                "consumer_id": "consumer_2",
                "content_type": "rpm",
                "profile": [
                    {
                        "name": "Package B",
                        "epoch": 0,
                        "version": "2.0.3",
                        "release": "3.el6",
                        "arch": "x86_64",
                        "vendor": "Red Hat, Inc.",
                    },
                    {
                        "name": "Package A",
                        "epoch": 0,
                        "version": "1.0.1",
                        "release": "1.el6",
                        "arch": "x86_64",
                        "vendor": "Red Hat, Inc.",
                    },
                    {
                        "name": "Package C",
                        "epoch": 0,
                        "version": "1.3.6",
                        "release": "2.el6",
                        "arch": "x86_64",
                        "vendor": "Red Hat, Inc.",
                    },
                ],
            },
            {
                "consumer_id": "consumer_3",
                "content_type": "rpm",
                "profile": [
                    {
                        "name": "Package A",
                        "epoch": 0,
                        "version": "1.0.1",
                        "release": "1.el6",
                        "arch": "x86_64",
                        "vendor": "Red Hat, Inc.",
                    },
                    {
                        "name": "Package B",
                        "epoch": 0,
                        "version": "2.0.3",
                        "release": "3.el6",
                        "arch": "x86_64",
                        "vendor": "Red Hat, Inc.",
                    },
                    {
                        "name": "Package C",
                        "epoch": 0,
                        "version": "1.3.6",
                        "release": "2.el6",
                        "arch": "x86_64",
                        "vendor": "Red Hat, Inc.",
                    },
                    {
                        "name": "Package D",
                        "epoch": 1,
                        "version": "12.1.6",
                        "release": "27.el6",
                        "arch": "x86_64",
                        "vendor": "Red Hat, Inc.",
                    },
                ],
            },
            {
                "consumer_id": "consumer_3",
                "content_type": "some_other_type_that_should_be_left_alone",
                "profile": [
                    {
                        "name": "Package A",
                        "epoch": 0,
                        "version": "1.0.1",
                        "release": "1.el6",
                        "arch": "x86_64",
                        "vendor": "Red Hat, Inc.",
                    },
                    {
                        "name": "Package B",
                        "epoch": 0,
                        "version": "2.0.3",
                        "release": "3.el6",
                        "arch": "x86_64",
                        "vendor": "Red Hat, Inc.",
                    },
                    {
                        "name": "Package C",
                        "epoch": 0,
                        "version": "1.3.6",
                        "release": "2.el6",
                        "arch": "x86_64",
                        "vendor": "Red Hat, Inc.",
                    },
                ],
            },
        ]
        self.collection.insert(consumer_unit_profiles)
        migration_module = _import_all_the_way("pulp_rpm.migrations.0014_add_consumer_profile_hash")

        # Run the migration
        migration_module.migrate("arg_1", kwarg_1="kwarg_1")

        # Get the profiles
        consumer_1_profile = self.collection.find_one({"consumer_id": "consumer_1"})
        consumer_2_profile = self.collection.find_one({"consumer_id": "consumer_2"})
        consumer_3_rpm_profile = self.collection.find_one({"consumer_id": "consumer_3", "content_type": "rpm"})
        consumer_3_other_profile = self.collection.find_one(
            {"consumer_id": "consumer_3", "content_type": "some_other_type_that_should_be_left_alone"}
        )

        # Consumer 1 and Consumer 2 should have the same hashes, even though the RPMs were recorded
        # in a different order
        self.assertEqual(consumer_1_profile["profile_hash"], consumer_2_profile["profile_hash"])
        # Consumer 3 should have a different hash, since it has an additional package
        self.assertNotEqual(consumer_1_profile["profile_hash"], consumer_3_rpm_profile["profile_hash"])

        # Consumer 3's non-RPM profile should not have a hash
        self.assertTrue("profile_hash" not in consumer_3_other_profile)

        # Now, let's make sure the hashes are actually correct. We only have to check 1 and 3, since
        # we already asserted that 1 is equal to 2
        profiler = yum.YumProfiler()
        for profile in [consumer_1_profile, consumer_3_rpm_profile]:
            sorted_profile = profiler.update_profile(None, profile["profile"], None, None)
            expected_hash = UnitProfile.calculate_hash(profile["profile"])
            self.assertEqual(profile["profile_hash"], expected_hash)
Exemplo n.º 36
0
"""
This module contains unit tests for pulp_rpm.plugins.migrations.0017_merge_sha_sha1.py.
"""
import unittest

from pulp.server.db.migrate.models import _import_all_the_way
from pymongo import collection, errors
import mock

migration = _import_all_the_way('pulp_rpm.plugins.migrations.0017_merge_sha_sha1')


SUM_NONE_ERRATA = [
    {u'issued': u'2012-01-27 16:08:06', u'references': [], u'_content_type_id': u'erratum',
     u'id': u'RHEA-2012:0002', u'from': u'*****@*****.**', u'severity': u'',
     u'title': u'Sea_Erratum', u'_ns': u'units_erratum', u'version': u'1',
     u'reboot_suggested': True, u'type': u'security',
     u'pkglist': [
         {u'packages': [
             {u'src': u'http://www.fedoraproject.org', u'name': u'walrus', u'sum': None,
              u'filename': u'walrus-0.71-1.noarch.rpm', u'epoch': u'0', u'version': u'0.71',
              u'release': u'1', u'reboot_suggested': u'False', u'arch': u'noarch'},
             {u'src': u'http://www.fedoraproject.org', u'name': u'penguin', u'sum': None,
              u'filename': u'penguin-0.9.1-1.noarch.rpm', u'epoch': u'0', u'version': u'0.9.1',
              u'release': u'1', u'reboot_suggested': u'False', u'arch': u'noarch'},
             {u'src': u'http://www.fedoraproject.org', u'name': u'shark', u'sum': None,
              u'filename': u'shark-0.1-1.noarch.rpm', u'epoch': u'0', u'version': u'0.1',
              u'release': u'1', u'reboot_suggested': u'False', u'arch': u'noarch'}],
          u'name': u'1', u'short': u''}],
     u'status': u'stable', u'updated': u'', u'description': u'Sea_Erratum',
     u'_last_updated': 1416857488, u'pushcount': u'', u'_storage_path': None, u'rights': u'',
Exemplo n.º 37
0
"""
This module contains functional tests for the 23rd migration.
"""
import os
import shutil
import stat
import tempfile
import unittest

import mock

from pulp.server.db import connection
from pulp.server.db.migrate.models import _import_all_the_way


migration = _import_all_the_way('pulp.server.db.migrations.0023_importer_tls_storage')


class TestMigrate(unittest.TestCase):
    def tearDown(self):
        """
        Remove any database objects that were created during the test.
        """
        connection._DATABASE.repo_importers.remove()

    def test_LOCAL_STORAGE(self):
        """
        Assert that the LOCAL_STORAGE variable is correct.
        """
        self.assertEqual(migration.LOCAL_STORAGE, '/var/lib/pulp')
Exemplo n.º 38
0
"""
This module contains functional tests for the 23rd migration.
"""
import os
import shutil
import stat
import tempfile
import unittest

import mock

from pulp.server.db import connection
from pulp.server.db.migrate.models import _import_all_the_way

migration = _import_all_the_way(
    'pulp.server.db.migrations.0023_importer_tls_storage')


class TestMigrate(unittest.TestCase):
    def tearDown(self):
        """
        Remove any database objects that were created during the test.
        """
        connection._DATABASE.repo_importers.remove()

    def test_LOCAL_STORAGE(self):
        """
        Assert that the LOCAL_STORAGE variable is correct.
        """
        self.assertEqual(migration.LOCAL_STORAGE, '/var/lib/pulp')
Exemplo n.º 39
0
"""
This module contains tests for pulp.server.db.migrations.0014_pulp_user_metadata.
"""
import unittest

import mock

from pulp.server import constants
from pulp.server.db.migrate.models import _import_all_the_way


migration = _import_all_the_way('pulp.server.db.migrations.0014_pulp_user_metadata')


class TestMigrate(unittest.TestCase):
    """
    Test the migrate() function.
    """
    @mock.patch('pulp.server.db.migrations.0014_pulp_user_metadata.database.type_units_collection')
    @mock.patch('pulp.server.db.migrations.0014_pulp_user_metadata.factory.plugin_manager')
    def test_migrate(self, plugin_manager, type_units_collection):
        """
        Ensure that migrate() runs the correct queries on the correct collections.
        """
        types = [{'id': 'type_a'}, {'id': 'type_2'}]

        class MockPluginManager(object):
            def types(self):
                return types

        plugin_manager.return_value = MockPluginManager()
Exemplo n.º 40
0
        mock = MagicMock(spec=file_spec)

    handle = MagicMock(spec=file_spec)
    handle.write.return_value = None
    fake_file = StringIO(read_data)
    if read_data is None:
        if hasattr(handle, '__enter__'):
            handle.__enter__.return_value = handle
    else:
        if hasattr(handle, '__enter__'):
            handle.__enter__.return_value = fake_file
        handle.read = fake_file.read
    mock.return_value = handle
    return mock

migration = _import_all_the_way('pulp.server.db.migrations.0015_load_content_types')


class TestMigrate(unittest.TestCase):
    @patch('pulp.plugins.types.database._drop_indexes')
    @patch('__builtin__.open', mock_open(read_data=_test_type_json))
    @patch('os.listdir', return_value=['test_type.json'])
    @patch('sys.argv', ["pulp-manage-db"])
    @patch('sys.stdout', MagicMock())
    @patch('pulp.server.db.manage._start_logging')
    def test_migrate(self, start_logging_mock, listdir_mock, mock_drop_indices):
        """
        Ensure that migrate() imports types on a clean types database.
        """
        migration.migrate()
        self.assertTrue(mock_drop_indices.called)
 def test_migration(self, mock_listdir, mock_path_exists, mock_move_directory):
     migration = _import_all_the_way('pulp_puppet.plugins.migrations.0002_puppet_'
                                     'publishing_directory_change')
     migration.migrate()
     mock_listdir.assert_called_once_with('/var/www/pulp_puppet')
     mock_path_exists.assert_has_call('/var/www/pulp_puppet')
"""
This module contains tests for pulp.server.db.migrations.0022_distributor_collection_trim.py
"""
import unittest

import mock

from pulp.server.db.migrate.models import _import_all_the_way

migration = _import_all_the_way(
    'pulp.server.db.migrations.0022_distributor_collection_trim')


class TestMigrate(unittest.TestCase):
    """
    Test the migrate() function.
    """
    @classmethod
    def setUpClass(cls):
        """
        Run the migration
        """
        with mock.patch.object(migration.connection,
                               'get_database') as mock_db:
            cls.collection = mock_db.return_value['archived_calls']
            migration.migrate()

    def test_index_removed(self):
        """
        Assert that the old index is removed.
        """
"""
This module contains tests for pulp.server.db.migrations.0015_load_content_types.
"""
import unittest
from mock import Mock, call, patch

from pulp.server.db.migrate.models import _import_all_the_way

migration = _import_all_the_way('pulp.server.db.migrations.'
                                '0016_remove_repo_content_unit_owner_type_and_id')


class TestMigrate(unittest.TestCase):

    @patch.object(migration.connection, 'get_database')
    def test_migrate_no_collection_in_db(self, mock_get_database):
        """
        Test doing nothing, no actual tests since if it tries to do any work it will raise
        """
        mock_get_database.return_value.collection_names.return_value = []
        migration.migrate()

    @patch.object(migration.connection, 'get_database')
    @patch.object(migration, 'remove_duplicates')
    def test_migrate_removes_duplicates(self, mock_remove_duplicates, mock_get_database):
        """
        Test that the migration calls the remove_duplicates method & drops the
        owner_type and owner_id fields from the repo_content_units collection
        """
        mock_get_database.return_value.collection_names.return_value = ['repo_content_units']
        collection = mock_get_database.return_value['repo_content_units']
Exemplo n.º 44
0
    def test_migration(self):
        """
        Assert that the migration adds the appropriate hashes to the three consumers.
        """
        consumer_unit_profiles = [
            {'consumer_id': 'consumer_1', 'content_type': 'rpm',
             'profile': [{'name': 'Package A', 'epoch': 0, 'version': '1.0.1', 'release': '1.el6',
                          'arch': 'x86_64', 'vendor': 'Red Hat, Inc.'},
                         {'name': 'Package B', 'epoch': 0, 'version': '2.0.3', 'release': '3.el6',
                          'arch': 'x86_64', 'vendor': 'Red Hat, Inc.'},
                         {'name': 'Package C', 'epoch': 0, 'version': '1.3.6', 'release': '2.el6',
                          'arch': 'x86_64', 'vendor': 'Red Hat, Inc.'}]},
            {'consumer_id': 'consumer_2', 'content_type': 'rpm',
             'profile': [{'name': 'Package B', 'epoch': 0, 'version': '2.0.3', 'release': '3.el6',
                          'arch': 'x86_64', 'vendor': 'Red Hat, Inc.'},
                         {'name': 'Package A', 'epoch': 0, 'version': '1.0.1', 'release': '1.el6',
                          'arch': 'x86_64', 'vendor': 'Red Hat, Inc.'},
                         {'name': 'Package C', 'epoch': 0, 'version': '1.3.6', 'release': '2.el6',
                          'arch': 'x86_64', 'vendor': 'Red Hat, Inc.'}]},
            {'consumer_id': 'consumer_3', 'content_type': 'rpm',
             'profile': [{'name': 'Package A', 'epoch': 0, 'version': '1.0.1', 'release': '1.el6',
                          'arch': 'x86_64', 'vendor': 'Red Hat, Inc.'},
                         {'name': 'Package B', 'epoch': 0, 'version': '2.0.3', 'release': '3.el6',
                          'arch': 'x86_64', 'vendor': 'Red Hat, Inc.'},
                         {'name': 'Package C', 'epoch': 0, 'version': '1.3.6', 'release': '2.el6',
                          'arch': 'x86_64', 'vendor': 'Red Hat, Inc.'},
                         {'name': 'Package D', 'epoch': 1, 'version': '12.1.6', 'release': '27.el6',
                          'arch': 'x86_64', 'vendor': 'Red Hat, Inc.'}]},
            {'consumer_id': 'consumer_3',
             'content_type': 'some_other_type_that_should_be_left_alone',
             'profile': [{'name': 'Package A', 'epoch': 0, 'version': '1.0.1', 'release': '1.el6',
                          'arch': 'x86_64', 'vendor': 'Red Hat, Inc.'},
                         {'name': 'Package B', 'epoch': 0, 'version': '2.0.3', 'release': '3.el6',
                          'arch': 'x86_64', 'vendor': 'Red Hat, Inc.'},
                         {'name': 'Package C', 'epoch': 0, 'version': '1.3.6', 'release': '2.el6',
                          'arch': 'x86_64', 'vendor': 'Red Hat, Inc.'}]},
        ]
        self.collection.insert(consumer_unit_profiles)
        migration_module = _import_all_the_way(
            'pulp_rpm.plugins.migrations.0014_add_consumer_profile_hash')

        # Run the migration
        migration_module.migrate('arg_1', kwarg_1='kwarg_1')

        # Get the profiles
        consumer_1_profile = self.collection.find_one({'consumer_id': 'consumer_1'})
        consumer_2_profile = self.collection.find_one({'consumer_id': 'consumer_2'})
        consumer_3_rpm_profile = self.collection.find_one({'consumer_id': 'consumer_3',
                                                           'content_type': 'rpm'})
        consumer_3_other_profile = self.collection.find_one(
            {'consumer_id': 'consumer_3',
             'content_type': 'some_other_type_that_should_be_left_alone'})

        # Consumer 1 and Consumer 2 should have the same hashes, even though the RPMs were recorded
        # in a different order
        self.assertEqual(consumer_1_profile['profile_hash'], consumer_2_profile['profile_hash'])
        # Consumer 3 should have a different hash, since it has an additional package
        self.assertNotEqual(consumer_1_profile['profile_hash'],
                            consumer_3_rpm_profile['profile_hash'])

        # Consumer 3's non-RPM profile should not have a hash
        self.assertTrue('profile_hash' not in consumer_3_other_profile)

        # Now, let's make sure the hashes are actually correct. We only have to check 1 and 3, since
        # we already asserted that 1 is equal to 2
        profiler = yum.YumProfiler()
        for profile in [consumer_1_profile, consumer_3_rpm_profile]:
            profiler.update_profile(None, profile['profile'], None, None)
            expected_hash = UnitProfile.calculate_hash(profile['profile'])
            self.assertEqual(profile['profile_hash'], expected_hash)
"""
This module contains unit tests for
pulp_rpm.plugins.migrations.0018_remove_old_repo_profile_applicability.

"""
import unittest

from pulp.server.db.migrate.models import _import_all_the_way
import mock

migration = _import_all_the_way('pulp_rpm.plugins.migrations.0018_remove_old_'
                                'repo_profile_applicability')


class TestMigrate(unittest.TestCase):
    """
    Test the migrate() function.
    """
    @mock.patch('pulp_rpm.plugins.migrations.0018_remove_old_repo_profile_applicability.'
                'connection.get_collection')
    def test_calls_correct_functions(self, get_collection):
        """
        Assert that migrate() drops the collection it should
        """
        fake_rpa = mock.Mock()
        get_collection.return_value = fake_rpa

        migration.migrate()

        get_collection.assert_called_once_with('repo_profile_applicability')
        fake_rpa.drop.assert_called_once_with()
from pulp.common.compat import unittest

from pulp.server.db.migrate.models import _import_all_the_way, MigrationRemovedError

migration = _import_all_the_way(
    'pulp.server.db.migrations.0006_migrations_removed')


class TestMigrate(unittest.TestCase):
    def test_raises_exception(self):
        with self.assertRaises(MigrationRemovedError) as assertion:
            migration.migrate()
        self.assertEqual(assertion.exception.migration_version, '0006')
        self.assertEqual(assertion.exception.component_version, '2.8.0')
        self.assertEqual(assertion.exception.min_component_version, '2.4.0')
        self.assertEqual(assertion.exception.component, 'pulp')
from pulp.common.compat import unittest

import mock

from pulp.server.db.migrate.models import _import_all_the_way


PATH_TO_MODULE = 'pulp_rpm.plugins.migrations.0031_checksums_and_templates'
migration = _import_all_the_way(PATH_TO_MODULE)


@mock.patch('pulp.server.db.connection.get_database')
@mock.patch.object(migration, 'migrate_rpm_base')
@mock.patch.object(migration, 'migrate_drpms')
class TestMigrate(unittest.TestCase):
    def test_calls_migrate_rpm_base(self, mock_migrate_drpms, mock_migrate_rpm_base, mock_get_db):
        mock_db = mock_get_db.return_value
        mock_unit = mock.MagicMock()
        mock_rpm_collection = mock_db['units_rpm']
        mock_rpm_collection.find.return_value = [mock_unit]
        mock_srpm_collection = mock_db['units_srpm']
        mock_srpm_collection.find.return_value = [mock_unit]

        migration.migrate()

        self.assertEqual(mock_migrate_rpm_base.call_count, 2)
        mock_migrate_rpm_base.assert_called_with(mock_rpm_collection, mock_unit)
        mock_migrate_rpm_base.assert_called_with(mock_srpm_collection, mock_unit)

    def test_calls_migrate_drpms(self, mock_migrate_drpms, mock_migrate_rpm_base, mock_get_db):
        mock_db = mock_get_db.return_value
import os.path
import unittest

from pulp.server.db.migrate.models import _import_all_the_way
import mock


migration = _import_all_the_way('pulp_rpm.plugins.migrations.0019_add_timestamp_'
                                'to_distribution')


class TestMigrate(unittest.TestCase):
    """
    Test the migrate() function.
    """
    @mock.patch.object(migration, 'get_collection')
    def test_calls_correct_functions(self, mock_get_collection):
        """
        """
        mock_get_collection.return_value.find.return_value = [
            fake_distribution1,
        ]

        migration.migrate()

        self.assertEqual(mock_get_collection.call_count, 1)
        mock_get_collection.return_value.update.assert_called_once_with(
            {'_id': fake_distribution1['_id']}, {'$set': {'timestamp': 1354213090.94}},
            safe=True
        )