if found:
return addresses
+ @staticmethod
+ def get_ip_neutron(instance_id):
+ subnets = json.loads(misc.sh("neutron subnet-list -f json -c id -c ip_version"))
+ subnet_id = None
+ for subnet in subnets:
+ if subnet['ip_version'] == 4:
+ subnet_id = subnet['id']
+ break
+ if not subnet_id:
+ raise Exception("no subnet with ip_version == 4")
+ ports = json.loads(misc.sh("neutron port-list -f json -c fixed_ips -c device_id"))
+ fixed_ips = None
+ for port in ports:
+ if port['device_id'] == instance_id:
+ fixed_ips = port['fixed_ips'].split("\n")
+ break
+ if not fixed_ips:
+ raise Exception("no fixed ip record found")
+ ip = None
+ for fixed_ip in fixed_ips:
+ record = json.loads(fixed_ip)
+ if record['subnet_id'] == subnet_id:
+ ip = record['ip_address']
+ break
+ if not ip:
+ raise Exception("no ip")
+ return ip
+
def get_ip(self, instance_id, network):
"""
- Return the private IP of the OpenStack instance_id. The network,
- if not the empty string, disambiguate multiple networks attached
- to the instance.
+ Return the private IP of the OpenStack instance_id.
"""
- return re.findall(network + '=([\d.]+)',
- self.get_addresses(instance_id))[0]
+ try:
+ return self.get_ip_neutron(instance_id)
+ except Exception as e:
+ log.debug("ignoring get_ip_neutron exception " + str(e))
+ return re.findall(network + '=([\d.]+)',
+ self.get_addresses(instance_id))[0]
class TeuthologyOpenStack(OpenStack):
import os
import pytest
import tempfile
+from mock import patch
import teuthology
from teuthology import misc
else:
del os.environ['OS_AUTH_URL']
+ def test_get_ip_neutron(self):
+ instance_id = '8e1fd70a-3065-46f8-9c30-84dc028c1834'
+ ip = '10.10.10.4'
+ def sh(cmd):
+ if 'neutron subnet-list' in cmd:
+ return """
+[
+ {
+ "ip_version": 6,
+ "id": "c45b9661-b2ba-4817-9e3a-f8f63bf32989"
+ },
+ {
+ "ip_version": 4,
+ "id": "e03a3dbc-afc8-4b52-952e-7bf755397b50"
+ }
+]
+ """
+ elif 'neutron port-list' in cmd:
+ return ("""
+[
+ {
+ "device_id": "915504ad-368b-4cce-be7c-4f8a83902e28",
+ "fixed_ips": "{\\"subnet_id\\": \\"e03a3dbc-afc8-4b52-952e-7bf755397b50\\", \\"ip_address\\": \\"10.10.10.1\\"}\\n{\\"subnet_id\\": \\"c45b9661-b2ba-4817-9e3a-f8f63bf32989\\", \\"ip_address\\": \\"2607:f298:6050:9afc::1\\"}"
+ },
+ {
+ "device_id": "{instance_id}",
+ "fixed_ips": "{\\"subnet_id\\": \\"e03a3dbc-afc8-4b52-952e-7bf755397b50\\", \\"ip_address\\": \\"{ip}\\"}\\n{\\"subnet_id\\": \\"c45b9661-b2ba-4817-9e3a-f8f63bf32989\\", \\"ip_address\\": \\"2607:f298:6050:9afc:f816:3eff:fe07:76c1\\"}"
+ },
+ {
+ "device_id": "17e4a968-4caa-4cee-8e4b-f950683a02bd",
+ "fixed_ips": "{\\"subnet_id\\": \\"e03a3dbc-afc8-4b52-952e-7bf755397b50\\", \\"ip_address\\": \\"10.10.10.5\\"}\\n{\\"subnet_id\\": \\"c45b9661-b2ba-4817-9e3a-f8f63bf32989\\", \\"ip_address\\": \\"2607:f298:6050:9afc:f816:3eff:fe9c:37f0\\"}"
+ }
+]
+ """.replace('{instance_id}', instance_id).
+ replace('{ip}', ip))
+ else:
+ raise Exception("unexpected " + cmd)
+ with patch.multiple(
+ misc,
+ sh=sh,
+ ):
+ assert ip == OpenStack.get_ip_neutron(instance_id)
+
class TestTeuthologyOpenStack(object):
@classmethod