From badf292d90448696a444d3432f6ece4aa96668b4 Mon Sep 17 00:00:00 2001 From: Federico Capoano Date: Thu, 10 Dec 2015 22:53:04 +0100 Subject: [PATCH] Updated command line utility script and examples Required by changes to implement #32 Closes #32 --- bin/netjsonconfig | 7 +++-- docs/source/general/commandline_utility.rst | 30 ++++++++++++++++++--- tests/test_bin.py | 12 ++++----- 3 files changed, 36 insertions(+), 13 deletions(-) diff --git a/bin/netjsonconfig b/bin/netjsonconfig index a32e69700..3cd0f99b2 100644 --- a/bin/netjsonconfig +++ b/bin/netjsonconfig @@ -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', diff --git a/docs/source/general/commandline_utility.rst b/docs/source/general/commandline_utility.rst index 7e69cab2e..8490301ed 100644 --- a/docs/source/general/commandline_utility.rst +++ b/docs/source/general/commandline_utility.rst @@ -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 diff --git a/tests/test_bin.py b/tests/test_bin.py index 4f218bd34..77848b8c6 100644 --- a/tests/test_bin.py +++ b/tests/test_bin.py @@ -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,7 +59,7 @@ 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) @@ -67,7 +67,7 @@ def test_templates(self): 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: