More Unit Tests for utils module
[functest.git] / functest / tests / unit / opnfv_tests / openstack / tempest / test_conf_utils.py
1 #!/usr/bin/env python
2
3 # All rights reserved. This program and the accompanying materials
4 # are made available under the terms of the Apache License, Version 2.0
5 # which accompanies this distribution, and is available at
6 # http://www.apache.org/licenses/LICENSE-2.0
7
8 import logging
9 import unittest
10
11 import mock
12
13 from functest.opnfv_tests.openstack.tempest import conf_utils
14 from functest.utils.constants import CONST
15
16
17 class OSTempestConfUtilsTesting(unittest.TestCase):
18
19     logging.disable(logging.CRITICAL)
20
21     def test_create_tempest_resources_missing_network_dic(self):
22         with mock.patch('functest.opnfv_tests.openstack.tempest.conf_utils.'
23                         'os_utils.get_keystone_client',
24                         return_value=mock.Mock()), \
25             mock.patch('functest.opnfv_tests.openstack.tempest.conf_utils.'
26                        'os_utils.create_tenant',
27                        return_value='test_tenant_id'), \
28             mock.patch('functest.opnfv_tests.openstack.tempest.conf_utils.'
29                        'os_utils.create_user',
30                        return_value='test_user_id'), \
31             mock.patch('functest.opnfv_tests.openstack.tempest.conf_utils.'
32                        'os_utils.create_shared_network_full',
33                        return_value=None), \
34                 self.assertRaises(Exception) as context:
35             conf_utils.create_tempest_resources()
36             msg = 'Failed to create private network'
37             self.assertTrue(msg in context)
38
39     def test_create_tempest_resources_missing_image(self):
40         CONST.tempest_use_custom_images = 'test_image'
41         with mock.patch('functest.opnfv_tests.openstack.tempest.conf_utils.'
42                         'os_utils.get_keystone_client',
43                         return_value=mock.Mock()), \
44             mock.patch('functest.opnfv_tests.openstack.tempest.conf_utils.'
45                        'os_utils.create_tenant',
46                        return_value='test_tenant_id'), \
47             mock.patch('functest.opnfv_tests.openstack.tempest.conf_utils.'
48                        'os_utils.create_user',
49                        return_value='test_user_id'), \
50             mock.patch('functest.opnfv_tests.openstack.tempest.conf_utils.'
51                        'os_utils.create_shared_network_full',
52                        return_value=mock.Mock()), \
53             mock.patch('functest.opnfv_tests.openstack.tempest.conf_utils.'
54                        'os_utils.get_or_create_image',
55                        return_value=(mock.Mock(), None)), \
56                 self.assertRaises(Exception) as context:
57             conf_utils.create_tempest_resources()
58             msg = 'Failed to create image'
59             self.assertTrue(msg in context)
60
61     def test_create_tempest_resources_missing_flavor(self):
62         CONST.tempest_use_custom_images = 'test_image'
63         CONST.tempest_use_custom_flavors = 'test_flavour'
64         with mock.patch('functest.opnfv_tests.openstack.tempest.conf_utils.'
65                         'os_utils.get_keystone_client',
66                         return_value=mock.Mock()), \
67             mock.patch('functest.opnfv_tests.openstack.tempest.conf_utils.'
68                        'os_utils.create_tenant',
69                        return_value='test_tenant_id'), \
70             mock.patch('functest.opnfv_tests.openstack.tempest.conf_utils.'
71                        'os_utils.create_user',
72                        return_value='test_user_id'), \
73             mock.patch('functest.opnfv_tests.openstack.tempest.conf_utils.'
74                        'os_utils.create_shared_network_full',
75                        return_value=mock.Mock()), \
76             mock.patch('functest.opnfv_tests.openstack.tempest.conf_utils.'
77                        'os_utils.get_or_create_image',
78                        return_value=(mock.Mock(), 'image_id')), \
79             mock.patch('functest.opnfv_tests.openstack.tempest.conf_utils.'
80                        'os_utils.get_or_create_flavor',
81                        return_value=(mock.Mock(), None)), \
82                 self.assertRaises(Exception) as context:
83             conf_utils.create_tempest_resources()
84             msg = 'Failed to create flavor'
85             self.assertTrue(msg in context)
86
87     def test_get_verifier_id_missing_verifier(self):
88         CONST.tempest_deployment_name = 'test_deploy_name'
89         with mock.patch('functest.opnfv_tests.openstack.tempest.'
90                         'conf_utils.subprocess.Popen') as mock_popen, \
91                 self.assertRaises(Exception):
92             mock_stdout = mock.Mock()
93             attrs = {'stdout.readline.return_value': ''}
94             mock_stdout.configure_mock(**attrs)
95             mock_popen.return_value = mock_stdout
96             conf_utils.get_verifier_id(),
97
98     def test_get_verifier_id_default(self):
99         CONST.tempest_deployment_name = 'test_deploy_name'
100         with mock.patch('functest.opnfv_tests.openstack.tempest.'
101                         'conf_utils.subprocess.Popen') as mock_popen:
102             mock_stdout = mock.Mock()
103             attrs = {'stdout.readline.return_value': 'test_deploy_id'}
104             mock_stdout.configure_mock(**attrs)
105             mock_popen.return_value = mock_stdout
106
107             self.assertEqual(conf_utils.get_verifier_id(),
108                              'test_deploy_id')
109
110     def test_get_verifier_deployment_id_missing_rally(self):
111         CONST.rally_deployment_name = 'test_rally_deploy_name'
112         with mock.patch('functest.opnfv_tests.openstack.tempest.'
113                         'conf_utils.subprocess.Popen') as mock_popen, \
114                 self.assertRaises(Exception):
115             mock_stdout = mock.Mock()
116             attrs = {'stdout.readline.return_value': ''}
117             mock_stdout.configure_mock(**attrs)
118             mock_popen.return_value = mock_stdout
119             conf_utils.get_verifier_deployment_id(),
120
121     def test_get_verifier_deployment_id_default(self):
122         CONST.rally_deployment_name = 'test_rally_deploy_name'
123         with mock.patch('functest.opnfv_tests.openstack.tempest.'
124                         'conf_utils.subprocess.Popen') as mock_popen:
125             mock_stdout = mock.Mock()
126             attrs = {'stdout.readline.return_value': 'test_deploy_id'}
127             mock_stdout.configure_mock(**attrs)
128             mock_popen.return_value = mock_stdout
129
130             self.assertEqual(conf_utils.get_verifier_deployment_id(),
131                              'test_deploy_id')
132
133     def test_get_verifier_repo_dir_default(self):
134         with mock.patch('functest.opnfv_tests.openstack.tempest.'
135                         'conf_utils.os.path.join',
136                         return_value='test_verifier_repo_dir'), \
137             mock.patch('functest.opnfv_tests.openstack.tempest.'
138                        'conf_utils.get_verifier_id') as m:
139             self.assertEqual(conf_utils.get_verifier_repo_dir(''),
140                              'test_verifier_repo_dir')
141             self.assertTrue(m.called)
142
143     def test_get_verifier_deployment_dir_default(self):
144         with mock.patch('functest.opnfv_tests.openstack.tempest.'
145                         'conf_utils.os.path.join',
146                         return_value='test_verifier_repo_dir'), \
147             mock.patch('functest.opnfv_tests.openstack.tempest.'
148                        'conf_utils.get_verifier_id') as m1, \
149             mock.patch('functest.opnfv_tests.openstack.tempest.'
150                        'conf_utils.get_verifier_deployment_id') as m2:
151             self.assertEqual(conf_utils.get_verifier_deployment_dir('', ''),
152                              'test_verifier_repo_dir')
153             self.assertTrue(m1.called)
154             self.assertTrue(m2.called)
155
156
157 if __name__ == "__main__":
158     unittest.main(verbosity=2)