trace-cmd: Make unit tests for trace-cmd
cover trace-cmd: Make unit tests for trace-cmd
From: "Steven Rostedt (Google)" <[email protected]>
The current unit tests in the trace-cmd repo are really for libtracefs. As libtracefs has moved to its own repo and has its own unit tests, the trace-cmd tests here are redundant.
Remove the libtracefs unit tests and replace them with tests that are specific for trace-cmd.
Currently there are only two tests added. A simple record and report, and a test of converting from v7 to v6 file format. But this adds infrastructure that should make it easier to add new tests.
Steven Rostedt (Google) (4):
trace-cmd utests: Remove libtracefs tests trace-cmd utest: Rename tracefs-utest.c to tracecmd-utest.c trace-cmd test: Add simple record/report test trace-cmd test: Add test to check conversion from 7 to 6
Makefile | 2 +- utest/Makefile | 2 +- utest/trace-utest.c | 23 +- utest/trace-utest.h | 7 +- utest/tracecmd-utest.c | 295 +++++++++++++++++++ utest/tracefs-utest.c | 630 ----------------------------------------- 6 files changed, 319 insertions(+), 640 deletions(-) create mode 100644 utest/tracecmd-utest.c delete mode 100644 utest/tracefs-utest.c
Signature · 1 line
2.35.1
PATCH 1/4 trace-cmd utests: Remove libtracefs tests
From: "Steven Rostedt (Google)" <[email protected]>
The unit tests in trace-cmd are all for libtracefs, and those tests are in the libtracefs repo. There's no reason to keep them here. Instead remove them all and start a template to add trace-cmd specific tests.
The initialization phase is just finding the in tree trace-cmd code.
--- Makefile | 2 +- utest/trace-utest.c | 16 +- utest/trace-utest.h | 4 +- utest/tracefs-utest.c | 613 ++---------------------------------------- 4 files changed, 41 insertions(+), 594 deletions(-)
Makefile +1 -1
@@ -442,7 +442,7 @@ gui: force @echo " Please use its new home at https://git.kernel.org/pub/scm/utils/trace-cmd/kernel-shark.git/" @echo "***************************" -test: force $(LIBTRACECMD_STATIC) +test: force trace-cmd ifneq ($(CUNIT_INSTALLED),1) $(error CUnit framework not installed, cannot build unit tests)) endif
utest/trace-utest.c +10 -6
@@ -13,9 +13,11 @@#include "trace-utest.h" +const char *argv0; +enum unit_tests {RUN_NONE =0 , -RUN_TRACEFS = (1 <<0 ), +RUN_TRACECMD = (1 <<0 ),RUN_ALL =0xFFFF };
@@ -24,7 +26,7 @@ static void print_help(char **argv)printf ("Usage: %s [OPTIONS]\n" ,basename (argv[0 ]));printf ("\t-s, --silent\tPrint test summary\n" );printf ("\t-r, --run test\tRun specific test:\n" ); -printf ("\t\t tracefs run libtracefs tests\n" ); +printf ("\t\t trace-cmd run trace-cmd tests\n" );printf ("\t-h, --help\tPrint usage information\n" );exit (0 ); }
@@ -34,6 +36,8 @@ int main(int argc, char **argv)CU_BasicRunMode verbose =CU_BRM_VERBOSE ;enum unit_tests tests =RUN_NONE ; + argv0 = argv[0 ]; +for (;;) {int c;int index =0 ;
@@ -50,8 +54,8 @@ int main(int argc, char **argv)break ;switch (c) {case 'r' : -if (strcmp (optarg,"tracefs" ) ==0 ) - tests |=RUN_TRACEFS ; +if (strcmp (optarg,"trace-cmd" ) ==0 ) + tests |=RUN_TRACECMD ;else print_help (argv);break ;
@@ -73,8 +77,8 @@ int main(int argc, char **argv)return -1 ; } -if (tests &RUN_TRACEFS ) -test_tracefs_lib (); +if (tests &RUN_TRACECMD ) +test_tracecmd_lib ();CU_basic_set_mode (verbose);CU_basic_run_tests ();
utest/trace-utest.h +3 -1
@@ -6,6 +6,8 @@#ifndef _TRACE_UTEST_H_#define _TRACE_UTEST_H_ -void test_tracefs_lib (void ); +extern const char *argv0; + +void test_tracecmd_lib (void );#endif /* _TRACE_UTEST_H_ */
utest/tracefs-utest.c +27 -586
644 lines not on this page · raw message
Signature · 1 line
2.35.1
PATCH 2/4 trace-cmd utest: Rename tracefs-utest.c to tracecmd-utest.c
In reply to Steven RostedtFrom: "Steven Rostedt (Google)" <[email protected]>
Now that the unit tests will be testing trace-cmd and not libtracefs, rename the file to reflect that.
---
utest/Makefile | 2 +-
utest/{tracefs-utest.c => tracecmd-utest.c} | 0
2 files changed, 1 insertion(+), 1 deletion(-)
rename utest/{tracefs-utest.c => tracecmd-utest.c} (100%)utest/Makefile +1 -1
@@ -8,7 +8,7 @@ TARGETS = $(bdir)/trace-utest OBJS = OBJS += trace-utest.o -OBJS += tracefs-utest.o +OBJS += tracecmd-utest.o LIBS += -lcunit $(LIBTRACEEVENT_LDLAGS) $(LIBTRACEFS_LDLAGS)
utest/tracecmd-utest.c renamed from utest/tracefs-utest.c +0 -0
Signature · 1 line
2.35.1
PATCH 3/4 trace-cmd test: Add simple record/report test
In reply to Steven RostedtFrom: "Steven Rostedt (Google)" <[email protected]>
Add a simple test that does a trace-cmd record of schedule events and then reads it with trace-cmd report. This also adds some infrastructure to make it easy to run trace-cmd commands.
--- utest/trace-utest.c | 7 +++- utest/trace-utest.h | 3 ++ utest/tracecmd-utest.c | 76 +++++++++++++++++++++++++++++++++++++++++- 3 files changed, 84 insertions(+), 2 deletions(-)
utest/trace-utest.c +6 -1
@@ -14,6 +14,7 @@#include "trace-utest.h" const char *argv0; +bool show_output;enum unit_tests {RUN_NONE =0 ,
@@ -41,10 +42,11 @@ int main(int argc, char **argv)for (;;) {int c;int index =0 ; -const char *opts ="+hsr:" ; +const char *opts ="+hsr:v" ;static struct option long_options[] = { {"silent" , no_argument,NULL ,'s' }, {"run" , required_argument,NULL ,'r' }, + {"verbose" , no_argument,NULL ,'v' }, {"help" , no_argument,NULL ,'h' }, {NULL ,0 ,NULL ,0 } };
@@ -62,6 +64,9 @@ int main(int argc, char **argv)case 's' : verbose =CU_BRM_SILENT ;break ; +case 'v' : + show_output = true; +break ;case 'h' :default :print_help (argv);
utest/trace-utest.h +3 -0
@@ -6,7 +6,10 @@#ifndef _TRACE_UTEST_H_#define _TRACE_UTEST_H_ +#include <stdbool.h> +extern const char *argv0; +extern bool show_output;void test_tracecmd_lib (void );
utest/tracecmd-utest.c +75 -1
102 lines not on this page · raw message
Signature · 1 line
2.35.1
PATCH 4/4 trace-cmd test: Add test to check conversion from 7 to 6
In reply to Steven RostedtFrom: "Steven Rostedt (Google)" <[email protected]>
Add a unit test that tests the conversion of trace-cmd 7 to trace-cmd 6. This also adds infrastructure to do a grep of the output of trace-cmd.
--- utest/tracecmd-utest.c | 202 +++++++++++++++++++++++++++++++++++------ 1 file changed, 176 insertions(+), 26 deletions(-)
utest/tracecmd-utest.c +176 -26
@@ -25,49 +25,79 @@ static char tracecmd_exec[PATH_MAX];#define TRACECMD_SUITE "trace-cmd"#define TRACECMD_FILE "__trace_test__.dat" +#define TRACECMD_FILE2 "__trace_test__2.dat"#define TRACECMD_OUT "-o", TRACECMD_FILE +#define TRACECMD_OUT2 "-o", TRACECMD_FILE2#define TRACECMD_IN "-i", TRACECMD_FILE +#define TRACECMD_IN2 "-i", TRACECMD_FILE2 -static void silent_output (void ) -{ -close (STDOUT_FILENO ); -open ("/dev/null" ,O_WRONLY ); -close (STDERR_FILENO ); -open ("/dev/null" ,O_WRONLY ); -} - -static int run_trace (const char *cmd, ...) +static char **get_args (const char *cmd,va_list ap) {const char *param; -va_list ap; -char **tmp;char **argv; -int status; -int ret =-1 ; -pid_t pid; +char **tmp; argv =tracefs_list_add (NULL , tracecmd_exec);if (!argv) -return -1 ; +return NULL ; tmp =tracefs_list_add (argv, cmd);if (!tmp) - goto out; + goto fail; argv = tmp; -va_start (ap, cmd); for (param = va_arg(ap, const char *); param; param = va_arg(ap, const char *)) { tmp =tracefs_list_add (argv, param);if (!tmp) - goto out; + goto fail; argv = tmp; } + +return argv; + fail: +tracefs_list_free (argv); +return NULL ; +} + +static void silent_output (void ) +{ +close (STDOUT_FILENO ); +open ("/dev/null" ,O_WRONLY ); +close (STDERR_FILENO ); +open ("/dev/null" ,O_WRONLY ); +} + +static int wait_for_exec (int pid) +{ +int status; +int ret; + + ret =waitpid (pid, &status,0 ); +if (ret != pid) +return -1 ; + +return WEXITSTATUS (status) ?-1 :0 ; +} + +static int run_trace (const char *cmd, ...) +{ +char **argv; +va_list ap; +int ret =-1 ; +pid_t pid; + +va_start (ap, cmd); + argv =get_args (cmd, ap);va_end (ap); +if (!argv) +return -1 ; + pid =fork ();if (pid <0 ) goto out; +if (!pid) {if (!show_output)silent_output ();
@@ -75,31 +105,149 @@ static int run_trace(const char *cmd, ...)exit (ret); } - ret =waitpid (pid, &status,0 ); -if (ret != pid) { - ret =-1 ; - goto out; - } - - ret =WEXIT_STATUS (status); + ret =wait_for_exec (pid); out:tracefs_list_free (argv);return ret; } +static int pipe_it (int *ofd,int *efd,const char *cmd,va_list ap) +{ +char **argv; +int obrass[2 ]; +int ebrass[2 ]; +pid_t pid; +int ret; + +if (pipe (obrass) <0 ) +return -1 ; + +if (pipe (ebrass) <0 ) + goto fail_out; + + pid =fork (); +if (pid <0 ) + goto fail; + +if (!pid) { + argv =get_args (cmd, ap); +if (!argv) +exit (-1 ); + +close (obrass[0 ]); +close (STDOUT_FILENO ); +if (dup2 (obrass[1 ],STDOUT_FILENO ) <0 ) +exit (-1 ); + +close (ebrass[0 ]); +close (STDERR_FILENO ); +if (dup2 (obrass[1 ],STDERR_FILENO ) <0 ) +exit (-1 ); + + ret =execvp (tracecmd_exec, argv); +exit (ret); + } + +close (obrass[1 ]); +close (ebrass[1 ]); + + *ofd = obrass[0 ]; + *efd = ebrass[0 ]; + +return pid; + + fail: +close (ebrass[0 ]); +close (ebrass[1 ]); + fail_out: +close (obrass[0 ]); +close (obrass[1 ]); +return -1 ; +} + +static int grep_it (const char *match,const char *cmd, ...) +{ +FILE *fp; +regex_t reg; +va_list ap; +char *buf =NULL ; +ssize_t n; +size_t l =0 ; +bool found = false; +int ofd; +int efd; +int pid; +int ret; + +if (regcomp (®, match,REG_ICASE |REG_NOSUB )) +return -1 ; + +va_start (ap, cmd); + pid =pipe_it (&ofd, &efd, cmd, ap); +va_end (ap); + +if (pid <0 ) { +regfree (®); +return -1 ; + } + + fp =fdopen (ofd,"r" ); +if (!fp) + goto out; + +do { + n =getline (&buf, &l, fp); +if (show_output && n >0 ) +printf ("%s" , buf); +if (n >0 &®exec (®, buf,0 ,NULL ,0 ) ==0 ) + found = true; + }while (n >=0 ); + +free (buf); + out: + ret =wait_for_exec (pid); +if (ret) + n =1 ; +if (fp) +fclose (fp); +else { +perror ("fp" ); +close (ofd); + } +close (efd); +regfree (®); + +return found ?0 :1 ; +} +static void test_trace_record_report (void ) {int ret; ret =run_trace ("record" ,TRACECMD_OUT ,"-e" ,"sched" ,"sleep" ,"1" ,NULL );CU_TEST (ret ==0 ); - ret =run_trace ("report" ,TRACECMD_IN ,NULL ); + ret =run_trace ("convert" ,"--file-version" ,"6" ,TRACECMD_IN ,TRACECMD_OUT2 ,NULL ); +CU_TEST (ret ==0 ); +} + +static void test_trace_convert6 (void ) +{ +struct stat st; +int ret; + +/* If the trace data is already created, just use it, otherwise make it again */ +if (stat (TRACECMD_FILE , &st) <0 ) { + ret =run_trace ("record" ,TRACECMD_OUT ,"-e" ,"sched" ,"sleep" ,"1" ,NULL ); +CU_TEST (ret ==0 ); + } + ret =grep_it ("[ \t]6[ \t]*\\[Version\\]" ,"dump" ,TRACECMD_IN2 ,NULL );CU_TEST (ret ==0 ); }static int test_suite_destroy (void ) {unlink (TRACECMD_FILE ); +unlink (TRACECMD_FILE2 );return 0 ; }
@@ -142,4 +290,6 @@ void test_tracecmd_lib(void) }CU_add_test (suite,"Simple record and report" , test_trace_record_report); +CU_add_test (suite,"Test convert from v7 to v6" , + test_trace_convert6); }
Signature · 1 line
2.35.1