Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #214 from wwade/plugin-prio
plugins: allow plugins to provide a relative priority value
- Loading branch information
Showing
2 changed files
with
171 additions
and
20 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
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,106 @@ | ||
import logging | ||
from typing import Tuple | ||
from unittest import mock | ||
|
||
import pytest | ||
|
||
from jobrunner.plugins import Plugins | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
class Plugin: | ||
@classmethod | ||
def load(cls): | ||
return cls | ||
|
||
|
||
class PluginAAANoPrio(Plugin): | ||
@staticmethod | ||
def workspaceProject() -> Tuple[str, bool]: | ||
return "lowest", True | ||
|
||
@staticmethod | ||
def getResources(jobs): | ||
_ = jobs | ||
return "[no-prio]" | ||
|
||
|
||
class PluginBBBLowPrioResources(Plugin): | ||
@staticmethod | ||
def workspaceProject() -> Tuple[str, bool]: | ||
return "lowest", True | ||
|
||
@staticmethod | ||
def getResources(jobs): | ||
_ = jobs | ||
return "[low-prio]" | ||
|
||
|
||
class PluginZABHighPrioNoProject(Plugin): | ||
@staticmethod | ||
def priority(): | ||
return {"": 0} | ||
|
||
@staticmethod | ||
def workspaceProject() -> Tuple[str, bool]: | ||
raise NotImplementedError | ||
|
||
|
||
class PluginZAAHighestPrio(Plugin): | ||
@staticmethod | ||
def priority(): | ||
return {"": 0} | ||
|
||
@staticmethod | ||
def workspaceProject() -> Tuple[str, bool]: | ||
return "highest", True | ||
|
||
|
||
class PluginMMMLowPrio(Plugin): | ||
@staticmethod | ||
def priority(): | ||
return {"": 1000} | ||
|
||
@staticmethod | ||
def workspaceProject() -> Tuple[str, bool]: | ||
return "low", True | ||
|
||
|
||
@pytest.mark.parametrize( | ||
["plugins", "workspaceProject", "resources"], | ||
[ | ||
( | ||
{PluginAAANoPrio, PluginMMMLowPrio, PluginZAAHighestPrio}, | ||
"highest", | ||
"[no-prio]", | ||
), | ||
( | ||
{PluginAAANoPrio, PluginMMMLowPrio, PluginZABHighPrioNoProject}, | ||
"low", | ||
"[no-prio]", | ||
), | ||
( | ||
{PluginAAANoPrio, PluginBBBLowPrioResources, PluginMMMLowPrio}, | ||
"low", | ||
"[no-prio][low-prio]", | ||
), | ||
] | ||
) | ||
def testPluginPriorities(plugins, workspaceProject, resources): | ||
with mock.patch("jobrunner.plugins.get_plugins") as gp, \ | ||
mock.patch("importlib.import_module") as im, \ | ||
mock.patch("socket.gethostname", return_value="xxx"): | ||
im.return_value = [] | ||
gp.return_value = plugins | ||
|
||
p = Plugins() | ||
|
||
logger.info("only PluginNoPrio implements getResources()") | ||
assert resources == p.getResources(None) | ||
|
||
logger.info("no plugins implement workspaceIdentity()") | ||
assert "xxx" == p.workspaceIdentity() | ||
|
||
logger.info("all plugins implement workspaceProject()") | ||
assert (workspaceProject, True) == p.workspaceProject() |