2018-06-07 18:24:42 +03:00
/*
* Copyright ( C ) 2018 Red Hat , Inc . All rights reserved .
*
* This file is part of LVM2 .
*
* This copyrighted material is made available to anyone wishing to use ,
* modify , copy , or redistribute it subject to the terms and conditions
* of the GNU General Public License v .2 .
*
* You should have received a copy of the GNU General Public License
* along with this program ; if not , write to the Free Software Foundation ,
* Inc . , 51 Franklin Street , Fifth Floor , Boston , MA 02110 - 1301 USA
*/
# include "units.h"
2018-06-19 21:43:31 +03:00
# include "scripts/generator-internals.c"
2018-06-07 18:24:42 +03:00
//----------------------------------------------------------------
static void _error ( const char * format , . . . )
{
va_list ap ;
va_start ( ap , format ) ;
vfprintf ( stderr , format , ap ) ;
va_end ( ap ) ;
}
//----------------------------------------------------------------
struct bw_test {
const char * input ;
const char * prefix ;
const char * val ;
} ;
static void _test_begins_with ( void * fixture )
{
static struct bw_test _tests [ ] = {
{ " " , " foo " , NULL } ,
{ " lskdj " , " foo " , NULL } ,
{ " foo " , " foobar " , NULL } ,
{ " fish " , " fish " , " " } ,
{ " foo=bar " , " foo= " , " bar " } ,
2018-12-23 02:55:00 +03:00
} ;
2018-06-07 18:24:42 +03:00
unsigned i ;
for ( i = 0 ; i < DM_ARRAY_SIZE ( _tests ) ; i + + ) {
2018-12-23 02:55:00 +03:00
const char * val ;
struct bw_test * t = _tests + i ;
if ( t - > val ) {
if ( ! _begins_with ( t - > input , t - > prefix , & val ) )
test_fail ( " _begins_with('%s', '%s') failed " , t - > input , t - > prefix ) ;
2018-06-07 18:24:42 +03:00
if ( strcmp ( val , t - > val ) )
2018-12-23 02:55:00 +03:00
test_fail ( " _begins_with('%s', '%s') -> '%s', expected '%s' " ,
t - > input , t - > prefix , val , t - > val ) ;
2018-06-07 18:24:42 +03:00
} else {
if ( _begins_with ( t - > input , t - > prefix , & val ) )
2018-12-23 02:55:00 +03:00
test_fail ( " _begins_with('%s', '%s') unexpectedly succeeded " ,
t - > input , t - > prefix ) ;
2018-06-07 18:24:42 +03:00
}
}
}
struct pb_test {
const char * input ;
bool parsed ;
bool result ;
} ;
static const char * _bool ( bool v )
{
2018-12-23 02:55:00 +03:00
return v ? " true " : " false " ;
2018-06-07 18:24:42 +03:00
}
static void _test_parse_bool ( void * fixture )
{
static struct pb_test _tests [ ] = {
{ " " , false , false } ,
{ " fish " , false , false } ,
{ " true " , false , false } ,
{ " false " , false , false } ,
{ " 1 " , true , true } ,
{ " \t 1 \t \t " , true , true } ,
{ " 0 " , true , false } ,
{ " \t 0 " , true , false }
} ;
unsigned i ;
for ( i = 0 ; i < DM_ARRAY_SIZE ( _tests ) ; i + + ) {
2018-12-23 02:55:00 +03:00
bool result ;
struct pb_test * t = _tests + i ;
2018-06-07 18:24:42 +03:00
2018-12-23 02:55:00 +03:00
if ( t - > parsed ) {
2018-06-07 18:24:42 +03:00
if ( ! _parse_bool ( t - > input , & result ) )
2018-12-23 02:55:00 +03:00
test_fail ( " _parse_bool('%s') unexpectedly failed " , t - > input ) ;
if ( result ! = t - > result )
test_fail ( " _parse_bool('%s') -> %s " , t - > input , _bool ( result ) ) ;
} else {
if ( _parse_bool ( t - > input , & result ) )
test_fail ( " _parse_bool('%s') unexpectedly succeeded " , t - > input ) ;
}
2018-06-07 18:24:42 +03:00
}
}
struct pl_test {
const char * input ;
bool success ;
2018-12-17 19:04:44 +03:00
bool event_activation ;
2018-06-07 18:24:42 +03:00
bool sysinit_needed ;
} ;
static void _test_parse_line ( void * fixture )
{
static struct pl_test _tests [ ] = {
2018-12-23 02:55:00 +03:00
{ " " , false , false , false } ,
{ " sldkjfs " , false , false , false } ,
{ " event_activation=1 " , true , true , true } ,
{ " event_activation=0 " , true , false , true } ,
{ " use_lvmpolld=1 " , true , false , false } ,
{ " use_lvmpolld=0 " , true , false , true }
2018-06-07 18:24:42 +03:00
} ;
unsigned i ;
for ( i = 0 ; i < DM_ARRAY_SIZE ( _tests ) ; i + + ) {
2018-12-23 02:55:00 +03:00
bool r ;
2018-11-30 00:52:46 +03:00
struct config cfg = {
. sysinit_needed = true
} ;
2018-06-07 18:24:42 +03:00
struct pl_test * t = _tests + i ;
r = _parse_line ( t - > input , & cfg ) ;
if ( t - > success ) {
if ( ! r )
2018-12-23 02:55:00 +03:00
test_fail ( " _parse_line('%s') failed " , t - > input ) ;
2018-06-07 18:24:42 +03:00
2018-12-23 02:54:39 +03:00
if ( cfg . event_activation ! = t - > event_activation )
test_fail ( " _parse_line('%s') -> event_activation='%s' " ,
t - > input , _bool ( cfg . event_activation ) ) ;
2018-06-07 18:24:42 +03:00
2018-12-23 02:55:00 +03:00
if ( cfg . sysinit_needed ! = t - > sysinit_needed )
test_fail ( " _parse_line('%s') -> sysinit_needed='%s' " ,
t - > input , _bool ( cfg . sysinit_needed ) ) ;
2018-06-07 18:24:42 +03:00
} else if ( r )
2018-12-23 02:55:00 +03:00
test_fail ( " _parse_line('%s') succeeded " , t - > input ) ;
2018-06-07 18:24:42 +03:00
}
}
static void _test_get_config_bad_path ( void * fixture )
{
2018-12-23 02:55:00 +03:00
struct config cfg ;
2018-06-07 18:24:42 +03:00
if ( _get_config ( & cfg , " /usr/bin/no-such-file " ) )
2018-12-23 02:55:00 +03:00
test_fail ( " _get_config() succeeded despite a bad lvmconfig path " ) ;
2018-06-07 18:24:42 +03:00
}
static void _test_get_config_bad_exit ( void * fixture )
{
2018-12-23 02:55:00 +03:00
struct config cfg ;
2018-06-07 18:24:42 +03:00
if ( _get_config ( & cfg , " /usr/bin/false " ) )
2018-12-23 02:55:00 +03:00
test_fail ( " _get_config() succeeded despite a bad lvmconfig exit " ) ;
2018-06-07 18:24:42 +03:00
}
struct gc_test {
2018-12-23 02:55:00 +03:00
const char * output ;
bool success ;
bool event_activation ;
bool sysinit_needed ;
2018-06-07 18:24:42 +03:00
} ;
static const char * _fake_lvmconfig ( const char * output )
{
2018-12-23 02:55:00 +03:00
const char * path = " ./fake-lvmconfig " ;
2018-06-07 18:24:42 +03:00
FILE * fp = fopen ( path , " w " ) ;
if ( ! fp )
2018-12-23 02:55:00 +03:00
return NULL ;
2018-06-07 18:24:42 +03:00
2018-12-23 02:54:39 +03:00
fprintf ( fp , " #!/usr/bin/env bash \n " ) ;
2018-06-07 18:24:42 +03:00
fprintf ( fp , " cat <<EOF \n " ) ;
fprintf ( fp , " %s " , output ) ;
fprintf ( fp , " EOF \n " ) ;
fclose ( fp ) ;
chmod ( path , 0770 ) ;
return path ;
}
static void _test_get_config ( void * fixture )
{
static struct gc_test _tests [ ] = {
{ " " , true , false , true } ,
{ " lsdjkf \n \n \n " , false , false , false } ,
2018-12-17 19:04:44 +03:00
{ " event_activation=0 \n use_lvmpolld=1 \n " , true , false , false } ,
{ " event_activation=1 \n use_lvmpolld=1 \n " , true , true , false } ,
{ " event_activation=1 \n use_lvmpolld=0 \n " , true , true , true } ,
2018-06-07 18:24:42 +03:00
} ;
bool r ;
unsigned i ;
const char * path ;
for ( i = 0 ; i < DM_ARRAY_SIZE ( _tests ) ; i + + ) {
2018-12-23 02:55:00 +03:00
struct gc_test * t = _tests + i ;
2018-11-30 00:52:46 +03:00
struct config cfg = {
. sysinit_needed = true
} ;
2018-06-07 18:24:42 +03:00
path = _fake_lvmconfig ( t - > output ) ;
if ( ! path )
2018-12-23 02:55:00 +03:00
test_fail ( " couldn't create fake lvmconfig " ) ;
2018-06-07 18:24:42 +03:00
2018-12-23 02:55:00 +03:00
r = _get_config ( & cfg , path ) ;
if ( t - > success ) {
2018-06-07 18:24:42 +03:00
if ( ! r )
2018-12-23 02:55:00 +03:00
test_fail ( " _get_config() <- '%s' failed " , t - > output ) ;
2018-06-07 18:24:42 +03:00
2018-12-23 02:54:39 +03:00
if ( t - > event_activation ! = cfg . event_activation )
test_fail ( " _get_config() <- '%s', event_activation = %s " ,
t - > output , _bool ( cfg . event_activation ) ) ;
2018-06-07 18:24:42 +03:00
2018-12-23 02:55:00 +03:00
if ( t - > sysinit_needed ! = cfg . sysinit_needed )
test_fail ( " _get_config() <- '%s', sysinit = %s " ,
t - > output , _bool ( cfg . sysinit_needed ) ) ;
} else {
2018-06-07 18:24:42 +03:00
if ( r )
2018-12-23 02:55:00 +03:00
test_fail ( " _get_config() <- '%s' unexpectedly succeeded " , t - > output ) ;
}
2018-06-07 18:24:42 +03:00
2018-12-23 02:55:00 +03:00
unlink ( path ) ;
2018-06-07 18:24:42 +03:00
}
}
//----------------------------------------------------------------
# define T(path, desc, fn) register_test(ts, " / activation-generator / " path, desc, fn)
static struct test_suite * _tests ( void )
{
struct test_suite * ts = test_suite_create ( NULL , NULL ) ;
if ( ! ts ) {
2018-12-23 02:55:00 +03:00
fprintf ( stderr , " out of memory \n " ) ;
exit ( 1 ) ;
2018-06-07 18:24:42 +03:00
} ;
T ( " begins-with " , " Test cases for _begins_with() " , _test_begins_with ) ;
T ( " parse-bool " , " Test cases for _parse_bool() " , _test_parse_bool ) ;
T ( " parse-line " , " Test cases for _parse_line() " , _test_parse_line ) ;
T ( " get-config-bad-path " , " _get_config() needs a valid lvmconfig path " , _test_get_config_bad_path ) ;
T ( " get-config-bad-exit " , " lvmconfig bad exit code gets propagated " , _test_get_config_bad_exit ) ;
T ( " get-config " , " Test cases for _get_config() " , _test_get_config ) ;
return ts ;
}
void activation_generator_tests ( struct dm_list * all_tests )
{
dm_list_add ( all_tests , & _tests ( ) - > list ) ;
}
//----------------------------------------------------------------