Merge "Update ODL testcase to OpenStack Shade"
[functest.git] / functest / tests / unit / odl / test_odl.py
1 #!/usr/bin/env python
2
3 # Copyright (c) 2016 Orange and others.
4 #
5 # All rights reserved. This program and the accompanying materials
6 # are made available under the terms of the Apache License, Version 2.0
7 # which accompanies this distribution, and is available at
8 # http://www.apache.org/licenses/LICENSE-2.0
9
10 """Define the classes required to fully cover odl."""
11
12 import logging
13 import os
14 import unittest
15
16 import mock
17 import munch
18 from robot.errors import RobotError
19 import six
20 from six.moves import urllib
21 from xtesting.core import testcase
22
23 from functest.opnfv_tests.sdn.odl import odl
24
25 __author__ = "Cedric Ollivier <cedric.ollivier@orange.com>"
26
27
28 class ODLTesting(unittest.TestCase):
29
30     """The super class which testing classes could inherit."""
31     # pylint: disable=missing-docstring
32
33     logging.disable(logging.CRITICAL)
34
35     _keystone_ip = "127.0.0.1"
36     _neutron_url = u"https://127.0.0.1:9696"
37     _neutron_id = u"dummy"
38     _sdn_controller_ip = "127.0.0.3"
39     _os_auth_url = "http://{}:5000/v3".format(_keystone_ip)
40     _os_projectname = "admin"
41     _os_username = "admin"
42     _os_password = "admin"
43     _odl_webport = "8080"
44     _odl_restconfport = "8181"
45     _odl_username = "admin"
46     _odl_password = "admin"
47     _os_userdomainname = 'Default'
48     _os_projectdomainname = 'Default'
49     _os_interface = "public"
50
51     def setUp(self):
52         for var in ("INSTALLER_TYPE", "SDN_CONTROLLER", "SDN_CONTROLLER_IP"):
53             if var in os.environ:
54                 del os.environ[var]
55         os.environ["OS_AUTH_URL"] = self._os_auth_url
56         os.environ["OS_USERNAME"] = self._os_username
57         os.environ["OS_USER_DOMAIN_NAME"] = self._os_userdomainname
58         os.environ["OS_PASSWORD"] = self._os_password
59         os.environ["OS_PROJECT_NAME"] = self._os_projectname
60         os.environ["OS_PROJECT_DOMAIN_NAME"] = self._os_projectdomainname
61         os.environ["OS_PASSWORD"] = self._os_password
62         os.environ["OS_INTERFACE"] = self._os_interface
63         self.test = odl.ODLTests(case_name='odl', project_name='functest')
64         self.defaultargs = {'odlusername': self._odl_username,
65                             'odlpassword': self._odl_password,
66                             'neutronurl': "http://{}:9696".format(
67                                 self._keystone_ip),
68                             'osauthurl': self._os_auth_url,
69                             'osusername': self._os_username,
70                             'osuserdomainname': self._os_userdomainname,
71                             'osprojectname': self._os_projectname,
72                             'osprojectdomainname': self._os_projectdomainname,
73                             'ospassword': self._os_password,
74                             'odlip': self._keystone_ip,
75                             'odlwebport': self._odl_webport,
76                             'odlrestconfport': self._odl_restconfport,
77                             'pushtodb': False}
78
79
80 class ODLRobotTesting(ODLTesting):
81
82     """The class testing ODLTests.set_robotframework_vars()."""
83     # pylint: disable=missing-docstring
84
85     @mock.patch('fileinput.input', side_effect=Exception())
86     def test_set_vars_ko(self, mock_method):
87         self.assertFalse(self.test.set_robotframework_vars())
88         mock_method.assert_called_once_with(
89             os.path.join(odl.ODLTests.odl_test_repo,
90                          'csit/variables/Variables.robot'), inplace=True)
91
92     @mock.patch('fileinput.input', return_value=[])
93     def test_set_vars_empty(self, mock_method):
94         self.assertTrue(self.test.set_robotframework_vars())
95         mock_method.assert_called_once_with(
96             os.path.join(odl.ODLTests.odl_test_repo,
97                          'csit/variables/Variables.robot'), inplace=True)
98
99     @mock.patch('sys.stdout', new_callable=six.StringIO)
100     def _test_set_vars(self, msg1, msg2, *args):
101         line = mock.MagicMock()
102         line.__iter__.return_value = [msg1]
103         with mock.patch('fileinput.input', return_value=line) as mock_method:
104             self.assertTrue(self.test.set_robotframework_vars())
105             mock_method.assert_called_once_with(
106                 os.path.join(odl.ODLTests.odl_test_repo,
107                              'csit/variables/Variables.robot'), inplace=True)
108             self.assertEqual(args[0].getvalue(), "{}\n".format(msg2))
109
110     def test_set_vars_auth_default(self):
111         self._test_set_vars(
112             "@{AUTH} ",
113             "@{AUTH}           admin    admin")
114
115     def test_set_vars_auth1(self):
116         self._test_set_vars(
117             "@{AUTH1}           foo    bar",
118             "@{AUTH1}           foo    bar")
119
120     @mock.patch('sys.stdout', new_callable=six.StringIO)
121     def test_set_vars_auth_foo(self, *args):
122         line = mock.MagicMock()
123         line.__iter__.return_value = ["@{AUTH} "]
124         with mock.patch('fileinput.input', return_value=line) as mock_method:
125             self.assertTrue(self.test.set_robotframework_vars('foo', 'bar'))
126             mock_method.assert_called_once_with(
127                 os.path.join(odl.ODLTests.odl_test_repo,
128                              'csit/variables/Variables.robot'), inplace=True)
129             self.assertEqual(
130                 args[0].getvalue(),
131                 "@{AUTH}           foo    bar\n")
132
133
134 class ODLMainTesting(ODLTesting):
135
136     """The class testing ODLTests.run_suites()."""
137     # pylint: disable=missing-docstring
138
139     def _get_run_suites_kwargs(self, key=None):
140         kwargs = {'odlusername': self._odl_username,
141                   'odlpassword': self._odl_password,
142                   'neutronurl': self._neutron_url,
143                   'osauthurl': self._os_auth_url,
144                   'osusername': self._os_username,
145                   'osuserdomainname': self._os_userdomainname,
146                   'osprojectname': self._os_projectname,
147                   'osprojectdomainname': self._os_projectdomainname,
148                   'ospassword': self._os_password,
149                   'odlip': self._sdn_controller_ip,
150                   'odlwebport': self._odl_webport,
151                   'odlrestconfport': self._odl_restconfport}
152         if key:
153             del kwargs[key]
154         return kwargs
155
156     def _test_run_suites(self, status, *args):
157         kwargs = self._get_run_suites_kwargs()
158         self.assertEqual(self.test.run_suites(**kwargs), status)
159         if args:
160             args[0].assert_called_once_with(self.test.odl_variables_file)
161         if len(args) > 1:
162             variable = [
163                 'KEYSTONEURL:{}://{}'.format(
164                     urllib.parse.urlparse(self._os_auth_url).scheme,
165                     urllib.parse.urlparse(self._os_auth_url).netloc),
166                 'NEUTRONURL:{}'.format(self._neutron_url),
167                 'OS_AUTH_URL:"{}"'.format(self._os_auth_url),
168                 'OSUSERNAME:"{}"'.format(self._os_username),
169                 'OSUSERDOMAINNAME:"{}"'.format(self._os_userdomainname),
170                 'OSTENANTNAME:"{}"'.format(self._os_projectname),
171                 'OSPROJECTDOMAINNAME:"{}"'.format(self._os_projectdomainname),
172                 'OSPASSWORD:"{}"'.format(self._os_password),
173                 'ODL_SYSTEM_IP:{}'.format(self._sdn_controller_ip),
174                 'PORT:{}'.format(self._odl_webport),
175                 'RESTCONFPORT:{}'.format(self._odl_restconfport)]
176             args[1].assert_called_once_with(
177                 odl.ODLTests.basic_suite_dir, odl.ODLTests.neutron_suite_dir,
178                 include=[],
179                 log='NONE',
180                 output=os.path.join(self.test.res_dir, 'output.xml'),
181                 report='NONE', stdout=mock.ANY, variable=variable,
182                 variablefile=[])
183
184     def _test_no_keyword(self, key):
185         kwargs = self._get_run_suites_kwargs(key)
186         self.assertEqual(self.test.run_suites(**kwargs),
187                          testcase.TestCase.EX_RUN_ERROR)
188
189     def test_no_odlusername(self):
190         self._test_no_keyword('odlusername')
191
192     def test_no_odlpassword(self):
193         self._test_no_keyword('odlpassword')
194
195     def test_no_neutronurl(self):
196         self._test_no_keyword('neutronurl')
197
198     def test_no_osauthurl(self):
199         self._test_no_keyword('osauthurl')
200
201     def test_no_osusername(self):
202         self._test_no_keyword('osusername')
203
204     def test_no_osprojectname(self):
205         self._test_no_keyword('osprojectname')
206
207     def test_no_ospassword(self):
208         self._test_no_keyword('ospassword')
209
210     def test_no_odlip(self):
211         self._test_no_keyword('odlip')
212
213     def test_no_odlwebport(self):
214         self._test_no_keyword('odlwebport')
215
216     def test_no_odlrestconfport(self):
217         self._test_no_keyword('odlrestconfport')
218
219     @mock.patch('os.path.isfile', return_value=True)
220     def test_set_vars_ko(self, *args):
221         with mock.patch.object(self.test, 'set_robotframework_vars',
222                                return_value=False) as mock_object:
223             self._test_run_suites(testcase.TestCase.EX_RUN_ERROR)
224             mock_object.assert_called_once_with(
225                 self._odl_username, self._odl_password)
226         args[0].assert_called_once_with(self.test.odl_variables_file)
227
228     @mock.patch('robot.run', side_effect=RobotError)
229     @mock.patch('os.path.isfile', return_value=True)
230     def test_run_ko(self, *args):
231         with mock.patch.object(self.test, 'set_robotframework_vars',
232                                return_value=True), \
233                 self.assertRaises(RobotError):
234             self._test_run_suites(testcase.TestCase.EX_RUN_ERROR, *args)
235
236     @mock.patch('robot.run')
237     @mock.patch('os.path.isfile', return_value=True)
238     def test_parse_results_ko(self, *args):
239         with mock.patch.object(self.test, 'set_robotframework_vars',
240                                return_value=True), \
241                 mock.patch.object(self.test, 'parse_results',
242                                   side_effect=RobotError):
243             self._test_run_suites(testcase.TestCase.EX_RUN_ERROR, *args)
244
245     @mock.patch('robot.run')
246     @mock.patch('os.path.isfile', return_value=True)
247     def test_ok(self, *args):
248         with mock.patch.object(self.test, 'set_robotframework_vars',
249                                return_value=True), \
250                 mock.patch.object(self.test, 'parse_results'):
251             self._test_run_suites(testcase.TestCase.EX_OK, *args)
252
253     @mock.patch('robot.run')
254     @mock.patch('os.path.isfile', return_value=False)
255     def test_ok_no_creds(self, *args):
256         with mock.patch.object(self.test, 'set_robotframework_vars',
257                                return_value=True) as mock_method, \
258                 mock.patch.object(self.test, 'parse_results'):
259             self._test_run_suites(testcase.TestCase.EX_OK, *args)
260             mock_method.assert_not_called()
261
262     @mock.patch('robot.run', return_value=1)
263     @mock.patch('os.path.isfile', return_value=True)
264     def test_testcases_in_failure(self, *args):
265         with mock.patch.object(self.test, 'set_robotframework_vars',
266                                return_value=True), \
267                 mock.patch.object(self.test, 'parse_results'):
268             self._test_run_suites(testcase.TestCase.EX_OK, *args)
269
270
271 class ODLRunTesting(ODLTesting):
272     """The class testing ODLTests.run()."""
273     # pylint: disable=too-many-public-methods,missing-docstring
274
275     @mock.patch('os_client_config.make_shade', side_effect=Exception)
276     def test_no_cloud(self, *args):
277         self.assertEqual(self.test.run(), testcase.TestCase.EX_RUN_ERROR)
278         args[0].assert_called_once_with()
279
280     @mock.patch('os_client_config.make_shade')
281     def test_no_service1(self, *args):
282         args[0].return_value.search_services.return_value = None
283         self.assertEqual(self.test.run(), testcase.TestCase.EX_RUN_ERROR)
284         args[0].return_value.search_services.assert_called_once_with('neutron')
285         args[0].return_value.search_endpoints.assert_not_called()
286
287     @mock.patch('os_client_config.make_shade')
288     def test_no_service2(self, *args):
289         args[0].return_value.search_services.return_value = []
290         self.assertEqual(self.test.run(), testcase.TestCase.EX_RUN_ERROR)
291         args[0].return_value.search_services.assert_called_once_with('neutron')
292         args[0].return_value.search_endpoints.assert_not_called()
293
294     @mock.patch('os_client_config.make_shade')
295     def test_no_service3(self, *args):
296         args[0].return_value.search_services.return_value = [
297             munch.Munch()]
298         self.assertEqual(self.test.run(), testcase.TestCase.EX_RUN_ERROR)
299         args[0].return_value.search_services.assert_called_once_with('neutron')
300         args[0].return_value.search_endpoints.assert_not_called()
301
302     @mock.patch('os_client_config.make_shade')
303     def test_no_endpoint1(self, *args):
304         args[0].return_value.search_services.return_value = [
305             munch.Munch(id=self._neutron_id)]
306         args[0].return_value.search_endpoints.return_value = None
307         self.assertEqual(self.test.run(), testcase.TestCase.EX_RUN_ERROR)
308         args[0].return_value.search_services.assert_called_once_with('neutron')
309         args[0].return_value.search_endpoints.assert_called_once_with(
310             filters={'interface': self._os_interface,
311                      'service_id': self._neutron_id})
312
313     @mock.patch('os_client_config.make_shade')
314     def test_no_endpoint2(self, *args):
315         args[0].return_value.search_services.return_value = [
316             munch.Munch(id=self._neutron_id)]
317         args[0].return_value.search_endpoints.return_value = []
318         self.assertEqual(self.test.run(), testcase.TestCase.EX_RUN_ERROR)
319         args[0].return_value.search_services.assert_called_once_with('neutron')
320         args[0].return_value.search_endpoints.assert_called_once_with(
321             filters={'interface': self._os_interface,
322                      'service_id': self._neutron_id})
323
324     @mock.patch('os_client_config.make_shade')
325     def test_no_endpoint3(self, *args):
326         args[0].return_value.search_services.return_value = [
327             munch.Munch(id=self._neutron_id)]
328         args[0].return_value.search_endpoints.return_value = [munch.Munch()]
329         self.assertEqual(self.test.run(), testcase.TestCase.EX_RUN_ERROR)
330         args[0].return_value.search_services.assert_called_once_with('neutron')
331         args[0].return_value.search_endpoints.assert_called_once_with(
332             filters={'interface': self._os_interface,
333                      'service_id': self._neutron_id})
334
335     @mock.patch('os_client_config.make_shade')
336     def test_endpoint_interface(self, *args):
337         args[0].return_value.search_services.return_value = [
338             munch.Munch(id=self._neutron_id)]
339         args[0].return_value.search_endpoints.return_value = [munch.Munch()]
340         self.assertEqual(self.test.run(), testcase.TestCase.EX_RUN_ERROR)
341         args[0].return_value.search_services.assert_called_once_with('neutron')
342         args[0].return_value.search_endpoints.assert_called_once_with(
343             filters={'interface': self._os_interface,
344                      'service_id': self._neutron_id})
345
346     @mock.patch('os_client_config.make_shade')
347     def _test_no_env_var(self, var, *args):
348         del os.environ[var]
349         self.assertEqual(self.test.run(), testcase.TestCase.EX_RUN_ERROR)
350         args[0].assert_called_once_with()
351
352     @mock.patch('os_client_config.make_shade')
353     def _test_missing_value(self, *args):
354         self.assertEqual(self.test.run(), testcase.TestCase.EX_RUN_ERROR)
355         args[0].assert_called_once_with()
356
357     @mock.patch('os_client_config.make_shade')
358     def _test_run(self, status=testcase.TestCase.EX_OK,
359                   exception=None, *args, **kwargs):
360         args[0].return_value.search_services.return_value = [
361             munch.Munch(id=self._neutron_id)]
362         args[0].return_value.search_endpoints.return_value = [
363             munch.Munch(url=self._neutron_url)]
364         odlip = kwargs['odlip'] if 'odlip' in kwargs else '127.0.0.3'
365         odlwebport = kwargs['odlwebport'] if 'odlwebport' in kwargs else '8080'
366         odlrestconfport = (kwargs['odlrestconfport']
367                            if 'odlrestconfport' in kwargs else '8181')
368         if exception:
369             self.test.run_suites = mock.Mock(side_effect=exception)
370         else:
371             self.test.run_suites = mock.Mock(return_value=status)
372         self.assertEqual(self.test.run(), status)
373         self.test.run_suites.assert_called_once_with(
374             odl.ODLTests.default_suites, neutronurl=self._neutron_url,
375             odlip=odlip, odlpassword=self._odl_password,
376             odlrestconfport=odlrestconfport, odlusername=self._odl_username,
377             odlwebport=odlwebport, osauthurl=self._os_auth_url,
378             ospassword=self._os_password, osprojectname=self._os_projectname,
379             osusername=self._os_username,
380             osprojectdomainname=self._os_projectdomainname,
381             osuserdomainname=self._os_userdomainname)
382         args[0].assert_called_once_with()
383         args[0].return_value.search_services.assert_called_once_with('neutron')
384         args[0].return_value.search_endpoints.assert_called_once_with(
385             filters={'interface': os.environ.get("OS_INTERFACE", "public"),
386                      'service_id': self._neutron_id})
387
388     @mock.patch('os_client_config.make_shade')
389     def _test_multiple_suites(self, suites,
390                               status=testcase.TestCase.EX_OK, *args, **kwargs):
391         args[0].return_value.search_endpoints.return_value = [
392             munch.Munch(url=self._neutron_url)]
393         args[0].return_value.search_services.return_value = [
394             munch.Munch(id=self._neutron_id)]
395         odlip = kwargs['odlip'] if 'odlip' in kwargs else '127.0.0.3'
396         odlwebport = kwargs['odlwebport'] if 'odlwebport' in kwargs else '8080'
397         odlrestconfport = (kwargs['odlrestconfport']
398                            if 'odlrestconfport' in kwargs else '8181')
399         self.test.run_suites = mock.Mock(return_value=status)
400         self.assertEqual(self.test.run(suites=suites), status)
401         self.test.run_suites.assert_called_once_with(
402             suites, neutronurl=self._neutron_url, odlip=odlip,
403             odlpassword=self._odl_password, odlrestconfport=odlrestconfport,
404             odlusername=self._odl_username, odlwebport=odlwebport,
405             osauthurl=self._os_auth_url, ospassword=self._os_password,
406             osprojectname=self._os_projectname, osusername=self._os_username,
407             osprojectdomainname=self._os_projectdomainname,
408             osuserdomainname=self._os_userdomainname)
409         args[0].assert_called_once_with()
410         args[0].return_value.search_services.assert_called_once_with('neutron')
411         args[0].return_value.search_endpoints.assert_called_once_with(
412             filters={'interface': os.environ.get("OS_INTERFACE", "public"),
413                      'service_id': self._neutron_id})
414
415     def test_exc(self):
416         with mock.patch('os_client_config.make_shade',
417                         side_effect=Exception()):
418             self.assertEqual(self.test.run(),
419                              testcase.TestCase.EX_RUN_ERROR)
420
421     def test_no_os_auth_url(self):
422         self._test_no_env_var("OS_AUTH_URL")
423
424     def test_no_os_username(self):
425         self._test_no_env_var("OS_USERNAME")
426
427     def test_no_os_password(self):
428         self._test_no_env_var("OS_PASSWORD")
429
430     def test_no_os__name(self):
431         self._test_no_env_var("OS_PROJECT_NAME")
432
433     def test_run_suites_false(self):
434         os.environ["SDN_CONTROLLER_IP"] = self._sdn_controller_ip
435         self._test_run(testcase.TestCase.EX_RUN_ERROR, None,
436                        odlip=self._sdn_controller_ip,
437                        odlwebport=self._odl_webport)
438
439     def test_run_suites_exc(self):
440         with self.assertRaises(Exception):
441             os.environ["SDN_CONTROLLER_IP"] = self._sdn_controller_ip
442             self._test_run(testcase.TestCase.EX_RUN_ERROR,
443                            Exception(),
444                            odlip=self._sdn_controller_ip,
445                            odlwebport=self._odl_webport)
446
447     def test_no_sdn_controller_ip(self):
448         self._test_missing_value()
449
450     def test_without_installer_type(self):
451         os.environ["SDN_CONTROLLER_IP"] = self._sdn_controller_ip
452         self._test_run(testcase.TestCase.EX_OK, None,
453                        odlip=self._sdn_controller_ip,
454                        odlwebport=self._odl_webport)
455
456     def test_without_os_interface(self):
457         del os.environ["OS_INTERFACE"]
458         os.environ["SDN_CONTROLLER_IP"] = self._sdn_controller_ip
459         self._test_run(testcase.TestCase.EX_OK, None,
460                        odlip=self._sdn_controller_ip,
461                        odlwebport=self._odl_webport)
462
463     def test_os_interface_public(self):
464         os.environ["OS_INTERFACE"] = "public"
465         os.environ["SDN_CONTROLLER_IP"] = self._sdn_controller_ip
466         self._test_run(testcase.TestCase.EX_OK, None,
467                        odlip=self._sdn_controller_ip,
468                        odlwebport=self._odl_webport)
469
470     def test_os_interface_internal(self):
471         os.environ["OS_INTERFACE"] = "internal"
472         os.environ["SDN_CONTROLLER_IP"] = self._sdn_controller_ip
473         self._test_run(testcase.TestCase.EX_OK, None,
474                        odlip=self._sdn_controller_ip,
475                        odlwebport=self._odl_webport)
476
477     def test_os_interface_admin(self):
478         os.environ["OS_INTERFACE"] = "admin"
479         os.environ["SDN_CONTROLLER_IP"] = self._sdn_controller_ip
480         self._test_run(testcase.TestCase.EX_OK, None,
481                        odlip=self._sdn_controller_ip,
482                        odlwebport=self._odl_webport)
483
484     def test_suites(self):
485         os.environ["SDN_CONTROLLER_IP"] = self._sdn_controller_ip
486         self._test_multiple_suites(
487             [odl.ODLTests.basic_suite_dir],
488             testcase.TestCase.EX_OK,
489             odlip=self._sdn_controller_ip,
490             odlwebport=self._odl_webport)
491
492     def test_fuel_no_controller_ip(self):
493         os.environ["INSTALLER_TYPE"] = "fuel"
494         self._test_missing_value()
495
496     def test_fuel(self):
497         os.environ["SDN_CONTROLLER_IP"] = self._sdn_controller_ip
498         os.environ["INSTALLER_TYPE"] = "fuel"
499         self._test_run(testcase.TestCase.EX_OK, None,
500                        odlip=self._sdn_controller_ip,
501                        odlwebport='8282',
502                        odlrestconfport='8282')
503
504     def test_apex_no_controller_ip(self):
505         os.environ["INSTALLER_TYPE"] = "apex"
506         self._test_missing_value()
507
508     def test_apex(self):
509         os.environ["SDN_CONTROLLER_IP"] = self._sdn_controller_ip
510         os.environ["INSTALLER_TYPE"] = "apex"
511         self._test_run(testcase.TestCase.EX_OK, None,
512                        odlip=self._sdn_controller_ip, odlwebport='8081',
513                        odlrestconfport='8081')
514
515     def test_netvirt_no_controller_ip(self):
516         os.environ["INSTALLER_TYPE"] = "netvirt"
517         self._test_missing_value()
518
519     def test_netvirt(self):
520         os.environ["SDN_CONTROLLER_IP"] = self._sdn_controller_ip
521         os.environ["INSTALLER_TYPE"] = "netvirt"
522         self._test_run(testcase.TestCase.EX_OK, None,
523                        odlip=self._sdn_controller_ip, odlwebport='8081',
524                        odlrestconfport='8081')
525
526     def test_compass(self):
527         os.environ["SDN_CONTROLLER_IP"] = self._sdn_controller_ip
528         os.environ["INSTALLER_TYPE"] = "compass"
529         self._test_run(testcase.TestCase.EX_OK, None,
530                        odlip=self._sdn_controller_ip,
531                        odlrestconfport='8080')
532
533     def test_daisy_no_controller_ip(self):
534         os.environ["INSTALLER_TYPE"] = "daisy"
535         self._test_missing_value()
536
537     def test_daisy(self):
538         os.environ["SDN_CONTROLLER_IP"] = self._sdn_controller_ip
539         os.environ["INSTALLER_TYPE"] = "daisy"
540         self._test_run(testcase.TestCase.EX_OK, None,
541                        odlip=self._sdn_controller_ip, odlwebport='8181',
542                        odlrestconfport='8087')
543
544
545 class ODLArgParserTesting(ODLTesting):
546
547     """The class testing ODLParser."""
548     # pylint: disable=missing-docstring
549
550     def setUp(self):
551         self.parser = odl.ODLParser()
552         super(ODLArgParserTesting, self).setUp()
553
554     def test_default(self):
555         self.assertEqual(self.parser.parse_args(), self.defaultargs)
556
557     def test_basic(self):
558         self.defaultargs['neutronurl'] = self._neutron_url
559         self.defaultargs['odlip'] = self._sdn_controller_ip
560         self.assertEqual(
561             self.parser.parse_args(
562                 ["--neutronurl={}".format(self._neutron_url),
563                  "--odlip={}".format(self._sdn_controller_ip)]),
564             self.defaultargs)
565
566     @mock.patch('sys.stderr', new_callable=six.StringIO)
567     def test_fail(self, mock_method):
568         self.defaultargs['foo'] = 'bar'
569         with self.assertRaises(SystemExit):
570             self.parser.parse_args(["--foo=bar"])
571         self.assertTrue(mock_method.getvalue().startswith("usage:"))
572
573     def _test_arg(self, arg, value):
574         self.defaultargs[arg] = value
575         self.assertEqual(
576             self.parser.parse_args(["--{}={}".format(arg, value)]),
577             self.defaultargs)
578
579     def test_odlusername(self):
580         self._test_arg('odlusername', 'foo')
581
582     def test_odlpassword(self):
583         self._test_arg('odlpassword', 'foo')
584
585     def test_osauthurl(self):
586         self._test_arg('osauthurl', 'http://127.0.0.4:5000/v2')
587
588     def test_neutronurl(self):
589         self._test_arg('neutronurl', 'http://127.0.0.4:9696')
590
591     def test_osusername(self):
592         self._test_arg('osusername', 'foo')
593
594     def test_osuserdomainname(self):
595         self._test_arg('osuserdomainname', 'domain')
596
597     def test_osprojectname(self):
598         self._test_arg('osprojectname', 'foo')
599
600     def test_osprojectdomainname(self):
601         self._test_arg('osprojectdomainname', 'domain')
602
603     def test_ospassword(self):
604         self._test_arg('ospassword', 'foo')
605
606     def test_odlip(self):
607         self._test_arg('odlip', '127.0.0.4')
608
609     def test_odlwebport(self):
610         self._test_arg('odlwebport', '80')
611
612     def test_odlrestconfport(self):
613         self._test_arg('odlrestconfport', '80')
614
615     def test_pushtodb(self):
616         self.defaultargs['pushtodb'] = True
617         self.assertEqual(self.parser.parse_args(["--{}".format('pushtodb')]),
618                          self.defaultargs)
619
620     def test_multiple_args(self):
621         self.defaultargs['neutronurl'] = self._neutron_url
622         self.defaultargs['odlip'] = self._sdn_controller_ip
623         self.assertEqual(
624             self.parser.parse_args(
625                 ["--neutronurl={}".format(self._neutron_url),
626                  "--odlip={}".format(self._sdn_controller_ip)]),
627             self.defaultargs)
628
629
630 if __name__ == "__main__":
631     logging.disable(logging.CRITICAL)
632     unittest.main(verbosity=2)