Ejemplo n.º 1
0
    def test_pulp_manage_db_loads_types(self, start_logging_mock, listdir_mock):
        """
        Test calling pulp-manage-db imports types on a clean types database.
        """
        manage.main()

        all_collection_names = types_db.all_type_collection_names()
        self.assertEqual(len(all_collection_names), 1)

        self.assertEqual(['units_test_type_id'], all_collection_names)

        # Let's make sure we loaded the type definitions correctly
        db_type_definitions = types_db.all_type_definitions()
        self.assertEquals(len(db_type_definitions), 1)
        test_json = json.loads(_test_type_json)
        for attribute in ['id', 'display_name', 'description', 'unit_key', 'search_indexes']:
            self.assertEquals(test_json['types'][0][attribute], db_type_definitions[0][attribute])

        # Now let's ensure that we have the correct indexes
        collection = types_db.type_units_collection('test_type_id')
        indexes = collection.index_information()
        self.assertEqual(indexes['_id_']['key'], [(u'_id', 1)])
        # Make sure we have the unique constraint on all three attributes
        self.assertEqual(indexes['attribute_1_1_attribute_2_1_attribute_3_1']['unique'], True)
        self.assertEqual(indexes['attribute_1_1_attribute_2_1_attribute_3_1']['dropDups'], False)
        self.assertEqual(indexes['attribute_1_1_attribute_2_1_attribute_3_1']['key'],
                         [(u'attribute_1', 1), (u'attribute_2', 1), (u'attribute_3', 1)])
        # Make sure we indexes attributes 1 and 3
        self.assertEqual(indexes['attribute_1_1']['dropDups'], False)
        self.assertEqual(indexes['attribute_1_1']['key'], [(u'attribute_1', 1)])
        self.assertEqual(indexes['attribute_3_1']['dropDups'], False)
        self.assertEqual(indexes['attribute_3_1']['key'], [(u'attribute_3', 1)])
        # Make sure we only have the indexes that we've hand inspected here
        self.assertEqual(indexes.keys(), [u'_id_', u'attribute_1_1_attribute_2_1_attribute_3_1',
                                          u'attribute_1_1', u'attribute_3_1'])
Ejemplo n.º 2
0
    def test_dry_run_no_changes(self, mock_file_config, mock_parse_args, initialize,
                                mocked_apply_migration, mock_entry, getLogger, mock_ensure_indexes):
        logger = MagicMock()
        getLogger.return_value = logger
        mock_args = Namespace(dry_run=True, test=False)
        mock_parse_args.return_value = mock_args

        # Test that when dry run is on, it returns 1 if migrations remain
        exit_code = manage.main()
        self.assertEqual(exit_code, 1)
        self.assertFalse(mock_ensure_indexes.called)
        initialize.assert_called_once_with(max_timeout=1)

        # Actually apply the migrations
        mock_args.dry_run = False
        mock_ensure_indexes.reset_mock()
        initialize.reset_mock()
        exit_code = manage.main()
        self.assertEqual(exit_code, 0)
        self.assertTrue(mock_ensure_indexes.called)
        initialize.assert_called_once_with(max_timeout=1)

        # Perform another dry run and check the return value is now 0
        mock_args.dry_run = True
        mock_ensure_indexes.reset_mock()
        initialize.reset_mock()
        exit_code = manage.main()
        self.assertEquals(exit_code, 0)
        self.assertFalse(mock_ensure_indexes.called)
        initialize.assert_called_once_with(max_timeout=1)
Ejemplo n.º 3
0
 def test_user_not_prompted_if_workers_timeout(self, mock__auto_manage_db, mock__user_input,
                                               mock_get_workers, *unused_mocks):
     mock_get_workers.return_value = [{'last_heartbeat':
                                      UTCDateTimeField().to_python(datetime(2000, 1, 1))}]
     manage.main()
     # make sure user is not asked for input when workers have timed out
     self.assertFalse(mock__user_input.called)
Ejemplo n.º 4
0
    def test_pulp_manage_db_loads_types(self, start_logging_mock, listdir_mock):
        """
        Test calling pulp-manage-db imports types on a clean types database.
        """
        manage.main()

        all_collection_names = types_db.all_type_collection_names()
        self.assertEqual(len(all_collection_names), 1)

        self.assertEqual(['units_test_type_id'], all_collection_names)

        # Let's make sure we loaded the type definitions correctly
        db_type_definitions = types_db.all_type_definitions()
        self.assertEquals(len(db_type_definitions), 1)
        test_json = json.loads(_test_type_json)
        for attribute in ['id', 'display_name', 'description', 'unit_key', 'search_indexes']:
            self.assertEquals(test_json['types'][0][attribute], db_type_definitions[0][attribute])

        # Now let's ensure that we have the correct indexes
        collection = types_db.type_units_collection('test_type_id')
        indexes = collection.index_information()
        self.assertEqual(indexes['_id_']['key'], [(u'_id', 1)])
        # Make sure we have the unique constraint on all three attributes
        self.assertEqual(indexes['attribute_1_1_attribute_2_1_attribute_3_1']['unique'], True)
        self.assertEqual(indexes['attribute_1_1_attribute_2_1_attribute_3_1']['dropDups'], False)
        self.assertEqual(indexes['attribute_1_1_attribute_2_1_attribute_3_1']['key'],
                         [(u'attribute_1', 1), (u'attribute_2', 1), (u'attribute_3', 1)])
        # Make sure we indexes attributes 1 and 3
        self.assertEqual(indexes['attribute_1_1']['dropDups'], False)
        self.assertEqual(indexes['attribute_1_1']['key'], [(u'attribute_1', 1)])
        self.assertEqual(indexes['attribute_3_1']['dropDups'], False)
        self.assertEqual(indexes['attribute_3_1']['key'], [(u'attribute_3', 1)])
        # Make sure we only have the indexes that we've hand inspected here
        self.assertEqual(indexes.keys(), [u'_id_', u'attribute_1_1_attribute_2_1_attribute_3_1',
                                          u'attribute_1_1', u'attribute_3_1'])
Ejemplo n.º 5
0
    def test_migrate(self, mock_file_config, initialize,
                     mocked_apply_migration, getLogger):
        """
        Let's set all the packages to be at version 0, and then check that the migrations get
        called in the correct order.
        """

        logger = MagicMock()
        getLogger.return_value = logger

        # Make sure we start out with a clean slate
        self.assertEquals(MigrationTracker.objects().count(), 0)
        # Make sure that our mock works. There are three valid packages.
        self.assertEquals(len(models.get_migration_packages()), 4)
        # Set all versions back to 0
        for package in models.get_migration_packages():
            package._migration_tracker.version = 0
            package._migration_tracker.save()
        manage.main()

        # There should have been a critical log about the Exception
        expected_messages = (
            'Applying migration '
            'unit.server.db.migration_packages.raise_exception.0002_oh_no failed.\n\n'
            'Halting migrations due to a migration failure.',
            "Bet you didn\'t see this coming.")
        critical_messages = ''.join(
            [mock_call[1][0] for mock_call in logger.critical.mock_calls])
        for msg in expected_messages:
            self.assertTrue(msg in critical_messages)

        migration_modules_called = [
            mock_call[1][1].name
            for mock_call in mocked_apply_migration.mock_calls
        ]
        # Note that none of the migrations that don't meet our criteria show up in this list. Also,
        # Note that migration_packages.raise_exception.0003_shouldnt_run doesn't appear
        # since migration_packages.raise_exception.0002_oh_no raised an Exception. Note
        # also that even though the raise_exception package raised an Exception, we still run all
        # the z migrations because we don't want one package to break another.
        expected_migration_modules_called = [
            'unit.server.db.migration_packages.platform.0001_stuff_and_junk',
            'unit.server.db.migration_packages.raise_exception.0001_works_fine',
            'unit.server.db.migration_packages.raise_exception.0002_oh_no'
        ]
        self.assertEquals(migration_modules_called,
                          expected_migration_modules_called)
        # Assert that our precious versions have been updated correctly
        for package in models.get_migration_packages():
            if package.name == 'unit.server.db.migration_packages.platform':
                self.assertEqual(package.current_version,
                                 package.latest_available_version)
            elif package.name == 'unit.server.db.migration_packages.raise_exception':
                # The raised Exception should have prevented us from getting past version 1
                self.assertEquals(package.current_version, 1)
            else:
                # raise_exception should cause the migrations to stop
                self.assertEqual(package.current_version, 0)

        initialize.assert_called_once_with(max_timeout=1)
Ejemplo n.º 6
0
    def test_migrate_with_new_packages(self, initialize, start_logging_mock,
                                       logger_mock, mocked_stdout,
                                       mocked_stderr):
        """
        Adding new packages to a system that doesn't have any trackers should advance
        each package to the latest available version, applying all migrate() functions along the
        way.
        """
        # Make sure we start out with a clean slate
        self.assertEquals(MigrationTracker.objects().count(), 0)
        # Make sure that our mock works. There are four valid packages.
        self.assertEquals(len(models.get_migration_packages()), 4)
        manage.main()

        for package in models.get_migration_packages():
            if 'raise_exception' in str(package):
                # The Exception raising package should get to version 1, because version 2 raises
                self.assertEqual(package.current_version, 1)
            elif 'z' in str(package):
                self.assertEquals(package.current_version, 0)
            else:
                # All other packages should reach their top versions
                self.assertEqual(package.current_version,
                                 package.latest_available_version)

        initialize.assert_called_once_with(max_timeout=1)
Ejemplo n.º 7
0
    def test_migrate(self, mock_file_config, initialize, mocked_apply_migration, getLogger):
        """
        Let's set all the packages to be at version 0, and then check that the migrations get
        called in the correct order.
        """

        logger = MagicMock()
        getLogger.return_value = logger

        # Make sure we start out with a clean slate
        self.assertEquals(MigrationTracker.objects().count(), 0)
        # Make sure that our mock works. There are three valid packages.
        self.assertEquals(len(models.get_migration_packages()), 4)
        # Set all versions back to 0
        for package in models.get_migration_packages():
            package._migration_tracker.version = 0
            package._migration_tracker.save()
        manage.main()

        # There should have been a critical log about the Exception
        expected_messages = (
            'Applying migration '
            'unit.server.db.migration_packages.raise_exception.0002_oh_no failed.\n\n'
            'Halting migrations due to a migration failure.',
            "Bet you didn\'t see this coming."
        )
        critical_messages = ''.join([mock_call[1][0] for mock_call in logger.critical.mock_calls])
        for msg in expected_messages:
            self.assertTrue(msg in critical_messages)

        migration_modules_called = [
            mock_call[1][1].name for mock_call in mocked_apply_migration.mock_calls]
        # Note that none of the migrations that don't meet our criteria show up in this list. Also,
        # Note that migration_packages.raise_exception.0003_shouldnt_run doesn't appear
        # since migration_packages.raise_exception.0002_oh_no raised an Exception. Note
        # also that even though the raise_exception package raised an Exception, we still run all
        # the z migrations because we don't want one package to break another.
        expected_migration_modules_called = [
            'unit.server.db.migration_packages.platform.0001_stuff_and_junk',
            'unit.server.db.migration_packages.raise_exception.0001_works_fine',
            'unit.server.db.migration_packages.raise_exception.0002_oh_no']
        self.assertEquals(migration_modules_called, expected_migration_modules_called)
        # Assert that our precious versions have been updated correctly
        for package in models.get_migration_packages():
            if package.name == 'unit.server.db.migration_packages.platform':
                self.assertEqual(package.current_version, package.latest_available_version)
            elif package.name == 'unit.server.db.migration_packages.raise_exception':
                # The raised Exception should have prevented us from getting past version 1
                self.assertEquals(package.current_version, 1)
            else:
                # raise_exception should cause the migrations to stop
                self.assertEqual(package.current_version, 0)

        initialize.assert_called_once_with(max_timeout=1)
Ejemplo n.º 8
0
    def test_migrate(self, file_config_mock, logger_mock,
                     mocked_apply_migration, mocked_stdout, mocked_stderr):
        """
        Let's set all the packages to be at version 0, and then check that the migrations get
        called in the correct order.
        """
        # Make sure we start out with a clean slate
        self.assertEquals(MigrationTracker.get_collection().find({}).count(),
                          0)
        # Make sure that our mock works. There are three valid packages.
        self.assertEquals(len(models.get_migration_packages()), 4)
        # Set all versions back to 0
        for package in models.get_migration_packages():
            package._migration_tracker.version = 0
            package._migration_tracker.save()
        manage.main()

        # There should have been a print to stderr about the Exception
        expected_stderr_calls = [(
            'Applying migration unit.server.db.migration_packages.raise_exception.0002_oh_no '
            'failed.'), ' ', ' See log for details.', '\n']
        stderr_calls = [
            mock_call[1][0] for mock_call in mocked_stderr.mock_calls
        ]
        self.assertEquals(stderr_calls, expected_stderr_calls)

        migration_modules_called = [
            mock_call[1][1].name
            for mock_call in mocked_apply_migration.mock_calls
        ]
        # Note that none of the migrations that don't meet our criteria show up in this list. Also,
        # Note that migration_packages.raise_exception.0003_shouldnt_run doesn't appear
        # since migration_packages.raise_exception.0002_oh_no raised an Exception. Note
        # also that even though the raise_exception package raised an Exception, we still run all
        # the z migrations because we don't want one package to break another.
        expected_migration_modules_called = [
            'unit.server.db.migration_packages.platform.0001_stuff_and_junk',
            'unit.server.db.migration_packages.raise_exception.0001_works_fine',
            'unit.server.db.migration_packages.raise_exception.0002_oh_no',
            'unit.server.db.migration_packages.z.0001_test',
            'unit.server.db.migration_packages.z.0002_test',
            'unit.server.db.migration_packages.z.0003_test'
        ]
        self.assertEquals(migration_modules_called,
                          expected_migration_modules_called)
        # Assert that our precious versions have been updated correctly
        for package in models.get_migration_packages():
            if package.name != 'unit.server.db.migration_packages.raise_exception':
                self.assertEqual(package.current_version,
                                 package.latest_available_version)
            else:
                # The raised Exception should have prevented us from getting past version 1
                self.assertEqual(package.current_version, 1)
Ejemplo n.º 9
0
    def test_migrate_with_test_flag(self, mock_file_config,
                                    mocked_apply_migration, getLogger):
        """
        Let's set all the packages to be at version 0, and then check that the migrations get called
        in the correct order. We will also set the --test flag and ensure that the migration
        versions do not get updated.
        """

        logger = MagicMock()
        getLogger.return_value = logger

        # Make sure we start out with a clean slate
        self.assertEquals(MigrationTracker.get_collection().find({}).count(),
                          0)
        # Make sure that our mock works. There are three valid packages.
        self.assertEquals(len(models.get_migration_packages()), 4)
        # Set all versions back to 0
        for package in models.get_migration_packages():
            package._migration_tracker.version = 0
            package._migration_tracker.save()
        manage.main()

        # There should have been a critical log about the Exception
        expected_messages = (
            'Applying migration unit.server.db.migration_packages.raise_exception.0002_oh_no '
            'failed.\n\nHalting migrations due to a migration failure.',
            'Bet you didn\'t see this coming.')
        critical_messages = [
            mock_call[1][0] for mock_call in logger.critical.mock_calls
        ]
        for msg in expected_messages:
            self.assertTrue(msg in critical_messages)

        migration_modules_called = [
            mock_call[1][1].name
            for mock_call in mocked_apply_migration.mock_calls
        ]
        # Note that none of the migrations that don't meet our criteria show up in this list. Also,
        # Note that migration_packages.raise_exception.0003_shouldnt_run doesn't appear
        # since migration_packages.raise_exception.0002_oh_no raised an Exception. Note
        # also that even though the raise_exception package raised an Exception, we still run all
        # the z migrations because we don't want one package to break another.
        expected_migration_modules_called = [
            'unit.server.db.migration_packages.platform.0001_stuff_and_junk',
            'unit.server.db.migration_packages.raise_exception.0001_works_fine',
            'unit.server.db.migration_packages.raise_exception.0002_oh_no'
        ]
        self.assertEquals(migration_modules_called,
                          expected_migration_modules_called)
        # Assert that our precious versions have not been updated, since we have the --test flag
        for package in models.get_migration_packages():
            self.assertEqual(package.current_version, 0)
Ejemplo n.º 10
0
    def test_admin_is_ensured(self, apply_migration, ensure_admin, ensure_super_user_role,
                              getLogger, factory, initialize, fileConfig, ensure_db_indexes):
        """
        pulp-manage-db is responsible for making sure the admin user and role are in place. This
        test makes sure the manager methods that do that are called.
        """
        logger = MagicMock()
        getLogger.return_value = logger

        code = manage.main()

        self.assertEqual(code, os.EX_OK)

        # Make sure all the right logging happens
        expected_messages = ('Ensuring the admin role and user are in place.',
                             'Admin role and user are in place.')
        info_messages = ''.join([mock_call[1][0] for mock_call in logger.info.mock_calls])
        for msg in expected_messages:
            self.assertTrue(msg in info_messages)

        # Make sure the admin user and role creation methods were called. We'll leave it up to other
        # tests to make sure they work.
        ensure_admin.assert_called_once_with()
        ensure_super_user_role.assert_called_once_with()

        # Also, make sure the factory was initialized
        factory.initialize.assert_called_once_with()

        initialize.assert_called_once_with(max_timeout=1)
Ejemplo n.º 11
0
    def test_current_version_too_high(self, mocked_file_config, initialize, getLogger):
        """
        Set the current package version higher than latest available version, then sit back and eat
        popcorn.
        """
        logger = MagicMock()
        getLogger.return_value = logger

        # Make sure we start out with a clean slate
        self.assertEquals(MigrationTracker.objects().count(), 0)
        # Make sure that our mock works. There are four valid packages.
        self.assertEquals(len(models.get_migration_packages()), 4)
        # Set all versions to ridiculously high values
        for package in models.get_migration_packages():
            package._migration_tracker.version = 9999999
            package._migration_tracker.save()
        error_code = manage.main()
        self.assertEqual(error_code, os.EX_DATAERR)

        # There should have been a critical log about the Exception
        expected_messages = (
            'The database for migration package unit.server.db.migration_packages.'
            'platform is at version 9999999, which is larger than the latest version available, 1.')
        critical_messages = ''.join([mock_call[1][0] for mock_call in logger.critical.mock_calls])
        for msg in expected_messages:
            self.assertTrue(msg in critical_messages)

        initialize.assert_called_once_with(max_timeout=1)
Ejemplo n.º 12
0
 def test_current_version_too_high(self, mocked_file_config, mocked_logger,
                                   mocked_stdout, mocked_stderr):
     """
     Set the current package version higher than latest available version, then sit back and eat
     popcorn.
     """
     # Make sure we start out with a clean slate
     self.assertEquals(MigrationTracker.get_collection().find({}).count(),
                       0)
     # Make sure that our mock works. There are four valid packages.
     self.assertEquals(len(models.get_migration_packages()), 4)
     # Set all versions to ridiculously high values
     for package in models.get_migration_packages():
         package._migration_tracker.version = 9999999
         package._migration_tracker.save()
     error_code = manage.main()
     self.assertEqual(error_code, os.EX_DATAERR)
     # There should have been a print to stderr about the Exception
     expected_stderr_calls = [(
         'The database for migration package unit.server.db.migration_packages.platform is at '
         'version 9999999, which is larger than the latest version available, 1.'
     ), '\n']
     stderr_calls = [
         mock_call[1][0] for mock_call in mocked_stderr.mock_calls
     ]
     self.assertEquals(stderr_calls, expected_stderr_calls)
Ejemplo n.º 13
0
    def test_migrate_with_dry_run_flag(self, mock_file_config, initialize, mocked_apply_migration,
                                       getLogger):
        """
        Test that when a dry run is performed, no migrations actually occur.
        """
        logger = MagicMock()
        getLogger.return_value = logger

        # Make sure we start out with a clean slate
        self.assertEquals(MigrationTracker.objects().count(), 0)
        # Make sure that our mock works. There are three valid packages.
        self.assertEquals(len(models.get_migration_packages()), 4)
        # Set all versions back to 0
        for package in models.get_migration_packages():
            package._migration_tracker.version = 0
            package._migration_tracker.save()
        result = manage.main()

        # Test that none of the mock objects were actually called
        migration_modules_called = [
            mock_call[1][1].name for mock_call in mocked_apply_migration.mock_calls]
        self.assertEquals(0, len(migration_modules_called))
        self.assertEquals(1, result)
        for package in models.get_migration_packages():
            self.assertEqual(package.current_version, 0)

        initialize.assert_called_once_with(max_timeout=1)
Ejemplo n.º 14
0
    def test_migration_removed(self, mock_auto_manage_db, mock_parse_args, mock_init,
                               mock_getLogger):
        e = models.MigrationRemovedError('0002', '1.2.0', '1.1.0', 'foo')
        mock_auto_manage_db.side_effect = e

        ret = manage.main()
        self.assertEqual(ret, os.EX_SOFTWARE)
Ejemplo n.º 15
0
    def test_migrate(self, file_config_mock, logger_mock, mocked_apply_migration, mocked_stdout,
                     mocked_stderr):
        """
        Let's set all the packages to be at version 0, and then check that the migrations get
        called in the correct order.
        """
        # Make sure we start out with a clean slate
        self.assertEquals(MigrationTracker.get_collection().find({}).count(), 0)
        # Make sure that our mock works. There are three valid packages.
        self.assertEquals(len(models.get_migration_packages()), 4)
        # Set all versions back to 0
        for package in models.get_migration_packages():
            package._migration_tracker.version = 0
            package._migration_tracker.save()
        manage.main()

        # There should have been a print to stderr about the Exception
        expected_stderr_calls = [
            ('Applying migration unit.server.db.migration_packages.raise_exception.0002_oh_no '
             'failed.\n\nHalting migrations due to a migration failure.'), ' ', ' See log for details.', '\n',
            'Bet you didn\'t see this coming.', '\n']
        stderr_calls = [mock_call[1][0] for mock_call in mocked_stderr.mock_calls]
        self.assertEquals(stderr_calls, expected_stderr_calls)

        migration_modules_called = [
            mock_call[1][1].name for mock_call in mocked_apply_migration.mock_calls]
        # Note that none of the migrations that don't meet our criteria show up in this list. Also,
        # Note that migration_packages.raise_exception.0003_shouldnt_run doesn't appear
        # since migration_packages.raise_exception.0002_oh_no raised an Exception. Note
        # also that even though the raise_exception package raised an Exception, we still run all
        # the z migrations because we don't want one package to break another.
        expected_migration_modules_called = [
            'unit.server.db.migration_packages.platform.0001_stuff_and_junk',
            'unit.server.db.migration_packages.raise_exception.0001_works_fine',
            'unit.server.db.migration_packages.raise_exception.0002_oh_no']
        self.assertEquals(migration_modules_called, expected_migration_modules_called)
        # Assert that our precious versions have been updated correctly
        for package in models.get_migration_packages():
            if package.name == 'unit.server.db.migration_packages.platform':
                self.assertEqual(package.current_version, package.latest_available_version)
            elif package.name == 'unit.server.db.migration_packages.raise_exception':
                # The raised Exception should have prevented us from getting past version 1
                self.assertEquals(package.current_version, 1)
            else:
                # raise_exception should cause the migrations to stop
                self.assertEqual(package.current_version, 0)
Ejemplo n.º 16
0
    def test_migrate_with_test_flag(self, mock_file_config, mocked_apply_migration, getLogger):
        """
        Let's set all the packages to be at version 0, and then check that the migrations get called
        in the correct order. We will also set the --test flag and ensure that the migration
        versions do not get updated.
        """

        logger = MagicMock()
        getLogger.return_value = logger

        # Make sure we start out with a clean slate
        self.assertEquals(MigrationTracker.get_collection().find({}).count(), 0)
        # Make sure that our mock works. There are three valid packages.
        self.assertEquals(len(models.get_migration_packages()), 4)
        # Set all versions back to 0
        for package in models.get_migration_packages():
            package._migration_tracker.version = 0
            package._migration_tracker.save()
        manage.main()

        # There should have been a critical log about the Exception
        expected_messages = (
            'Applying migration unit.server.db.migration_packages.raise_exception.0002_oh_no '
            'failed.\n\nHalting migrations due to a migration failure.',
            'Bet you didn\'t see this coming.'
        )
        critical_messages = [mock_call[1][0] for mock_call in logger.critical.mock_calls]
        for msg in expected_messages:
            self.assertTrue(msg in critical_messages)

        migration_modules_called = [
            mock_call[1][1].name for mock_call in mocked_apply_migration.mock_calls]
        # Note that none of the migrations that don't meet our criteria show up in this list. Also,
        # Note that migration_packages.raise_exception.0003_shouldnt_run doesn't appear
        # since migration_packages.raise_exception.0002_oh_no raised an Exception. Note
        # also that even though the raise_exception package raised an Exception, we still run all
        # the z migrations because we don't want one package to break another.
        expected_migration_modules_called = [
            'unit.server.db.migration_packages.platform.0001_stuff_and_junk',
            'unit.server.db.migration_packages.raise_exception.0001_works_fine',
            'unit.server.db.migration_packages.raise_exception.0002_oh_no']
        self.assertEquals(migration_modules_called, expected_migration_modules_called)
        # Assert that our precious versions have not been updated, since we have the --test flag
        for package in models.get_migration_packages():
            self.assertEqual(package.current_version, 0)
Ejemplo n.º 17
0
 def test_migrate_with_test_flag(self, start_logging_mock,
                                 mocked_apply_migration, mocked_stderr):
     """
     Let's set all the packages to be at version 0, and then check that the migrations get called
     in the correct order. We will also set the --test flag and ensure that the migration
     versions do not get updated.
     """
     # Make sure we start out with a clean slate
     self.assertEquals(MigrationTracker.get_collection().find({}).count(),
                       0)
     # Make sure that our mock works. There are three valid packages.
     self.assertEquals(len(models.get_migration_packages()), 4)
     # Set all versions back to 0
     for package in models.get_migration_packages():
         package._migration_tracker.version = 0
         package._migration_tracker.save()
     manage.main()
     # There should have been a print to stderr about the Exception
     expected_stderr_calls = [
         'Applying migration data.test_migration_packages.raise_exception.0002_oh_no failed.',
         ' ', ' See log for details.', '\n'
     ]
     stderr_calls = [call[1][0] for call in mocked_stderr.mock_calls]
     self.assertEquals(stderr_calls, expected_stderr_calls)
     migration_modules_called = [
         call[1][1].name for call in mocked_apply_migration.mock_calls
     ]
     # Note that none of the migrations that don't meet our criteria show up in this list. Also,
     # Note that data.test_migration_packages.raise_exception.0003_shouldnt_run doesn't appear
     # since data.test_migration_packages.raise_exception.0002_oh_no raised an Exception. Note
     # also that even though the raise_exception package raised an Exception, we still run all
     # the z migrations because we don't want one package to break another.
     expected_migration_modules_called = [
         'data.test_migration_packages.platform.0001_stuff_and_junk',
         'data.test_migration_packages.raise_exception.0001_works_fine',
         'data.test_migration_packages.raise_exception.0002_oh_no',
         'data.test_migration_packages.z.0001_test',
         'data.test_migration_packages.z.0002_test',
         'data.test_migration_packages.z.0003_test'
     ]
     self.assertEquals(migration_modules_called,
                       expected_migration_modules_called)
     # Assert that our precious versions have not been updated, since we have the --test flag
     for package in models.get_migration_packages():
         self.assertEqual(package.current_version, 0)
Ejemplo n.º 18
0
    def test_wont_run_as_root(self, mock_getuid, mock_stderr):
        ret = manage.main()

        # make sure the exit code reflect a usage error
        self.assertEqual(ret, os.EX_USAGE)
        # make sure a message was written to stderr with appropriate keywords
        self.assertTrue(mock_stderr.write.call_count > 0)
        self.assertTrue('root' in mock_stderr.write.call_args_list[0][0][0])
        self.assertTrue('apache' in mock_stderr.write.call_args_list[0][0][0])
Ejemplo n.º 19
0
    def test_migrate_with_new_packages(self, start_logging_mock, logger_mock, mocked_stderr):
        """
        Adding new packages to a system that doesn't have any trackers should advance
        each package to the latest available version, applying all migrate() functions along the way.
        """
        # Make sure we start out with a clean slate
        self.assertEquals(MigrationTracker.get_collection().find({}).count(), 0)
        # Make sure that our mock works. There are four valid packages.
        self.assertEquals(len(models.get_migration_packages()), 4)
        manage.main()

        for package in models.get_migration_packages():
            if 'raise_exception' in str(package):
                # The Exception raising package should get to version 1, because version 2 raises
                self.assertEqual(package.current_version, 1)
            else:
                # All other packages should reach their top versions
                self.assertEqual(package.current_version, package.latest_available_version)
Ejemplo n.º 20
0
 def test_user_prompted_if_workers_tasks_exist(self, mock__auto_manage_db,
                                               mock__user_input, mock_get_workers,
                                               *unused_mocks):
     mock_get_workers.return_value = [{'last_heartbeat':
                                      UTCDateTimeField().to_python(datetime.now())}]
     ret = manage.main()
     # make sure system exits ok if user chooses not to proceed
     self.assertEqual(ret, os.EX_OK)
     # make sure user is asked for input when there are workers running
     self.assertTrue(mock__user_input.called)
Ejemplo n.º 21
0
    def test_dry_run_no_changes(self, mock_file_config, mock_parse_args, mocked_apply_migration, mock_entry, getLogger):
        logger = MagicMock()
        getLogger.return_value = logger
        mock_args = Namespace(dry_run=True, test=False)
        mock_parse_args.return_value = mock_args

        # Test that when dry run is on, it returns 1 if migrations remain
        exit_code = manage.main()
        self.assertEqual(exit_code, 1)

        # Actually apply the migrations
        mock_args.dry_run = False
        exit_code = manage.main()
        self.assertEqual(exit_code, 0)

        # Perform another dry run and check the return value is now 0
        mock_args.dry_run = True
        exit_code = manage.main()
        self.assertEquals(exit_code, 0)
Ejemplo n.º 22
0
    def test_dry_run_no_changes(self, mock_file_config, mock_parse_args, mocked_apply_migration,
                                mock_entry, getLogger):
        logger = MagicMock()
        getLogger.return_value = logger
        mock_args = Namespace(dry_run=True, test=False)
        mock_parse_args.return_value = mock_args

        # Test that when dry run is on, it returns 1 if migrations remain
        exit_code = manage.main()
        self.assertEqual(exit_code, 1)

        # Actually apply the migrations
        mock_args.dry_run = False
        exit_code = manage.main()
        self.assertEqual(exit_code, 0)

        # Perform another dry run and check the return value is now 0
        mock_args.dry_run = True
        exit_code = manage.main()
        self.assertEquals(exit_code, 0)
Ejemplo n.º 23
0
    def test_admin_creation_dry_run(self, mock_file_config, mocked_apply_migration, getLogger,
                                    mock_ensure_admin, mock_ensure_super_role):
        logger = MagicMock()
        getLogger.return_value = logger

        exit_code = manage.main()

        self.assertEqual(exit_code, 1)

        # Make sure the admin user and role creation methods were not called
        self.assertEquals(0, mock_ensure_admin.call_count)
        self.assertEquals(0, mock_ensure_super_role.call_count)
Ejemplo n.º 24
0
    def test_migrate_with_new_packages(self, mocked_apply_migration):
        """
        Adding new packages to a system that doesn't have any trackers should automatically advance
        each package to the latest available version without calling any migrate() functions.
        """
        # Make sure we start out with a clean slate
        self.assertEquals(MigrationTracker.get_collection().find({}).count(), 0)
        # Make sure that our mock works. There are three valid packages.
        self.assertEquals(len(models.get_migration_packages()), 4)
        manage.main()
        # No calls to apply_migration should have been made, and we should be at the latest package
        # versions for each of the packages that have valid migrations.
        self.assertFalse(mocked_apply_migration.called)
        for package in models.get_migration_packages():
            self.assertEqual(package.current_version, package.latest_available_version)

        # Calling main() again should still not call apply_migration() or change the versions
        manage.main()
        self.assertFalse(mocked_apply_migration.called)
        for package in models.get_migration_packages():
            self.assertEqual(package.current_version, package.latest_available_version)
Ejemplo n.º 25
0
    def test_admin_creation_dry_run(self, mock_file_config, mocked_apply_migration, getLogger,
                                    mock_ensure_admin, mock_ensure_super_role):
        logger = MagicMock()
        getLogger.return_value = logger

        exit_code = manage.main()

        self.assertEqual(exit_code, 1)

        # Make sure the admin user and role creation methods were not called
        self.assertEquals(0, mock_ensure_admin.call_count)
        self.assertEquals(0, mock_ensure_super_role.call_count)
Ejemplo n.º 26
0
 def test_migrate_with_test_flag(self, start_logging_mock, mocked_apply_migration, mocked_stdout,
                                 mocked_stderr):
     """
     Let's set all the packages to be at version 0, and then check that the migrations get called
     in the correct order. We will also set the --test flag and ensure that the migration
     versions do not get updated.
     """
     # Make sure we start out with a clean slate
     self.assertEquals(MigrationTracker.get_collection().find({}).count(), 0)
     # Make sure that our mock works. There are three valid packages.
     self.assertEquals(len(models.get_migration_packages()), 4)
     # Set all versions back to 0
     for package in models.get_migration_packages():
         package._migration_tracker.version = 0
         package._migration_tracker.save()
     manage.main()
     # There should have been a print to stderr about the Exception
     expected_stderr_calls = [
         ('Applying migration unit.server.db.migration_packages.raise_exception.0002_oh_no '
          'failed.'), ' ', ' See log for details.', '\n']
     stderr_calls = [mock_call[1][0] for mock_call in mocked_stderr.mock_calls]
     self.assertEquals(stderr_calls, expected_stderr_calls)
     migration_modules_called = [
         mock_call[1][1].name for mock_call in mocked_apply_migration.mock_calls]
     # Note that none of the migrations that don't meet our criteria show up in this list. Also,
     # Note that migration_packages.raise_exception.0003_shouldnt_run doesn't appear
     # since migration_packages.raise_exception.0002_oh_no raised an Exception. Note
     # also that even though the raise_exception package raised an Exception, we still run all
     # the z migrations because we don't want one package to break another.
     expected_migration_modules_called = [
         'unit.server.db.migration_packages.platform.0001_stuff_and_junk',
         'unit.server.db.migration_packages.raise_exception.0001_works_fine',
         'unit.server.db.migration_packages.raise_exception.0002_oh_no',
         'unit.server.db.migration_packages.z.0001_test',
         'unit.server.db.migration_packages.z.0002_test',
         'unit.server.db.migration_packages.z.0003_test']
     self.assertEquals(migration_modules_called, expected_migration_modules_called)
     # Assert that our precious versions have not been updated, since we have the --test flag
     for package in models.get_migration_packages():
         self.assertEqual(package.current_version, 0)
Ejemplo n.º 27
0
    def test_migrate_with_new_packages(self, initialize, start_logging_mock, logger_mock,
                                       mocked_stdout, mocked_stderr):
        """
        Adding new packages to a system that doesn't have any trackers should advance
        each package to the latest available version, applying no migrate() functions along the
        way.
        """
        # Make sure we start out with a clean slate
        self.assertEquals(MigrationTracker.objects().count(), 0)
        # Make sure that our mock works. There are five valid packages.
        self.assertEquals(len(models.get_migration_packages()), 5)
        manage.main()

        for package in models.get_migration_packages():
            if 'raise_exception' in str(package):
                # The Exception raising package should get to version 3, despite the fact that
                # version 2 raises an exception, because new trackers get fast-forwarded.
                self.assertEqual(package.current_version, 3)
            else:
                # All other packages should reach their top versions
                self.assertEqual(package.current_version, package.latest_available_version)

        initialize.assert_called_once_with(max_timeout=1)
Ejemplo n.º 28
0
 def test_current_version_too_high(self, mocked_file_config, mocked_logger, mocked_stderr):
     """
     Set the current package version higher than latest available version, then sit back and eat
     popcorn.
     """
     # Make sure we start out with a clean slate
     self.assertEquals(MigrationTracker.get_collection().find({}).count(), 0)
     # Make sure that our mock works. There are four valid packages.
     self.assertEquals(len(models.get_migration_packages()), 4)
     # Set all versions to ridiculously high values
     for package in models.get_migration_packages():
         package._migration_tracker.version = 9999999
         package._migration_tracker.save()
     error_code = manage.main()
     self.assertEqual(error_code, os.EX_DATAERR)
     # There should have been a print to stderr about the Exception
     expected_stderr_calls = [
         'The database for migration package data.test_migration_packages.platform is at ' +\
         'version 9999999, which is larger than the latest version available, 1.', '\n']
     stderr_calls = [call[1][0] for call in mocked_stderr.mock_calls]
     self.assertEquals(stderr_calls, expected_stderr_calls)
Ejemplo n.º 29
0
    def test_admin_is_ensured(self, apply_migration, stdout, ensure_admin,
                              ensure_super_user_role, getLogger, factory,
                              fileConfig):
        """
        pulp-manage-db is responsible for making sure the admin user and role are in place. This
        test makes sure the manager methods that do that are called.
        """
        logger = MagicMock()

        def get_logger(*args):
            """
            This is used to side effect getLogger so that we can mock the logger.
            """
            return logger

        getLogger.side_effect = get_logger

        code = manage.main()

        self.assertEqual(code, os.EX_OK)

        # Make sure all the right logging and printing happens
        expected_messages = ('Ensuring the admin role and user are in place.',
                             'Admin role and user are in place.')
        stdout_messages = ''.join(
            [mock_call[1][0] for mock_call in stdout.mock_calls])
        info_messages = ''.join(
            [mock_call[1][0] for mock_call in logger.info.mock_calls])
        for msg in expected_messages:
            self.assertTrue(msg in stdout_messages)
            self.assertTrue(msg in info_messages)

        # Make sure the admin user and role creation methods were called. We'll leave it up to other
        # tests to make sure they work.
        ensure_admin.assert_called_once_with()
        ensure_super_user_role.assert_called_once_with()

        # Also, make sure the factory was initialized
        factory.initialize.assert_called_once_with()
Ejemplo n.º 30
0
    def test_admin_is_ensured(self, apply_migration, stdout, ensure_admin, ensure_super_user_role,
                              getLogger, factory, fileConfig):
        """
        pulp-manage-db is responsible for making sure the admin user and role are in place. This
        test makes sure the manager methods that do that are called.
        """
        logger = MagicMock()

        def get_logger(*args):
            """
            This is used to side effect getLogger so that we can mock the logger.
            """
            return logger

        getLogger.side_effect = get_logger

        code = manage.main()

        self.assertEqual(code, os.EX_OK)

        # Make sure all the right logging and printing happens
        expected_messages = ('Ensuring the admin role and user are in place.',
                             'Admin role and user are in place.')
        stdout_messages = ''.join([mock_call[1][0] for mock_call in stdout.mock_calls])
        info_messages = ''.join([mock_call[1][0] for mock_call in logger.info.mock_calls])
        for msg in expected_messages:
            self.assertTrue(msg in stdout_messages)
            self.assertTrue(msg in info_messages)

        # Make sure the admin user and role creation methods were called. We'll leave it up to other
        # tests to make sure they work.
        ensure_admin.assert_called_once_with()
        ensure_super_user_role.assert_called_once_with()

        # Also, make sure the factory was initialized
        factory.initialize.assert_called_once_with()
Ejemplo n.º 31
0
 def test_user_not_prompted_if_no_workers(self, mock_auto_manage_db,
                                          mock_user_input, *unused_mocks):
     manage.main()
     # make sure user is not asked for input when there are no tasks/workers
     self.assertFalse(mock_user_input.called)
     self.assertTrue(mock_auto_manage_db.called)
Ejemplo n.º 32
0
    def test_set_connection_timeout(self, mock_auto_manage_db, mock_initialize):
        manage.main()

        mock_initialize.assert_called_once_with(max_timeout=1)
Ejemplo n.º 33
0
    def test_set_connection_timeout(self, mock_initialize, *unused_mocks):
        manage.main()

        mock_initialize.assert_called_once_with(max_timeout=1)