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