Get auth token when checking deployment
[functest.git] / functest / tests / unit / ci / test_check_deployment.py
1 #!/usr/bin/env python
2
3 # Copyright (c) 2017 Ericsson 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 import logging
11 import mock
12 import unittest
13
14 from functest.ci import check_deployment
15
16 __author__ = "Jose Lausuch <jose.lausuch@ericsson.com>"
17
18
19 class CheckDeploymentTesting(unittest.TestCase):
20     """The super class which testing classes could inherit."""
21     # pylint: disable=missing-docstring
22
23     logging.disable(logging.CRITICAL)
24
25     def setUp(self):
26         self.client_test = mock.Mock()
27         self.deployment = check_deployment.CheckDeployment()
28         self.service_test = 'compute'
29         self.rc_file = self.deployment.rc_file
30         self.endpoint_test = 'http://192.168.0.6:5000/v3'
31         creds_attr = {'auth_url': self.endpoint_test,
32                       'proxy_settings': ''}
33         proxy_attr = {'host': '192.168.0.1', 'port': '5000'}
34         proxy_settings = mock.Mock()
35         proxy_settings.configure_mock(**proxy_attr)
36         self.os_creds = mock.Mock()
37         self.os_creds.configure_mock(**creds_attr)
38         self.os_creds.proxy_settings = proxy_settings
39         self.deployment.os_creds = self.os_creds
40
41     def test_check_rc(self):
42         with mock.patch('functest.ci.check_deployment.os.path.isfile',
43                         returns=True) as m, \
44                 mock.patch('six.moves.builtins.open',
45                            mock.mock_open(read_data='OS_AUTH_URL')):
46             self.deployment.check_rc()
47             self.assertTrue(m.called)
48
49     def test_check_rc_missing_file(self):
50         with mock.patch('functest.ci.check_deployment.os.path.isfile',
51                         return_value=False), \
52                 self.assertRaises(Exception) as context:
53             msg = 'RC file {} does not exist!'.format(self.rc_file)
54             self.deployment.check_rc(self.rc_file)
55             self.assertTrue(msg in context)
56
57     def test_check_rc_missing_os_auth(self):
58         with mock.patch('six.moves.builtins.open',
59                         mock.mock_open(read_data='test')), \
60                 self.assertRaises(Exception) as context:
61             msg = 'OS_AUTH_URL not defined in {}.'.format(self.rc_file)
62             self.assertTrue(msg in context)
63
64     def test_check_auth_endpoint(self):
65         with mock.patch('functest.ci.check_deployment.verify_connectivity',
66                         return_value=True) as m,\
67                 mock.patch('functest.ci.check_deployment.get_auth_token',
68                            return_value='gAAAAABaOhXGS') as mock_token:
69             self.deployment.check_auth_endpoint()
70             self.assertTrue(m.called)
71             self.assertTrue(mock_token.called)
72
73     def test_check_auth_endpoint_not_reachable(self):
74         with mock.patch('functest.ci.check_deployment.verify_connectivity',
75                         return_value=False) as m, \
76                 self.assertRaises(Exception) as context:
77             endpoint = self.os_creds.auth_url
78             self.deployment.check_auth_endpoint()
79             msg = "OS_AUTH_URL {} is not reachable.".format(endpoint)
80             self.assertTrue(m.called)
81             self.assertTrue(msg in context)
82
83     def test_check_public_endpoint(self):
84         with mock.patch('functest.ci.check_deployment.verify_connectivity',
85                         return_value=True) as m, \
86                 mock.patch('functest.ci.check_deployment.keystone_utils.'
87                            'get_endpoint') as n:
88             self.deployment.check_public_endpoint()
89             self.assertTrue(m.called)
90             self.assertTrue(n.called)
91
92     def test_check_public_endpoint_not_reachable(self):
93         with mock.patch('functest.ci.check_deployment.verify_connectivity',
94                         return_value=False) as m, \
95                 mock.patch('functest.ci.check_deployment.keystone_utils.'
96                            'get_endpoint',
97                            return_value=self.endpoint_test) as n, \
98                 self.assertRaises(Exception) as context:
99             self.deployment.check_public_endpoint()
100             msg = ("Public endpoint {} is not reachable."
101                    .format(self.mock_endpoint))
102             self.assertTrue(m.called)
103             self.assertTrue(n.called)
104             self.assertTrue(msg in context)
105
106     def test_check_service_endpoint(self):
107         with mock.patch('functest.ci.check_deployment.verify_connectivity',
108                         return_value=True) as m, \
109                 mock.patch('functest.ci.check_deployment.keystone_utils.'
110                            'get_endpoint') as n:
111             self.deployment.check_service_endpoint(self.service_test)
112             self.assertTrue(m.called)
113             self.assertTrue(n.called)
114
115     def test_check_service_endpoint_not_reachable(self):
116         with mock.patch('functest.ci.check_deployment.verify_connectivity',
117                         return_value=False) as m, \
118                 mock.patch('functest.ci.check_deployment.keystone_utils.'
119                            'get_endpoint',
120                            return_value=self.endpoint_test) as n, \
121                 self.assertRaises(Exception) as context:
122             self.deployment.check_service_endpoint(self.service_test)
123             msg = "{} endpoint {} is not reachable.".format(self.service_test,
124                                                             self.endpoint_test)
125             self.assertTrue(m.called)
126             self.assertTrue(n.called)
127             self.assertTrue(msg in context)
128
129     def test_check_nova(self):
130         with mock.patch('functest.ci.check_deployment.nova_utils.nova_client',
131                         return_value=self.client_test) as m:
132             self.deployment.check_nova()
133             self.assertTrue(m.called)
134
135     def test_check_nova_fail(self):
136         with mock.patch('functest.ci.check_deployment.nova_utils.nova_client',
137                         return_value=self.client_test) as m, \
138                 mock.patch.object(self.client_test, 'servers.list',
139                                   side_effect=Exception):
140             self.deployment.check_nova()
141             self.assertTrue(m.called)
142             self.assertRaises(Exception)
143
144     def test_check_neutron(self):
145         with mock.patch('functest.ci.check_deployment.neutron_utils.'
146                         'neutron_client', return_value=self.client_test) as m:
147             self.deployment.check_neutron()
148             self.assertTrue(m.called)
149
150     def test_check_neutron_fail(self):
151         with mock.patch('functest.ci.check_deployment.neutron_utils.'
152                         'neutron_client',
153                         return_value=self.client_test) as m, \
154                 mock.patch.object(self.client_test, 'list_networks',
155                                   side_effect=Exception), \
156                 self.assertRaises(Exception):
157             self.deployment.check_neutron()
158             self.assertRaises(Exception)
159             self.assertTrue(m.called)
160
161     def test_check_glance(self):
162         with mock.patch('functest.ci.check_deployment.glance_utils.'
163                         'glance_client', return_value=self.client_test) as m:
164             self.deployment.check_glance()
165             self.assertTrue(m.called)
166
167     def test_check_glance_fail(self):
168         with mock.patch('functest.ci.check_deployment.glance_utils.'
169                         'glance_client', return_value=self.client_test) as m, \
170                 mock.patch.object(self.client_test, 'images.list',
171                                   side_effect=Exception):
172             self.deployment.check_glance()
173             self.assertRaises(Exception)
174             self.assertTrue(m.called)
175
176     @mock.patch('functest.ci.check_deployment.LOGGER.info')
177     @mock.patch('functest.opnfv_tests.openstack.snaps.snaps_utils.'
178                 'get_ext_net_name')
179     def test_check_extnet(self, mock_getext, mock_loginfo):
180         test_network = 'ext-net'
181         mock_getext.return_value = test_network
182         self.deployment.check_ext_net()
183         self.assertTrue(mock_getext.called)
184         mock_loginfo.assert_called_once_with(
185             "External network found: %s", test_network)
186
187     @mock.patch('functest.opnfv_tests.openstack.snaps.snaps_utils.'
188                 'get_ext_net_name', return_value='')
189     def test_check_extnet_None(self, mock_getext):
190         with self.assertRaises(Exception) as context:
191             self.deployment.check_ext_net()
192             self.assertTrue(mock_getext.called)
193             msg = 'ERROR: No external networks in the deployment.'
194             self.assertTrue(msg in context)
195
196
197 if __name__ == "__main__":
198     logging.disable(logging.CRITICAL)
199     unittest.main(verbosity=2)