From 897b8e238be8ce71c0d484c31155fc8390cda6f0 Mon Sep 17 00:00:00 2001 From: David Goulet Date: Mon, 25 Jul 2011 16:50:12 -0400 Subject: [PATCH] Add kernel trace data structure test Test all kernel data structure found in ltt-sessiond/trace.c/.h. Also adds a utils.h having a pretty print for OK and FAIL. The session test is modified to use those prints. Better runall.sh which iterates over an array containing test binaries name and terminates when one test fails. Signed-off-by: David Goulet --- .gitignore | 1 + tests/Makefile.am | 5 +- tests/runall.sh | 14 ++- tests/test_kernel_data_trace.c | 198 +++++++++++++++++++++++++++++++++ tests/test_sessions.c | 17 +-- tests/utils.h | 26 +++++ 6 files changed, 249 insertions(+), 12 deletions(-) create mode 100644 tests/test_kernel_data_trace.c create mode 100644 tests/utils.h diff --git a/.gitignore b/.gitignore index 7fe9bf16a..e2c7857cc 100644 --- a/.gitignore +++ b/.gitignore @@ -29,3 +29,4 @@ lttng/lttng ltt-kconsumerd/ltt-kconsumerd tests/test_sessions +tests/test_kernel_data_trace diff --git a/tests/Makefile.am b/tests/Makefile.am index 761586e4c..29b8ba4f9 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -1,11 +1,14 @@ AM_CFLAGS=-I$(top_srcdir)/include -I$(top_srcdir)/libkernelctl \ -I$(top_srcdir)/liblttngctl -g -Wall -noinst_PROGRAMS = test_sessions +noinst_PROGRAMS = test_sessions test_kernel_data_trace SESSIONS=$(top_srcdir)/ltt-sessiond/session.c +KERN_DATA_TRACE=$(top_srcdir)/ltt-sessiond/trace.c test_sessions_SOURCES = test_sessions.c $(SESSIONS) +test_kernel_data_trace_SOURCES = test_kernel_data_trace.c $(KERN_DATA_TRACE) + check-am: ./runall.sh diff --git a/tests/runall.sh b/tests/runall.sh index e7f83ed11..b0ae4fd4c 100755 --- a/tests/runall.sh +++ b/tests/runall.sh @@ -19,12 +19,20 @@ #### ADD TESTS HERE #### -for bin in test_sessions; +test_suite=( test_sessions test_kernel_data_trace ) + +#### END TESTS HERE #### + +for bin in ${test_suite[@]}; do ./$bin + # Test must return 0 to pass. + if [ $? -ne 0 ]; then + echo -e '\e[1;31mFAIL\e[0m' + echo "" + exit 1 + fi done -#### END TESTS HERE #### - echo "" exit 0 diff --git a/tests/test_kernel_data_trace.c b/tests/test_kernel_data_trace.c new file mode 100644 index 000000000..93fa64cac --- /dev/null +++ b/tests/test_kernel_data_trace.c @@ -0,0 +1,198 @@ +/* + * Copyright (c) 2011 David Goulet + * + * 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 + * as published by the Free Software Foundation; only version 2 + * of the License. + * + * This program 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 General Public License for more details. + * + * 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. + */ + +#define _GNU_SOURCE +#include +#include +#include +#include +#include +#include +#include + +#include "ltt-sessiond/trace.h" +#include "utils.h" + +/* This path will NEVER be created in this test */ +#define PATH1 "/tmp/.test-junk-lttng" + +/* For lttngerr.h */ +int opt_quiet = 1; +int opt_verbose = 0; + +static const char alphanum[] = + "0123456789" + "ABCDEFGHIJKLMNOPQRSTUVWXYZ" + "abcdefghijklmnopqrstuvwxyz"; + +static struct ltt_kernel_session *kern; + +/* + * Return random string of 10 characters. + */ +static char *get_random_string(void) +{ + int i; + char *str = malloc(11); + + for (i = 0; i < 10; i++) { + str[i] = alphanum[rand() % (sizeof(alphanum) - 1)]; + } + + str[10] = '\0'; + + return str; +} + +static void create_one_kernel_session(void) +{ + printf("Create kernel session: "); + kern = trace_create_kernel_session(); + assert(kern != NULL); + PRINT_OK(); + + printf("Validating kernel session: "); + assert(kern->fd == 0); + assert(kern->metadata_stream_fd == 0); + assert(kern->kconsumer_fds_sent == 0); + assert(kern->channel_count == 0); + assert(kern->stream_count_global == 0); + assert(kern->metadata == NULL); + PRINT_OK(); + + /* Init list in order to avoid sefaults from cds_list_del */ + trace_destroy_kernel_session(kern); +} + +static void create_kernel_metadata(void) +{ + assert(kern != NULL); + + printf("Create kernel metadata: "); + kern->metadata = trace_create_kernel_metadata(PATH1); + assert(kern->metadata != NULL); + PRINT_OK(); + + printf("Validating kernel session metadata: "); + assert(kern->metadata->fd == 0); + assert(strlen(kern->metadata->pathname)); + assert(kern->metadata->conf != NULL); + assert(kern->metadata->conf->attr.overwrite + == DEFAULT_CHANNEL_OVERWRITE); + assert(kern->metadata->conf->attr.subbuf_size + == DEFAULT_CHANNEL_SUBBUF_SIZE); + assert(kern->metadata->conf->attr.num_subbuf + == DEFAULT_CHANNEL_SUBBUF_NUM); + assert(kern->metadata->conf->attr.switch_timer_interval + == DEFAULT_CHANNEL_SWITCH_TIMER); + assert(kern->metadata->conf->attr.read_timer_interval + == DEFAULT_CHANNEL_READ_TIMER); + assert(kern->metadata->conf->attr.output + == DEFAULT_KERNEL_CHANNEL_OUTPUT); + PRINT_OK(); + + trace_destroy_kernel_metadata(kern->metadata); +} + +static void create_kernel_channel(void) +{ + struct ltt_kernel_channel *chan; + struct lttng_channel attr; + + printf("Creating kernel channel: "); + chan = trace_create_kernel_channel(&attr, PATH1); + assert(chan != NULL); + PRINT_OK(); + + printf("Validating kernel channel: "); + assert(chan->fd == 0); + assert(chan->enabled == 1); + assert(strcmp(PATH1, chan->pathname) == 0); + assert(chan->stream_count == 0); + assert(chan->ctx == NULL); + assert(chan->channel->attr.overwrite == attr.attr.overwrite); + PRINT_OK(); + + /* Init list in order to avoid sefaults from cds_list_del */ + CDS_INIT_LIST_HEAD(&chan->list); + trace_destroy_kernel_channel(chan); +} + +static void create_kernel_event(void) +{ + struct ltt_kernel_event *event; + struct lttng_event ev; + + strncpy(ev.name, get_random_string(), LTTNG_SYM_NAME_LEN); + ev.type = LTTNG_EVENT_TRACEPOINT; + + printf("Creating kernel event: "); + event = trace_create_kernel_event(&ev); + assert(event != NULL); + PRINT_OK(); + + printf("Validating kernel event: "); + assert(event->fd == 0); + assert(event->enabled == 1); + assert(event->ctx == NULL); + assert(event->event->instrumentation == LTTNG_KERNEL_TRACEPOINT); + assert(strlen(event->event->name)); + PRINT_OK(); + + /* Init list in order to avoid sefaults from cds_list_del */ + CDS_INIT_LIST_HEAD(&event->list); + trace_destroy_kernel_event(event); +} + +static void create_kernel_stream(void) +{ + struct ltt_kernel_stream *stream; + + printf("Creating kernel stream: "); + stream = trace_create_kernel_stream(); + assert(stream != NULL); + PRINT_OK(); + + printf("Validating kernel stream: "); + assert(stream->fd == 0); + assert(stream->pathname == NULL); + assert(stream->state == 0); + PRINT_OK(); + + /* Init list in order to avoid sefaults from cds_list_del */ + CDS_INIT_LIST_HEAD(&stream->list); + trace_destroy_kernel_stream(stream); +} + +int main(int argc, char **argv) +{ + printf("\nTesting kernel data structures:\n-----------\n"); + + create_one_kernel_session(); + + create_kernel_metadata(); + create_kernel_channel(); + + + create_kernel_event(); + + create_kernel_stream(); + + /* Success */ + return 0; +} diff --git a/tests/test_sessions.c b/tests/test_sessions.c index f3ffe6a43..422c41603 100644 --- a/tests/test_sessions.c +++ b/tests/test_sessions.c @@ -26,6 +26,7 @@ #include #include "ltt-sessiond/session.h" +#include "utils.h" #define SESSION1 "test1" @@ -259,7 +260,7 @@ int main(int argc, char **argv) if (ret < 0) { return -1; } - printf("Success\n"); + PRINT_OK(); printf("Validating created session %s: ", SESSION1); tmp = find_session_by_name(SESSION1); @@ -274,21 +275,21 @@ int main(int argc, char **argv) lock_session(tmp); unlock_session(tmp); - printf("Success\n"); + PRINT_OK(); printf("Destroy 1 session %s: ", SESSION1); ret = destroy_one_session(SESSION1); if (ret < 0) { return -1; } - printf("Success\n"); + PRINT_OK(); printf("Two session with same name: "); ret = two_session_same_name(); if (ret < 0) { return -1; } - printf("Success\n"); + PRINT_OK(); empty_session_list(); @@ -297,14 +298,14 @@ int main(int argc, char **argv) if (ret < 0) { return -1; } - printf("Success\n"); + PRINT_OK(); printf("Fuzzing destroy_session argument: "); ret = fuzzing_destroy_args(); if (ret < 0) { return -1; } - printf("Success\n"); + PRINT_OK(); printf("Creating %d sessions: ", MAX_SESSIONS); for (i = 0; i < MAX_SESSIONS; i++) { @@ -316,7 +317,7 @@ int main(int argc, char **argv) } free(tmp_name); } - printf("Success\n"); + PRINT_OK(); printf("Destroying %d sessions: ", MAX_SESSIONS); for (i = 0; i < MAX_SESSIONS; i++) { @@ -328,7 +329,7 @@ int main(int argc, char **argv) } } } - printf("Success\n"); + PRINT_OK(); /* Session list must be 0 */ assert(!session_list->count); diff --git a/tests/utils.h b/tests/utils.h new file mode 100644 index 000000000..52893b207 --- /dev/null +++ b/tests/utils.h @@ -0,0 +1,26 @@ +/* + * Copyright (c) 2011 David Goulet + * + * 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 + * as published by the Free Software Foundation; only version 2 + * of the License. + * + * This program 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 General Public License for more details. + * + * 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 + +#define BRIGHT 1 +#define GREEN 32 +#define RED 31 + +#define PRINT_OK() printf("%c[%d;%dmOK%c[%dm\n", 0x1B, BRIGHT, GREEN, 0x1B, 0); +#define PRINT_FAIL() printf("%c[%d;%dmFAIL%c[%dm\n", 0x1B, BRIGHT, RED, 0x1B, 0); -- 2.34.1