def write_vip_interface_file(self, interface_file_path, primary_interface, vip, ip, broadcast, netmask, gateway, mtu, vrrp_ip, vrrp_version, render_host_routes, template_vip): # write interface file mode = stat.S_IRUSR | stat.S_IWUSR | stat.S_IRGRP | stat.S_IROTH # If we are using a consolidated interfaces file, just append # otherwise clear the per interface file as we are rewriting it # TODO(johnsom): We need a way to clean out old interfaces records if CONF.amphora_agent.agent_server_network_file: flags = os.O_WRONLY | os.O_CREAT | os.O_APPEND else: flags = os.O_WRONLY | os.O_CREAT | os.O_TRUNC with os.fdopen(os.open(interface_file_path, flags, mode), 'w') as text_file: text = template_vip.render( interface=primary_interface, vip=vip, vip_ipv6=ip.version is 6, prefix=utils.netmask_to_prefix(netmask), broadcast=broadcast, netmask=netmask, gateway=gateway, mtu=mtu, vrrp_ip=vrrp_ip, vrrp_ipv6=vrrp_version is 6, host_routes=render_host_routes, ) text_file.write(text)
def write_vip_interface_file(self, interface_file_path, interface, vip, ip, broadcast, netmask, gateway, mtu, auxiliary_ip, auxiliary_version, render_host_routes, template_vip): # write interface file mode = stat.S_IRUSR | stat.S_IWUSR | stat.S_IRGRP | stat.S_IROTH # If we are using a consolidated interfaces file, just append # otherwise clear the per interface file as we are rewriting it # TODO(johnsom): We need a way to clean out old interfaces records if CONF.amphora_agent.agent_server_network_file: flags = os.O_WRONLY | os.O_CREAT | os.O_APPEND else: flags = os.O_WRONLY | os.O_CREAT | os.O_TRUNC with os.fdopen(os.open(interface_file_path, flags, mode), 'w') as text_file: text = template_vip.render( consts=consts, interface=interface, vip=vip, ip=ip, vip_ipv6=ip.version == 6, # For ipv6 the netmask is already the prefix prefix=(netmask if ip.version == 6 else utils.netmask_to_prefix(netmask)), broadcast=broadcast, netmask=netmask, gateway=gateway, network=utils.ip_netmask_to_cidr(vip, netmask), mtu=mtu, auxiliary_ip=auxiliary_ip, auxiliary_ipv6=auxiliary_version == 6, host_routes=render_host_routes, topology=CONF.controller_worker.loadbalancer_topology, ) text_file.write(text)
def write_vip_interface_file(self, interface_file_path, primary_interface, vip, ip, broadcast, netmask, gateway, mtu, vrrp_ip, vrrp_version, render_host_routes, template_vip): # write interface file mode = stat.S_IRUSR | stat.S_IWUSR | stat.S_IRGRP | stat.S_IROTH # If we are using a consolidated interfaces file, just append # otherwise clear the per interface file as we are rewriting it # TODO(johnsom): We need a way to clean out old interfaces records if CONF.amphora_agent.agent_server_network_file: flags = os.O_WRONLY | os.O_CREAT | os.O_APPEND else: flags = os.O_WRONLY | os.O_CREAT | os.O_TRUNC with os.fdopen(os.open(interface_file_path, flags, mode), 'w') as text_file: text = template_vip.render( consts=consts, interface=primary_interface, vip=vip, vip_ipv6=ip.version == 6, # For ipv6 the netmask is already the prefix prefix=(netmask if ip.version == 6 else utils.netmask_to_prefix(netmask)), broadcast=broadcast, netmask=netmask, gateway=gateway, network=utils.ip_netmask_to_cidr(vip, netmask), mtu=mtu, vrrp_ip=vrrp_ip, vrrp_ipv6=vrrp_version == 6, host_routes=render_host_routes, topology=CONF.controller_worker.loadbalancer_topology, ) text_file.write(text)
def test_write_vip_interface_file(self): netns_interface = u'eth1234' FIXED_IP = u'192.0.2.2' SUBNET_CIDR = u'192.0.2.0/24' GATEWAY = u'192.51.100.1' DEST1 = u'198.51.100.0/24' DEST2 = u'203.0.113.0/24' NEXTHOP = u'192.0.2.1' MTU = 1450 FIXED_IP_IPV6 = u'2001:0db8:0000:0000:0000:0000:0000:0001' # Subnet prefix is purposefully not 32, because that coincidentally # matches the result of any arbitrary IPv4->prefixlen conversion SUBNET_CIDR_IPV6 = u'2001:db8::/70' ip = ipaddress.ip_address(FIXED_IP) network = ipaddress.ip_network(SUBNET_CIDR) broadcast = network.broadcast_address.exploded netmask = network.netmask.exploded netmask_prefix = utils.netmask_to_prefix(netmask) ipv6 = ipaddress.ip_address(FIXED_IP_IPV6) networkv6 = ipaddress.ip_network(SUBNET_CIDR_IPV6) broadcastv6 = networkv6.broadcast_address.exploded netmaskv6 = networkv6.prefixlen host_routes = [{ 'gw': NEXTHOP, 'network': ipaddress.ip_network(DEST1) }, { 'gw': NEXTHOP, 'network': ipaddress.ip_network(DEST2) }] path = self.ubuntu_os_util.get_network_interface_file(netns_interface) mock_open = self.useFixture(test_utils.OpenFixture(path)).mock_open mock_template = mock.MagicMock() # Test an IPv4 VIP with mock.patch('os.open'), mock.patch.object(os, 'fdopen', mock_open): self.ubuntu_os_util.write_vip_interface_file( interface_file_path=path, primary_interface=netns_interface, vip=FIXED_IP, ip=ip, broadcast=broadcast, netmask=netmask, gateway=GATEWAY, mtu=MTU, vrrp_ip=None, vrrp_version=None, render_host_routes=host_routes, template_vip=mock_template) mock_template.render.assert_called_once_with( consts=consts, interface=netns_interface, vip=FIXED_IP, vip_ipv6=False, prefix=netmask_prefix, broadcast=broadcast, netmask=netmask, gateway=GATEWAY, network=SUBNET_CIDR, mtu=MTU, vrrp_ip=None, vrrp_ipv6=False, host_routes=host_routes, topology="SINGLE", ) # Now test with an IPv6 VIP mock_template.reset_mock() with mock.patch('os.open'), mock.patch.object(os, 'fdopen', mock_open): self.ubuntu_os_util.write_vip_interface_file( interface_file_path=path, primary_interface=netns_interface, vip=FIXED_IP_IPV6, ip=ipv6, broadcast=broadcastv6, netmask=netmaskv6, gateway=GATEWAY, mtu=MTU, vrrp_ip=None, vrrp_version=None, render_host_routes=host_routes, template_vip=mock_template) mock_template.render.assert_called_once_with( consts=consts, interface=netns_interface, vip=FIXED_IP_IPV6, vip_ipv6=True, prefix=netmaskv6, broadcast=broadcastv6, netmask=netmaskv6, gateway=GATEWAY, network=SUBNET_CIDR_IPV6, mtu=MTU, vrrp_ip=None, vrrp_ipv6=False, host_routes=host_routes, topology="SINGLE", )
def test_write_vip_interface_file(self): netns_interface = u'eth1234' FIXED_IP = u'192.0.2.2' SUBNET_CIDR = u'192.0.2.0/24' GATEWAY = u'192.51.100.1' DEST1 = u'198.51.100.0/24' DEST2 = u'203.0.113.0/24' NEXTHOP = u'192.0.2.1' MTU = 1450 FIXED_IP_IPV6 = u'2001:0db8:0000:0000:0000:0000:0000:0001' # Subnet prefix is purposefully not 32, because that coincidentally # matches the result of any arbitrary IPv4->prefixlen conversion SUBNET_CIDR_IPV6 = u'2001:db8::/70' ip = ipaddress.ip_address(FIXED_IP) network = ipaddress.ip_network(SUBNET_CIDR) broadcast = network.broadcast_address.exploded netmask = network.netmask.exploded netmask_prefix = utils.netmask_to_prefix(netmask) ipv6 = ipaddress.ip_address(FIXED_IP_IPV6) networkv6 = ipaddress.ip_network(SUBNET_CIDR_IPV6) broadcastv6 = networkv6.broadcast_address.exploded netmaskv6 = networkv6.prefixlen host_routes = [ {'gw': NEXTHOP, 'network': ipaddress.ip_network(DEST1)}, {'gw': NEXTHOP, 'network': ipaddress.ip_network(DEST2)} ] path = self.ubuntu_os_util.get_network_interface_file(netns_interface) mock_open = self.useFixture(test_utils.OpenFixture(path)).mock_open mock_template = mock.MagicMock() # Test an IPv4 VIP with mock.patch('os.open'), mock.patch.object( os, 'fdopen', mock_open): self.ubuntu_os_util.write_vip_interface_file( interface_file_path=path, primary_interface=netns_interface, vip=FIXED_IP, ip=ip, broadcast=broadcast, netmask=netmask, gateway=GATEWAY, mtu=MTU, vrrp_ip=None, vrrp_version=None, render_host_routes=host_routes, template_vip=mock_template) mock_template.render.assert_called_once_with( consts=consts, interface=netns_interface, vip=FIXED_IP, vip_ipv6=False, prefix=netmask_prefix, broadcast=broadcast, netmask=netmask, gateway=GATEWAY, network=SUBNET_CIDR, mtu=MTU, vrrp_ip=None, vrrp_ipv6=False, host_routes=host_routes, topology="SINGLE", ) # Now test with an IPv6 VIP mock_template.reset_mock() with mock.patch('os.open'), mock.patch.object( os, 'fdopen', mock_open): self.ubuntu_os_util.write_vip_interface_file( interface_file_path=path, primary_interface=netns_interface, vip=FIXED_IP_IPV6, ip=ipv6, broadcast=broadcastv6, netmask=netmaskv6, gateway=GATEWAY, mtu=MTU, vrrp_ip=None, vrrp_version=None, render_host_routes=host_routes, template_vip=mock_template) mock_template.render.assert_called_once_with( consts=consts, interface=netns_interface, vip=FIXED_IP_IPV6, vip_ipv6=True, prefix=netmaskv6, broadcast=broadcastv6, netmask=netmaskv6, gateway=GATEWAY, network=SUBNET_CIDR_IPV6, mtu=MTU, vrrp_ip=None, vrrp_ipv6=False, host_routes=host_routes, topology="SINGLE", )
def test_netmask_to_prefix(self): self.assertEqual(utils.netmask_to_prefix('255.0.0.0'), 8) self.assertEqual(utils.netmask_to_prefix('255.255.0.0'), 16) self.assertEqual(utils.netmask_to_prefix('255.255.255.0'), 24) self.assertEqual(utils.netmask_to_prefix('255.255.255.128'), 25)