3 # Copyright (c) 2017 Ericsson and others.
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
14 from functest.ci import check_deployment
16 __author__ = "Jose Lausuch <jose.lausuch@ericsson.com>"
19 class CheckDeploymentTesting(unittest.TestCase):
20 """The super class which testing classes could inherit."""
21 # pylint: disable=missing-docstring
23 logging.disable(logging.CRITICAL)
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,
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
41 def test_check_rc(self):
42 with mock.patch('functest.ci.check_deployment.os.path.isfile',
44 mock.patch('__builtin__.open',
45 mock.mock_open(read_data='OS_AUTH_URL')):
46 self.deployment.check_rc()
47 self.assertTrue(m.called)
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)
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)
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)
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)
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.'
85 self.deployment.check_public_endpoint()
86 self.assertTrue(m.called)
87 self.assertTrue(n.called)
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.'
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)
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)
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.'
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,
122 self.assertTrue(m.called)
123 self.assertTrue(n.called)
124 self.assertTrue(msg in context)
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)
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)
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)
147 def test_check_neutron_fail(self):
148 with mock.patch('functest.ci.check_deployment.neutron_utils.'
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)
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)
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)
174 if __name__ == "__main__":
175 logging.disable(logging.CRITICAL)
176 unittest.main(verbosity=2)