Skip to content

Commit

Permalink
[bin] added validate to method options
Browse files Browse the repository at this point in the history
This method provides a way to check the validity of the NetJSON input
from the command line and can print the ValidationError to the user,
along with a detailed explanation of which part is faulty
  • Loading branch information
edoput committed May 17, 2017
1 parent 9e58e6b commit e5b91a1
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 6 deletions.
5 changes: 3 additions & 2 deletions bin/netjsonconfig
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,13 @@ output.add_argument('--backend', '-b',

output.add_argument('--method', '-m',
required=True,
choices=['render', 'generate', 'write'],
choices=['render', 'generate', 'write', 'validate'],
action='store',
help='Backend method to use. '
'"render" returns the configuration in text format'
'"generate" returns a tar.gz archive as output; '
'"write" is like generate but writes to disk; ')
'"write" is like generate but writes to disk; '
'"validate" only validate the resulting NetJSON;')

output.add_argument('--args', '-a',
nargs='*', # zero or more
Expand Down
14 changes: 11 additions & 3 deletions docs/source/general/commandline_utility.rst
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ Check out the available options yourself with::
$ netjsonconfig --help
usage: netjsonconfig [-h] --config CONFIG
[--templates [TEMPLATES [TEMPLATES ...]]] --backend
{openwrt,openwisp} --method {render,generate,write}
{openwrt,openwisp} --method {render,generate,write,validate}
[--args [ARGS [ARGS ...]]] [--verbose] [--version]

Converts a NetJSON DeviceConfiguration object to native router configurations.
Expand All @@ -38,11 +38,12 @@ Check out the available options yourself with::
output:
--backend {openwrt,openwisp}, -b {openwrt,openwisp}
Configuration backend: openwrt or openwisp
--method {render,generate,write}, -m {render,generate,write}
--method {render,generate,write}, -m {render,generate,write, validate}
Backend method to use. "render" returns the
configuration in text format"generate" returns a
tar.gz archive as output; "write" is like generate but
writes to disk;
writes to disk, "validate" validates the resulting NetJSON
against the backend schema;
--args [ARGS [ARGS ...]], -a [ARGS [ARGS ...]]
Optional arguments that can be passed to methods

Expand All @@ -65,6 +66,9 @@ Here's the common use cases explained::
# same as previous but exclude additional files
netjsonconfig --config config.json --backend openwrt --method render --args files=0

# validate the config.json file against the openwrt backend
netjsonconfig --config config.json --backend openwrt --method validate

# abbreviated options
netjsonconfig -c config.json -b openwrt -m render -a files=0

Expand All @@ -75,6 +79,10 @@ Using templates::

netjsonconfig -c config.json -t template1.json template2.json -b openwrt -m render

# validate the result of merging config.json, template1.json and template2.json
# against the openwrt backend
netjsonconfig -c config.json -t template1.json template2.json -b openwrt -m validate

Environment variables
---------------------

Expand Down
4 changes: 3 additions & 1 deletion netjsonconfig/exceptions.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
class NetJsonConfigException(Exception):
""" root netjsonconfig exception """
pass

def __str__(self):
return "%s %s %s" % (self.__class__.__name__, self.message, self.details)


class ValidationError(NetJsonConfigException):
Expand Down
10 changes: 10 additions & 0 deletions tests/test_bin.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,16 @@ def test_invalid_netjson_verbose(self):
with self.assertRaises(subprocess.CalledProcessError):
subprocess.check_output(command, shell=True)

def test_check_invalid_netjson(self):
command = '''netjsonconfig -c '{ "interfaces":["w"] }' -b openwrt -m validate'''
with self.assertRaises(subprocess.CalledProcessError):
subprocess.check_output(command, shell=True)

def test_check_invalid_netjson_verbose(self):
command = '''netjsonconfig -c '{ "interfaces":["w"] }' -b openwrt -m validate --verbose'''
with self.assertRaises(subprocess.CalledProcessError):
subprocess.check_output(command, shell=True)

def test_empty_netjson(self):
output = subprocess.check_output("netjsonconfig -c '{}' -b openwrt -m render", shell=True)
self.assertEqual(output.decode(), '')
Expand Down

0 comments on commit e5b91a1

Please sign in to comment.