from mox import IgnoreArg
from mox import IsA
import os
-import socket
from cinder import context
from cinder import exception
def test_check_share_in_use_incorrect_host(self):
drv = self._driver
mox = self.mox
- mox.StubOutWithMock(socket, 'gethostbyname')
- socket.gethostbyname(IgnoreArg()).AndRaise(Exception())
+ mox.StubOutWithMock(drv, '_resolve_hostname')
+ drv._resolve_hostname(IgnoreArg()).AndRaise(Exception())
mox.ReplayAll()
share = drv._check_share_in_use('incorrect:8989', '/dir')
mox.VerifyAll()
drv = self._driver
mox = self.mox
drv._mounted_shares = ['127.0.0.1:/dir/share']
- mox.StubOutWithMock(socket, 'gethostbyname')
+ mox.StubOutWithMock(drv, '_resolve_hostname')
mox.StubOutWithMock(drv, '_share_match_for_ip')
- socket.gethostbyname(IgnoreArg()).AndReturn('10.22.33.44')
+ drv._resolve_hostname(IgnoreArg()).AndReturn('10.22.33.44')
drv._share_match_for_ip(
'10.22.33.44', ['127.0.0.1:/dir/share']).AndReturn('share')
mox.ReplayAll()
try:
if conn:
host = conn.split(':')[0]
- ipv4 = socket.gethostbyname(host)
+ ip = self._resolve_hostname(host)
share_candidates = []
for sh in self._mounted_shares:
sh_exp = sh.split(':')[1]
if share_candidates:
LOG.debug(_('Found possible share matches %s'),
share_candidates)
- return self._share_match_for_ip(ipv4, share_candidates)
+ return self._share_match_for_ip(ip, share_candidates)
except Exception:
LOG.warn(_("Unexpected exception while short listing used share."))
return None
"""Checks if share is compatible with volume to host it."""
raise NotImplementedError()
+ def _resolve_hostname(self, hostname):
+ """Resolves hostname to IP address."""
+ res = socket.getaddrinfo(hostname, None)[0]
+ family, socktype, proto, canonname, sockaddr = res
+ return sockaddr[0]
+
class NetAppDirectNfsDriver (NetAppNFSDriver):
"""Executes commands related to volumes on NetApp filer."""
net_if_iter.add_new_child('max-records', '10')
query = NaElement('query')
net_if_iter.add_child_elem(query)
- query.add_node_with_children('net-interface-info', **{'address': ip})
+ query.add_node_with_children('net-interface-info',
+ **{'address': self._resolve_hostname(ip)})
result = self._invoke_successfully(net_if_iter)
if result.get_child_content('num-records') and\
int(result.get_child_content('num-records')) >= 1:
_('No interface found on cluster for ip %s')
% (ip))
- def _get_verver_ips(self, vserver):
+ def _get_vserver_ips(self, vserver):
"""Get ips for the vserver."""
result = na_utils.invoke_api(
self._client, api_name='net-interface-get-iter',
LOG.warn(_("No shares found hence skipping ssc refresh."))
return
mnt_share_vols = set()
- vs_ifs = self._get_verver_ips(self.vserver)
+ vs_ifs = self._get_vserver_ips(self.vserver)
for vol in vols['all']:
for sh in self._mounted_shares:
host = sh.split(':')[0]
junction = sh.split(':')[1]
- ipv4 = socket.gethostbyname(host)
- if (self._ip_in_ifs(ipv4, vs_ifs) and
+ ip = self._resolve_hostname(host)
+ if (self._ip_in_ifs(ip, vs_ifs) and
junction == vol.id['junction_path']):
mnt_share_vols.add(vol)
vol.export['path'] = sh