Permalink
Browse files

Updated command line utility script and examples

Required by changes to implement #32
Closes #32
  • Loading branch information...
nemesisdesign committed Dec 10, 2015
1 parent 0778fd5 commit badf292d90448696a444d3432f6ece4aa96668b4
Showing with 36 additions and 13 deletions.
  1. +3 −4 bin/netjsonconfig
  2. +27 −3 docs/source/general/commandline_utility.rst
  3. +6 −6 tests/test_bin.py
@@ -40,16 +40,15 @@ parser.add_argument('--templates', '-t',
parser.add_argument('--backend', '-b',
choices=['openwrt', 'openwisp'],
action='store',
default='openwrt',
type=str,
help='Configuration backend: openwrt or openwisp')
parser.add_argument('--method', '-m',
choices=['generate', 'render'],
action='store',
default='generate',
help='Backend method to use. ("generate" creates a tar.gz, "render"'
'returns the entire config as a string)')
help='Backend method to use. '\
'"generate" returns a tar.gz archive as output; '
'"render" returns the configuration in text format')
parser.add_argument('--verbose',
action='store_true',
@@ -8,12 +8,36 @@ languages (via system calls).
Check out the available options yourself with::
netjsonconfig --help
$ netjsonconfig --help
usage: netjsonconfig [-h] [--templates [TEMPLATES [TEMPLATES ...]]]
[--backend {openwrt,openwisp}]
[--method {generate,render}] [--verbose] [--version]
config
Converts a NetJSON DeviceConfiguration objectto working router configurations.
positional arguments:
config config file or string, must be valid NetJSON
DeviceConfiguration
optional arguments:
-h, --help show this help message and exit
--templates [TEMPLATES [TEMPLATES ...]], -t [TEMPLATES [TEMPLATES ...]]
list of template config files or strings separated by
space
--backend {openwrt,openwisp}, -b {openwrt,openwisp}
Configuration backend: openwrt or openwisp
--method {generate,render}, -m {generate,render}
Backend method to use. "generate" returns a tar.gz
archive as output; "render" returns the configuration
in text format
--verbose verbose output
--version, -v show program's version number and exit
Here's the common use cases explained::
# generate tar.gz from a NetJSON DeviceConfiguration object
netjsonconfig --backend openwrt config.json
# generate tar.gz from a NetJSON DeviceConfiguration object and save it to a file
netjsonconfig --backend openwrt --method generate config.json > config.tar.gz
# see output of OpenWrt render method
netjsonconfig --backend openwrt --method render config.json
@@ -11,20 +11,20 @@ class TestBin(unittest.TestCase, _TabsMixin):
"""
def test_file_not_found(self):
with self.assertRaises(subprocess.CalledProcessError):
output = subprocess.check_output("netjsonconfig WRONG", shell=True)
output = subprocess.check_output("netjsonconfig WRONG -b openwrt -m generate", shell=True)
def test_invalid_netjson(self):
command = '''netjsonconfig '{ "interfaces":["w"] }' -m render'''
command = '''netjsonconfig '{ "interfaces":["w"] }' -b openwrt -m render'''
with self.assertRaises(subprocess.CalledProcessError):
output = subprocess.check_output(command, shell=True)
def test_invalid_netjson_verbose(self):
command = '''netjsonconfig '{ "interfaces":["w"] }' -m render --verbose'''
command = '''netjsonconfig '{ "interfaces":["w"] }' -b openwrt -m render --verbose'''
with self.assertRaises(subprocess.CalledProcessError):
output = subprocess.check_output(command, shell=True)
def test_empty_netjson(self):
output = subprocess.check_output("netjsonconfig '{}' -m render", shell=True)
output = subprocess.check_output("netjsonconfig '{}' -b openwrt -m render", shell=True)
self.assertEqual(output.decode(), '')
def test_templates(self):
@@ -59,15 +59,15 @@ def test_templates(self):
}
]
})
command = """netjsonconfig '{0}' -m render --templates '{1}' '{2}'"""
command = """netjsonconfig '{0}' -b openwrt -m render --templates '{1}' '{2}'"""
command = command.format(config, template1, template2)
output = subprocess.check_output(command, shell=True).decode()
self.assertIn("hostname 'template_test'", output)
self.assertIn("interface 'eth0'", output)
self.assertIn("interface 'wlan0'", output)
def test_invalid_template(self):
command = "netjsonconfig '{}' -t WRONG -m render"
command = "netjsonconfig '{}' -b openwrt -t WRONG -m render"
try:
output = subprocess.check_output(command, shell=True)
except subprocess.CalledProcessError as e:

0 comments on commit badf292

Please sign in to comment.