Skip to content

Commit f3ae67f

Browse files
committed
Mock getaddrinfo in cinder store tests
With change Ic79815972e654a8bfe2775f57c68cfa0bf115e2f, we started using getaddrinfo method from socket lib to fetch the system IP address (v4 or v6) and pass it to os-brick. The test works for most systems since a valid IP is returned but for some deployments, it fails with the following error. raise socket.gaierror(socket.EAI_NONAME, 'No address found') In any case, it is a good practice to mock it in tests. This patch mocks getaddrinfo in the current cinder store tests. Change-Id: Ib03fefbb6034a7e7d2ff1e16430d28962c2ca355 (cherry picked from commit f40ac40)
1 parent 20110a7 commit f3ae67f

File tree

1 file changed

+15
-5
lines changed

1 file changed

+15
-5
lines changed

glance/tests/functional/v2/test_legacy_update_cinder_store.py

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
# License for the specific language governing permissions and limitations
1414
# under the License.
1515

16+
import socket
1617
from unittest import mock
1718
import uuid
1819

@@ -52,6 +53,8 @@ def setUp(self):
5253
get=lambda v_id: mock.MagicMock(volume_type='fast'),
5354
create=lambda size_gb, name, metadata, volume_type:
5455
self.volume))
56+
fake_ip = '127.0.0.1'
57+
self.fake_socket_return = [[0, 1, 2, 3, [fake_ip]]]
5558

5659
def setup_stores(self):
5760
pass
@@ -138,14 +141,16 @@ def _mock_wait_volume_status(self, volume, status_transition,
138141
@mock.patch.object(cinder, 'open')
139142
@mock.patch('glance_store._drivers.cinder.Store._wait_volume_status')
140143
@mock.patch.object(strutils, 'mask_dict_password')
141-
def test_create_image(self, mock_mask_pass, mock_wait, mock_open,
142-
mock_connector, mock_chown, mocked_cc):
144+
@mock.patch.object(socket, 'getaddrinfo')
145+
def test_create_image(self, mock_host_addr, mock_mask_pass, mock_wait,
146+
mock_open, mock_connector, mock_chown, mocked_cc):
143147
# setup multiple cinder stores
144148
self.setup_multiple_stores()
145149
self.start_server()
146150

147151
mocked_cc.return_value = self.cinder_store_mock
148152
mock_wait.side_effect = self._mock_wait_volume_status
153+
mock_host_addr.return_value = self.fake_socket_return
149154
# create an image
150155
image_id = self._create_and_import(stores=['store1'])
151156
image = self.api_get('/v2/images/%s' % image_id).json
@@ -166,8 +171,9 @@ def test_create_image(self, mock_mask_pass, mock_wait, mock_open,
166171
@mock.patch.object(cinder, 'open')
167172
@mock.patch('glance_store._drivers.cinder.Store._wait_volume_status')
168173
@mock.patch.object(strutils, 'mask_dict_password')
169-
def test_migrate_image_after_upgrade(self, mock_mask_pass, mock_wait,
170-
mock_open, mock_connector,
174+
@mock.patch.object(socket, 'getaddrinfo')
175+
def test_migrate_image_after_upgrade(self, mock_host_addr, mock_mask_pass,
176+
mock_wait, mock_open, mock_connector,
171177
mock_chown, mocked_cc):
172178
"""Test to check if an image is successfully migrated when we
173179
@@ -178,6 +184,7 @@ def test_migrate_image_after_upgrade(self, mock_mask_pass, mock_wait,
178184
self.start_server()
179185
mocked_cc.return_value = self.cinder_store_mock
180186
mock_wait.side_effect = self._mock_wait_volume_status
187+
mock_host_addr.return_value = self.fake_socket_return
181188

182189
# create image in single store
183190
image_id = self._create_and_import(stores=['store1'])
@@ -212,7 +219,9 @@ def test_migrate_image_after_upgrade(self, mock_mask_pass, mock_wait,
212219
@mock.patch.object(cinder, 'open')
213220
@mock.patch('glance_store._drivers.cinder.Store._wait_volume_status')
214221
@mock.patch.object(strutils, 'mask_dict_password')
215-
def test_migrate_image_after_upgrade_not_owner(self, mock_mask_pass,
222+
@mock.patch.object(socket, 'getaddrinfo')
223+
def test_migrate_image_after_upgrade_not_owner(self, mock_host_addr,
224+
mock_mask_pass,
216225
mock_wait, mock_open,
217226
mock_connector,
218227
mock_chown, mocked_cc):
@@ -225,6 +234,7 @@ def test_migrate_image_after_upgrade_not_owner(self, mock_mask_pass,
225234
self.start_server()
226235
mocked_cc.return_value = self.cinder_store_mock
227236
mock_wait.side_effect = self._mock_wait_volume_status
237+
mock_host_addr.return_value = self.fake_socket_return
228238

229239
# create image in single store, owned by someone else
230240
image_id = self._create_and_import(stores=['store1'],

0 commit comments

Comments
 (0)