diff --git a/ceilometer/publisher/udp.py b/ceilometer/publisher/udp.py index 718161f07..4320b39a6 100644 --- a/ceilometer/publisher/udp.py +++ b/ceilometer/publisher/udp.py @@ -38,7 +38,11 @@ class UDPPublisher(publisher.PublisherBase): self.host, self.port = netutils.parse_host_port( parsed_url.netloc, default_port=cfg.CONF.collector.udp_port) - self.socket = socket.socket(socket.AF_INET, + if netutils.is_valid_ipv6(self.host): + addr_family = socket.AF_INET6 + else: + addr_family = socket.AF_INET + self.socket = socket.socket(addr_family, socket.SOCK_DGRAM) def publish_samples(self, context, samples): diff --git a/ceilometer/tests/publisher/test_udp.py b/ceilometer/tests/publisher/test_udp.py index 0f6ad0598..0461fa859 100644 --- a/ceilometer/tests/publisher/test_udp.py +++ b/ceilometer/tests/publisher/test_udp.py @@ -16,6 +16,7 @@ """ import datetime +import socket import mock import msgpack @@ -112,6 +113,20 @@ class TestUDPPublisher(base.BaseTestCase): self.CONF = self.useFixture(fixture_config.Config()).conf self.CONF.publisher.telemetry_secret = 'not-so-secret' + def _check_udp_socket(self, url, expected_addr_family): + with mock.patch.object(socket, 'socket') as mock_socket: + udp.UDPPublisher(netutils.urlsplit(url)) + mock_socket.assert_called_with(expected_addr_family, + socket.SOCK_DGRAM) + + def test_publisher_udp_socket_ipv4(self): + self._check_udp_socket('udp://127.0.0.1:4952', + socket.AF_INET) + + def test_publisher_udp_socket_ipv6(self): + self._check_udp_socket('udp://[::1]:4952', + socket.AF_INET6) + def test_published(self): self.data_sent = [] with mock.patch('socket.socket',