def get_plugin_manifest(plugin_config_file,
                        plugin_config_content,
                        stop_build,
                        skip_id_validation=False):
    """
    Validates the given plugin config content using a pre-defined schema.
    Plugin config file name is used to get the absolute path of plugin source
    directory. Returns a manifest which indicates method implemented in the
    plugin module.
    """
    validation_mode = (ValidationMode.ERROR
                       if stop_build else ValidationMode.WARNING)
    src_dir = file_util.get_src_dir_path(plugin_config_file,
                                         plugin_config_content['srcDir'])
    entry_point_module, entry_point_object = PluginValidator.split_entry_point(
        plugin_config_content['entryPoint'])
    plugin_type = plugin_config_content['pluginType']

    importer = PluginImporter(src_dir, entry_point_module, entry_point_object,
                              plugin_type, True)

    with validate_error_handler(plugin_config_file, validation_mode):
        importer.validate_plugin_module()

    return importer.result
예제 #2
0
    def test_get_plugin_manifest(mock_import, src_dir, plugin_type,
                                 plugin_name, plugin_entry_point_name,
                                 plugin_module_content, plugin_manifest):
        mock_import.return_value = plugin_module_content
        importer = PluginImporter(src_dir, plugin_name,
                                  plugin_entry_point_name, plugin_type, False)
        manifest, warnings = importer.import_plugin()

        assert not warnings
        assert manifest == plugin_manifest
예제 #3
0
    def test_get_plugin_manifest(mock_import, src_dir, plugin_type,
                                 entry_point_module, entry_point_object,
                                 plugin_module_content, plugin_manifest):
        mock_import.return_value = plugin_module_content

        importer = PluginImporter(src_dir, entry_point_module,
                                  entry_point_object, plugin_type, False)
        importer.validate_plugin_module()

        assert importer.result.plugin_manifest == plugin_manifest
예제 #4
0
    def test_plugin_entry_object_none(mock_import, src_dir, plugin_type,
                                      plugin_name, plugin_module_content):
        mock_import.return_value = plugin_module_content
        result = ()

        with pytest.raises(exceptions.UserError) as err_info:
            importer = PluginImporter(src_dir, plugin_name, None, plugin_type,
                                      False)
            importer.validate_plugin_module()
            result = importer.result

        message = str(err_info)
        assert result == ()
        assert 'Plugin entry point object is None.' in message
예제 #5
0
    def test_plugin_entry_object_none(mock_import, src_dir, plugin_type,
                                      plugin_name, plugin_module_content):
        mock_import.return_value = plugin_module_content
        manifest = {}
        warnings = defaultdict(list)

        with pytest.raises(exceptions.UserError) as err_info:
            importer = PluginImporter(src_dir, plugin_name, None, plugin_type,
                                      False)
            manifest, warnings = importer.import_plugin()

        message = str(err_info)
        assert warnings.items() > 0
        assert manifest == {}
        assert "Plugin entry point object is None." in message
예제 #6
0
    def test_plugin_module_content_none(mock_import, src_dir, plugin_type,
                                        entry_point_module,
                                        entry_point_object):
        mock_import.return_value = None
        importer = PluginImporter(src_dir, entry_point_module,
                                  entry_point_object, plugin_type, False)
        importer.validate_plugin_module()
        result = importer.result

        #
        # If module_content is None, importer does not perform any validations
        # and just does a return. So result should have an empty manifest and
        # assert to make sure it is the case.
        #
        assert result.plugin_manifest == {}
예제 #7
0
    def test_plugin_entry_point_nonexistent(mock_import, src_dir, plugin_type,
                                            plugin_name,
                                            plugin_module_content):
        entry_point_name = "nonexistent entry point"
        mock_import.return_value = plugin_module_content
        result = ()

        with pytest.raises(exceptions.UserError) as err_info:
            importer = PluginImporter(src_dir, plugin_name, entry_point_name,
                                      plugin_type, False)
            importer.validate_plugin_module()
            result = importer.result

        message = err_info.value.message
        assert result == ()
        assert ('\'{}\' is not a symbol in module'.format(entry_point_name)
                in message)
예제 #8
0
    def test_plugin_entry_point_nonexistent(mock_import, src_dir, plugin_type,
                                            plugin_name,
                                            plugin_module_content):
        entry_point_name = "nonexistent entry point"
        mock_import.return_value = plugin_module_content
        manifest = {}
        warnings = defaultdict(list)

        with pytest.raises(exceptions.UserError) as err_info:
            importer = PluginImporter(src_dir, plugin_name, entry_point_name,
                                      plugin_type, False)
            manifest, warnings = importer.import_plugin()

        message = err_info.value.message
        assert warnings.items() > 0
        assert manifest == {}
        assert "'{}' is not a symbol in module".format(
            entry_point_name) in message
예제 #9
0
    def test_plugin_object_none(mock_import, src_dir, plugin_type, plugin_name,
                                plugin_module_content):
        none_entry_point = "none_entry_point"
        setattr(plugin_module_content, none_entry_point, None)

        mock_import.return_value = plugin_module_content
        result = ()

        with pytest.raises(exceptions.UserError) as err_info:
            importer = PluginImporter(src_dir, plugin_name, none_entry_point,
                                      plugin_type, False)
            importer.validate_plugin_module()
            result = importer.result

        message = err_info.value.message
        assert result == ()
        assert ('Plugin object retrieved from the entry point {} is'
                ' None'.format(none_entry_point)) in message
예제 #10
0
    def test_plugin_object_none(mock_import, src_dir, plugin_type, plugin_name,
                                plugin_module_content):
        none_entry_point = "none_entry_point"
        setattr(plugin_module_content, none_entry_point, None)

        mock_import.return_value = plugin_module_content
        manifest = {}
        warnings = defaultdict(list)

        with pytest.raises(exceptions.UserError) as err_info:
            importer = PluginImporter(src_dir, plugin_name, none_entry_point,
                                      plugin_type, False)
            manifest, warnings = importer.import_plugin()

        message = err_info.value.message
        assert warnings.items() > 0
        assert manifest == {}
        assert ("Plugin object retrieved from the entry point {} is"
                " None".format(none_entry_point)) in message
예제 #11
0
def get_plugin_importer(plugin_config_file):
    plugin_config_content = None
    with open(plugin_config_file, 'rb') as f:
        plugin_config_content = yaml.safe_load(f)

    src_dir = file_util.get_src_dir_path(plugin_config_file,
                                         plugin_config_content['srcDir'])
    entry_point_module, entry_point_object = plugin_validator.PluginValidator\
        .split_entry_point(plugin_config_content['entryPoint'])
    plugin_type = plugin_config_content['pluginType']

    return PluginImporter(src_dir, entry_point_module, entry_point_object,
                          plugin_type, True)