Fix the config of fixed_network_name in tempest
[functest.git] / functest / tests / unit / openstack / refstack_client / test_refstack_client.py
1 #!/usr/bin/env python
2
3 # Copyright (c) 2017 Huawei Technologies Co.,Ltd 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 # pylint: disable=missing-docstring
11
12 import logging
13 import unittest
14
15 import mock
16 import pkg_resources
17
18 from functest.core import testcase
19 from functest.opnfv_tests.openstack.refstack_client.refstack_client import \
20     RefstackClient, RefstackClientParser
21 from functest.utils.constants import CONST
22
23 from snaps.openstack.os_credentials import OSCreds
24
25 __author__ = ("Matthew Li <matthew.lijun@huawei.com>,"
26               "Linda Wang <wangwulin@huawei.com>")
27
28
29 class OSRefstackClientTesting(unittest.TestCase):
30     """The class testing RefstackClient """
31     # pylint: disable=missing-docstring, too-many-public-methods
32
33     _config = pkg_resources.resource_filename(
34         'functest',
35         'opnfv_tests/openstack/refstack_client/refstack_tempest.conf')
36     _testlist = pkg_resources.resource_filename(
37         'functest', 'opnfv_tests/openstack/refstack_client/defcore.txt')
38
39     def setUp(self):
40         self.default_args = {'config': self._config,
41                              'testlist': self._testlist}
42         CONST.__setattr__('OS_AUTH_URL', 'https://ip:5000/v3')
43         CONST.__setattr__('OS_INSECURE', 'true')
44         self.case_name = 'refstack_defcore'
45         self.result = 0
46         self.os_creds = OSCreds(
47             username='user', password='pass',
48             auth_url='http://foo.com:5000/v3', project_name='bar')
49         self.details = {"tests": 3,
50                         "failures": 1,
51                         "success": ['tempest.api.compute [18.464988s]'],
52                         "errors": ['tempest.api.volume [0.230334s]'],
53                         "skipped": ['tempest.api.network [1.265828s]']}
54
55     def _create_client(self):
56         with mock.patch('snaps.openstack.tests.openstack_tests.'
57                         'get_credentials', return_value=self.os_creds):
58             return RefstackClient()
59
60     def test_run_defcore_insecure(self):
61         insecure = '-k'
62         config = 'tempest.conf'
63         testlist = 'testlist'
64         client = self._create_client()
65         with mock.patch('functest.opnfv_tests.openstack.refstack_client.'
66                         'refstack_client.ft_utils.execute_command') as m_cmd:
67             cmd = ("refstack-client test {0} -c {1} -v --test-list {2}"
68                    .format(insecure, config, testlist))
69             client.run_defcore(config, testlist)
70             m_cmd.assert_any_call(cmd)
71
72     def test_run_defcore(self):
73         CONST.__setattr__('OS_AUTH_URL', 'http://ip:5000/v3')
74         insecure = ''
75         config = 'tempest.conf'
76         testlist = 'testlist'
77         client = self._create_client()
78         with mock.patch('functest.opnfv_tests.openstack.refstack_client.'
79                         'refstack_client.ft_utils.execute_command') as m_cmd:
80             cmd = ("refstack-client test {0} -c {1} -v --test-list {2}"
81                    .format(insecure, config, testlist))
82             client.run_defcore(config, testlist)
83             m_cmd.assert_any_call(cmd)
84
85     @mock.patch('functest.opnfv_tests.openstack.refstack_client.'
86                 'refstack_client.LOGGER.info')
87     @mock.patch('__builtin__.open', side_effect=Exception)
88     def test_parse_refstack_result_fail(self, *args):
89         self._create_client().parse_refstack_result()
90         args[1].assert_called_once_with(
91             "Testcase %s success_rate is %s%%",
92             self.case_name, self.result)
93
94     def test_parse_refstack_result_ok(self):
95         log_file = ('''
96                     {0} tempest.api.compute [18.464988s] ... ok
97                     {0} tempest.api.volume [0.230334s] ... FAILED
98                     {0} tempest.api.network [1.265828s] ... SKIPPED:
99                     Ran: 3 tests in 1259.0000 sec.
100                     - Passed: 1
101                     - Skipped: 1
102                     - Failed: 1
103                    ''')
104         client = self._create_client()
105         with mock.patch('__builtin__.open',
106                         mock.mock_open(read_data=log_file)):
107             client.parse_refstack_result()
108             self.assertEqual(client.details, self.details)
109
110     def _get_main_kwargs(self, key=None):
111         kwargs = {'config': self._config,
112                   'testlist': self._testlist}
113         if key:
114             del kwargs[key]
115         return kwargs
116
117     def _test_main_missing_keyword(self, key):
118         kwargs = self._get_main_kwargs(key)
119         client = self._create_client()
120         self.assertEqual(client.main(**kwargs),
121                          testcase.TestCase.EX_RUN_ERROR)
122
123     def test_main_missing_conf(self):
124         self._test_main_missing_keyword('config')
125
126     def test_main_missing_testlist(self):
127         self._test_main_missing_keyword('testlist')
128
129     def _test_argparser(self, arg, value):
130         self.default_args[arg] = value
131         parser = RefstackClientParser()
132         self.assertEqual(parser.parse_args(["--{}={}".format(arg, value)]),
133                          self.default_args)
134
135     def test_argparser_conf(self):
136         self._test_argparser('config', self._config)
137
138     def test_argparser_testlist(self):
139         self._test_argparser('testlist', self._testlist)
140
141     def test_argparser_multiple_args(self):
142         self.default_args['config'] = self._config
143         self.default_args['testlist'] = self._testlist
144         parser = RefstackClientParser()
145         self.assertEqual(parser.parse_args(
146             ["--config={}".format(self._config),
147              "--testlist={}".format(self._testlist)]), self.default_args)
148
149
150 if __name__ == "__main__":
151     logging.disable(logging.CRITICAL)
152     unittest.main(verbosity=2)