#! /usr/bin/env avocado-runner-avocado-classless # SPDX-License-Identifier: GPL-2.0-or-later # # Copyright Red Hat # Author: David Gibson """ Test A Simple Socket Transport dhcpv6.py - Helpers for testing DHCPv6 """ import contextlib import os from avocado_classless.test import assert_in, test_output from tasst.address import IpiAllocator, TEST_NET6_TASST_A from tasst.dhcp import dhclient, setup_dhcpd_common def dhclientv6(site, ifname): return dhclient(site, ifname, '6') def test_dhcpv6(ifname, expected_addr): def dec(setupfn): def test_addr(setup): with setup as site, dhclientv6(site, ifname): addrs = [a.ip for a in site.addrs(ifname, family='inet6', scope='global')] assert_in(expected_addr, addrs) # Might also have a SLAAC address return test_output(test_addr)(setupfn) return dec DHCPD = 'dhcpd' SUBNET = TEST_NET6_TASST_A ipa = IpiAllocator(SUBNET) (SERVER_IP6,) = ipa.next_ipis() (CLIENT_IP6,) = ipa.next_ipis() IFNAME = 'clientif' @test_dhcpv6(IFNAME, CLIENT_IP6.ip) @contextlib.contextmanager def setup_dhcpdv6(): server_ifname = 'serverif' with setup_dhcpd_common(IFNAME, server_ifname) as (client, server, tmpdir): # Sort out link local addressing server.ifup('lo') server.ifup(server_ifname, SERVER_IP6) client.ifup('lo') client.ifup(IFNAME) server.addr_wait(server_ifname, family='inet6', scope='link') # Configure the DHCP server confpath = os.path.join(tmpdir, 'dhcpd.conf') open(confpath, 'w', encoding='UTF-8').write( f'''subnet6 {SUBNET} {{ range6 {CLIENT_IP6.ip} {CLIENT_IP6.ip}; }}''') pidfile = os.path.join(tmpdir, 'dhcpd.pid') leasepath = os.path.join(tmpdir, 'dhcpd.leases') open(leasepath, 'wb').write(b'') opts = f'-f -d -6 -cf {confpath} -lf {leasepath} -pf {pidfile}' server.fg(f'{DHCPD} -t {opts}') # test config with server.bg(f'{DHCPD} {opts}', sudo=True, ignore_status=True, pidfile=pidfile): yield client