2014-05-19 14:47:31 +02:00
# include <config.h>
2020-02-16 22:59:28 +01:00
# include <unistd.h>
2014-05-19 14:47:31 +02:00
# include "testutils.h"
# ifdef WITH_VBOX
# include "vbox / vbox_snapshot_conf.h"
# define VIR_FROM_THIS VIR_FROM_NONE
static const char * testSnapshotXMLVariableLineRegexStr =
" lastStateChange=[0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2}Z " ;
2019-11-13 16:06:45 +01:00
GRegex * testSnapshotXMLVariableLineRegex = NULL ;
2014-05-19 14:47:31 +02:00
static char *
testFilterXML ( char * xml )
{
2020-07-02 19:35:41 -04:00
g_auto ( virBuffer ) buf = VIR_BUFFER_INITIALIZER ;
2021-11-01 10:34:10 +01:00
g_auto ( GStrv ) xmlLines = NULL ;
2014-05-19 14:47:31 +02:00
char * * xmlLine ;
2021-02-05 18:35:07 +01:00
if ( ! ( xmlLines = g_strsplit ( xml , " \n " , 0 ) ) ) {
2014-05-19 14:47:31 +02:00
VIR_FREE ( xml ) ;
2021-12-10 16:21:59 +01:00
return NULL ;
2014-05-19 14:47:31 +02:00
}
VIR_FREE ( xml ) ;
for ( xmlLine = xmlLines ; * xmlLine ; xmlLine + + ) {
2019-11-13 16:06:45 +01:00
if ( g_regex_match ( testSnapshotXMLVariableLineRegex , * xmlLine , 0 , NULL ) )
2014-05-19 14:47:31 +02:00
continue ;
virBufferStrcat ( & buf , * xmlLine , " \n " , NULL ) ;
}
2021-12-10 16:21:59 +01:00
return virBufferContentAndReset ( & buf ) ;
2014-05-19 14:47:31 +02:00
}
static int
testCompareXMLtoXMLFiles ( const char * xml )
{
2021-09-04 22:37:44 +02:00
g_autofree char * xmlData = NULL ;
g_autofree char * actual = NULL ;
g_autofree char * pathResult = NULL ;
2014-05-19 14:47:31 +02:00
int ret = - 1 ;
2021-03-11 08:16:13 +01:00
virVBoxSnapshotConfMachine * machine = NULL ;
2014-07-01 15:23:36 +02:00
2019-10-20 13:49:46 +02:00
pathResult = g_strdup ( abs_builddir " /vboxsnapshotxmldata/testResult.vbox " ) ;
2014-05-19 14:47:31 +02:00
2021-02-26 09:37:10 +01:00
if ( g_mkdir_with_parents ( abs_builddir " /vboxsnapshotxmldata " , 0777 ) < 0 )
2014-07-01 15:23:36 +02:00
goto cleanup ;
2016-05-26 17:01:52 +02:00
if ( virTestLoadFile ( xml , & xmlData ) < 0 )
2014-07-01 15:23:36 +02:00
goto cleanup ;
2014-05-19 14:47:31 +02:00
if ( ! ( machine = virVBoxSnapshotConfLoadVboxFile ( xml , ( char * ) " " ) ) )
2014-07-01 15:23:36 +02:00
goto cleanup ;
2014-05-19 14:47:31 +02:00
if ( virVBoxSnapshotConfSaveVboxFile ( machine , pathResult ) < 0 )
2014-07-01 15:23:36 +02:00
goto cleanup ;
2014-05-19 14:47:31 +02:00
2016-05-26 17:01:52 +02:00
if ( virTestLoadFile ( pathResult , & actual ) < 0 )
2014-07-01 15:23:36 +02:00
goto cleanup ;
2014-05-19 14:47:31 +02:00
if ( ! ( actual = testFilterXML ( actual ) ) )
2014-07-01 15:23:36 +02:00
goto cleanup ;
2014-05-19 14:47:31 +02:00
if ( ! ( xmlData = testFilterXML ( xmlData ) ) )
2014-07-01 15:23:36 +02:00
goto cleanup ;
2014-05-19 14:47:31 +02:00
tests: Use virTestCompareToString() more
Instead of using:
if (STRNEQ(a, b)) {
virTestDifference(stderr, a, b);
...
}
we can use:
if (virTestCompareToString(a, b) < ) {
...
}
Generated by the following spatch:
@@
expression a, b;
@@
- if (STRNEQ(a, b)) {
+ if (virTestCompareToString(a, b) < 0) {
...
- virTestDifference(stderr, a, b);
...
}
and its variations (STRNEQ_NULLABLE() instead of STRNEQ(), then
in some cases variables passed to STRNEQ() are in reversed order
when compared to virTestCompareToString()).
However, coccinelle failed to recognize the pattern in
testNWFilterEBIPTablesAllTeardown() so I had to fix it manually.
Also, I manually fixed testFormat() in tests/sockettest.c as I
didn't bother writing another spatch rule just for that.
Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
Reviewed-by: Jonathon Jongsma <jjongsma@redhat.com>
2022-11-30 09:57:49 +01:00
if ( virTestCompareToString ( xmlData , actual ) < 0 ) {
2014-07-01 15:23:36 +02:00
goto cleanup ;
2014-05-19 14:47:31 +02:00
}
ret = 0 ;
2014-07-01 15:23:36 +02:00
cleanup :
unlink ( pathResult ) ;
rmdir ( abs_builddir " /vboxsnapshotxmldata " ) ;
2014-05-19 14:47:31 +02:00
virVBoxSnapshotConfMachineFree ( machine ) ;
return ret ;
}
static int
testCompareXMLToXMLHelper ( const void * data )
{
int result = - 1 ;
2021-09-04 22:37:44 +02:00
g_autofree char * xml = NULL ;
2014-05-19 14:47:31 +02:00
2019-10-22 15:26:14 +02:00
xml = g_strdup_printf ( " %s/vboxsnapshotxmldata/%s.vbox " , abs_srcdir ,
( const char * ) data ) ;
2014-05-19 14:47:31 +02:00
result = testCompareXMLtoXMLFiles ( xml ) ;
return result ;
}
static int
mymain ( void )
{
int ret = 0 ;
2019-11-13 16:06:45 +01:00
g_autoptr ( GError ) err = NULL ;
testSnapshotXMLVariableLineRegex = g_regex_new ( testSnapshotXMLVariableLineRegexStr ,
0 , 0 , & err ) ;
2014-05-19 14:47:31 +02:00
2019-11-13 16:06:45 +01:00
if ( ! testSnapshotXMLVariableLineRegex ) {
2014-05-19 14:47:31 +02:00
ret = - 1 ;
virReportError ( VIR_ERR_INTERNAL_ERROR , " %s " ,
" failed to compile test regex " ) ;
goto cleanup ;
}
2017-11-03 13:09:47 +01:00
# define DO_TEST(name) \
if ( virTestRun ( " VBox Snapshot XML-2-XML " name , \
testCompareXMLToXMLHelper , ( name ) ) < 0 ) \
2014-05-19 14:47:31 +02:00
ret = - 1
DO_TEST ( " 2disks-nosnap " ) ;
DO_TEST ( " 2disks-1snap " ) ;
DO_TEST ( " 2disks-2snap " ) ;
DO_TEST ( " 2disks-3snap " ) ;
DO_TEST ( " 2disks-3snap-brother " ) ;
cleanup :
2020-09-08 14:57:14 +02:00
if ( testSnapshotXMLVariableLineRegex )
g_regex_unref ( testSnapshotXMLVariableLineRegex ) ;
2014-05-19 14:47:31 +02:00
return ret = = 0 ? EXIT_SUCCESS : EXIT_FAILURE ;
}
2017-03-29 16:45:42 +02:00
VIR_TEST_MAIN ( mymain )
2014-05-19 14:47:31 +02:00
# else
int main ( void )
{
return EXIT_AM_SKIP ;
}
2020-01-24 21:30:04 +01:00
# endif /* WITH_VBOX */