-
Notifications
You must be signed in to change notification settings - Fork 343
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Jeffrey Martin <[email protected]>
- Loading branch information
1 parent
c6ab97f
commit 34397b2
Showing
1 changed file
with
67 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import pytest | ||
import os | ||
import tempfile | ||
|
||
from garak._plugins import PluginCache | ||
|
||
|
||
@pytest.fixture | ||
def temp_cache_location(request) -> None: | ||
# override the cache file with a tmp location | ||
with tempfile.NamedTemporaryFile(buffering=0, delete=False) as tmp: | ||
PluginCache._user_plugin_cache_file = tmp.name | ||
PluginCache._plugin_cache_file = tmp.name | ||
os.remove(tmp.name) | ||
# reset the class level singleton | ||
PluginCache._plugin_cache_dict = None | ||
|
||
def remove_cache_file(): | ||
if os.path.exists(tmp.name): | ||
os.remove(tmp.name) | ||
|
||
request.addfinalizer(remove_cache_file) | ||
|
||
return tmp.name | ||
|
||
|
||
def test_create(temp_cache_location): | ||
cache = PluginCache.instance() | ||
assert os.path.isfile(temp_cache_location) | ||
assert isinstance(cache, dict) | ||
|
||
|
||
def test_existing(): | ||
info = PluginCache.plugin_info("probes.test.Test") | ||
assert isinstance(info, dict) | ||
|
||
|
||
def test_missing_from_cache(): | ||
cache = PluginCache.instance()["probes"] | ||
del cache["probes.test.Test"] | ||
assert cache.get("probes.test.Test") is None | ||
info = PluginCache.plugin_info("probes.test.Test") | ||
assert isinstance(info, dict) | ||
|
||
|
||
def test_unknown_type(): | ||
with pytest.raises(ValueError) as exc_info: | ||
info = PluginCache.plugin_info("fake.test.missing") | ||
assert "plugin type" in str(exc_info.value) | ||
|
||
|
||
def test_unknown_class(): | ||
with pytest.raises(ValueError) as exc_info: | ||
info = PluginCache.plugin_info("probes.test.missing") | ||
assert "plugin from " in str(exc_info.value) | ||
|
||
|
||
def test_unknown_module(): | ||
with pytest.raises(ValueError) as exc_info: | ||
info = PluginCache.plugin_info("probes.invalid.missing") | ||
assert "plugin module" in str(exc_info.value) | ||
|
||
|
||
def test_unknown_module(): | ||
with pytest.raises(ValueError) as exc_info: | ||
info = PluginCache.plugin_info("probes.invalid.format.length") | ||
assert "plugin class" in str(exc_info.value) |