def test_should_log_two_elements_if_a_list_with_two_elements_is_given(self):

        log_elements_of_list(self.mock_log, "Created %s elements.", ["element0", "element1"])

        self.mock_log.assert_any_call("Created %s elements. Listing in sorted order:", 2)
        self.mock_log.assert_any_call("    #%s: %s", 0, "element0")
        self.mock_log.assert_any_call("    #%s: %s", 1, "element1")
    def test_should_log_one_element_if_a_list_with_one_element_is_given(self):

        log_elements_of_list(self.mock_log, 'Created %s elements.',
                             ['element'])

        self.mock_log.assert_any_call(
            'Created %s elements. Listing in sorted order:', 1)
        self.mock_log.assert_any_call('    #%s: %s', 0, 'element')
    def test_should_log_elements_in_sorted_order(self):

        log_elements_of_list(self.mock_log, 'Created %s elements.',
                             ['element0', 'element1', 'element2'])

        self.assertEqual([
            call('Created %s elements. Listing in sorted order:', 3),
            call('    #%s: %s', 0, 'element0'),
            call('    #%s: %s', 1, 'element1'),
            call('    #%s: %s', 2, 'element2')
        ], self.mock_log.call_args_list)
    def test_should_log_elements_in_sorted_order(self):

        log_elements_of_list(self.mock_log, "Created %s elements.", ["element0", "element1", "element2"])

        self.assertEqual(
            [
                call("Created %s elements. Listing in sorted order:", 3),
                call("    #%s: %s", 0, "element0"),
                call("    #%s: %s", 1, "element1"),
                call("    #%s: %s", 2, "element2"),
            ],
            self.mock_log.call_args_list,
        )
Example #5
0
    def get_changed_paths(self, revision):
        """Return list of all paths that were changed in given revision"""

        path_with_action = self.get_changed_paths_with_action(revision)

        changed_paths_and_action = []
        changed_paths = []
        for path, action in path_with_action:
            changed_paths.append(path)
            changed_paths_and_action.append("%s (%s)" % (path, action))

        log_elements_of_list(LOGGER.debug, 'The commit change set contained '
                                           '%s changed path(s). Listing with svn action.',
                             changed_paths_and_action)
        return changed_paths
Example #6
0
    def _build_hosts(self, hosts):
        if not hosts:
            LOGGER.warn('Trying to build rpms for hosts, but no hosts given!')
            return

        for host in hosts:
            self.host_queue.put(host)

        rpm_queue = Queue()
        svn_service_queue = Queue()
        svn_service_queue.put(self.svn_service)

        thread_count = self._get_thread_count(hosts)
        thread_pool = [
            BuildHostThread(
                name='Thread-%d' % i,
                revision=self.revision,
                svn_service_queue=svn_service_queue,
                rpm_queue=rpm_queue,
                notify_that_host_failed=self._notify_that_host_failed,
                host_queue=self.host_queue,
                work_dir=self.work_dir,
                error_logging_handler=self.error_handler)
            for i in range(thread_count)
        ]

        for thread in thread_pool:
            LOGGER.debug('%s: starting ...', thread.name)
            thread.start()

        for thread in thread_pool:
            thread.join()

        failed_hosts = dict(self._consume_queue(self.failed_host_queue))
        if failed_hosts:
            failed_hosts_str = [
                '\n%s:\n\n%s\n\n' % (key, value)
                for (key, value) in failed_hosts.iteritems()
            ]
            raise CouldNotBuildSomeRpmsException(
                "Could not build config rpm for some host(s): %s" %
                '\n'.join(failed_hosts_str))

        LOGGER.info("Finished building configuration rpm(s).")
        built_rpms = self._consume_queue(rpm_queue)
        log_elements_of_list(LOGGER.debug, 'Built %s rpm(s).', built_rpms)

        return built_rpms
    def _build_hosts(self, hosts):
        if not hosts:
            LOGGER.warn("Trying to build rpms for hosts, but no hosts given!")
            return

        host_queue = Queue()
        for host in hosts:
            host_queue.put(host)

        rpm_queue = Queue()
        svn_service_queue = Queue()
        failed_host_queue = Queue()
        svn_service_queue.put(self.svn_service)

        thread_count = self._get_thread_count(hosts)
        thread_pool = [
            BuildHostThread(
                name="Thread-%d" % i,
                revision=self.revision,
                svn_service_queue=svn_service_queue,
                rpm_queue=rpm_queue,
                host_queue=host_queue,
                failed_host_queue=failed_host_queue,
                work_dir=self.work_dir,
                error_logging_handler=self.error_handler,
            )
            for i in range(thread_count)
        ]

        for thread in thread_pool:
            LOGGER.debug("%s: starting ...", thread.name)
            thread.start()

        for thread in thread_pool:
            thread.join()

        failed_hosts = dict(self._consume_queue(failed_host_queue))
        if failed_hosts:
            failed_hosts_str = ["\n%s:\n\n%s\n\n" % (key, value) for (key, value) in failed_hosts.iteritems()]
            raise CouldNotBuildSomeRpmsException(
                "Could not build config rpm for some host(s): %s" % "\n".join(failed_hosts_str)
            )

        LOGGER.info("Finished building configuration rpm(s).")
        built_rpms = self._consume_queue(rpm_queue)
        log_elements_of_list(LOGGER.debug, "Built %s rpm(s).", built_rpms)

        return built_rpms
    def get_changed_paths(self, revision):
        """ Returns the list of all changed paths from the change set with the given revision """

        path_with_action = self.get_changed_paths_with_action(revision)

        changed_paths_and_action = []
        changed_paths = []
        for path, action in path_with_action:
            changed_paths.append(path)
            changed_paths_and_action.append("%s (%s)" % (path, action))

        log_elements_of_list(
            LOGGER.debug,
            'The commit change set contained %s changed path(s). Listing with svn action.',
            changed_paths_and_action)
        return changed_paths
Example #9
0
    def build(self):
        LOGGER.info('Working on revision %s', self.revision)
        self.logger.info("Starting with revision %s", self.revision)
        try:
            changed_paths = self.svn_service.get_changed_paths(self.revision)
            if not changed_paths:
                LOGGER.info(
                    "No rpm(s) built. No change in configuration directory.")
                return
            available_hosts = self.svn_service.get_hosts(self.revision)

            affected_hosts = list(
                self._get_affected_hosts(changed_paths, available_hosts))
            if not affected_hosts:
                LOGGER.info(
                    "No rpm(s) built. No host affected by change set: %s",
                    str(changed_paths))
                return

            log_elements_of_list(LOGGER.debug, 'Detected %s affected host(s).',
                                 affected_hosts)

            self._prepare_work_dir()
            rpms = self._build_hosts(affected_hosts)
            self._upload_rpms(rpms)
            self._move_configviewer_dirs_to_final_destination(affected_hosts)

        except BaseConfigRpmMakerException as exception:
            self.logger.error('Last error during build:\n%s' % str(exception))
            self.__build_error_msg_and_move_to_public_access(self.revision)
            raise exception

        except Exception as exception:
            self.logger.exception('Last error during build:')
            error_msg = self.__build_error_msg_and_move_to_public_access(
                self.revision)
            raise Exception(
                'Unexpected error occurred, stacktrace will follow.\n%s\n\n%s'
                % (traceback.format_exc(), error_msg))

        self._clean_up_work_dir()
        return rpms
    def build(self):
        LOGGER.info("Working on revision %s", self.revision)
        self.logger.info("Starting with revision %s", self.revision)
        try:
            changed_paths = self.svn_service.get_changed_paths(self.revision)
            if not changed_paths:
                LOGGER.info("No rpm(s) built. No change in configuration directory.")
                return
            available_hosts = self.svn_service.get_hosts(self.revision)

            affected_hosts = list(self._get_affected_hosts(changed_paths, available_hosts))
            if not affected_hosts:
                LOGGER.info("No rpm(s) built. No host affected by change set: %s", str(changed_paths))
                return

            log_elements_of_list(LOGGER.debug, "Detected %s affected host(s).", affected_hosts)

            self._prepare_work_dir()
            rpms = self._build_hosts(affected_hosts)
            self._upload_rpms(rpms)
            self._move_configviewer_dirs_to_final_destination(affected_hosts)

        except BaseConfigRpmMakerException as exception:
            self.logger.error("Last error during build:\n%s" % str(exception))
            self.__build_error_msg_and_move_to_public_access(self.revision)
            raise exception

        except Exception as exception:
            self.logger.exception("Last error during build:")
            error_msg = self.__build_error_msg_and_move_to_public_access(self.revision)
            raise Exception(
                "Unexpected error occurred, stacktrace will follow.\n%s\n\n%s" % (traceback.format_exc(), error_msg)
            )

        self._clean_up_work_dir()
        return rpms
    def test_should_only_log_summary_when_list_has_no_elements(self):

        log_elements_of_list(self.mock_log, 'Created %s elements.', [])

        self.mock_log.assert_called_with('Created %s elements.', 0)
    def test_should_only_log_summary_when_list_has_no_elements(self):

        log_elements_of_list(self.mock_log, "Created %s elements.", [])

        self.mock_log.assert_called_with("Created %s elements.", 0)