#!/bin/sh
# Copyright (c) 2015 Oracle and/or its affiliates. All Rights Reserved.
# Copyright (c) International Business Machines  Corp., 2005
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License as
# published by the Free Software Foundation; either version 2 of
# the License, or (at your option) any later version.
#
# This program is distributed in the hope that it would be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
# Author: Mitsuru Chinen <mitch@jp.ibm.com>

TST_TOTAL=2

TST_CLEANUP="netstress_cleanup"

. test_net_stress.sh

CHECK_INTERVAL=${CHECK_INTERVAL:-$(($IF_UPDOWN_TIMES / 20))}

test_body()
{
	local cmd_type=$1

	case $cmd_type in
	if_cmd) local cmd_name='ifconfig' ;;
	ip_cmd) local cmd_name='ip' ;;
	*) tst_brkm TBROK "Unknown test parameter '$cmd_type'"
	esac

	local iface=$(tst_iface)

	tst_resm TINFO "'$cmd_name ups/downs $iface $IF_UPDOWN_TIMES times"
	tst_resm TINFO "check connectivity interval is $CHECK_INTERVAL"

	local cnt=1
	while [ $cnt -le $IF_UPDOWN_TIMES ]; do
		case $cmd_type in
		if_cmd) ifconfig $iface down ;;
		ip_cmd) ip link set $iface down ;;
		esac
		if [ $? -ne 0 ]; then
			tst_resm TFAIL "Failed to down $iface"
			return
		fi

		case $cmd_type in
		if_cmd) ifconfig $iface up ;;
		ip_cmd) ip link set $iface up ;;
		esac
		if [ $? -ne 0 ]; then
			tst_resm TFAIL "Failed to up $iface"
			return
		fi

		check_connectivity_interval $cnt restore_ip || return

		cnt=$(($cnt + 1))
	done

	tst_resm TPASS "Test is finished correctly"
}

netstress_setup

tst_check_cmds ifconfig

test_body 'if_cmd'
test_body 'ip_cmd'

tst_exit