#! /usr/bin/python3 # SPDX-License-Identifier: GPL-2.0-or-later # # avocado/pasta_dhcp.py - Test DHCP & DHCPv6 with pasta # # Copyright Red Hat # Author: David Gibson import ipaddress import json import common from pasta import PastaUnconfiguredTest class PastaDhcpTest(PastaUnconfiguredTest): DHCLIENT = '/sbin/dhclient' def setUp(self): super().setUp() # Allow namespaced dhclient to write lease & pid files self.innerx('mount -t tmpfs none /var/lib/dhclient', sudo=True) self.innerx('mount -t tmpfs none /var/run', sudo=True) # We need '-nc' because we're running with capabilities but # not UID 0. Without -nc dhclient drops capabilities before # invoking dhclient-script, so it's unable to actually # configure the interface self.innerx('{} -4 -v -nc {}'.format(self.DHCLIENT, self.IFNAME), sudo=True) def test_addr(self): ifinfo = self.innerx('ip -4 -j addr show {}'.format(self.IFNAME)) ifinfo = json.loads(ifinfo)[0] self.assertEquals(len(ifinfo['addr_info']), 1) adinfo = ifinfo['addr_info'][0] addr = ipaddress.ip_address(adinfo['local']) self.assertEquals(addr, self.OUTER_IP4) self.shutdown() def test_route(self): rinfo = self.innerx('ip -j -4 route show') rinfo = json.loads(rinfo) for route in rinfo: if route['dst'] != 'default': continue gateway = ipaddress.ip_address(route['gateway']) self.assertEquals(gateway, self.GW_IP4) self.shutdown() def test_mtu(self): ifinfo = self.innerx('ip -j link show {}'.format(self.IFNAME)) ifinfo = json.loads(ifinfo)[0] self.assertEquals(ifinfo['mtu'], 65520) self.shutdown() class PastaDhcpv6Test(PastaUnconfiguredTest): DHCLIENT = '/sbin/dhclient' timeout = 10.0 def setUp(self): super().setUp() # Allow namespaced dhclient to write lease & pid files self.innerx('mount -t tmpfs none /var/lib/dhclient', sudo=True) self.innerx('mount -t tmpfs none /var/run', sudo=True) # We need '-nc' because we're running with capabilities but # not UID 0. Without -nc dhclient drops capabilities before # invoking dhclient-script, so it's unable to actually # configure the interface self.innerx('{} -6 -nc -v {}'.format(self.DHCLIENT, self.IFNAME), sudo=True) def test_addr(self): ifinfo = self.innerx('ip -6 -j addr show {}'.format(self.IFNAME)) ifinfo = json.loads(ifinfo)[0] for adinfo in ifinfo['addr_info']: if adinfo['scope'] == 'global' and not 'dynamic' in adinfo: global_addr = ipaddress.ip_address(adinfo['local']) self.assertEquals(global_addr, self.OUTER_IP6) self.shutdown() def test_route(self): rinfo = self.innerx('ip -j -6 route show') rinfo = json.loads(rinfo) for route in rinfo: if route['dst'] != 'default': continue gateway = ipaddress.ip_address(route['gateway']) self.assertEquals(gateway, self.gw_ll) self.shutdown()