Fix checking IP version when using IPv6.

This commit fixes an issue with ceilometer incorrectly determining the
IP version to use.  Previously, it checked whether the hostname was a
valid IPv6 address, but it should be resolving first to check whether
that is a valid IP address instead.

tests: Created test case that enables creating a UDPPublisher with
hostname for ipv4 and ipv6.

Closes-bug: #1616496

Change-Id: I586104fe69a03e64a0fb7572442b3175f880d80d
Co-authored-by: Joseph Richard <Joseph.Richard@windriver.com>
This commit is contained in:
Amy Fong 2016-08-24 11:24:38 -04:00
parent 683069a150
commit e41adba727
2 changed files with 38 additions and 3 deletions

View File

@ -23,7 +23,7 @@ from oslo_log import log
from oslo_utils import netutils
import ceilometer
from ceilometer.i18n import _
from ceilometer.i18n import _, _LW
from ceilometer import publisher
from ceilometer.publisher import utils
@ -38,9 +38,22 @@ class UDPPublisher(publisher.PublisherBase):
self.host, self.port = netutils.parse_host_port(
parsed_url.netloc,
default_port=cfg.CONF.collector.udp_port)
if netutils.is_valid_ipv6(self.host):
addr_family = socket.AF_INET6
addrinfo = None
try:
addrinfo = socket.getaddrinfo(self.host, None, socket.AF_INET6,
socket.SOCK_DGRAM)[0]
except socket.gaierror:
try:
addrinfo = socket.getaddrinfo(self.host, None, socket.AF_INET,
socket.SOCK_DGRAM)[0]
except socket.gaierror:
pass
if addrinfo:
addr_family = addrinfo[0]
else:
LOG.warning(_LW(
"Cannot resolve host %s, creating AF_INET socket..."),
self.host)
addr_family = socket.AF_INET
self.socket = socket.socket(addr_family,
socket.SOCK_DGRAM)

View File

@ -127,6 +127,28 @@ class TestUDPPublisher(base.BaseTestCase):
self._check_udp_socket('udp://[::1]:4952',
socket.AF_INET6)
def test_publisher_udp_socket_ipv4_hostname(self):
host = "ipv4.google.com"
try:
socket.getaddrinfo(host, None,
socket.AF_INET,
socket.SOCK_DGRAM)
except socket.gaierror:
self.skipTest("cannot resolve not running test")
url = "udp://"+host+":4952"
self._check_udp_socket(url, socket.AF_INET)
def test_publisher_udp_socket_ipv6_hostname(self):
host = "ipv6.google.com"
try:
socket.getaddrinfo(host, None,
socket.AF_INET6,
socket.SOCK_DGRAM)
except socket.gaierror:
self.skipTest("cannot resolve not running test")
url = "udp://"+host+":4952"
self._check_udp_socket(url, socket.AF_INET6)
def test_published(self):
self.data_sent = []
with mock.patch('socket.socket',