def test_migrate(self, m_get_collection):
        """
        Test last_updated and last_override_config fields added.
        """
        collection = Mock()
        found = [
            # these three should trigger a save
            {LAST_SYNC: '2016-05-04T18:19:01Z', LAST_UPDATED: '2016-05-03T18:19:01Z'},
            {LAST_SYNC: '2016-05-04T18:20:01Z'},
            {},
            # this one should not trigger a save
            {LAST_OVERRIDE_CONFIG: '2016-05-04T18:20:01Z', LAST_UPDATED: '2016-05-03T18:19:01Z'},
        ]
        collection.find.return_value = deepcopy(found)
        m_get_collection.return_value = collection

        # test
        module = MigrationModule(MIGRATION)._module
        module.migrate()

        # validation
        m_get_collection.assert_called_once_with('repo_importers')
        collection.find.assert_called_once_with()
        self.assertTrue(LAST_UPDATED in dist for dist in collection.save.call_args_list)
        self.assertTrue(LAST_OVERRIDE_CONFIG in dist for dist in collection.save.call_args_list)
        self.assertEqual(
            len(collection.save.call_args_list), 3)
    def test_migrate(self, m_get_collection, now_utc_datetime):
        """
        Test last_updated and last_override_config fields added.
        """
        collection = Mock()
        found = [
            {LAST_PUBLISH: '2016-05-04T18:19:01Z', LAST_UPDATED: '2016-05-03T18:19:01Z'},
            {LAST_PUBLISH: '2016-05-04T18:20:01Z'},
            {},
        ]
        collection.find.return_value = deepcopy(found)
        m_get_collection.return_value = collection

        # test
        module = MigrationModule(MIGRATION)._module
        module.migrate()

        # validation
        m_get_collection.assert_called_once_with('repo_distributors')
        collection.find.assert_called_once_with()
        now_utc_datetime.assert_called_once_with()
        self.assertTrue(LAST_UPDATED in dist for dist in collection.save.call_args_list)
        self.assertTrue(LAST_OVERRIDE_CONFIG in dist for dist in collection.save.call_args_list)
        self.assertEqual(
            len(collection.save.call_args_list), 2)
    def test_upgrade_idempotency(self):
        """
        Simplest way to check the migration can run twice is simply to run it twice. The
        primary goal is to make sure an exception isn't raised.
        """

        # Setup
        coll = Bind.get_collection()

        for counter in range(0, 3):
            bind_dict = {
                'consumer_id': 'consumer_%s' % counter,
                'repo_id': 'repo_%s' % counter,
                'distributor_id': 'distributor_%s' % counter,
            }

            coll.insert(bind_dict)

        # Test
        module = MigrationModule('pulp.server.db.migrations.0003_bind_additions')._module
        module.migrate()
        module.migrate()

        # Verify
        bindings = coll.find()
        for b in bindings:
            self.assertTrue('notify_agent' in b)
            self.assertEqual(b['notify_agent'], True)
            self.assertTrue('binding_config' in b)
            self.assertEqual(b['binding_config'], None)
Esempio n. 4
0
 def test_migration(self):
     # setup
     collection = Bind.get_collection()
     for n in range(0, MAX_BINDINGS):
         if n % 2 == 0:
             conf = {ID: n}
         else:
             conf = None
         binding = {
             ID: n,
             CONSUMER_ID: n,
             REPO_ID: n,
             DISTRIBUTOR_ID: n,
             BINDING_CONFIG: conf,
             NOTIFY_AGENT: True,
         }
         collection.save(binding, safe=True)
     # migrate
     module = MigrationModule(MIGRATION)._module
     module.migrate()
     # verify
     bindings = list(collection.find({}))
     self.assertEqual(len(bindings), MAX_BINDINGS)
     for binding in bindings:
         conf = binding[BINDING_CONFIG]
         bind_id = binding[ID]
         if bind_id % 2 == 0:
             # untouched
             self.assertEqual(conf, {ID: bind_id})
         else:
             # fixed
             self.assertEqual(conf, {})
    def test_migrate(self, distributor, parse_iso8601_datetime):
        collection = Mock()
        found = [
            {LAST_PUBLISH: '2015-04-28T18:19:01Z'},
            {LAST_PUBLISH: datetime.now()},
            {LAST_PUBLISH: '2015-04-28T18:20:01Z'},
            {LAST_PUBLISH: datetime.now()},
        ]
        parsed = [1, 2]
        collection.find.return_value = deepcopy(found)
        distributor.get_collection.return_value = collection
        parse_iso8601_datetime.side_effect = parsed

        # test
        module = MigrationModule(MIGRATION)._module
        module.migrate()

        # validation
        distributor.get_collection.assert_called_once_with()
        collection.find.assert_called_once_with()
        self.assertEqual(
            parse_iso8601_datetime.call_args_list,
            [
                call(found[0][LAST_PUBLISH]),
                call(found[2][LAST_PUBLISH]),
            ])
        self.assertEqual(
            collection.save.call_args_list,
            [
                call({LAST_PUBLISH: parsed[0]}, safe=True),
                call({LAST_PUBLISH: parsed[1]}, safe=True)
            ])
    def test_update_called(self, mock_get_collection):
        module = MigrationModule('pulp.server.db.migrations.0002_rename_http_notifier')._module
        module.migrate()

        # make sure the correct mongo query is being passed down
        mock_get_collection.return_value.update.assert_called_once_with(
            {'notifier_type_id': 'rest-api'}, {'$set': {'notifier_type_id': 'http'}}
        )
    def test_update_called(self, mock_get_collection):
        module = MigrationModule("pulp.server.db.migrations.0002_rename_http_notifier")._module
        module.migrate()

        # make sure the correct mongo query is being passed down
        mock_get_collection.return_value.update.assert_called_once_with(
            {"notifier_type_id": "rest-api"}, {"$set": {"notifier_type_id": "http"}}
        )
Esempio n. 8
0
    def test_del_agent_queues_deletes_all_existing_queues(self):
        self.fake_broker.getQueue.side_effect = [Mock(), Mock()]

        # test
        migration = MigrationModule(MIGRATION)._module
        migration._del_agent_queues(self.fake_broker)

        expected_calls = [call(self.fake_broker, 'dog'), call(self.fake_broker, 'cat')]
        self.mock_del_queue_catch_queue_in_use_exc.assert_has_calls(expected_calls)
Esempio n. 9
0
    def test_del_agent_queues_skips_existing_queues(self):
        self.fake_broker.getQueue.side_effect = [None, None]

        # test
        migration = MigrationModule(MIGRATION)._module
        migration._del_agent_queues(self.fake_broker)

        self.fake_broker.getQueue.assert_has_calls([call('dog'), call('cat')])
        self.assertTrue(not self.mock_del_queue_catch_queue_in_use_exc.called)
Esempio n. 10
0
    def test_time_to_utc_on_collection_skips_utc(self, mock_connection):
        migration = MigrationModule(MIGRATION)._module
        collection = mock_connection.get_collection.return_value
        unit = {'bar': '2014-07-09T11:09:07Z'}
        collection.find.return_value = [unit]

        migration.update_time_to_utc_on_collection('foo', 'bar')
        mock_connection.get_collection.assert_called_once_with('foo')
        self.assertFalse(collection.save.called)
Esempio n. 11
0
    def test_migrate_agent_queues(self, fake_add_agent_queues, fake_del_agent_queues):
        fake_broker = Mock()

        # test
        migration = MigrationModule(MIGRATION)._module
        migration._migrate_agent_queues(fake_broker)

        # validation
        fake_add_agent_queues.assert_called_with(fake_broker)
        fake_del_agent_queues.assert_called_with(fake_broker)
Esempio n. 12
0
 def test_migrate(self, mock_connection):
     """
     Test the schema change happens like it should.
     """
     role_schema = copy.deepcopy(CURRENT)
     migration = MigrationModule(MIGRATION)._module
     collection = mock_connection.get_collection.return_value
     collection.find.return_value = role_schema
     migration.migrate()
     self.assertEquals(role_schema, TARGET)
Esempio n. 13
0
 def test_idempotence(self, mock_connection):
     """
     Test the idempotence of the migration
     """
     role_schema = copy.deepcopy(TARGET)
     migration = MigrationModule(MIGRATION)._module
     collection = mock_connection.get_collection.return_value
     collection.find.return_value = role_schema
     migration.migrate()
     self.assertFalse(collection.save.called)
     self.assertEquals(role_schema, TARGET)
Esempio n. 14
0
    def test_migrate_reply_queue_not_found_does_not_delete_or_add_reply_queue(self):
        fake_broker = Mock()
        fake_broker.getQueue.return_value = None

        # test
        migration = MigrationModule(MIGRATION)._module
        migration._migrate_reply_queue(fake_broker)

        # validation
        fake_broker.getQueue.assert_called_with(Services.REPLY_QUEUE)
        self.assertTrue(not self.mock_del_queue_catch_queue_in_use_exc.called)
        self.assertTrue(not fake_broker.addQueue.called)
Esempio n. 15
0
    def test_time_to_utc_on_collection(self, mock_connection):
        migration = MigrationModule(MIGRATION)._module
        collection = mock_connection.get_collection.return_value
        unit = {'bar': '2014-07-09T11:09:07-04:00'}
        utc_value = '2014-07-09T15:09:07Z'
        collection.find.return_value = [unit]

        migration.update_time_to_utc_on_collection('foo', 'bar')

        mock_connection.get_collection.assert_called_once_with('foo')
        self.assertEquals(unit['bar'], utc_value)
        collection.save.assert_called_once_with(unit)
Esempio n. 16
0
    def test_migrate(self, mock_connection, mock_update):
        """
        Verify that only known & valid collections are updated
        """
        migration = MigrationModule(MIGRATION)._module
        collection_list = ['repo_distributors']

        mock_connection.get_database.return_value.collection_names.return_value = collection_list

        migration.migrate()

        mock_update.assert_called_once_with('repo_distributors', 'last_publish')
Esempio n. 17
0
 def test_migrate(self, mock_connection):
     """
     Test the schema change happens like it should.
     """
     permissions_schema = [{"resource": "/", "id": "5356d55b37382030f4a80b5e",
                            "users": {"admin": [0, 1, 2, 3, 4]}}]
     new_schema = [{"resource": "/", "id": "5356d55b37382030f4a80b5e",
                   "users": [{"username": "******", "permissions": [0, 1, 2, 3, 4]}]}]
     migration = MigrationModule(MIGRATION)._module
     collection = mock_connection.get_collection.return_value
     collection.find.return_value = permissions_schema
     migration.migrate()
     self.assertEquals(permissions_schema, new_schema)
Esempio n. 18
0
    def test_migrate_reply_queue_not_found(self):
        fake_queue = Mock()
        fake_broker = Mock()
        fake_broker.getQueue.return_value = None

        # test
        migration = MigrationModule(MIGRATION)._module
        migration._migrate_reply_queue(fake_broker)

        # validation
        fake_broker.getQueue.assert_called_with(Services.REPLY_QUEUE)
        self.assertFalse(fake_broker.called)
        self.assertFalse(fake_broker.called)
Esempio n. 19
0
    def test_add_agent_queues_cat_only(self, fake_get):
        fake_collection = Mock()
        fake_collection.find = Mock(return_value=[{'id': 'dog'}, {'id': 'cat'}])
        fake_get.return_value = fake_collection
        fake_broker = Mock()
        fake_broker.getQueue.side_effect = [None, Mock()]

        # test
        migration = MigrationModule(MIGRATION)._module
        migration._add_agent_queues(fake_broker)

        fake_broker.getQueue.assert_any('pulp.agent.dog')
        fake_broker.getQueue.assert_any('pulp.agent.cat')
        fake_broker.addQueue.assert_called__once_with('pulp.agent.dog', durable=True)
Esempio n. 20
0
    def test_del_agent_queues_cat_only(self, fake_get):
        fake_collection = Mock()
        fake_collection.find = Mock(return_value=[{'id': 'dog'}, {'id': 'cat'}])
        fake_get.return_value = fake_collection
        fake_broker = Mock()
        fake_broker.getQueue.side_effect = [None, Mock()]

        # test
        migration = MigrationModule(MIGRATION)._module
        migration._del_agent_queues(fake_broker)

        fake_broker.getQueue.assert_any('dog')
        fake_broker.getQueue.assert_any('cat')
        fake_broker.delQueue.assert_calle_with('cat')
Esempio n. 21
0
 def test(self):
     # migrate
     module = MigrationModule(MIGRATION)._module
     module.migrate()
     # validation
     for collection in [connection.get_collection(n) for n in TEST_COLLECTIONS]:
         for unit in collection.find({}):
             self.assertTrue(LAST_UPDATED in unit)
             unit_id = unit[ID]
             last_updated = unit[LAST_UPDATED]
             if unit_id % 2 == 0:
                 self.assertEqual(last_updated, 1)
             else:
                 self.assertTrue(isinstance(last_updated, float))
Esempio n. 22
0
    def test__del_queue_catch_no_queue_in_use_exception_catches_cannot_delete_queue(self):
        fake_broker = Mock()
        mock_name = Mock()
        exc_to_raise = Exception("Cannot delete queue celery; queue in use")
        fake_broker.delQueue.side_effect = exc_to_raise

        migration = MigrationModule(MIGRATION)._module
        try:
            migration._del_queue_catch_queue_in_use_exception(fake_broker, mock_name)
            self.fail('An exception should have been raised, and was not.')
        except Exception as error:
            string_a = 'Consumers are still bound to the queue'
            string_b = 'All consumers must be unregistered, upgraded, or off before you can continue'
            if string_a not in error.message or string_b not in error.message:
                self.fail("Migration 0009 does not handle a 'queue in use' exception")
    def test_database_integration(self):
        # make sure the migration works on a live document in mongo
        collection = EventListener.get_collection()
        event_listener_id = str(
            collection.insert({"notifier_type_id": "rest-api", "event_types": ["*"], "notifier_config": {}}, safe=True)
        )
        event_listener_factory = managers.factory.event_listener_manager()

        module = MigrationModule("pulp.server.db.migrations.0002_rename_http_notifier")._module
        module.migrate()

        event_listener = event_listener_factory.get(event_listener_id)
        self.assertEqual(event_listener["notifier_type_id"], "http")

        # cleanup
        collection.remove()
Esempio n. 24
0
    def test_migrate_reply_queue(self):
        fake_queue = Mock()
        fake_queue.values = {
            'exclusive': True,
            'arguments': {}
        }
        fake_broker = Mock()
        fake_broker.getQueue.return_value = fake_queue

        # test
        migration = MigrationModule(MIGRATION)._module
        migration._migrate_reply_queue(fake_broker)

        # validation
        fake_broker.getQueue.assert_called_with(Services.REPLY_QUEUE)
        fake_broker.delQueue.assert_called_with(Services.REPLY_QUEUE)
        fake_broker.addQueue.assert_called_with(Services.REPLY_QUEUE, durable=True)
Esempio n. 25
0
    def test_migrate_reply_queue_not_exclusive_does_not_delete_or_add_reply_queue(self):
        fake_queue = Mock()
        fake_queue.values = {
            'exclusive': False,
            'arguments': {}
        }
        fake_broker = Mock()
        fake_broker.getQueue.return_value = fake_queue

        # test
        migration = MigrationModule(MIGRATION)._module
        migration._migrate_reply_queue(fake_broker)

        # validation
        fake_broker.getQueue.assert_called_with(Services.REPLY_QUEUE)
        self.assertTrue(not self.mock_del_queue_catch_queue_in_use_exc.called)
        self.assertTrue(not fake_broker.addQueue.called)
Esempio n. 26
0
    def test_migrate_reply_queue_not_exclusive(self):
        fake_queue = Mock()
        fake_queue.values = {
            'exclusive': False,
            'arguments': {}
        }
        fake_broker = Mock()
        fake_broker.getQueue.return_value = fake_queue

        # test
        migration = MigrationModule(MIGRATION)._module
        migration._migrate_reply_queue(fake_broker)

        # validation
        fake_broker.getQueue.assert_called_with(Services.REPLY_QUEUE)
        self.assertFalse(fake_broker.called)
        self.assertFalse(fake_broker.called)
    def test_migrate(self, m_get_collection):
        """
        Test last_updated and last_override_config fields added.
        """
        collection = Mock()
        m_get_collection.return_value = collection

        # test
        module = MigrationModule(MIGRATION)._module
        module.migrate()

        # validation
        m_get_collection.assert_called_once_with('repo_importers')
        # can't do much more than see that update was called
        # for each key to be removed (2 total calls)
        self.assertEqual(
            len(collection.update.call_args_list), 2)
Esempio n. 28
0
    def test_migrate_reply_queue_arguments_is_exclusive_deletes_and_add_reply_queue(self):
        fake_queue = Mock()
        fake_queue.values = {
            'exclusive': False,
            'arguments': {'exclusive': True}
        }
        fake_broker = Mock()
        fake_broker.getQueue.return_value = fake_queue

        # test
        migration = MigrationModule(MIGRATION)._module
        migration._migrate_reply_queue(fake_broker)

        # validation
        fake_broker.getQueue.assert_called_with(Services.REPLY_QUEUE)
        expected_call = call(fake_broker, Services.REPLY_QUEUE)
        self.mock_del_queue_catch_queue_in_use_exc.assert_has_calls(expected_call)
        fake_broker.addQueue.assert_called_with(Services.REPLY_QUEUE, durable=True)
    def test_database_integration(self):
        # make sure the migration works on a live document in mongo
        collection = EventListener.get_collection()
        event_listener_id = str(collection.insert({
            'notifier_type_id': 'rest-api',
            'event_types': ['*'],
            'notifier_config': {},
        }))
        event_listener_factory = managers.factory.event_listener_manager()

        module = MigrationModule('pulp.server.db.migrations.0002_rename_http_notifier')._module
        module.migrate()

        event_listener = event_listener_factory.get(event_listener_id)
        self.assertEqual(event_listener['notifier_type_id'], 'http')

        # cleanup
        collection.remove()
Esempio n. 30
0
    def test_migrate_not_qpid(self,
                     fake_conf,
                     fake_migrate_agent_queues,
                     fake_migrate_reply_queue,
                     fake_connection,
                     fake_broker):

        fake_conf.get.return_value = 'not-qpid'

        # test
        migration = MigrationModule(MIGRATION)._module
        migration.migrate()

        # validation
        self.assertFalse(fake_connection.called)
        self.assertFalse(fake_broker.called)
        self.assertFalse(fake_migrate_reply_queue.called)
        self.assertFalse(fake_migrate_agent_queues.called)
Esempio n. 31
0
    def test__del_queue_catch_no_queue_in_use_exception_catches_cannot_delete_queue(
            self):
        fake_broker = Mock()
        mock_name = Mock()
        exc_to_raise = Exception("Cannot delete queue celery; queue in use")
        fake_broker.delQueue.side_effect = exc_to_raise

        migration = MigrationModule(MIGRATION)._module
        try:
            migration._del_queue_catch_queue_in_use_exception(
                fake_broker, mock_name)
            self.fail('An exception should have been raised, and was not.')
        except Exception as error:
            string_a = 'Consumers are still bound to the queue'
            string_b = (
                'All consumers must be unregistered, upgraded, or off before you can '
                'continue')
            if string_a not in error.message or string_b not in error.message:
                self.fail(
                    "Migration 0009 does not handle a 'queue in use' exception"
                )
Esempio n. 32
0
 def test_idempotence(self, mock_connection):
     """
     Test the idempotence of the migration
     """
     permissions_schema = [{
         "resource": "/",
         "id": "5356d55b37382030f4a80b5e",
         "users": {
             "admin": [0, 1, 2, 3, 4]
         }
     }]
     new_schema = [{
         "resource":
         "/",
         "id":
         "5356d55b37382030f4a80b5e",
         "users": [{
             "username": "******",
             "permissions": [0, 1, 2, 3, 4]
         }]
     }]
     migration = MigrationModule(MIGRATION)._module
     collection = mock_connection.get_collection.return_value
     collection.find.return_value = permissions_schema
     migration.migrate()
     self.assertEquals(permissions_schema, new_schema)
     migration.migrate()
     self.assertEquals(permissions_schema, new_schema)
Esempio n. 33
0
    def test_database_integration(self):
        # make sure the migration works on a live document in mongo
        collection = EventListener.get_collection()
        event_listener_id = str(
            collection.insert(
                {
                    'notifier_type_id': 'rest-api',
                    'event_types': ['*'],
                    'notifier_config': {},
                },
                safe=True))
        event_listener_factory = managers.factory.event_listener_manager()

        module = MigrationModule(
            'pulp.server.db.migrations.0002_rename_http_notifier')._module
        module.migrate()

        event_listener = event_listener_factory.get(event_listener_id)
        self.assertEqual(event_listener['notifier_type_id'], 'http')

        # cleanup
        collection.remove()
Esempio n. 34
0
    def test_migrate_reply_queue_arguments_is_exclusive_deletes_and_add_reply_queue(
            self):
        fake_queue = Mock()
        fake_queue.values = {
            'exclusive': False,
            'arguments': {
                'exclusive': True
            }
        }
        fake_broker = Mock()
        fake_broker.getQueue.return_value = fake_queue

        # test
        migration = MigrationModule(MIGRATION)._module
        migration._migrate_reply_queue(fake_broker)

        # validation
        fake_broker.getQueue.assert_called_with(Services.REPLY_QUEUE)
        expected_call = call(fake_broker, Services.REPLY_QUEUE)
        self.mock_del_queue_catch_queue_in_use_exc.assert_has_calls(
            expected_call)
        fake_broker.addQueue.assert_called_with(Services.REPLY_QUEUE,
                                                durable=True)
class TestMigrationContentUnitCount(base.PulpServerTests):
    def setUp(self):
        super(TestMigrationContentUnitCount, self).setUp()
        self.module = MigrationModule('pulp.server.db.migrations.0004_content_unit_counts')._module

    @mock.patch('pulp.server.db.model.repository.Repo.get_collection')
    @mock.patch('pulp.server.managers.repo.cud.RepoManager.rebuild_content_unit_counts')
    def test_calls(self, mock_rebuild, mock_get_collection):
        self.module.migrate()

        mock_rebuild.assert_called_once_with()
        mock_update = mock_get_collection.return_value.update
        self.assertEqual(mock_update.call_count, 1)

        self.assertTrue(mock_update.call_args[1].get('safe') is True)
        self.assertEqual(mock_update.call_args[0][0], {})
        self.assertEqual(mock_update.call_args[0][1], {'$unset': {'content_unit_count': 1}})

    def test_with_db(self):
        REPO_ID = 'repo123'
        repo_collection = Repo.get_collection()
        repo_collection.save({'id': REPO_ID, 'content_unit_count': 0})

        assoc_collection = RepoContentUnit.get_collection()
        assoc_collection.insert({'repo_id': REPO_ID, 'unit_type_id': 'rpm', 'unit_id': 'unit1'})
        assoc_collection.insert({'repo_id': REPO_ID, 'unit_type_id': 'rpm', 'unit_id': 'unit2'})

        self.module.migrate()

        repo = repo_collection.find({'id': REPO_ID})[0]

        self.assertTrue('content_unit_count' not in repo)
        self.assertEqual(repo['content_unit_counts'], {'rpm': 2})

        # cleanup
        repo_collection.remove({'id': REPO_ID})
        assoc_collection.remove({'repo_id': REPO_ID})
Esempio n. 36
0
    def test_upgrade(self):
        # Setup
        coll = Bind.get_collection()

        for counter in range(0, 3):
            bind_dict = {
                'consumer_id' : 'consumer_%s' % counter,
                'repo_id' : 'repo_%s' % counter,
                'distributor_id' : 'distributor_%s' % counter,
            }

            coll.insert(bind_dict, safe=True)

        # Test
        module = MigrationModule('pulp.server.db.migrations.0003_bind_additions')._module
        module.migrate()

        # Verify
        bindings = coll.find()
        for b in bindings:
            self.assertTrue('notify_agent' in b)
            self.assertEqual(b['notify_agent'], True)
            self.assertTrue('binding_config' in b)
            self.assertEqual(b['binding_config'], None)
    def test_migrate(self, m_get_collection):
        """
        Test last_updated and last_override_config fields added.
        """
        collection = Mock()
        found = [
            # these three should trigger a save
            {
                LAST_SYNC: '2016-05-04T18:19:01Z',
                LAST_UPDATED: '2016-05-03T18:19:01Z'
            },
            {
                LAST_SYNC: '2016-05-04T18:20:01Z'
            },
            {},
            # this one should not trigger a save
            {
                LAST_OVERRIDE_CONFIG: '2016-05-04T18:20:01Z',
                LAST_UPDATED: '2016-05-03T18:19:01Z'
            },
        ]
        collection.find.return_value = deepcopy(found)
        m_get_collection.return_value = collection

        # test
        module = MigrationModule(MIGRATION)._module
        module.migrate()

        # validation
        m_get_collection.assert_called_once_with('repo_importers')
        collection.find.assert_called_once_with()
        self.assertTrue(LAST_UPDATED in dist
                        for dist in collection.save.call_args_list)
        self.assertTrue(LAST_OVERRIDE_CONFIG in dist
                        for dist in collection.save.call_args_list)
        self.assertEqual(len(collection.save.call_args_list), 3)
Esempio n. 38
0
    def test_migrate(self, m_get_collection, parse_iso8601_datetime):
        collection = Mock()
        found = [
            {
                LAST_PUBLISH: '2015-04-28T18:19:01Z'
            },
            {
                LAST_PUBLISH: datetime.now()
            },
            {
                LAST_PUBLISH: '2015-04-28T18:20:01Z'
            },
            {
                LAST_PUBLISH: datetime.now()
            },
        ]
        parsed = [1, 2]
        collection.find.return_value = deepcopy(found)
        m_get_collection.return_value = collection
        parse_iso8601_datetime.side_effect = parsed

        # test
        module = MigrationModule(MIGRATION)._module
        module.migrate()

        # validation
        m_get_collection.assert_called_once_with('repo_distributors')
        collection.find.assert_called_once_with()
        self.assertEqual(parse_iso8601_datetime.call_args_list, [
            call(found[0][LAST_PUBLISH]),
            call(found[2][LAST_PUBLISH]),
        ])
        self.assertEqual(
            collection.save.call_args_list,
            [call({LAST_PUBLISH: parsed[0]}),
             call({LAST_PUBLISH: parsed[1]})])
Esempio n. 39
0
class TestMigration(TestCase):
    def setUp(self):
        # Remove dependencies on qpidtoollibs and qpid.messaging
        self.migration = MigrationModule(MIGRATION)._module
        if self.migration.QPIDTOOLLIBS_AVAILABLE:
            self.broker = self.migration.BrokerAgent
        if self.migration.QPID_MESSAGING_AVAILABLE:
            self.connection = self.migration.Connection

        self.qpidtoollibs = self.migration.QPIDTOOLLIBS_AVAILABLE
        self.qpid_messaging = self.migration.QPID_MESSAGING_AVAILABLE
        self.migration.QPIDTOOLLIBS_AVAILABLE = True
        self.migration.QPID_MESSAGING_AVAILABLE = True
        self.migration.BrokerAgent = Mock()
        self.migration.Connection = Mock()

    def tearDown(self):
        self.migration.QPIDTOOLLIBS_AVAILABLE = self.qpidtoollibs
        self.migration.QPID_MESSAGING_AVAILABLE = self.qpid_messaging
        if self.migration.QPIDTOOLLIBS_AVAILABLE:
            self.migration.BrokerAgent = self.broker
        if self.migration.QPID_MESSAGING_AVAILABLE:
            self.migration.Connection = self.connection

    @patch(MIGRATION + '._migrate_reply_queue')
    @patch(MIGRATION + '._migrate_agent_queues')
    @patch(MIGRATION + '.pulp_conf', PULP_CONF)
    def test_migrate(self, fake_migrate_agent_queues,
                     fake_migrate_reply_queue):
        # test
        self.migration.migrate()

        # validation
        self.migration.Connection.assert_called_with(
            host='myhost',
            port=1234,
            transport='tcp',
            reconnect=False,
            ssl_certfile='TEST-CERTIFICATE',
            ssl_skip_hostname_check=True)

        self.migration.Connection().attach.assert_called_with()
        self.migration.BrokerAgent.assert_called_with(
            self.migration.Connection())
        fake_migrate_reply_queue.assert_called_with(
            self.migration.BrokerAgent())
        fake_migrate_agent_queues.assert_called_with(
            self.migration.BrokerAgent())
        self.migration.Connection().detach.assert_called_with()

    @patch(MIGRATION + '._migrate_reply_queue')
    @patch(MIGRATION + '._migrate_agent_queues')
    @patch(MIGRATION + '.pulp_conf')
    def test_migrate_not_qpid(
        self,
        fake_conf,
        fake_migrate_agent_queues,
        fake_migrate_reply_queue,
    ):

        fake_conf.get.return_value = 'not-qpid'

        # test
        self.migration.migrate()

        # validation
        self.assertFalse(self.migration.Connection.called)
        self.assertFalse(self.migration.BrokerAgent.called)
        self.assertFalse(fake_migrate_reply_queue.called)
        self.assertFalse(fake_migrate_agent_queues.called)

    @patch(MIGRATION + '._del_agent_queues')
    @patch(MIGRATION + '._add_agent_queues')
    def test_migrate_agent_queues(self, fake_add_agent_queues,
                                  fake_del_agent_queues):
        fake_broker = Mock()

        # test
        self.migration._migrate_agent_queues(fake_broker)

        # validation
        fake_add_agent_queues.assert_called_with(fake_broker)
        fake_del_agent_queues.assert_called_with(fake_broker)

    @patch('pulp.server.db.model.consumer.Consumer.get_collection')
    def test_add_agent_queues(self, fake_get):
        fake_collection = Mock()
        fake_collection.find = Mock(return_value=[{
            'id': 'dog'
        }, {
            'id': 'cat'
        }])
        fake_get.return_value = fake_collection
        fake_broker = Mock()
        fake_broker.getQueue.side_effect = [None, None]

        # test
        self.migration._add_agent_queues(fake_broker)

        fake_broker.getQueue.assert_any('pulp.agent.dog')
        fake_broker.getQueue.assert_any('pulp.agent.cat')
        fake_broker.addQueue.assert_any('pulp.agent.dog')
        fake_broker.addQueue.assert_any('pulp.agent.cat')

    @patch('pulp.server.db.model.consumer.Consumer.get_collection')
    def test_add_agent_queues_cat_only(self, fake_get):
        fake_collection = Mock()
        fake_collection.find = Mock(return_value=[{
            'id': 'dog'
        }, {
            'id': 'cat'
        }])
        fake_get.return_value = fake_collection
        fake_broker = Mock()
        fake_broker.getQueue.side_effect = [None, Mock()]

        # test
        self.migration._add_agent_queues(fake_broker)

        fake_broker.getQueue.assert_any('pulp.agent.dog')
        fake_broker.getQueue.assert_any('pulp.agent.cat')
        fake_broker.addQueue.assert_called__once_with('pulp.agent.dog',
                                                      durable=True)
Esempio n. 40
0
class TestMigrationAddMetadata(rpm_support_base.PulpRPMTests):
    def setUp(self):
        super(TestMigrationAddMetadata, self).setUp()
        self.module = MigrationModule(
            'pulp_rpm.plugins.migrations.0005_rpm_changelog_files')._module

        self.fake_unit = {'_storage_path': '/tmp/foo'}
        self.mock_ts = mock.MagicMock()
        self.mock_collection = mock.MagicMock()

        self.mock_package = mock.MagicMock()
        self.mock_package.changelog = [
            ('now', '*****@*****.**', 'description'),
        ]
        self.mock_package.filelist = ['/a', '/b', '/c']
        self.mock_package.files = {
            'ghost': ['/xyz'],
            'dir': ['/foo', '/bar'],
            'file': ['/a', '/b', '/c'],
        }

    def verify_saved_unit(self, saved_unit):
        self.assertTrue('files' in saved_unit)
        for key, value in saved_unit['files'].iteritems():
            for item in value:
                self.assertTrue(isinstance(item, unicode))

        self.assertTrue('filelist' in saved_unit)
        for value in saved_unit['filelist']:
            self.assertTrue(isinstance(value, unicode))

        self.assertTrue('changelog' in saved_unit)
        for entry in saved_unit['changelog']:
            # don't inspect timestamp, which is the first item
            for item in entry[1:]:
                self.assertTrue(isinstance(item, unicode))
        # this is how mongo will encode it, so make sure this is possible
        BSON.encode(saved_unit)

    @mock.patch('os.path.exists', return_value=True)
    @mock.patch('createrepo.yumbased.CreateRepoPackage')
    def test_migrate_filelist(self, mock_package_class, mock_exists):
        mock_package_class.return_value = self.mock_package

        self.module._migrate_unit(self.fake_unit, self.mock_ts,
                                  self.mock_collection)

        self.assertEqual(self.mock_collection.save.call_count, 1)
        saved_unit = self.mock_collection.save.call_args[0][0]
        self.verify_saved_unit(saved_unit)
        self.assertEqual(saved_unit['files'], self.mock_package.files)
        self.assertEqual(saved_unit['filelist'], self.mock_package.filelist)
        self.assertEqual(
            saved_unit['changelog'][0],
            self.module._decode_changelog(self.mock_package.changelog[0]))

    def test_with_real_packages(self):
        current_dir = os.path.dirname(__file__)
        paths = glob.glob(
            os.path.join(
                current_dir,
                '../../data/repos.fedorapeople.org/repos/pulp/pulp/demo_repos/zoo/*.rpm'
            ))
        ts = transaction.initReadOnlyTransaction()

        for path in paths:
            fake_unit = {'_storage_path': path}
            self.module._migrate_unit(fake_unit, ts, self.mock_collection)
            saved_unit = self.mock_collection.save.call_args[0][0]
            self.verify_saved_unit(saved_unit)

    @mock.patch('os.path.exists', return_value=True)
    @mock.patch('createrepo.yumbased.CreateRepoPackage')
    def test_latin1_metadata(self, mock_package_class, mock_exists):
        # the following string cannot be decoded as utf8, so this checks that the
        # migration handles latin1 decoding also. Mongo will barf if we hand it
        # this string.
        self.mock_package.filelist.append(
            '/usr/share/doc/man-pages-da-0.1.1/l\xe6smig')
        mock_package_class.return_value = self.mock_package
        self.module._migrate_unit(self.fake_unit, self.mock_ts,
                                  self.mock_collection)

        saved_unit = self.mock_collection.save.call_args[0][0]
        self.verify_saved_unit(saved_unit)
Esempio n. 41
0
 def setUp(self):
     super(TestMigrationContentUnitCount, self).setUp()
     self.module = MigrationModule(
         'pulp.server.db.migrations.0004_content_unit_counts')._module
Esempio n. 42
0
# http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt.

import os
import pickle
import time
import unittest

import bson
import celery
import mock

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


PATH = 'pulp.server.db.migrations.0007_scheduled_task_conversion'
migration = MigrationModule(PATH)._module


consumer_install_path = os.path.join(os.path.dirname(__file__),
                                     '../../../../data/migration_0007/consumer_install.pickle')
consumer_install = open(consumer_install_path).read()
publish_path = os.path.join(os.path.dirname(__file__),
                                     '../../../../data/migration_0007/publish.pickle')
publish = open(publish_path).read()


@mock.patch('pulp.server.db.connection.get_collection')
class TestMigrate(unittest.TestCase):
    def fake_get_collection(self, name):
        mocks = {
            'scheduled_calls': self.mock_sched_collection,