aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorCédric Ollivier <cedric.ollivier@orange.com>2017-01-04 16:32:08 +0100
committerCédric Ollivier <cedric.ollivier@orange.com>2017-01-04 16:35:22 +0100
commitb30ad39874519322b5b866d2a5fe6a43c6efd6a4 (patch)
treee935a171c229e4bf1dbe7a62d40a12a9a6a75f05
parenta7af7049fa6ee0b5d4dadb026c31e3587d8f3b4c (diff)
Create ODLParser to parse sys.argv
It helps covering lines about args parsing without calling main. It also excludes 'if __name__ == .__main__.:' from coverage. The related unit tests are added too. Change-Id: I8e001a9d35081000977e0e546ae505f7cf2870e6 Signed-off-by: Cédric Ollivier <cedric.ollivier@orange.com>
-rw-r--r--.coveragerc3
-rwxr-xr-xfunctest/opnfv_tests/sdn/odl/odl.py80
-rw-r--r--functest/tests/unit/odl/test_odl.py82
3 files changed, 129 insertions, 36 deletions
diff --git a/.coveragerc b/.coveragerc
new file mode 100644
index 00000000..fe258c6c
--- /dev/null
+++ b/.coveragerc
@@ -0,0 +1,3 @@
+[report]
+exclude_lines =
+ if __name__ == .__main__.:
diff --git a/functest/opnfv_tests/sdn/odl/odl.py b/functest/opnfv_tests/sdn/odl/odl.py
index 0905e55c..80827e42 100755
--- a/functest/opnfv_tests/sdn/odl/odl.py
+++ b/functest/opnfv_tests/sdn/odl/odl.py
@@ -180,44 +180,52 @@ class ODLTests(testcase_base.TestcaseBase):
return self.main(**kwargs)
+class ODLParser():
+
+ def __init__(self):
+ self.parser = argparse.ArgumentParser()
+ self.parser.add_argument(
+ '-k', '--keystoneip', help='Keystone IP',
+ default='127.0.0.1')
+ self.parser.add_argument(
+ '-n', '--neutronip', help='Neutron IP',
+ default='127.0.0.1')
+ self.parser.add_argument(
+ '-a', '--osusername', help='Username for OpenStack',
+ default='admin')
+ self.parser.add_argument(
+ '-b', '--ostenantname', help='Tenantname for OpenStack',
+ default='admin')
+ self.parser.add_argument(
+ '-c', '--ospassword', help='Password for OpenStack',
+ default='admin')
+ self.parser.add_argument(
+ '-o', '--odlip', help='OpenDaylight IP',
+ default='127.0.0.1')
+ self.parser.add_argument(
+ '-w', '--odlwebport', help='OpenDaylight Web Portal Port',
+ default='8080')
+ self.parser.add_argument(
+ '-r', '--odlrestconfport', help='OpenDaylight RESTConf Port',
+ default='8181')
+ self.parser.add_argument(
+ '-d', '--odlusername', help='Username for ODL',
+ default='admin')
+ self.parser.add_argument(
+ '-e', '--odlpassword', help='Password for ODL',
+ default='admin')
+ self.parser.add_argument(
+ '-p', '--pushtodb', help='Push results to DB',
+ action='store_true')
+
+ def parse_args(self, argv=[]):
+ return vars(self.parser.parse_args(argv))
+
+
if __name__ == '__main__':
- parser = argparse.ArgumentParser()
- parser.add_argument('-k', '--keystoneip',
- help='Keystone IP',
- default='127.0.0.1')
- parser.add_argument('-n', '--neutronip',
- help='Neutron IP',
- default='127.0.0.1')
- parser.add_argument('-a', '--osusername',
- help='Username for OpenStack',
- default='admin')
- parser.add_argument('-b', '--ostenantname',
- help='Tenantname for OpenStack',
- default='admin')
- parser.add_argument('-c', '--ospassword',
- help='Password for OpenStack',
- default='admin')
- parser.add_argument('-o', '--odlip',
- help='OpenDaylight IP',
- default='127.0.0.1')
- parser.add_argument('-w', '--odlwebport',
- help='OpenDaylight Web Portal Port',
- default='8080')
- parser.add_argument('-r', '--odlrestconfport',
- help='OpenDaylight RESTConf Port',
- default='8181')
- parser.add_argument('-d', '--odlusername',
- help='Username for ODL',
- default='admin')
- parser.add_argument('-e', '--odlpassword',
- help='Password for ODL',
- default='admin')
- parser.add_argument('-p', '--pushtodb',
- help='Push results to DB',
- action='store_true')
-
- args = vars(parser.parse_args())
odl = ODLTests()
+ parser = ODLParser()
+ args = parser.parse_args(sys.argv[1:])
try:
result = odl.main(**args)
if result != testcase_base.TestcaseBase.EX_OK:
diff --git a/functest/tests/unit/odl/test_odl.py b/functest/tests/unit/odl/test_odl.py
index 8dcc9bd9..3878337b 100644
--- a/functest/tests/unit/odl/test_odl.py
+++ b/functest/tests/unit/odl/test_odl.py
@@ -46,6 +46,17 @@ class ODLTesting(unittest.TestCase):
os.environ["OS_PASSWORD"] = self._os_password
os.environ["OS_TENANT_NAME"] = self._os_tenantname
self.test = odl.ODLTests()
+ self.defaultargs = {'odlusername': self._odl_username,
+ 'odlpassword': self._odl_password,
+ 'keystoneip': self._keystone_ip,
+ 'neutronip': self._keystone_ip,
+ 'osusername': self._os_username,
+ 'ostenantname': self._os_tenantname,
+ 'ospassword': self._os_password,
+ 'odlip': self._keystone_ip,
+ 'odlwebport': self._odl_webport,
+ 'odlrestconfport': self._odl_restconfport,
+ 'pushtodb': False}
def test_empty_visitor(self):
visitor = odl.ODLResultVisitor()
@@ -397,6 +408,77 @@ class ODLTesting(unittest.TestCase):
self._test_run(testcase_base.TestcaseBase.EX_OK,
odlip=self._neutron_ip, odlwebport='8181')
+ def test_argparser_default(self):
+ parser = odl.ODLParser()
+ self.assertEqual(parser.parse_args(), self.defaultargs)
+
+ def test_argparser_basic(self):
+ self.defaultargs['neutronip'] = self._neutron_ip
+ self.defaultargs['odlip'] = self._sdn_controller_ip
+ parser = odl.ODLParser()
+ self.assertEqual(parser.parse_args(
+ ["--neutronip={}".format(self._neutron_ip),
+ "--odlip={}".format(self._sdn_controller_ip)
+ ]), self.defaultargs)
+
+ @mock.patch('sys.stderr', new_callable=StringIO.StringIO)
+ def test_argparser_fail(self, *args):
+ self.defaultargs['foo'] = 'bar'
+ parser = odl.ODLParser()
+ with self.assertRaises(SystemExit):
+ parser.parse_args(["--foo=bar"])
+
+ def _test_argparser(self, arg, value):
+ self.defaultargs[arg] = value
+ parser = odl.ODLParser()
+ self.assertEqual(parser.parse_args(["--{}={}".format(arg, value)]),
+ self.defaultargs)
+
+ def test_argparser_odlusername(self):
+ self._test_argparser('odlusername', 'foo')
+
+ def test_argparser_odlpassword(self):
+ self._test_argparser('odlpassword', 'foo')
+
+ def test_argparser_keystoneip(self):
+ self._test_argparser('keystoneip', '127.0.0.4')
+
+ def test_argparser_neutronip(self):
+ self._test_argparser('neutronip', '127.0.0.4')
+
+ def test_argparser_osusername(self):
+ self._test_argparser('osusername', 'foo')
+
+ def test_argparser_ostenantname(self):
+ self._test_argparser('ostenantname', 'foo')
+
+ def test_argparser_ospassword(self):
+ self._test_argparser('ospassword', 'foo')
+
+ def test_argparser_odlip(self):
+ self._test_argparser('odlip', '127.0.0.4')
+
+ def test_argparser_odlwebport(self):
+ self._test_argparser('odlwebport', '80')
+
+ def test_argparser_odlrestconfport(self):
+ self._test_argparser('odlrestconfport', '80')
+
+ def test_argparser_pushtodb(self):
+ self.defaultargs['pushtodb'] = True
+ parser = odl.ODLParser()
+ self.assertEqual(parser.parse_args(["--{}".format('pushtodb')]),
+ self.defaultargs)
+
+ def test_argparser_multiple_args(self):
+ self.defaultargs['neutronip'] = self._neutron_ip
+ self.defaultargs['odlip'] = self._sdn_controller_ip
+ parser = odl.ODLParser()
+ self.assertEqual(parser.parse_args(
+ ["--neutronip={}".format(self._neutron_ip),
+ "--odlip={}".format(self._sdn_controller_ip)
+ ]), self.defaultargs)
+
if __name__ == "__main__":
unittest.main(verbosity=2)