2014-01-23 09:17:05 +01:00
/*
* Copyright ( C ) 2014 Red Hat , Inc .
*
* This library is free software ; you can redistribute it and / or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation ; either
* version 2.1 of the License , or ( at your option ) any later version .
*
* This library is distributed in the hope that it will be useful ,
* but WITHOUT ANY WARRANTY ; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the GNU
* Lesser General Public License for more details .
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library . If not , see
* < http : //www.gnu.org/licenses/>.
*/
# include <config.h>
# include "testutils.h"
2018-12-13 14:53:50 +00:00
# define LIBVIRT_VIRCOMMANDPRIV_H_ALLOW
2014-03-11 11:59:58 +01:00
# include "vircommandpriv.h"
2014-01-23 09:17:05 +01:00
# include "virnetdevbandwidth.h"
# include "netdev_bandwidth_conf.c"
# define VIR_FROM_THIS VIR_FROM_NONE
struct testMinimalStruct {
const char * expected_result ;
const char * band1 ;
const char * band2 ;
} ;
2014-01-27 14:53:00 +01:00
struct testSetStruct {
const char * band ;
const char * exp_cmd ;
const char * iface ;
const bool hierarchical_class ;
} ;
2021-08-20 15:08:34 +02:00
static int
testVirNetDevBandwidthParse ( virNetDevBandwidth * * var ,
const char * xml )
{
g_autoptr ( xmlDoc ) doc = NULL ;
g_autoptr ( xmlXPathContext ) ctxt = NULL ;
if ( ! xml )
return 0 ;
if ( ! ( doc = virXMLParseStringCtxt ( ( xml ) ,
" bandwidth definition " ,
& ctxt ) ) )
return - 1 ;
return virNetDevBandwidthParse ( var ,
NULL ,
ctxt - > node ,
true ) ;
}
2014-01-23 09:17:05 +01:00
2014-01-27 14:53:00 +01:00
static int
testVirNetDevBandwidthSet ( const void * data )
{
const struct testSetStruct * info = data ;
const char * iface = info - > iface ;
2021-03-03 11:48:19 +01:00
g_autoptr ( virNetDevBandwidth ) band = NULL ;
2020-07-02 19:35:41 -04:00
g_auto ( virBuffer ) buf = VIR_BUFFER_INITIALIZER ;
2021-08-20 14:58:13 +02:00
g_autofree char * actual_cmd = NULL ;
2021-04-01 17:54:09 +02:00
g_autoptr ( virCommandDryRunToken ) dryRunToken = virCommandDryRunTokenNew ( ) ;
2014-01-27 14:53:00 +01:00
2021-08-20 15:08:34 +02:00
if ( testVirNetDevBandwidthParse ( & band , info - > band ) < 0 )
return - 1 ;
2014-01-27 14:53:00 +01:00
if ( ! iface )
iface = " eth0 " ;
2021-04-06 10:56:23 +02:00
virCommandSetDryRun ( dryRunToken , & buf , false , false , NULL , NULL ) ;
2014-01-27 14:53:00 +01:00
2017-10-02 14:12:44 +02:00
if ( virNetDevBandwidthSet ( iface , band , info - > hierarchical_class , true ) < 0 )
2021-08-20 15:11:52 +02:00
return - 1 ;
2014-01-27 14:53:00 +01:00
if ( ! ( actual_cmd = virBufferContentAndReset ( & buf ) ) ) {
/* This is interesting, no command has been executed.
* Maybe that ' s expected , actually . */
}
if ( STRNEQ_NULLABLE ( info - > exp_cmd , actual_cmd ) ) {
2016-05-26 17:01:51 +02:00
virTestDifference ( stderr ,
NULLSTR ( info - > exp_cmd ) ,
NULLSTR ( actual_cmd ) ) ;
2021-08-20 15:11:52 +02:00
return - 1 ;
2014-01-27 14:53:00 +01:00
}
2021-08-20 15:11:52 +02:00
return 0 ;
2014-01-27 14:53:00 +01:00
}
2014-01-23 09:17:05 +01:00
static int
mymain ( void )
{
int ret = 0 ;
2017-11-03 13:09:47 +01:00
# define DO_TEST_SET(Band, Exp_cmd, ...) \
do { \
struct testSetStruct data = { . band = Band , \
. exp_cmd = Exp_cmd , \
__VA_ARGS__ } ; \
if ( virTestRun ( " virNetDevBandwidthSet " , \
testVirNetDevBandwidthSet , \
& data ) < 0 ) \
ret = - 1 ; \
2014-01-27 14:53:00 +01:00
} while ( 0 )
2014-01-23 09:17:05 +01:00
2014-01-31 15:04:03 +01:00
DO_TEST_SET ( NULL , NULL ) ;
2015-01-07 17:53:04 +01:00
DO_TEST_SET ( " <bandwidth/> " , NULL ) ;
2014-01-31 15:04:03 +01:00
DO_TEST_SET ( ( " <bandwidth> "
" <inbound average='1024'/> "
" </bandwidth> " ) ,
( TC " qdisc del dev eth0 root \n "
TC " qdisc del dev eth0 ingress \n "
TC " qdisc add dev eth0 root handle 1: htb default 1 \n "
2016-02-02 12:48:44 +01:00
TC " class add dev eth0 parent 1: classid 1:1 htb rate 1024kbps quantum 87 \n "
2014-01-31 15:04:03 +01:00
TC " qdisc add dev eth0 parent 1:1 handle 2: sfq perturb 10 \n "
2015-04-14 15:40:17 +02:00
TC " filter add dev eth0 parent 1:0 protocol all prio 1 handle 1 fw flowid 1 \n " ) ) ;
2014-01-31 15:04:03 +01:00
DO_TEST_SET ( ( " <bandwidth> "
" <outbound average='1024'/> "
" </bandwidth> " ) ,
( TC " qdisc del dev eth0 root \n "
TC " qdisc del dev eth0 ingress \n "
TC " qdisc add dev eth0 ingress \n "
2014-04-04 15:39:33 +02:00
TC " filter add dev eth0 parent ffff: protocol all u32 match u32 0 0 "
2014-01-31 15:04:03 +01:00
" police rate 1024kbps burst 1024kb mtu 64kb drop flowid :1 \n " ) ) ;
2014-01-27 14:53:00 +01:00
DO_TEST_SET ( ( " <bandwidth> "
" <inbound average='1' peak='2' floor='3' burst='4'/> "
" <outbound average='5' peak='6' burst='7'/> "
" </bandwidth> " ) ,
2014-01-31 09:52:30 +01:00
( TC " qdisc del dev eth0 root \n "
TC " qdisc del dev eth0 ingress \n "
TC " qdisc add dev eth0 root handle 1: htb default 1 \n "
2016-02-02 12:48:44 +01:00
TC " class add dev eth0 parent 1: classid 1:1 htb rate 1kbps ceil 2kbps burst 4kb quantum 1 \n "
2014-01-31 09:52:30 +01:00
TC " qdisc add dev eth0 parent 1:1 handle 2: sfq perturb 10 \n "
2015-04-14 15:40:17 +02:00
TC " filter add dev eth0 parent 1:0 protocol all prio 1 handle 1 fw flowid 1 \n "
2014-01-31 09:52:30 +01:00
TC " qdisc add dev eth0 ingress \n "
2014-04-04 15:39:33 +02:00
TC " filter add dev eth0 parent ffff: protocol all u32 match u32 0 0 "
2014-01-27 14:53:00 +01:00
" police rate 5kbps burst 7kb mtu 64kb drop flowid :1 \n " ) ) ;
2021-03-05 10:13:36 +01:00
DO_TEST_SET ( ( " <bandwidth> "
" <inbound average='4294967295'/> "
" <outbound average='4294967295'/> "
" </bandwidth> " ) ,
( TC " qdisc del dev eth0 root \n "
TC " qdisc del dev eth0 ingress \n "
TC " qdisc add dev eth0 root handle 1: htb default 1 \n "
TC " class add dev eth0 parent 1: classid 1:1 htb rate 4294967295kbps quantum 366503875 \n "
TC " qdisc add dev eth0 parent 1:1 handle 2: sfq perturb 10 \n "
TC " filter add dev eth0 parent 1:0 protocol all prio 1 handle 1 fw flowid 1 \n "
TC " qdisc add dev eth0 ingress \n "
TC " filter add dev eth0 parent ffff: protocol all u32 match "
" u32 0 0 police rate 4294967295kbps burst 4194303kb mtu 64kb "
" drop flowid :1 \n " ) ) ;
2021-03-04 18:52:32 +01:00
return ret = = 0 ? EXIT_SUCCESS : EXIT_FAILURE ;
2014-01-23 09:17:05 +01:00
}
2019-08-21 19:13:16 +03:00
VIR_TEST_MAIN_PRELOAD ( mymain , VIR_TEST_MOCK ( " virnetdevbandwidth " ) )