Ejemplo n.º 1
0
def find_and_configure_plugins(env_deployments):
    """Find and configure plugins that may have been declared.

    If the option 'plugins' is not defined, this function is a no-op.  If the
    plugins value is not a string or list, the an error is logged and the
    function just returns; i.e. misconfiguring the plugins option in the yaml
    file doesn't end the test.

    :param env_deployments: The enviroments that are doing to be run.
    :type env_deployments: List[EnvironmentDeploy]
    :raises Exception: a plugin may explode, which means this function can
        fail.
    """
    plugins = get_option("plugins")
    if plugins is None:
        return
    if isinstance(plugins, str):
        plugins = (plugins, )
    if not isinstance(plugins, Iterable):
        logger.error("Couldn't load plugins as it doesn't seem to be a list?")
        return
    for plugin_func in plugins:
        logger.info("Running plugin func %s()", plugin_func)
        try:
            utils.get_class(plugin_func)(env_deployments)
        except Exception as e:
            logger.error("Plugin func: '%s' failed with %s", plugin_func,
                         str(e))
            import traceback
            logger.error(traceback.format_exc())
            raise
Ejemplo n.º 2
0
def run_configure_list(functions):
    """Run the configure scripts as defined in the list of test classes in
       series.

    :param functions: List of configure functions functions
    :type tests: ['zaza.charms_tests.svc.setup', ...]
    """
    for func in functions:
        utils.get_class(func)()
Ejemplo n.º 3
0
def run_post_upgrade_functions(post_upgrade_functions):
    """Execute list supplied functions.

    :param post_upgrade_functions: List of functions
    :type post_upgrade_functions: [function, function, ...]
    """
    if post_upgrade_functions:
        for func in post_upgrade_functions:
            logging.info("Running {}".format(func))
            cl_utils.get_class(func)()
Ejemplo n.º 4
0
def run_configure_list(functions):
    """Run the configure scripts.

    Run the configure scripts as defined in the list of configuration methods
    in series.

    :param functions: List of configure functions functions
    :type tests: ['zaza.charms_tests.svc.setup', ...]
    """
    for func in functions:
        run_report.register_event_start('Configure {}'.format(func))
        utils.get_class(func)()
        run_report.register_event_finish('Configure {}'.format(func))
Ejemplo n.º 5
0
def run_before_deploy_list(functions):
    """Run the pre-deploy scripts.

    Run the pre-deploy scripts as defined in the list of methods in
    series.

    :param functions: List of pre-deploy functions functions
    :type tests: ['zaza.charms_tests.svc.setup', ...]
    """
    for func in functions:
        run_report.register_event_start('Before Deploy {}'.format(func))
        utils.get_class(func)()
        run_report.register_event_finish('Before Deploy {}'.format(func))
Ejemplo n.º 6
0
def run_configure_list(functions):
    """Run the configure scripts.

    Run the configure scripts as defined in the list of configuration methods
    in series.

    :param functions: List of configure functions functions
    :type tests: ['zaza.charms_tests.svc.setup', ...]
    """
    for func in functions:
        with notify_around(NotifyEvents.CONFIGURE_FUNCTION, function=func):
            # TODO: change run_report to use zaza.notifications
            run_report.register_event_start('Configure {}'.format(func))
            utils.get_class(func)()
            run_report.register_event_finish('Configure {}'.format(func))
Ejemplo n.º 7
0
def run_before_deploy_list(functions):
    """Run the pre-deploy scripts.

    Run the pre-deploy scripts as defined in the list of methods in
    series.

    :param functions: List of pre-deploy functions functions
    :type tests: ['zaza.charms_tests.svc.setup', ...]
    """
    for func in functions:
        with notify_around(NotifyEvents.BEFORE_DEPLOY_FUNCTION, function=func):
            # TODO: change run_report to use zaza.notifications
            run_report.register_event_start('Before Deploy {}'.format(func))
            utils.get_class(func)()
            run_report.register_event_finish('Before Deploy {}'.format(func))
async def run_post_application_upgrade_functions(post_upgrade_functions):
    """Execute list supplied functions.

    :param post_upgrade_functions: List of awaitable functions
    :type post_upgrade_functions: [function, function, ...]
    """
    if post_upgrade_functions:
        for func in post_upgrade_functions:
            logging.info("Running {}".format(func))
            m = cl_utils.get_class(func)
            await m()
Ejemplo n.º 9
0
Archivo: test.py Proyecto: sahid/zaza
def run_test_list(tests):
    """Run the tests as defined in the list of test classes in series.

    :param tests: List of test class strings
    :type tests: ['zaza.charms_tests.svc.TestSVCClass1', ...]
    :raises: AssertionError if test run fails
    """
    for _testcase in tests:
        testcase = utils.get_class(_testcase)
        suite = unittest.TestLoader().loadTestsFromTestCase(testcase)
        test_result = unittest.TextTestRunner(verbosity=2).run(suite)
        assert test_result.wasSuccessful(), "Test run failed"
async def run_pre_upgrade_functions(machine, pre_upgrade_functions):
    """Execute list supplied functions.

    Each of the supplied functions will be called with a single
    argument of the machine that is about to be upgraded.

    :param machine: Machine that is about to be upgraded
    :type machine: str
    :param pre_upgrade_functions: List of awaitable functions
    :type pre_upgrade_functions: [function, function, ...]
    """
    if pre_upgrade_functions:
        for func in pre_upgrade_functions:
            logging.info("Running {}".format(func))
            m = cl_utils.get_class(func)
            await m(machine)
Ejemplo n.º 11
0
def run_test_list(tests):
    """Run the tests as defined in the list of test classes in series.

    :param tests: List of test class strings
    :type tests: ['zaza.charms_tests.svc.TestSVCClass1', ...]
    :raises: AssertionError if test run fails
    """
    for _testcase in tests:
        run_report.register_event_start('Test {}'.format(_testcase))
        logging.info('## Running Test {} ##'.format(_testcase))
        testcase = utils.get_class(_testcase)
        suite = unittest.TestLoader().loadTestsFromTestCase(testcase)
        test_result = unittest.TextTestRunner(stream=Stream2Logger(),
                                              verbosity=2).run(suite)
        run_report.register_event_finish('Test {}'.format(_testcase))
        assert test_result.wasSuccessful(), "Test run failed"
Ejemplo n.º 12
0
def run_test_list(tests):
    """Run each test in the list using the appropriate test runner.

    Test classes should declare the class viariable 'test_runner'
    which will indicate which runner should be used. If none is provided
    then the unittest runner is used.

    :param tests: List of tests to be run.
    :type tests: List
    """
    for _testcase in tests:
        run_report.register_event_start('Test {}'.format(_testcase))
        logging.info('## Running Test {} ##'.format(_testcase))
        testcase = utils.get_class(_testcase)
        try:
            runner = testcase.test_runner
        except AttributeError:
            runner = UNITTEST
        get_test_runners()[runner](testcase, _testcase)
Ejemplo n.º 13
0
 def test_get_class(self):
     self.assertEqual(
         type(
             lc_utils.get_class('unit_tests.'
                                'test_zaza_charm_lifecycle_utils.'
                                'TestCharmLifecycleUtils')()), type(self))
Ejemplo n.º 14
0
    def handle_before_bundle(self,
                             event,
                             when,
                             *args,
                             env_deployment=None,
                             **kwargs):
        """Handle when a new bundle is about to be performed.

        This resets/configures the collection. This allows a complete test run
        be a bundle that gets all its logs collected and then saved "somewhere"
        according to the config.

        :param event: This must be NotifyEvents.BUNDLE
        :type event: NotifyEvents
        :param when: This must be NotifyType.BEFORE
        :type when: NotifyType
        :param *args: Any additional args passed; these are ignored.
        :type *args: Tuple(ANY)
        :param env_deployment: The deployment details for this model.
        :type env_deployment: EnvironmentDeploy
        :param **kwargs: Any additional kwargs; these are ignored.
        :type **kwargs: Dict[str, ANY]
        """
        logger.info("handle_before_bundle() called for env_deployment:%s",
                    env_deployment)
        assert event is NotifyEvents.BUNDLE
        assert when is NotifyType.BEFORE
        assert env_deployment is not None
        # Note that get_collection gets a zaza-events.* configured collection
        # if the collection-name key is present.
        collection = ze_collection.get_collection()
        collection.reset()
        context = event_context_vars(env_deployment)
        log_collection_name = expand_vars(
            context,
            get_option('zaza-events.log-collection-name', env_deployment.name))
        description = get_option('zaza-events.collection-description')
        logs_dir_name = expand_vars(context, "{bundle}-{date}")
        try:
            logs_dir_base = self.logs_dir_base.name
        except AttributeError:
            logs_dir_base = self.logs_dir_base
        logs_dir = os.path.join(logs_dir_base, logs_dir_name)
        Path(logs_dir).mkdir(parents=True, exist_ok=True)
        collection.configure(collection=log_collection_name,
                             description=description,
                             logs_dir=logs_dir)

        # Now configure any additional modules automagically
        modules = get_option('zaza-events.modules', [])
        if isinstance(modules, str) or not isinstance(modules, Iterable):
            logger.error("Option zaza-events.module isn't a list? %s", modules)
            return
        for module_spec in modules:
            # if module is a dictionary then it's a key: spec, otherwise there
            # is no spec for the module.
            if isinstance(module_spec, dict):
                if len(module_spec.keys()) != 1:
                    logger.error(
                        "Module key %s is not formatted as a single-key "
                        "dictionary", module_spec)
                    continue
                module, config = list(module_spec.items())[0]
                if not isinstance(config, dict):
                    logger.error("Module key %s has invalid config %s", module,
                                 config)
                    continue
            elif isinstance(module_spec, str):
                module, config = (module_spec, {})
            else:
                logger.error("Can configure with %s.", module_spec)
                continue
            configure_func = (
                "zaza.events.plugins.{}.auto_configure_with_collection".format(
                    module))
            try:
                logger.info("Running autoconfigure for zaza-events func %s()",
                            configure_func)
                utils.get_class(configure_func)(collection, config)
            except Exception as e:
                logger.error(
                    "Error running autoconfigure for zaza-events %s: %s",
                    configure_func, str(e))
                if get_option('zaza-events.raise-exceptions', False):
                    raise

        # events = logging_manager.get_global_event_logger_instance()
        events = get_global_event_logger_instance()
        events.log(Events.START_TEST,
                   comment="Starting {}".format(log_collection_name))