Example #1
0
    def test_failing_import2(self):
        self.write_content("plugin.json",
                           '{"service": "weaveenv.plugins.tmp"}')
        with pytest.raises(PluginLoadError) as ex:
            load_plugin_json(self.plugin_dir)

        assert ex.value.extra == "Bad service specification in plugin.json"
Example #2
0
    def test_invalid_json(self):
        self.write_content("plugin.json", "hello word.")

        with pytest.raises(PluginLoadError) as ex:
            load_plugin_json(self.plugin_dir)

        assert ex.value.extra == "Error parsing plugin.json."
Example #3
0
    def test_not_base_plugin(self):
        # This just tries to import from weaveenv
        self.write_content(
            "plugin.json", '{"service": "weaveenv.plugins.VirtualEnvManager"}')
        with pytest.raises(PluginLoadError) as ex:
            load_plugin_json(self.plugin_dir)

        assert ex.value.extra == "Service must inherit BasePlugin."
Example #4
0
    def test_bad_dependency(self):
        spec = {
            "service": "plugin.main.TestPlugin",
            "config": {
                "hello": "world"
            },
            "start_timeout": 10,
            "deps": [1, 2, 4],
            "required_rpc_classes": ["http", "bad"],
        }

        self.write_content("plugin.json", json.dumps(spec))
        with pytest.raises(PluginLoadError, match=".*dependencies.*"):
            load_plugin_json(self.plugin_dir, load_service=False)
Example #5
0
def handle_weave_launch():
    params = json.loads(sys.stdin.readline().strip())

    plugin_dir = params["plugin_dir"]
    os.chdir(plugin_dir)
    sys.path.append(plugin_dir)

    if params.get("venv_dir"):
        venv = VirtualEnvManager(params["venv_dir"])
        venv.activate()

    plugin_info = load_plugin_json(plugin_dir)

    if issubclass(plugin_info["service_cls"], MessagingEnabled):
        conn = WeaveConnection.discover()
        conn.connect()
        params["conn"] = conn

    app = plugin_info["service_cls"](**params)

    signal.signal(signal.SIGTERM, lambda x, y: app.on_service_stop())
    signal.signal(signal.SIGINT, lambda x, y: app.on_service_stop())

    app.before_service_start()
    app.on_service_start()
Example #6
0
    def test_bad_exported_rpc(self):
        spec = {
            "service": "plugin.main.TestPlugin",
            "config": {
                "hello": "world"
            },
            "start_timeout": 10,
            "deps": [1, 2, 4],
            "exported_rpc_classes": {
                "http": "HTTP",
                "bad": ".."
            }
        }

        self.write_content("plugin.json", json.dumps(spec))
        with pytest.raises(PluginLoadError, match=".*rpc_class.*"):
            load_plugin_json(self.plugin_dir, load_service=False)
Example #7
0
    def test_good_plugin_load(self):
        spec = {
            "service": "plugin.main.TestPlugin",
            "config": {
                "hello": "world"
            },
            "start_timeout": 10,
            "deps": [1, 2, 4]
        }
        main_py = """
        from weavelib.services import BasePlugin

        class TestPlugin(BasePlugin):
            pass
        """
        package_dir = os.path.join(self.plugin_dir, "plugin")
        main_py_path = os.path.join(self.plugin_dir, "plugin", "main.py")

        os.makedirs(self.plugin_dir + "/plugin")
        self.write_content("plugin.json", json.dumps(spec))
        self.write_content(main_py_path, textwrap.dedent(main_py))

        expected = {
            "deps": [1, 2, 4],
            "package_path": "plugin.main.TestPlugin",
            "config": {
                "hello": "world"
            },
            "start_timeout": 10,
            "service_name": "TestPlugin",
            "exported_rpc_classes": {},
            "required_rpc_classes": [],
        }

        plugin_info = load_plugin_json(self.plugin_dir)
        plugin_class = plugin_info.pop("service_cls")

        assert expected == plugin_info
        assert plugin_class.__name__ == "TestPlugin"
Example #8
0
    def test_failing_import(self):
        self.write_content("plugin.json", '{"service": "a.b.c"}')
        with pytest.raises(PluginLoadError) as ex:
            load_plugin_json(self.plugin_dir)

        assert ex.value.extra == "Failed to import dependencies."
Example #9
0
    def test_bad_service_specification(self):
        self.write_content("plugin.json", '{"service": "hello world"}')
        with pytest.raises(PluginLoadError) as ex:
            load_plugin_json(self.plugin_dir)

        assert ex.value.extra == "Bad 'service' specification in plugin.json."
Example #10
0
    def test_no_service_field(self):
        self.write_content("plugin.json", '{"hello": "world"}')
        with pytest.raises(PluginLoadError) as ex:
            load_plugin_json(self.plugin_dir)

        assert ex.value.extra == "Required field not found in plugin.json."
Example #11
0
    def test_no_plugin_json(self):
        with pytest.raises(PluginLoadError) as ex:
            load_plugin_json(self.plugin_dir)

        assert ex.value.extra == "Error opening plugin.json."