• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/bin/sh
2# SPDX-License-Identifier: GPL-2.0-or-later
3# Copyright (c) 2014-2018 Oracle and/or its affiliates. All Rights Reserved.
4# Copyright (c) 2018 Petr Vorel <pvorel@suse.cz>
5#
6# Author:       Alexey Kodanev alexey.kodanev@oracle.com
7
8TST_SETUP="init"
9TST_TESTFUNC="test01"
10TST_CLEANUP="cleanup"
11TST_NEEDS_TMPDIR=1
12TST_NEEDS_ROOT=1
13TST_NEEDS_CMDS="cat $dhcp_name awk ip pgrep pkill dhclient"
14
15. tst_net.sh
16. daemonlib.sh
17
18iface0="ltp_veth0"
19iface1="ltp_veth1"
20
21stop_dhcp()
22{
23	[ "$(pgrep -x $dhcp_name)" ] || return 0
24
25	tst_res TINFO "stopping $dhcp_name"
26	local count=0
27	while [ $count -le 10 ]; do
28		pkill -x $dhcp_name
29		[ "$(pgrep -x $dhcp_name)" ] || return 0
30		tst_sleep 100ms
31		count=$((count + 1))
32	done
33
34	pkill -9 -x $dhcp_name
35	tst_sleep 100ms
36	[ "$(pgrep -x $dhcp_name)" ] && return 1 || return 0
37}
38
39init()
40{
41	if [ $TST_IPV6 ]; then
42		ip_addr="fd00:1:1:2::12/64"
43		ip_addr_check="fd00:1:1:2::100/64"
44	else
45		ip_addr="10.1.1.12/24"
46		ip_addr_check="10.1.1.100/24"
47	fi
48
49	lsmod | grep -q '^veth ' && veth_loaded=yes || veth_loaded=no
50
51	tst_res TINFO "create veth interfaces"
52	ip li add $iface0 type veth peer name $iface1 || \
53		tst_brk TBROK "failed to add veth $iface0"
54
55	veth_added=1
56	ip li set up $iface0 || tst_brk TBROK "failed to bring $iface0 up"
57	ip li set up $iface1 || tst_brk TBROK "failed to bring $iface1 up"
58
59	stop_dhcp || tst_brk TBROK "Failed to stop dhcp server"
60
61	dhclient_lease="/var/lib/dhclient/dhclient${TST_IPV6}.leases"
62	if [ -f $dhclient_lease ]; then
63		tst_res TINFO "backup dhclient${TST_IPV6}.leases"
64		mv $dhclient_lease .
65	fi
66
67	tst_res TINFO "add $ip_addr to $iface0"
68	ip addr add $ip_addr dev $iface0 || \
69		tst_brk TBROK "failed to add ip address"
70}
71
72cleanup()
73{
74	[ -z "$veth_loaded" ] && return
75
76	stop_dhcp
77
78	pkill -f "dhclient -$TST_IPVER $iface1"
79
80	cleanup_dhcp
81
82	# restore dhclient leases
83	[ $dhclient_lease ] && rm -f $dhclient_lease
84	[ -f "dhclient${TST_IPV6}.leases" ] && \
85		mv dhclient${TST_IPV6}.leases $dhclient_lease
86
87	[ $veth_added ] && ip li del $iface0
88
89	[ "$veth_loaded" = "no" ] && lsmod | grep -q '^veth ' && rmmod veth
90}
91
92test01()
93{
94	local wicked
95
96	tst_res TINFO "testing DHCP server $dhcp_name: $(print_dhcp_version)"
97	tst_res TINFO "using DHCP client: $(dhclient --version 2>&1)"
98
99	tst_res TINFO "starting DHCPv$TST_IPVER server on $iface0"
100
101	start_dhcp$TST_IPV6
102	if [ $? -ne 0 ]; then
103		print_dhcp_log
104		tst_brk TBROK "Failed to start $dhcp_name"
105	fi
106
107	sleep 1
108
109	if [ "$(pgrep '$dhcp_name')" ]; then
110		print_dhcp_log
111		tst_brk TBROK "Failed to start $dhcp_name"
112	fi
113
114	if [ $HAVE_SYSTEMCTL -eq 1 ] && \
115		systemctl --no-pager -p Id show network.service | grep -q Id=wicked.service; then
116		tst_res TINFO "temporarily disabling wicked"
117		wicked=1
118		systemctl disable wicked
119	fi
120	tst_res TINFO "starting dhclient -$TST_IPVER $iface1"
121	dhclient -$TST_IPVER $iface1 || tst_brk TBROK "dhclient failed"
122
123	# check that we get configured ip address
124	ip addr show $iface1 | grep $ip_addr_check > /dev/null
125	if [ $? -eq 0 ]; then
126		tst_res TPASS "'$ip_addr_check' configured by DHCPv$TST_IPVER"
127	else
128		tst_res TFAIL "'$ip_addr_check' not configured by DHCPv$TST_IPVER"
129		print_dhcp_log
130	fi
131
132	if [ "$wicked" ]; then
133		tst_res TINFO "reenabling wicked"
134		systemctl enable wicked
135	fi
136
137	stop_dhcp
138}
139