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