def testPushAdvisorConfigChangeToString(self):
        advice = self.services_messages.Advice(
            description=
            'Change the config so that this message does not appear. '
            '\u2019')
        change_type = (self.services_messages.ConfigChange.
                       ChangeTypeValueValuesEnum.ADDED)
        element = 'element with unicode char \u2019'
        new_value = 'bar with unicode char \u2019'
        old_value = 'foo with unicode char \u2019'
        config_change = self.services_messages.ConfigChange(
            advices=[advice],
            changeType=change_type,
            element=element,
            newValue=new_value,
            oldValue=old_value)

        expected_result = (
            'Element [{element}] (old value = {old_value}, '
            'new value = {new_value}) was {change_type}. Advice:\n'
            '\t* {advice}'.format(
                element=config_change.element,
                old_value=config_change.oldValue,
                new_value=config_change.newValue,
                change_type=services_util.PushAdvisorChangeTypeToString(
                    config_change.changeType),
                advice=advice.description))
        result = services_util.PushAdvisorConfigChangeToString(config_change)

        self.assertEqual(expected_result, result)
예제 #2
0
    def ShowConfigReport(self,
                         service,
                         service_config_id,
                         log_func=log.warning):
        """Run and display results (if any) from the Push Advisor.

    Args:
      service: The name of the service for which to compare configs.
      service_config_id: The new config ID to compare against the active config.
      log_func: The function to which to pass advisory messages
        (default: log.warning).

    Returns:
      The number of advisory messages returned by the Push Advisor.
    """
        num_changes_with_advice = 0

        reporter = config_reporter.ConfigReporter(service)

        # Set the new config as the recently generated service config ID
        reporter.new_config.SetConfigId(service_config_id)

        # We always want to compare agaisnt the active config, so use default here
        reporter.old_config.SetConfigUseDefaultId()

        change_report = reporter.RunReport()
        if not change_report or not change_report.configChanges:
            return 0

        changes = change_report.configChanges

        for change in changes:
            if change.advices:
                if num_changes_with_advice < NUM_ADVICE_TO_PRINT:
                    log_func(
                        '%s\n',
                        services_util.PushAdvisorConfigChangeToString(change))
                num_changes_with_advice += 1

        if num_changes_with_advice > NUM_ADVICE_TO_PRINT:
            log_func(
                '%s total changes with advice found, check config report file '
                'for full list.\n', num_changes_with_advice)

        return num_changes_with_advice