Update to Alpine 3.14
[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 ("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('os.makedirs')
229     @mock.patch('robot.run', side_effect=RobotError)
230     @mock.patch('os.path.isfile', return_value=True)
231     def test_run_ko(self, *args):
232         with mock.patch.object(self.test, 'set_robotframework_vars',
233                                return_value=True), \
234                 self.assertRaises(RobotError):
235             self._test_run_suites(testcase.TestCase.EX_RUN_ERROR, *args)
236
237     @mock.patch('os.makedirs')
238     @mock.patch('robot.run')
239     @mock.patch('os.path.isfile', return_value=True)
240     def test_parse_results_ko(self, *args):
241         with mock.patch.object(self.test, 'set_robotframework_vars',
242                                return_value=True), \
243                 mock.patch.object(self.test, 'parse_results',
244                                   side_effect=RobotError):
245             self._test_run_suites(testcase.TestCase.EX_RUN_ERROR, *args)
246
247     @mock.patch('os.makedirs')
248     @mock.patch('robot.run')
249     @mock.patch('os.path.isfile', return_value=True)
250     def test_generate_report_ko(self, *args):
251         with mock.patch.object(self.test, 'set_robotframework_vars',
252                                return_value=True), \
253                 mock.patch.object(self.test, 'parse_results'), \
254                 mock.patch.object(self.test, 'generate_report',
255                                   return_value=1):
256             self._test_run_suites(testcase.TestCase.EX_OK, *args)
257
258     @mock.patch('os.makedirs')
259     @mock.patch('robot.run')
260     @mock.patch('os.path.isfile', return_value=True)
261     def test_generate_report_exc(self, *args):
262         with mock.patch.object(self.test, 'set_robotframework_vars',
263                                return_value=True), \
264                 mock.patch.object(self.test, 'parse_results'), \
265                 mock.patch.object(self.test, 'generate_report',
266                                   side_effect=Exception):
267             self._test_run_suites(testcase.TestCase.EX_RUN_ERROR, *args)
268
269     @mock.patch('os.makedirs')
270     @mock.patch('robot.run')
271     @mock.patch('os.path.isfile', return_value=True)
272     def test_ok(self, *args):
273         with mock.patch.object(self.test, 'set_robotframework_vars',
274                                return_value=True), \
275                 mock.patch.object(self.test, 'parse_results'), \
276                 mock.patch.object(self.test, 'generate_report',
277                                   return_value=0):
278             self._test_run_suites(testcase.TestCase.EX_OK, *args)
279
280     @mock.patch('os.makedirs')
281     @mock.patch('robot.run')
282     @mock.patch('os.path.isfile', return_value=False)
283     def test_ok_no_creds(self, *args):
284         with mock.patch.object(self.test, 'set_robotframework_vars',
285                                return_value=True) as mock_method, \
286                 mock.patch.object(self.test, 'parse_results'), \
287                 mock.patch.object(self.test, 'generate_report',
288                                   return_value=0):
289             self._test_run_suites(testcase.TestCase.EX_OK, *args)
290             mock_method.assert_not_called()
291
292     @mock.patch('os.makedirs')
293     @mock.patch('robot.run', return_value=1)
294     @mock.patch('os.path.isfile', return_value=True)
295     def test_testcases_in_failure(self, *args):
296         with mock.patch.object(self.test, 'set_robotframework_vars',
297                                return_value=True), \
298                 mock.patch.object(self.test, 'parse_results'), \
299                 mock.patch.object(self.test, 'generate_report',
300                                   return_value=0):
301             self._test_run_suites(testcase.TestCase.EX_OK, *args)
302
303
304 class ODLRunTesting(ODLTesting):
305     """The class testing ODLTests.run()."""
306     # pylint: disable=too-many-public-methods,missing-docstring
307
308     @mock.patch('os_client_config.make_shade', side_effect=Exception)
309     def test_no_cloud(self, *args):
310         self.assertEqual(self.test.run(), testcase.TestCase.EX_RUN_ERROR)
311         args[0].assert_called_once_with()
312
313     @mock.patch('os_client_config.make_shade')
314     def test_no_service1(self, *args):
315         args[0].return_value.search_services.return_value = None
316         self.assertEqual(self.test.run(), testcase.TestCase.EX_RUN_ERROR)
317         args[0].return_value.search_services.assert_called_once_with('neutron')
318         args[0].return_value.search_endpoints.assert_not_called()
319
320     @mock.patch('os_client_config.make_shade')
321     def test_no_service2(self, *args):
322         args[0].return_value.search_services.return_value = []
323         self.assertEqual(self.test.run(), testcase.TestCase.EX_RUN_ERROR)
324         args[0].return_value.search_services.assert_called_once_with('neutron')
325         args[0].return_value.search_endpoints.assert_not_called()
326
327     @mock.patch('os_client_config.make_shade')
328     def test_no_service3(self, *args):
329         args[0].return_value.search_services.return_value = [
330             munch.Munch()]
331         self.assertEqual(self.test.run(), testcase.TestCase.EX_RUN_ERROR)
332         args[0].return_value.search_services.assert_called_once_with('neutron')
333         args[0].return_value.search_endpoints.assert_not_called()
334
335     @mock.patch('os_client_config.make_shade')
336     def test_no_endpoint1(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 = None
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_endpoint2(self, *args):
348         args[0].return_value.search_services.return_value = [
349             munch.Munch(id=self._neutron_id)]
350         args[0].return_value.search_endpoints.return_value = []
351         self.assertEqual(self.test.run(), testcase.TestCase.EX_RUN_ERROR)
352         args[0].return_value.search_services.assert_called_once_with('neutron')
353         args[0].return_value.search_endpoints.assert_called_once_with(
354             filters={'interface': self._os_interface,
355                      'service_id': self._neutron_id})
356
357     @mock.patch('os_client_config.make_shade')
358     def test_no_endpoint3(self, *args):
359         args[0].return_value.search_services.return_value = [
360             munch.Munch(id=self._neutron_id)]
361         args[0].return_value.search_endpoints.return_value = [munch.Munch()]
362         self.assertEqual(self.test.run(), testcase.TestCase.EX_RUN_ERROR)
363         args[0].return_value.search_services.assert_called_once_with('neutron')
364         args[0].return_value.search_endpoints.assert_called_once_with(
365             filters={'interface': self._os_interface,
366                      'service_id': self._neutron_id})
367
368     @mock.patch('os_client_config.make_shade')
369     def test_endpoint_interface(self, *args):
370         args[0].return_value.search_services.return_value = [
371             munch.Munch(id=self._neutron_id)]
372         args[0].return_value.search_endpoints.return_value = [munch.Munch()]
373         self.assertEqual(self.test.run(), testcase.TestCase.EX_RUN_ERROR)
374         args[0].return_value.search_services.assert_called_once_with('neutron')
375         args[0].return_value.search_endpoints.assert_called_once_with(
376             filters={'interface': self._os_interface,
377                      'service_id': self._neutron_id})
378
379     @mock.patch('os_client_config.make_shade')
380     def _test_no_env_var(self, var, *args):
381         del os.environ[var]
382         self.assertEqual(self.test.run(), testcase.TestCase.EX_RUN_ERROR)
383         args[0].assert_called_once_with()
384
385     @mock.patch('os_client_config.make_shade')
386     def _test_missing_value(self, *args):
387         self.assertEqual(self.test.run(), testcase.TestCase.EX_RUN_ERROR)
388         args[0].assert_called_once_with()
389
390     @mock.patch('os_client_config.make_shade')
391     def _test_run(self, status=testcase.TestCase.EX_OK,
392                   exception=None, *args, **kwargs):
393         # pylint: disable=keyword-arg-before-vararg
394         args[0].return_value.search_services.return_value = [
395             munch.Munch(id=self._neutron_id)]
396         args[0].return_value.search_endpoints.return_value = [
397             munch.Munch(url=self._neutron_url)]
398         odlip = kwargs['odlip'] if 'odlip' in kwargs else '127.0.0.3'
399         odlwebport = kwargs['odlwebport'] if 'odlwebport' in kwargs else '8080'
400         odlrestconfport = (kwargs['odlrestconfport']
401                            if 'odlrestconfport' in kwargs else '8181')
402         if exception:
403             self.test.run_suites = mock.Mock(side_effect=exception)
404         else:
405             self.test.run_suites = mock.Mock(return_value=status)
406         self.assertEqual(self.test.run(), status)
407         self.test.run_suites.assert_called_once_with(
408             odl.ODLTests.default_suites, neutronurl=self._neutron_url,
409             odlip=odlip, odlpassword=self._odl_password,
410             odlrestconfport=odlrestconfport, odlusername=self._odl_username,
411             odlwebport=odlwebport, osauthurl=self._os_auth_url,
412             ospassword=self._os_password, osprojectname=self._os_projectname,
413             osusername=self._os_username,
414             osprojectdomainname=self._os_projectdomainname,
415             osuserdomainname=self._os_userdomainname)
416         args[0].assert_called_once_with()
417         args[0].return_value.search_services.assert_called_once_with('neutron')
418         args[0].return_value.search_endpoints.assert_called_once_with(
419             filters={
420                 'interface': os.environ.get(
421                     "OS_INTERFACE", "public").replace('URL', ''),
422                 'service_id': self._neutron_id})
423
424     @mock.patch('os_client_config.make_shade')
425     def _test_multiple_suites(self, suites,
426                               status=testcase.TestCase.EX_OK, *args, **kwargs):
427         # pylint: disable=keyword-arg-before-vararg
428         args[0].return_value.search_endpoints.return_value = [
429             munch.Munch(url=self._neutron_url)]
430         args[0].return_value.search_services.return_value = [
431             munch.Munch(id=self._neutron_id)]
432         odlip = kwargs['odlip'] if 'odlip' in kwargs else '127.0.0.3'
433         odlwebport = kwargs['odlwebport'] if 'odlwebport' in kwargs else '8080'
434         odlrestconfport = (kwargs['odlrestconfport']
435                            if 'odlrestconfport' in kwargs else '8181')
436         self.test.run_suites = mock.Mock(return_value=status)
437         self.assertEqual(self.test.run(suites=suites), status)
438         self.test.run_suites.assert_called_once_with(
439             suites, neutronurl=self._neutron_url, odlip=odlip,
440             odlpassword=self._odl_password, odlrestconfport=odlrestconfport,
441             odlusername=self._odl_username, odlwebport=odlwebport,
442             osauthurl=self._os_auth_url, ospassword=self._os_password,
443             osprojectname=self._os_projectname, osusername=self._os_username,
444             osprojectdomainname=self._os_projectdomainname,
445             osuserdomainname=self._os_userdomainname)
446         args[0].assert_called_once_with()
447         args[0].return_value.search_services.assert_called_once_with('neutron')
448         args[0].return_value.search_endpoints.assert_called_once_with(
449             filters={'interface': os.environ.get("OS_INTERFACE", "public"),
450                      'service_id': self._neutron_id})
451
452     def test_exc(self):
453         with mock.patch('os_client_config.make_shade',
454                         side_effect=Exception()):
455             self.assertEqual(self.test.run(),
456                              testcase.TestCase.EX_RUN_ERROR)
457
458     def test_no_os_auth_url(self):
459         self._test_no_env_var("OS_AUTH_URL")
460
461     def test_no_os_username(self):
462         self._test_no_env_var("OS_USERNAME")
463
464     def test_no_os_password(self):
465         self._test_no_env_var("OS_PASSWORD")
466
467     def test_no_os__name(self):
468         self._test_no_env_var("OS_PROJECT_NAME")
469
470     def test_run_suites_false(self):
471         os.environ["SDN_CONTROLLER_IP"] = self._sdn_controller_ip
472         self._test_run(testcase.TestCase.EX_RUN_ERROR, None,
473                        odlip=self._sdn_controller_ip,
474                        odlwebport=self._odl_webport)
475
476     def test_run_suites_exc(self):
477         with self.assertRaises(Exception):
478             os.environ["SDN_CONTROLLER_IP"] = self._sdn_controller_ip
479             self._test_run(testcase.TestCase.EX_RUN_ERROR,
480                            Exception(),
481                            odlip=self._sdn_controller_ip,
482                            odlwebport=self._odl_webport)
483
484     def test_no_sdn_controller_ip(self):
485         self._test_missing_value()
486
487     def test_without_installer_type(self):
488         os.environ["SDN_CONTROLLER_IP"] = self._sdn_controller_ip
489         self._test_run(testcase.TestCase.EX_OK, None,
490                        odlip=self._sdn_controller_ip,
491                        odlwebport=self._odl_webport)
492
493     def test_without_os_interface(self):
494         del os.environ["OS_INTERFACE"]
495         os.environ["SDN_CONTROLLER_IP"] = self._sdn_controller_ip
496         self._test_run(testcase.TestCase.EX_OK, None,
497                        odlip=self._sdn_controller_ip,
498                        odlwebport=self._odl_webport)
499
500     def test_os_interface_public(self):
501         os.environ["OS_INTERFACE"] = "public"
502         os.environ["SDN_CONTROLLER_IP"] = self._sdn_controller_ip
503         self._test_run(testcase.TestCase.EX_OK, None,
504                        odlip=self._sdn_controller_ip,
505                        odlwebport=self._odl_webport)
506
507     def test_os_interface_publicurl(self):
508         os.environ["OS_INTERFACE"] = "publicURL"
509         os.environ["SDN_CONTROLLER_IP"] = self._sdn_controller_ip
510         self._test_run(testcase.TestCase.EX_OK, None,
511                        odlip=self._sdn_controller_ip,
512                        odlwebport=self._odl_webport)
513
514     def test_os_interface_internal(self):
515         os.environ["OS_INTERFACE"] = "internal"
516         os.environ["SDN_CONTROLLER_IP"] = self._sdn_controller_ip
517         self._test_run(testcase.TestCase.EX_OK, None,
518                        odlip=self._sdn_controller_ip,
519                        odlwebport=self._odl_webport)
520
521     def test_os_interface_admin(self):
522         os.environ["OS_INTERFACE"] = "admin"
523         os.environ["SDN_CONTROLLER_IP"] = self._sdn_controller_ip
524         self._test_run(testcase.TestCase.EX_OK, None,
525                        odlip=self._sdn_controller_ip,
526                        odlwebport=self._odl_webport)
527
528     def test_suites(self):
529         os.environ["SDN_CONTROLLER_IP"] = self._sdn_controller_ip
530         self._test_multiple_suites(
531             [odl.ODLTests.basic_suite_dir],
532             testcase.TestCase.EX_OK,
533             odlip=self._sdn_controller_ip,
534             odlwebport=self._odl_webport)
535
536
537 class ODLArgParserTesting(ODLTesting):
538
539     """The class testing ODLParser."""
540     # pylint: disable=missing-docstring
541
542     def setUp(self):
543         self.parser = odl.ODLParser()
544         super().setUp()
545
546     def test_default(self):
547         self.assertEqual(self.parser.parse_args(), self.defaultargs)
548
549     def test_basic(self):
550         self.defaultargs['neutronurl'] = self._neutron_url
551         self.defaultargs['odlip'] = self._sdn_controller_ip
552         self.assertEqual(
553             self.parser.parse_args(
554                 ["--neutronurl={}".format(self._neutron_url),
555                  "--odlip={}".format(self._sdn_controller_ip)]),
556             self.defaultargs)
557
558     @mock.patch('sys.stderr', new_callable=six.StringIO)
559     def test_fail(self, mock_method):
560         self.defaultargs['foo'] = 'bar'
561         with self.assertRaises(SystemExit):
562             self.parser.parse_args(["--foo=bar"])
563         self.assertTrue(mock_method.getvalue().startswith("usage:"))
564
565     def _test_arg(self, arg, value):
566         self.defaultargs[arg] = value
567         self.assertEqual(
568             self.parser.parse_args(["--{}={}".format(arg, value)]),
569             self.defaultargs)
570
571     def test_odlusername(self):
572         self._test_arg('odlusername', 'foo')
573
574     def test_odlpassword(self):
575         self._test_arg('odlpassword', 'foo')
576
577     def test_osauthurl(self):
578         self._test_arg('osauthurl', 'http://127.0.0.4:5000/v2')
579
580     def test_neutronurl(self):
581         self._test_arg('neutronurl', 'http://127.0.0.4:9696')
582
583     def test_osusername(self):
584         self._test_arg('osusername', 'foo')
585
586     def test_osuserdomainname(self):
587         self._test_arg('osuserdomainname', 'domain')
588
589     def test_osprojectname(self):
590         self._test_arg('osprojectname', 'foo')
591
592     def test_osprojectdomainname(self):
593         self._test_arg('osprojectdomainname', 'domain')
594
595     def test_ospassword(self):
596         self._test_arg('ospassword', 'foo')
597
598     def test_odlip(self):
599         self._test_arg('odlip', '127.0.0.4')
600
601     def test_odlwebport(self):
602         self._test_arg('odlwebport', '80')
603
604     def test_odlrestconfport(self):
605         self._test_arg('odlrestconfport', '80')
606
607     def test_pushtodb(self):
608         self.defaultargs['pushtodb'] = True
609         self.assertEqual(self.parser.parse_args(["--{}".format('pushtodb')]),
610                          self.defaultargs)
611
612     def test_multiple_args(self):
613         self.defaultargs['neutronurl'] = self._neutron_url
614         self.defaultargs['odlip'] = self._sdn_controller_ip
615         self.assertEqual(
616             self.parser.parse_args(
617                 ["--neutronurl={}".format(self._neutron_url),
618                  "--odlip={}".format(self._sdn_controller_ip)]),
619             self.defaultargs)
620
621
622 if __name__ == "__main__":
623     logging.disable(logging.CRITICAL)
624     unittest.main(verbosity=2)