-
-
Notifications
You must be signed in to change notification settings - Fork 72
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[WIP][backend] Added support for plugins. #120
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,6 +21,7 @@ class BaseBackend(object): | |
schema = None | ||
FILE_SECTION_DELIMITER = '# ---------- files ---------- #' | ||
list_identifiers = [] | ||
plugins = [] | ||
|
||
def __init__(self, config=None, native=None, templates=None, context=None): | ||
""" | ||
|
@@ -49,6 +50,17 @@ def __init__(self, config=None, native=None, templates=None, context=None): | |
raise ValueError('Expecting either config or native argument to be ' | ||
'passed during the initialization of the backend') | ||
|
||
@classmethod | ||
def add_plugins(cls, plugins): | ||
for plugin in plugins: | ||
plugin.schema.pop('$schema', None) | ||
if getattr(cls, 'schema'): | ||
cls.schema['properties'][plugin.key] = plugin.schema | ||
if getattr(cls, 'converters'): | ||
cls.converters.append(plugin.converter) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think it's the same here, what's the use case of not having any converter declared? Wouldn't it just be wrong? |
||
else: | ||
cls.converters = [plugin.converter] | ||
|
||
def _load(self, config): | ||
""" | ||
Loads config from string or dict | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,6 +8,34 @@ | |
from netjsonconfig import OpenWrt | ||
from netjsonconfig.exceptions import ValidationError | ||
from netjsonconfig.utils import _TabsMixin | ||
from netjsonconfig.backends.openwrt.converters.base import OpenWrtConverter | ||
|
||
|
||
class CoovaConverter(OpenWrtConverter): | ||
netjson_key = 'chilli' | ||
_uci_types = ['chilli'] | ||
|
||
def to_intermediate_loop(self, block, result, index=None): | ||
if block: | ||
block.update({ | ||
'.type': 'chilli', | ||
'.name': 'chilli' | ||
}) | ||
result['chilli'] = [self.sorted_dict(block)] | ||
return result | ||
|
||
|
||
class CoovaPlugin(object): | ||
key = 'chilli' | ||
converter = CoovaConverter | ||
schema = { | ||
"$schema": "http://json-schema.org/draft-04/schema#", | ||
"type": "object", | ||
"properties": { | ||
"disabled": {"type": "integer"}, | ||
"uamsecret": {"type": "string"}, | ||
}, | ||
} | ||
|
||
|
||
class TestBackend(unittest.TestCase, _TabsMixin): | ||
|
@@ -19,6 +47,37 @@ def test_config_copy(self): | |
o.validate() | ||
self.assertDictEqual(config, {'interfaces': []}) | ||
|
||
def test_plugin(self): | ||
config = { | ||
"chilli": { | ||
'disabled': 1, | ||
'uamsecret': 'asd123fghj' | ||
}, | ||
} | ||
OpenWrt.add_plugins([CoovaPlugin()]) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. keep in mind that once you do this, the plugin will be enabled during all the tests that are called afterwards (even other test classes), this may have unintended consequences in the future, I think you should do two things:
|
||
o = OpenWrt(config) | ||
self.assertEqual(self._tabs('''package chilli | ||
|
||
config chilli 'chilli' | ||
option disabled '1' | ||
option uamsecret 'asd123fghj' | ||
'''), o.render()) | ||
|
||
def test_bad_plugin_config(self): | ||
config = { | ||
"chilli": { | ||
'disabled': 'wrong_value', | ||
'uamsecret': 'asd123fghj' | ||
}, | ||
} | ||
OpenWrt.add_plugins([CoovaPlugin()]) | ||
o = OpenWrt(config) | ||
try: | ||
o.render() | ||
self.fail() | ||
except ValidationError as e: | ||
self.assertEqual(e.message, "'wrong_value' is not of type 'integer'") | ||
|
||
def test_json_method(self): | ||
config = { | ||
"type": "DeviceConfiguration", | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
as I was saying, we expect backend to always have a
schema
, therefore ifschema
is not present we should "fail early and loud"