2021-02-25 08:09:14 +00:00
|
|
|
from typing import Any, Dict
|
2019-05-29 09:14:06 +00:00
|
|
|
import unittest
|
2021-02-25 08:09:14 +00:00
|
|
|
from unittest.mock import Mock, patch
|
2019-05-29 09:14:06 +00:00
|
|
|
|
2019-06-05 13:43:51 +00:00
|
|
|
from tutor import config as tutor_config
|
2019-05-29 09:14:06 +00:00
|
|
|
from tutor import exceptions
|
2020-09-18 10:39:22 +00:00
|
|
|
from tutor import fmt
|
2019-05-29 09:14:06 +00:00
|
|
|
from tutor import plugins
|
|
|
|
|
|
|
|
|
|
|
|
class PluginsTests(unittest.TestCase):
|
2021-02-25 08:09:14 +00:00
|
|
|
def setUp(self) -> None:
|
2019-06-06 19:58:21 +00:00
|
|
|
plugins.Plugins.clear()
|
2019-05-29 09:14:06 +00:00
|
|
|
|
2020-04-29 14:08:43 +00:00
|
|
|
@patch.object(plugins.DictPlugin, "iter_installed", return_value=[])
|
2021-02-25 08:09:14 +00:00
|
|
|
def test_iter_installed(self, _dict_plugin_iter_installed: Mock) -> None:
|
|
|
|
with patch.object(plugins.pkg_resources, "iter_entry_points", return_value=[]): # type: ignore
|
2019-05-29 09:14:06 +00:00
|
|
|
self.assertEqual([], list(plugins.iter_installed()))
|
|
|
|
|
2021-02-25 08:09:14 +00:00
|
|
|
def test_is_installed(self) -> None:
|
2019-05-29 09:14:06 +00:00
|
|
|
self.assertFalse(plugins.is_installed("dummy"))
|
|
|
|
|
2020-04-29 14:08:43 +00:00
|
|
|
@patch.object(plugins.DictPlugin, "iter_installed", return_value=[])
|
2021-02-25 08:09:14 +00:00
|
|
|
def test_official_plugins(self, _dict_plugin_iter_installed: Mock) -> None:
|
|
|
|
with patch.object(plugins.importlib, "import_module", return_value=42): # type: ignore
|
2020-10-15 14:28:55 +00:00
|
|
|
plugin1 = plugins.OfficialPlugin.load("plugin1")
|
2021-02-25 08:09:14 +00:00
|
|
|
with patch.object(plugins.importlib, "import_module", return_value=43): # type: ignore
|
2020-10-15 14:28:55 +00:00
|
|
|
plugin2 = plugins.OfficialPlugin.load("plugin2")
|
2020-04-29 14:08:43 +00:00
|
|
|
with patch.object(
|
2020-10-15 15:33:28 +00:00
|
|
|
plugins.EntrypointPlugin,
|
|
|
|
"iter_installed",
|
|
|
|
return_value=[plugin1],
|
2019-06-06 19:58:21 +00:00
|
|
|
):
|
|
|
|
self.assertEqual(
|
2020-10-15 15:33:28 +00:00
|
|
|
[plugin1, plugin2],
|
|
|
|
list(plugins.iter_installed()),
|
2019-06-06 19:58:21 +00:00
|
|
|
)
|
|
|
|
|
2021-02-25 08:09:14 +00:00
|
|
|
def test_enable(self) -> None:
|
|
|
|
config: Dict[str, Any] = {plugins.CONFIG_KEY: []}
|
2020-04-29 14:08:43 +00:00
|
|
|
with patch.object(plugins, "is_installed", return_value=True):
|
2019-05-29 09:14:06 +00:00
|
|
|
plugins.enable(config, "plugin2")
|
|
|
|
plugins.enable(config, "plugin1")
|
|
|
|
self.assertEqual(["plugin1", "plugin2"], config[plugins.CONFIG_KEY])
|
|
|
|
|
2021-02-25 08:09:14 +00:00
|
|
|
def test_enable_twice(self) -> None:
|
|
|
|
config: Dict[str, Any] = {plugins.CONFIG_KEY: []}
|
2020-04-29 14:08:43 +00:00
|
|
|
with patch.object(plugins, "is_installed", return_value=True):
|
2019-05-29 09:14:06 +00:00
|
|
|
plugins.enable(config, "plugin1")
|
|
|
|
plugins.enable(config, "plugin1")
|
|
|
|
self.assertEqual(["plugin1"], config[plugins.CONFIG_KEY])
|
|
|
|
|
2021-02-25 08:09:14 +00:00
|
|
|
def test_enable_not_installed_plugin(self) -> None:
|
|
|
|
config: Dict[str, Any] = {"PLUGINS": []}
|
2020-04-29 14:08:43 +00:00
|
|
|
with patch.object(plugins, "is_installed", return_value=False):
|
2019-05-29 09:14:06 +00:00
|
|
|
self.assertRaises(exceptions.TutorError, plugins.enable, config, "plugin1")
|
|
|
|
|
2021-02-25 08:09:14 +00:00
|
|
|
def test_disable(self) -> None:
|
|
|
|
config: Dict[str, Any] = {"PLUGINS": ["plugin1", "plugin2"]}
|
2020-09-18 10:39:22 +00:00
|
|
|
with patch.object(fmt, "STDOUT"):
|
|
|
|
plugins.disable(config, "plugin1")
|
2019-06-05 13:43:51 +00:00
|
|
|
self.assertEqual(["plugin2"], config["PLUGINS"])
|
|
|
|
|
2021-02-25 08:09:14 +00:00
|
|
|
def test_disable_removes_set_config(self) -> None:
|
2020-09-18 10:39:22 +00:00
|
|
|
with patch.object(
|
|
|
|
plugins.Plugins,
|
|
|
|
"iter_enabled",
|
|
|
|
return_value=[
|
|
|
|
plugins.DictPlugin(
|
|
|
|
{
|
|
|
|
"name": "plugin1",
|
|
|
|
"version": "1.0.0",
|
|
|
|
"config": {"set": {"KEY": "value"}},
|
|
|
|
}
|
|
|
|
)
|
|
|
|
],
|
|
|
|
):
|
|
|
|
config = {"PLUGINS": ["plugin1"], "KEY": "value"}
|
|
|
|
with patch.object(fmt, "STDOUT"):
|
|
|
|
plugins.disable(config, "plugin1")
|
|
|
|
self.assertEqual([], config["PLUGINS"])
|
|
|
|
self.assertNotIn("KEY", config)
|
|
|
|
|
2021-03-30 04:18:02 +00:00
|
|
|
def test_none_plugins(self) -> None:
|
|
|
|
config = {plugins.CONFIG_KEY: None}
|
|
|
|
self.assertFalse(plugins.is_enabled(config, "myplugin"))
|
|
|
|
|
2021-02-25 08:09:14 +00:00
|
|
|
def test_patches(self) -> None:
|
2019-05-29 09:14:06 +00:00
|
|
|
class plugin1:
|
|
|
|
patches = {"patch1": "Hello {{ ID }}"}
|
|
|
|
|
2020-04-29 14:08:43 +00:00
|
|
|
with patch.object(
|
2020-01-14 12:32:45 +00:00
|
|
|
plugins.Plugins,
|
|
|
|
"iter_enabled",
|
|
|
|
return_value=[plugins.BasePlugin("plugin1", plugin1)],
|
2019-05-29 09:14:06 +00:00
|
|
|
):
|
|
|
|
patches = list(plugins.iter_patches({}, "patch1"))
|
|
|
|
self.assertEqual([("plugin1", "Hello {{ ID }}")], patches)
|
|
|
|
|
2021-02-25 08:09:14 +00:00
|
|
|
def test_plugin_without_patches(self) -> None:
|
2020-04-29 14:08:43 +00:00
|
|
|
with patch.object(
|
2020-01-14 12:32:45 +00:00
|
|
|
plugins.Plugins,
|
|
|
|
"iter_enabled",
|
|
|
|
return_value=[plugins.BasePlugin("plugin1", None)],
|
2019-05-29 09:14:06 +00:00
|
|
|
):
|
|
|
|
patches = list(plugins.iter_patches({}, "patch1"))
|
|
|
|
self.assertEqual([], patches)
|
|
|
|
|
2021-02-25 08:09:14 +00:00
|
|
|
def test_configure(self) -> None:
|
2019-06-05 13:43:51 +00:00
|
|
|
config = {"ID": "id"}
|
2021-02-25 08:09:14 +00:00
|
|
|
defaults: Dict[str, Any] = {}
|
2019-06-05 17:57:30 +00:00
|
|
|
|
2019-05-29 09:14:06 +00:00
|
|
|
class plugin1:
|
|
|
|
config = {
|
|
|
|
"add": {"PARAM1": "value1", "PARAM2": "value2"},
|
2019-06-05 13:43:51 +00:00
|
|
|
"set": {"PARAM3": "value3"},
|
|
|
|
"defaults": {"PARAM4": "value4"},
|
2019-05-29 09:14:06 +00:00
|
|
|
}
|
|
|
|
|
2020-04-29 14:08:43 +00:00
|
|
|
with patch.object(
|
2020-01-14 12:32:45 +00:00
|
|
|
plugins.Plugins,
|
|
|
|
"iter_enabled",
|
|
|
|
return_value=[plugins.BasePlugin("plugin1", plugin1)],
|
2019-05-29 09:14:06 +00:00
|
|
|
):
|
2019-06-05 13:43:51 +00:00
|
|
|
tutor_config.load_plugins(config, defaults)
|
|
|
|
|
2019-05-29 09:14:06 +00:00
|
|
|
self.assertEqual(
|
2019-06-05 13:43:51 +00:00
|
|
|
{
|
|
|
|
"ID": "id",
|
|
|
|
"PARAM3": "value3",
|
|
|
|
"PLUGIN1_PARAM1": "value1",
|
|
|
|
"PLUGIN1_PARAM2": "value2",
|
|
|
|
},
|
|
|
|
config,
|
2019-05-29 09:14:06 +00:00
|
|
|
)
|
2019-06-05 13:43:51 +00:00
|
|
|
self.assertEqual({"PLUGIN1_PARAM4": "value4"}, defaults)
|
|
|
|
|
2021-02-25 08:09:14 +00:00
|
|
|
def test_configure_set_does_not_override(self) -> None:
|
2019-06-05 13:43:51 +00:00
|
|
|
config = {"ID": "oldid"}
|
2019-05-29 09:14:06 +00:00
|
|
|
|
|
|
|
class plugin1:
|
2019-06-05 13:43:51 +00:00
|
|
|
config = {"set": {"ID": "newid"}}
|
2019-05-29 09:14:06 +00:00
|
|
|
|
2020-04-29 14:08:43 +00:00
|
|
|
with patch.object(
|
2020-01-14 12:32:45 +00:00
|
|
|
plugins.Plugins,
|
|
|
|
"iter_enabled",
|
|
|
|
return_value=[plugins.BasePlugin("plugin1", plugin1)],
|
2019-06-05 13:43:51 +00:00
|
|
|
):
|
|
|
|
tutor_config.load_plugins(config, {})
|
|
|
|
|
|
|
|
self.assertEqual({"ID": "oldid"}, config)
|
|
|
|
|
2021-02-25 08:09:14 +00:00
|
|
|
def test_configure_set_random_string(self) -> None:
|
|
|
|
config: Dict[str, Any] = {}
|
2019-06-05 17:57:30 +00:00
|
|
|
|
2019-06-05 13:43:51 +00:00
|
|
|
class plugin1:
|
|
|
|
config = {"set": {"PARAM1": "{{ 128|random_string }}"}}
|
|
|
|
|
2020-04-29 14:08:43 +00:00
|
|
|
with patch.object(
|
2020-01-14 12:32:45 +00:00
|
|
|
plugins.Plugins,
|
|
|
|
"iter_enabled",
|
|
|
|
return_value=[plugins.BasePlugin("plugin1", plugin1)],
|
2019-06-05 13:43:51 +00:00
|
|
|
):
|
|
|
|
tutor_config.load_plugins(config, {})
|
|
|
|
self.assertEqual(128, len(config["PARAM1"]))
|
|
|
|
|
2021-02-25 08:09:14 +00:00
|
|
|
def test_configure_default_value_with_previous_definition(self) -> None:
|
|
|
|
config: Dict[str, Any] = {}
|
2019-06-05 13:43:51 +00:00
|
|
|
defaults = {"PARAM1": "value"}
|
2019-06-05 17:57:30 +00:00
|
|
|
|
2019-06-05 13:43:51 +00:00
|
|
|
class plugin1:
|
|
|
|
config = {"defaults": {"PARAM2": "{{ PARAM1 }}"}}
|
2019-06-05 17:57:30 +00:00
|
|
|
|
2020-04-29 14:08:43 +00:00
|
|
|
with patch.object(
|
2020-01-14 12:32:45 +00:00
|
|
|
plugins.Plugins,
|
|
|
|
"iter_enabled",
|
|
|
|
return_value=[plugins.BasePlugin("plugin1", plugin1)],
|
2019-06-05 13:43:51 +00:00
|
|
|
):
|
|
|
|
tutor_config.load_plugins(config, defaults)
|
|
|
|
self.assertEqual("{{ PARAM1 }}", defaults["PLUGIN1_PARAM2"])
|
|
|
|
|
2021-02-25 08:09:14 +00:00
|
|
|
def test_configure_add_twice(self) -> None:
|
|
|
|
config: Dict[str, Any] = {}
|
2019-07-03 14:09:33 +00:00
|
|
|
|
|
|
|
class plugin1:
|
|
|
|
config = {"add": {"PARAM1": "{{ 10|random_string }}"}}
|
|
|
|
|
2020-04-29 14:08:43 +00:00
|
|
|
with patch.object(
|
2020-01-14 12:32:45 +00:00
|
|
|
plugins.Plugins,
|
|
|
|
"iter_enabled",
|
|
|
|
return_value=[plugins.BasePlugin("plugin1", plugin1)],
|
2019-07-03 14:09:33 +00:00
|
|
|
):
|
|
|
|
tutor_config.load_plugins(config, {})
|
|
|
|
value1 = config["PLUGIN1_PARAM1"]
|
2020-04-29 14:08:43 +00:00
|
|
|
with patch.object(
|
2020-01-14 12:32:45 +00:00
|
|
|
plugins.Plugins,
|
|
|
|
"iter_enabled",
|
|
|
|
return_value=[plugins.BasePlugin("plugin1", plugin1)],
|
2019-07-03 14:09:33 +00:00
|
|
|
):
|
|
|
|
tutor_config.load_plugins(config, {})
|
|
|
|
value2 = config["PLUGIN1_PARAM1"]
|
|
|
|
|
|
|
|
self.assertEqual(10, len(value1))
|
|
|
|
self.assertEqual(10, len(value2))
|
|
|
|
self.assertEqual(value1, value2)
|
|
|
|
|
2021-02-25 08:09:14 +00:00
|
|
|
def test_hooks(self) -> None:
|
2019-06-05 13:43:51 +00:00
|
|
|
class plugin1:
|
2019-06-06 19:58:21 +00:00
|
|
|
hooks = {"init": ["myclient"]}
|
2019-06-05 17:57:30 +00:00
|
|
|
|
2020-04-29 14:08:43 +00:00
|
|
|
with patch.object(
|
2020-01-14 12:32:45 +00:00
|
|
|
plugins.Plugins,
|
|
|
|
"iter_enabled",
|
|
|
|
return_value=[plugins.BasePlugin("plugin1", plugin1)],
|
2019-05-29 09:14:06 +00:00
|
|
|
):
|
|
|
|
self.assertEqual(
|
2019-07-02 20:16:44 +00:00
|
|
|
[("plugin1", ["myclient"])], list(plugins.iter_hooks({}, "init"))
|
2019-05-29 09:14:06 +00:00
|
|
|
)
|
2019-06-06 19:58:21 +00:00
|
|
|
|
2021-02-25 08:09:14 +00:00
|
|
|
def test_plugins_are_updated_on_config_change(self) -> None:
|
|
|
|
config: Dict[str, Any] = {"PLUGINS": []}
|
2020-01-14 12:32:45 +00:00
|
|
|
plugins1 = plugins.Plugins(config)
|
|
|
|
self.assertEqual(0, len(list(plugins1.iter_enabled())))
|
2019-08-20 15:46:53 +00:00
|
|
|
config["PLUGINS"].append("plugin1")
|
2020-04-29 14:08:43 +00:00
|
|
|
with patch.object(
|
2020-01-14 12:32:45 +00:00
|
|
|
plugins.Plugins,
|
|
|
|
"iter_installed",
|
|
|
|
return_value=[plugins.BasePlugin("plugin1", None)],
|
2019-08-20 15:46:53 +00:00
|
|
|
):
|
2020-01-14 12:32:45 +00:00
|
|
|
plugins2 = plugins.Plugins(config)
|
|
|
|
self.assertEqual(1, len(list(plugins2.iter_enabled())))
|
2020-01-14 14:42:20 +00:00
|
|
|
|
2021-02-25 08:09:14 +00:00
|
|
|
def test_dict_plugin(self) -> None:
|
2020-01-14 14:42:20 +00:00
|
|
|
plugin = plugins.DictPlugin(
|
|
|
|
{"name": "myplugin", "config": {"set": {"KEY": "value"}}, "version": "0.1"}
|
|
|
|
)
|
|
|
|
self.assertEqual("myplugin", plugin.name)
|
|
|
|
self.assertEqual({"KEY": "value"}, plugin.config_set)
|