Beispiel #1
0
def test_hplugin_get_logger(hplugin_meta):
    "Test get logger"

    with mock.patch.object(hplugin_meta, 'get_logger') as mget_logger:

        def hinit(self, *args, **kwargs):
            self.logger = self.get_logger()

        hplugin_meta.__init__ = hinit

        m_log = mock.Mock(spec=hlogger.Logger)
        pmanager = PluginManager()
        pnode = pmanager.register(hplugin_meta, m_log)
        pmanager.init_plugins()

        mget_logger.assert_called_with()


#@pytest.mark.parametrize("id_count, node_func",
#                          [
#                              (2, lambda i, h: (h.))
#                              ])
#def test_plugin_dependency(id_count, node_func, hplugin):

#    node_ids = [uuid.uuid4() for x in range(id_count)]
#    nodes = node_func(node_ids, hplugin)

#    pm = PluginManager()
#    for x in nodes:
#        hpm = plugins._plugin_load(x, 'test', _logger=mock.Mock(spec=hlogger.Logger), _manager=pm)

#    node_order, f = pm._solve(pm._nodes)
#    assert [n.plugin.ID for n in node_order] == [x.ID for x in nodes]
Beispiel #2
0
    def test_plugin_register(self, hplugin_a, hplugin_b):
        pm = PluginManager()
        for h in (hplugin_a, hplugin_b):
            node = plugins.plugin_load(pm, str(h))
            assert isinstance(node, plugins.PluginNode) and pm.get_node(
                node.info.id)

        with mock.patch("happypanda.core.plugins.log_plugin_error"):
            with pytest.raises(hp_exceptions.PluginLoadError) as excinfo:
                plugins.plugin_load(pm, str(hplugin_a))
            assert "Plugin ID already exists" in str(excinfo.value)
Beispiel #3
0
def test_hplugin_on_command_handler_error(hplugin_meta):
    "Test for error on invalid command handler"
    cmd_name = "test_command"
    constants.available_commands.add(cmd_name)

    def hinit(self, *args, **kwargs):
        self.on_command(cmd_name, None)

    hplugin_meta.__init__ = hinit

    pmanager = PluginManager()
    pnode = pmanager.register(hplugin_meta, mock.Mock(spec=hlogger.Logger))
    with pytest.raises(hp_exceptions.PluginCommandError) as excinfo:
        pmanager.init_plugins()
    assert "Handler should be callable" in str(excinfo)
Beispiel #4
0
    def test_plugin_load(self, hplugin_a):
        "Test if a plugin can be loaded"
        with mock.patch("happypanda.core.plugins.PluginManager.register") as pm_register,\
            mock.patch("happypanda.core.plugins.log_plugin_error"):
            pm = PluginManager()
            hpm = plugins.plugin_load(pm, str(hplugin_a))

            assert pm_register.called

            hplugin_a.join('main.py').remove()
            with pytest.raises(hp_exceptions.PluginLoadError) as excinfo:
                hpm = plugins.plugin_load(pm, str(hplugin_a))
            assert "Plugin entry" in str(excinfo.value)
            with open(str(hplugin_a.join('main.py')), 'x') as f:
                pass
            hplugin_a.join('test.py').remove()
            with pytest.raises(hp_exceptions.PluginLoadError) as excinfo:
                hpm = plugins.plugin_load(pm, str(hplugin_a))
            assert "Plugin test entry" in str(excinfo.value)
            with open(str(hplugin_a.join('test.py')), 'x') as f:
                pass
            with open(str(hplugin_a.join('hplugin.json')), 'w') as f:
                f.write('')

            with pytest.raises(hp_exceptions.PluginLoadError) as excinfo:
                hpm = plugins.plugin_load(pm, str(hplugin_a))
            assert "Failed to decode manifest file:" in str(excinfo.value)

            hplugin_a.join('hplugin.json').remove()
            with pytest.raises(hp_exceptions.PluginLoadError) as excinfo:
                hpm = plugins.plugin_load(pm, str(hplugin_a))
            assert "No manifest file named 'hplugin.json' found" in str(
                excinfo.value)
Beispiel #5
0
def test_hplugin_get_logger(hplugin_meta):
    "Test get logger"

    with mock.patch.object(hplugin_meta, 'get_logger') as mget_logger:

        def hinit(self, *args, **kwargs):
            self.logger = self.get_logger()

        hplugin_meta.__init__ = hinit

        m_log = mock.Mock(spec=hlogger.Logger)
        pmanager = PluginManager()
        pnode = pmanager.register(hplugin_meta, m_log)
        pmanager.init_plugins()

        assert mget_logger.assert_called_with()
Beispiel #6
0
def test_hplugin_shortnme_attrib_error(hplugin):
    "Test for errors if plugin class has empty required attributes"
    for a in ("okname", "verylongnameexceeeeeeeding", "is spaces allowed?"):
        setattr(hplugin, "SHORTNAME", a)
        if len(a) > constants.plugin_shortname_length:
            with pytest.raises(hp_exceptions.PluginAttributeError) as excinfo:
                hpm = plugins._plugin_load(
                    hplugin,
                    'test',
                    _logger=mock.Mock(spec=hlogger.Logger),
                    _manager=PluginManager())
            assert "shortname cannot exceed" in str(excinfo.value)
        else:
            hpm = plugins._plugin_load(hplugin,
                                       'test',
                                       _logger=mock.Mock(spec=hlogger.Logger),
                                       _manager=PluginManager())
Beispiel #7
0
def test_hplugin_on_command_name_error(hplugin_meta):
    "Test for error on nonexistant command"
    cmd_name = "test_command"

    def phandler():
        pass

    def hinit(self, *args, **kwargs):
        self.on_command("test", phandler)

    hplugin_meta.__init__ = hinit

    pmanager = PluginManager()
    pnode = pmanager.register(hplugin_meta, mock.Mock(spec=hlogger.Logger))
    with pytest.raises(hp_exceptions.PluginCommandError) as excinfo:
        pmanager.init_plugins()
    assert "does not exist" in str(excinfo)
Beispiel #8
0
def test_hplugin_on_command(m_method, hplugin_meta):
    "Test plugin class on_command method"
    cmd_name = "test_command"
    constants.available_commands.add(cmd_name)

    def phandler():
        pass

    def hinit(self, *args, **kwargs):
        self.on_command(cmd_name, phandler)

    hplugin_meta.__init__ = hinit

    pmanager = PluginManager()
    pnode = pmanager.register(hplugin_meta, mock.Mock(spec=hlogger.Logger))
    pmanager.init_plugins()
    m_method.assert_called_with(pnode, cmd_name, phandler)
Beispiel #9
0
def test_plugin_init_sig(hplugin_meta):
    "Test if plugin class has the expected signature"
    pm = PluginManager()

    pm.register(hplugin_meta, mock.Mock(spec=hlogger.Logger))

    # good __init__
    hplugin_meta.__init__ = lambda self, *args, **kwargs: None
    pm.init_plugins()
Beispiel #10
0
def test_plugin_init_sig_error(init_func, exc_msg, hplugin_meta):
    "Test for errors if plugin class has wrong signature"
    pm = PluginManager()

    pm.register(hplugin_meta, mock.Mock(spec=hlogger.Logger))

    hplugin_meta.__init__ = init_func
    with pytest.raises(hp_exceptions.PluginSignatureError) as excinfo:
        pm.init_plugins()
    assert exc_msg in str(excinfo.value)
Beispiel #11
0
def test_hplugin_logger_creation(m_open, hplugin):
    "Test plugin-specific logger is created"

    plugins.registered = PluginManager()

    p = os.path.join(os.getcwd(), "plugin")
    p_log = "plugin.log"
    enc = 'utf-8'

    hplugin.NAME = "test"

    node = plugins._plugin_load(hplugin, p)

    m_open.assert_called_with(os.path.join(p, p_log), 'a', encoding=enc)

    assert node.logger._logger.name == 'HPX Plugin.' + hplugin.NAME