1f44d07870fadca4abb152da07266944ed387a43
[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('__builtin__.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('__builtin__.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             self.deployment.check_auth_endpoint()
68             self.assertTrue(m.called)
69
70     def test_check_auth_endpoint_not_reachable(self):
71         with mock.patch('functest.ci.check_deployment.verify_connectivity',
72                         return_value=False) as m, \
73                 self.assertRaises(Exception) as context:
74             endpoint = self.os_creds.auth_url
75             self.deployment.check_auth_endpoint()
76             msg = "OS_AUTH_URL {} is not reachable.".format(endpoint)
77             self.assertTrue(m.called)
78             self.assertTrue(msg in context)
79
80     def test_check_public_endpoint(self):
81         with mock.patch('functest.ci.check_deployment.verify_connectivity',
82                         return_value=True) as m, \
83                 mock.patch('functest.ci.check_deployment.keystone_utils.'
84                            'get_endpoint') as n:
85             self.deployment.check_public_endpoint()
86             self.assertTrue(m.called)
87             self.assertTrue(n.called)
88
89     def test_check_public_endpoint_not_reachable(self):
90         with mock.patch('functest.ci.check_deployment.verify_connectivity',
91                         return_value=False) as m, \
92                 mock.patch('functest.ci.check_deployment.keystone_utils.'
93                            'get_endpoint',
94                            return_value=self.endpoint_test) as n, \
95                 self.assertRaises(Exception) as context:
96             self.deployment.check_public_endpoint()
97             msg = ("Public endpoint {} is not reachable."
98                    .format(self.mock_endpoint))
99             self.assertTrue(m.called)
100             self.assertTrue(n.called)
101             self.assertTrue(msg in context)
102
103     def test_check_service_endpoint(self):
104         with mock.patch('functest.ci.check_deployment.verify_connectivity',
105                         return_value=True) as m, \
106                 mock.patch('functest.ci.check_deployment.keystone_utils.'
107                            'get_endpoint') as n:
108             self.deployment.check_service_endpoint(self.service_test)
109             self.assertTrue(m.called)
110             self.assertTrue(n.called)
111
112     def test_check_service_endpoint_not_reachable(self):
113         with mock.patch('functest.ci.check_deployment.verify_connectivity',
114                         return_value=False) as m, \
115                 mock.patch('functest.ci.check_deployment.keystone_utils.'
116                            'get_endpoint',
117                            return_value=self.endpoint_test) as n, \
118                 self.assertRaises(Exception) as context:
119             self.deployment.check_service_endpoint(self.service_test)
120             msg = "{} endpoint {} is not reachable.".format(self.service_test,
121                                                             self.endpoint_test)
122             self.assertTrue(m.called)
123             self.assertTrue(n.called)
124             self.assertTrue(msg in context)
125
126     def test_check_nova(self):
127         with mock.patch('functest.ci.check_deployment.nova_utils.nova_client',
128                         return_value=self.client_test) as m:
129             self.deployment.check_nova()
130             self.assertTrue(m.called)
131
132     def test_check_nova_fail(self):
133         with mock.patch('functest.ci.check_deployment.nova_utils.nova_client',
134                         return_value=self.client_test) as m, \
135                 mock.patch.object(self.client_test, 'servers.list',
136                                   side_effect=Exception):
137             self.deployment.check_nova()
138             self.assertTrue(m.called)
139             self.assertRaises(Exception)
140
141     def test_check_neutron(self):
142         with mock.patch('functest.ci.check_deployment.neutron_utils.'
143                         'neutron_client', return_value=self.client_test) as m:
144             self.deployment.check_neutron()
145             self.assertTrue(m.called)
146
147     def test_check_neutron_fail(self):
148         with mock.patch('functest.ci.check_deployment.neutron_utils.'
149                         'neutron_client',
150                         return_value=self.client_test) as m, \
151                 mock.patch.object(self.client_test, 'list_networks',
152                                   side_effect=Exception), \
153                 self.assertRaises(Exception):
154             self.deployment.check_neutron()
155             self.assertRaises(Exception)
156             self.assertTrue(m.called)
157
158     def test_check_glance(self):
159         with mock.patch('functest.ci.check_deployment.glance_utils.'
160                         'glance_client', return_value=self.client_test) as m:
161             self.deployment.check_glance()
162             self.assertTrue(m.called)
163
164     def test_check_glance_fail(self):
165         with mock.patch('functest.ci.check_deployment.glance_utils.'
166                         'glance_client', return_value=self.client_test) as m, \
167                 mock.patch.object(self.client_test, 'images.list',
168                                   side_effect=Exception):
169             self.deployment.check_glance()
170             self.assertRaises(Exception)
171             self.assertTrue(m.called)
172
173
174 if __name__ == "__main__":
175     logging.disable(logging.CRITICAL)
176     unittest.main(verbosity=2)