mirror of
https://github.com/Significant-Gravitas/AutoGPT.git
synced 2026-04-08 03:00:28 -04:00
Rework plugin config to be file-based (#4673)
This commit is contained in:
@@ -1,7 +1,9 @@
|
||||
import os
|
||||
from pathlib import Path
|
||||
from tempfile import TemporaryDirectory
|
||||
|
||||
import pytest
|
||||
import yaml
|
||||
from pytest_mock import MockerFixture
|
||||
|
||||
from autogpt.agent.agent import Agent
|
||||
@@ -32,9 +34,25 @@ def workspace(workspace_root: Path) -> Workspace:
|
||||
return Workspace(workspace_root, restrict_to_workspace=True)
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def temp_plugins_config_file():
|
||||
"""Create a plugins_config.yaml file in a temp directory so that it doesn't mess with existing ones"""
|
||||
config_directory = TemporaryDirectory()
|
||||
config_file = os.path.join(config_directory.name, "plugins_config.yaml")
|
||||
with open(config_file, "w+") as f:
|
||||
f.write(yaml.dump({}))
|
||||
|
||||
yield config_file
|
||||
|
||||
|
||||
@pytest.fixture()
|
||||
def config(mocker: MockerFixture, workspace: Workspace) -> Config:
|
||||
def config(
|
||||
temp_plugins_config_file: str, mocker: MockerFixture, workspace: Workspace
|
||||
) -> Config:
|
||||
config = Config()
|
||||
config.plugins_dir = "tests/unit/data/test_plugins"
|
||||
config.plugins_config_file = temp_plugins_config_file
|
||||
config.load_plugins_config()
|
||||
|
||||
# Do a little setup and teardown since the config object is a singleton
|
||||
mocker.patch.multiple(
|
||||
|
||||
@@ -1,71 +0,0 @@
|
||||
import pytest
|
||||
|
||||
from autogpt.config import Config
|
||||
from autogpt.plugins import scan_plugins
|
||||
|
||||
PLUGINS_TEST_DIR = "tests/unit/data/test_plugins"
|
||||
PLUGIN_TEST_OPENAI = "https://weathergpt.vercel.app/"
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def mock_config_denylist_allowlist_check():
|
||||
class MockConfig:
|
||||
"""Mock config object for testing the denylist_allowlist_check function"""
|
||||
|
||||
plugins_denylist = ["BadPlugin"]
|
||||
plugins_allowlist = ["GoodPlugin"]
|
||||
authorise_key = "y"
|
||||
exit_key = "n"
|
||||
|
||||
return MockConfig()
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def config_with_plugins():
|
||||
"""Mock config object for testing the scan_plugins function"""
|
||||
# Test that the function returns the correct number of plugins
|
||||
cfg = Config()
|
||||
cfg.plugins_dir = PLUGINS_TEST_DIR
|
||||
cfg.plugins_openai = ["https://weathergpt.vercel.app/"]
|
||||
return cfg
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def mock_config_openai_plugin():
|
||||
"""Mock config object for testing the scan_plugins function"""
|
||||
|
||||
class MockConfig:
|
||||
"""Mock config object for testing the scan_plugins function"""
|
||||
|
||||
plugins_dir = PLUGINS_TEST_DIR
|
||||
plugins_openai = [PLUGIN_TEST_OPENAI]
|
||||
plugins_denylist = ["AutoGPTPVicuna", "auto_gpt_guanaco"]
|
||||
plugins_allowlist = [PLUGIN_TEST_OPENAI]
|
||||
|
||||
return MockConfig()
|
||||
|
||||
|
||||
def test_scan_plugins_openai(mock_config_openai_plugin):
|
||||
# Test that the function returns the correct number of plugins
|
||||
result = scan_plugins(mock_config_openai_plugin, debug=True)
|
||||
assert len(result) == 1
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def mock_config_generic_plugin():
|
||||
"""Mock config object for testing the scan_plugins function"""
|
||||
|
||||
# Test that the function returns the correct number of plugins
|
||||
class MockConfig:
|
||||
plugins_dir = PLUGINS_TEST_DIR
|
||||
plugins_openai = []
|
||||
plugins_denylist = []
|
||||
plugins_allowlist = ["AutoGPTPVicuna", "auto_gpt_guanaco"]
|
||||
|
||||
return MockConfig()
|
||||
|
||||
|
||||
def test_scan_plugins_generic(mock_config_generic_plugin):
|
||||
# Test that the function returns the correct number of plugins
|
||||
result = scan_plugins(mock_config_generic_plugin, debug=True)
|
||||
assert len(result) == 2
|
||||
@@ -1,10 +1,61 @@
|
||||
import pytest
|
||||
import os
|
||||
|
||||
from autogpt.plugins import denylist_allowlist_check, inspect_zip_for_modules
|
||||
import yaml
|
||||
|
||||
from autogpt.config.config import Config
|
||||
from autogpt.plugins import inspect_zip_for_modules, scan_plugins
|
||||
from autogpt.plugins.plugin_config import PluginConfig
|
||||
|
||||
PLUGINS_TEST_DIR = "tests/unit/data/test_plugins"
|
||||
PLUGIN_TEST_ZIP_FILE = "Auto-GPT-Plugin-Test-master.zip"
|
||||
PLUGIN_TEST_INIT_PY = "Auto-GPT-Plugin-Test-master/src/auto_gpt_vicuna/__init__.py"
|
||||
PLUGIN_TEST_OPENAI = "https://weathergpt.vercel.app/"
|
||||
|
||||
|
||||
def test_scan_plugins_openai(config: Config):
|
||||
config.plugins_openai = [PLUGIN_TEST_OPENAI]
|
||||
plugins_config = config.plugins_config
|
||||
plugins_config.plugins[PLUGIN_TEST_OPENAI] = PluginConfig(
|
||||
name=PLUGIN_TEST_OPENAI, enabled=True
|
||||
)
|
||||
|
||||
# Test that the function returns the correct number of plugins
|
||||
result = scan_plugins(config, debug=True)
|
||||
assert len(result) == 1
|
||||
|
||||
|
||||
def test_scan_plugins_generic(config: Config):
|
||||
# Test that the function returns the correct number of plugins
|
||||
plugins_config = config.plugins_config
|
||||
plugins_config.plugins["auto_gpt_guanaco"] = PluginConfig(
|
||||
name="auto_gpt_guanaco", enabled=True
|
||||
)
|
||||
plugins_config.plugins["auto_gpt_vicuna"] = PluginConfig(
|
||||
name="auto_gptp_vicuna", enabled=True
|
||||
)
|
||||
result = scan_plugins(config, debug=True)
|
||||
plugin_class_names = [plugin.__class__.__name__ for plugin in result]
|
||||
|
||||
assert len(result) == 2
|
||||
assert "AutoGPTGuanaco" in plugin_class_names
|
||||
assert "AutoGPTPVicuna" in plugin_class_names
|
||||
|
||||
|
||||
def test_scan_plugins_not_enabled(config: Config):
|
||||
# Test that the function returns the correct number of plugins
|
||||
plugins_config = config.plugins_config
|
||||
plugins_config.plugins["auto_gpt_guanaco"] = PluginConfig(
|
||||
name="auto_gpt_guanaco", enabled=True
|
||||
)
|
||||
plugins_config.plugins["auto_gpt_vicuna"] = PluginConfig(
|
||||
name="auto_gptp_vicuna", enabled=False
|
||||
)
|
||||
result = scan_plugins(config, debug=True)
|
||||
plugin_class_names = [plugin.__class__.__name__ for plugin in result]
|
||||
|
||||
assert len(result) == 1
|
||||
assert "AutoGPTGuanaco" in plugin_class_names
|
||||
assert "AutoGPTPVicuna" not in plugin_class_names
|
||||
|
||||
|
||||
def test_inspect_zip_for_modules():
|
||||
@@ -12,62 +63,49 @@ def test_inspect_zip_for_modules():
|
||||
assert result == [PLUGIN_TEST_INIT_PY]
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def mock_config_denylist_allowlist_check():
|
||||
class MockConfig:
|
||||
"""Mock config object for testing the denylist_allowlist_check function"""
|
||||
def test_create_base_config(config: Config):
|
||||
"""Test the backwards-compatibility shim to convert old plugin allow/deny list to a config file"""
|
||||
config.plugins_allowlist = ["a", "b"]
|
||||
config.plugins_denylist = ["c", "d"]
|
||||
|
||||
plugins_denylist = ["BadPlugin"]
|
||||
plugins_allowlist = ["GoodPlugin"]
|
||||
authorise_key = "y"
|
||||
exit_key = "n"
|
||||
os.remove(config.plugins_config_file)
|
||||
plugins_config = config.load_plugins_config()
|
||||
|
||||
return MockConfig()
|
||||
# Check the structure of the plugins config data
|
||||
assert len(plugins_config.plugins) == 4
|
||||
assert plugins_config.get("a").enabled
|
||||
assert plugins_config.get("b").enabled
|
||||
assert not plugins_config.get("c").enabled
|
||||
assert not plugins_config.get("d").enabled
|
||||
|
||||
# Check the saved config file
|
||||
with open(config.plugins_config_file, "r") as saved_config_file:
|
||||
saved_config = yaml.load(saved_config_file, Loader=yaml.FullLoader)
|
||||
|
||||
assert saved_config == {
|
||||
"a": {"enabled": True, "config": {}},
|
||||
"b": {"enabled": True, "config": {}},
|
||||
"c": {"enabled": False, "config": {}},
|
||||
"d": {"enabled": False, "config": {}},
|
||||
}
|
||||
|
||||
|
||||
def test_denylist_allowlist_check_denylist(
|
||||
mock_config_denylist_allowlist_check, monkeypatch
|
||||
):
|
||||
# Test that the function returns False when the plugin is in the denylist
|
||||
monkeypatch.setattr("builtins.input", lambda _: "y")
|
||||
assert not denylist_allowlist_check(
|
||||
"BadPlugin", mock_config_denylist_allowlist_check
|
||||
)
|
||||
def test_load_config(config: Config):
|
||||
"""Test that the plugin config is loaded correctly from the plugins_config.yaml file"""
|
||||
# Create a test config and write it to disk
|
||||
test_config = {
|
||||
"a": {"enabled": True, "config": {"api_key": "1234"}},
|
||||
"b": {"enabled": False, "config": {}},
|
||||
}
|
||||
with open(config.plugins_config_file, "w+") as f:
|
||||
f.write(yaml.dump(test_config))
|
||||
|
||||
# Load the config from disk
|
||||
plugins_config = config.load_plugins_config()
|
||||
|
||||
def test_denylist_allowlist_check_allowlist(
|
||||
mock_config_denylist_allowlist_check, monkeypatch
|
||||
):
|
||||
# Test that the function returns True when the plugin is in the allowlist
|
||||
monkeypatch.setattr("builtins.input", lambda _: "y")
|
||||
assert denylist_allowlist_check("GoodPlugin", mock_config_denylist_allowlist_check)
|
||||
|
||||
|
||||
def test_denylist_allowlist_check_user_input_yes(
|
||||
mock_config_denylist_allowlist_check, monkeypatch
|
||||
):
|
||||
# Test that the function returns True when the user inputs "y"
|
||||
monkeypatch.setattr("builtins.input", lambda _: "y")
|
||||
assert denylist_allowlist_check(
|
||||
"UnknownPlugin", mock_config_denylist_allowlist_check
|
||||
)
|
||||
|
||||
|
||||
def test_denylist_allowlist_check_user_input_no(
|
||||
mock_config_denylist_allowlist_check, monkeypatch
|
||||
):
|
||||
# Test that the function returns False when the user inputs "n"
|
||||
monkeypatch.setattr("builtins.input", lambda _: "n")
|
||||
assert not denylist_allowlist_check(
|
||||
"UnknownPlugin", mock_config_denylist_allowlist_check
|
||||
)
|
||||
|
||||
|
||||
def test_denylist_allowlist_check_user_input_invalid(
|
||||
mock_config_denylist_allowlist_check, monkeypatch
|
||||
):
|
||||
# Test that the function returns False when the user inputs an invalid value
|
||||
monkeypatch.setattr("builtins.input", lambda _: "invalid")
|
||||
assert not denylist_allowlist_check(
|
||||
"UnknownPlugin", mock_config_denylist_allowlist_check
|
||||
)
|
||||
# Check that the loaded config is equal to the test config
|
||||
assert len(plugins_config.plugins) == 2
|
||||
assert plugins_config.get("a").enabled
|
||||
assert plugins_config.get("a").config == {"api_key": "1234"}
|
||||
assert not plugins_config.get("b").enabled
|
||||
assert plugins_config.get("b").config == {}
|
||||
|
||||
Reference in New Issue
Block a user