Skip to content

Commit

Permalink
Updated command line utility script and examples
Browse files Browse the repository at this point in the history
Required by changes to implement #32
Closes #32
  • Loading branch information
nemesifier committed Dec 10, 2015
1 parent 0778fd5 commit badf292
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 13 deletions.
7 changes: 3 additions & 4 deletions bin/netjsonconfig
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
30 changes: 27 additions & 3 deletions docs/source/general/commandline_utility.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 6 additions & 6 deletions tests/test_bin.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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:
Expand Down

0 comments on commit badf292

Please sign in to comment.