From 5a04a4438186a02f78cdb68f89f672f4a6cdbcf4 Mon Sep 17 00:00:00 2001 From: Timothy Stack Date: Sun, 2 Mar 2014 08:55:00 -0800 Subject: [PATCH] [tests] add some tests that use headless mode --- TESTS_ENVIRONMENT.in | 7 ++- src/help.txt | 4 ++ src/lnav.cc | 71 ++++++++++++++++------------- src/lnav.hh | 2 +- src/lnav_commands.cc | 26 +++++++++++ src/lnav_log.cc | 2 - src/log_format.cc | 8 ++-- src/logfile.hh | 3 ++ test/Makefile.am | 1 + test/Makefile.in | 9 +++- test/test_sql.sh | 104 +++++++++++++++++++++++++++++++++++++++++++ 11 files changed, 197 insertions(+), 40 deletions(-) create mode 100644 test/test_sql.sh diff --git a/TESTS_ENVIRONMENT.in b/TESTS_ENVIRONMENT.in index e2e716ec..ec288d65 100644 --- a/TESTS_ENVIRONMENT.in +++ b/TESTS_ENVIRONMENT.in @@ -5,7 +5,10 @@ export top_srcdir # The top build directory, derived from the path to this script. top_builddir=`dirname $0` +export top_builddir +test_dir="@abssrcdir@/test" +export test_dir # The full path of the test case test_file=$1 @@ -52,8 +55,8 @@ run_test() { check_output() { diff -u ${test_file_base}_${test_num}.tmp - > ${test_file_base}_${test_num}.diff if test $? -ne 0; then - echo $LAST_TEST - echo $1 + echo $LAST_TEST + echo $1 cat ${test_file_base}_${test_num}.diff exit 1 fi diff --git a/src/help.txt b/src/help.txt index 8e613579..d645f5d5 100644 --- a/src/help.txt +++ b/src/help.txt @@ -414,6 +414,10 @@ COMMANDS Delete an SQL table created by the 'create-logline-table' command. + switch-to-view + Switch the display to the given view, which can be one of: + help, log, text, histogram, db, and schema. + SQL QUERIES (experimental) ----------- diff --git a/src/lnav.cc b/src/lnav.cc index 7548fa7d..126b40b4 100644 --- a/src/lnav.cc +++ b/src/lnav.cc @@ -135,7 +135,7 @@ static const int HIST_ZOOM_LEVELS = sizeof(HIST_ZOOM_VALUES) / static bookmark_type_t BM_EXAMPLE; static bookmark_type_t BM_QUERY; -const char *lnav_view_strings[LNV__MAX] = { +const char *lnav_view_strings[LNV__MAX + 1] = { "log", "text", "help", @@ -144,6 +144,8 @@ const char *lnav_view_strings[LNV__MAX] = { "db", "example", "schema", + + NULL }; static const char *view_titles[LNV__MAX] = { @@ -1040,6 +1042,28 @@ void redo_search(lnav_view_t view_index) lnav_data.ld_scroll_broadcaster.invoke(tc); } +static void open_schema_view(void) +{ + textview_curses *schema_tc = &lnav_data.ld_views[LNV_SCHEMA]; + string schema; + + dump_sqlite_schema(lnav_data.ld_db, schema); + + schema += "\n\n-- Virtual Table Definitions --\n\n"; + for (log_vtab_manager::iterator vtab_iter = + lnav_data.ld_vtab_manager->begin(); + vtab_iter != lnav_data.ld_vtab_manager->end(); + ++vtab_iter) { + schema += vtab_iter->second->get_table_statement(); + } + + if (schema_tc->get_sub_source() != NULL) { + delete schema_tc->get_sub_source(); + } + + schema_tc->set_sub_source(new plain_text_source(schema)); +} + /** * Ensure that the view is on the top of the view stack. * @@ -1050,6 +1074,10 @@ void ensure_view(textview_curses *expected_tc) textview_curses *tc = lnav_data.ld_view_stack.top(); if (tc != expected_tc) { + if (expected_tc == &lnav_data.ld_views[LNV_SCHEMA]) { + open_schema_view(); + } + toggle_view(expected_tc); } } @@ -2008,29 +2036,6 @@ string execute_command(string cmdline) return msg; } -static void open_schema_view(void) -{ - textview_curses *schema_tc = &lnav_data.ld_views[LNV_SCHEMA]; - string schema; - - dump_sqlite_schema(lnav_data.ld_db, schema); - - schema += "\n\n-- Virtual Table Definitions --\n\n"; - for (log_vtab_manager::iterator vtab_iter = - lnav_data.ld_vtab_manager->begin(); - vtab_iter != lnav_data.ld_vtab_manager->end(); - ++vtab_iter) { - schema += vtab_iter->second->get_table_statement(); - } - - if (schema_tc->get_sub_source() != NULL) { - delete schema_tc->get_sub_source(); - } - - schema_tc->set_sub_source(new plain_text_source(schema)); - ensure_view(schema_tc); -} - string execute_sql(string sql, string &alt_msg) { db_label_source & dls = lnav_data.ld_db_rows; @@ -2047,7 +2052,7 @@ string execute_sql(string sql, string &alt_msg) if (stmt_str == ".schema") { alt_msg = ""; - open_schema_view(); + ensure_view(&lnav_data.ld_views[LNV_SCHEMA]); lnav_data.ld_mode = LNM_PAGING; return ""; @@ -3123,10 +3128,13 @@ static void looper(void) lnav_data.ld_rl_view = &rlc; - lnav_data.ld_rl_view-> - add_possibility(LNM_COMMAND, "graph", "\\d+(?:\\.\\d+)?"); - lnav_data.ld_rl_view-> - add_possibility(LNM_COMMAND, "graph", "([:= \\t]\\d+(?:\\.\\d+)?)"); + lnav_data.ld_rl_view->add_possibility( + LNM_COMMAND, "graph", "\\d+(?:\\.\\d+)?"); + lnav_data.ld_rl_view->add_possibility( + LNM_COMMAND, "graph", "([:= \\t]\\d+(?:\\.\\d+)?)"); + + lnav_data.ld_rl_view->add_possibility( + LNM_COMMAND, "viewname", lnav_view_strings); (void)signal(SIGINT, sigint); (void)signal(SIGTERM, sigint); @@ -3277,7 +3285,7 @@ static void looper(void) if (!initial_build && lnav_data.ld_log_source.text_line_count() == 0 && !lnav_data.ld_other_files.empty()) { - open_schema_view(); + ensure_view(&lnav_data.ld_views[LNV_SCHEMA]); } if (!initial_build && lnav_data.ld_flags & LNF_HELP) { @@ -4102,7 +4110,8 @@ int main(int argc, char *argv[]) for (vis_line_t vl = tc->get_top(); vl < tc->get_inner_height(); ++vl, ++y) { - while (los->list_value_for_overlay(*tc, y, al)) { + while (los != NULL && + los->list_value_for_overlay(*tc, y, al)) { printf("%s\n", line.c_str()); ++y; } diff --git a/src/lnav.hh b/src/lnav.hh index 355c55c0..d95cd644 100644 --- a/src/lnav.hh +++ b/src/lnav.hh @@ -106,7 +106,7 @@ typedef enum { LNV__MAX } lnav_view_t; -extern const char *lnav_view_strings[LNV__MAX]; +extern const char *lnav_view_strings[LNV__MAX + 1]; /** The status bars. */ typedef enum { diff --git a/src/lnav_commands.cc b/src/lnav_commands.cc index a0161829..0a28f6ef 100644 --- a/src/lnav_commands.cc +++ b/src/lnav_commands.cc @@ -1178,6 +1178,31 @@ static string com_add_test(string cmdline, vector &args) return retval; } +static string com_switch_to_view(string cmdline, vector &args) +{ + string retval = ""; + + if (args.size() == 0) { + args.push_back("viewname"); + } + else if (args.size() > 1) { + bool found = false; + + for (int lpc = 0; lnav_view_strings[lpc] && !found; lpc++) { + if (strcasecmp(args[1].c_str(), lnav_view_strings[lpc]) == 0) { + ensure_view(&lnav_data.ld_views[lpc]); + found = true; + } + } + + if (!found) { + retval = "error: invalid view name -- " + args[1]; + } + } + + return retval; +} + void init_lnav_commands(readline_context::command_map_t &cmd_map) { cmd_map["adjust-log-time"] = com_adjust_log_time; @@ -1203,6 +1228,7 @@ void init_lnav_commands(readline_context::command_map_t &cmd_map) cmd_map["partition-name"] = com_partition_name; cmd_map["session"] = com_session; cmd_map["summarize"] = com_summarize; + cmd_map["switch-to-view"] = com_switch_to_view; if (getenv("LNAV_SRC") != NULL) { cmd_map["add-test"] = com_add_test; diff --git a/src/lnav_log.cc b/src/lnav_log.cc index acff7622..6f3a997b 100644 --- a/src/lnav_log.cc +++ b/src/lnav_log.cc @@ -209,8 +209,6 @@ static void sigabrt(int sig) } if (lnav_log_orig_termios != NULL) { - char *bp, buf[1024]; - tcsetattr(STDOUT_FILENO, TCSAFLUSH, lnav_log_orig_termios); } fprintf(stderr, CRASH_MSG, crash_path); diff --git a/src/log_format.cc b/src/log_format.cc index 3681e607..df1d4829 100644 --- a/src/log_format.cc +++ b/src/log_format.cc @@ -616,6 +616,7 @@ void external_log_format::annotate(shared_buffer_ref &line, for (size_t lpc = 0; lpc < pat.p_value_by_index.size(); lpc++) { const value_def &vd = pat.p_value_by_index[lpc]; const struct scaling_factor *scaling = NULL; + pcre_context::capture_t *cap = pc[vd.vd_index]; shared_buffer_ref field; if (vd.vd_unit_field_index >= 0) { @@ -634,7 +635,8 @@ void external_log_format::annotate(shared_buffer_ref &line, } } - field.subset(line, pc[vd.vd_index]->c_begin, pc[vd.vd_index]->length()); + log_info("field %s %s", vd.vd_name.c_str(), string(&line.get_data()[cap->c_begin], cap->length()).c_str()); + field.subset(line, cap->c_begin, cap->length()); values.push_back(logline_value(vd.vd_name, vd.vd_kind, @@ -642,8 +644,8 @@ void external_log_format::annotate(shared_buffer_ref &line, vd.vd_identifier, scaling, vd.vd_column, - pc[vd.vd_index]->c_begin, - pc[vd.vd_index]->c_end)); + cap->c_begin, + cap->c_end)); if (pc[vd.vd_index]->c_begin != -1 && vd.vd_identifier) { lr.lr_start = pc[vd.vd_index]->c_begin; diff --git a/src/logfile.hh b/src/logfile.hh index 33c4d63f..ef599497 100644 --- a/src/logfile.hh +++ b/src/logfile.hh @@ -248,6 +248,9 @@ public: if (next_line == this->end()) { retval = this->lf_index_size - ll->get_offset(); + if (retval > 0) { + retval -= 1; + } } else { retval = next_line->get_offset() - ll->get_offset() - 1; diff --git a/test/Makefile.am b/test/Makefile.am index 32b0e58a..02df099a 100644 --- a/test/Makefile.am +++ b/test/Makefile.am @@ -192,6 +192,7 @@ TESTS = \ test_grep_proc2 \ test_hist_source \ test_pcrepp \ + test_sql.sh \ test_sql_coll_func.sh \ test_sql_fs_func.sh \ test_sql_str_func.sh \ diff --git a/test/Makefile.in b/test/Makefile.in index 4009e9fb..74c2aa63 100644 --- a/test/Makefile.in +++ b/test/Makefile.in @@ -95,7 +95,7 @@ TESTS = test_ansi_scrubber$(EXEEXT) test_auto_fd$(EXEEXT) \ test_auto_mem$(EXEEXT) test_bookmarks$(EXEEXT) \ test_line_buffer.sh test_line_buffer2$(EXEEXT) \ test_listview.sh test_grep_proc.sh test_grep_proc2$(EXEEXT) \ - test_hist_source$(EXEEXT) test_pcrepp$(EXEEXT) \ + test_hist_source$(EXEEXT) test_pcrepp$(EXEEXT) test_sql.sh \ test_sql_coll_func.sh test_sql_fs_func.sh test_sql_str_func.sh \ test_view_colors.sh test_vt52_curses.sh \ test_top_status$(EXEEXT) test_data_parser.sh \ @@ -1187,6 +1187,13 @@ test_pcrepp.log: test_pcrepp$(EXEEXT) --log-file $$b.log --trs-file $$b.trs \ $(am__common_driver_flags) $(AM_LOG_DRIVER_FLAGS) $(LOG_DRIVER_FLAGS) -- $(LOG_COMPILE) \ "$$tst" $(AM_TESTS_FD_REDIRECT) +test_sql.sh.log: test_sql.sh + @p='test_sql.sh'; \ + b='test_sql.sh'; \ + $(am__check_pre) $(LOG_DRIVER) --test-name "$$f" \ + --log-file $$b.log --trs-file $$b.trs \ + $(am__common_driver_flags) $(AM_LOG_DRIVER_FLAGS) $(LOG_DRIVER_FLAGS) -- $(LOG_COMPILE) \ + "$$tst" $(AM_TESTS_FD_REDIRECT) test_sql_coll_func.sh.log: test_sql_coll_func.sh @p='test_sql_coll_func.sh'; \ b='test_sql_coll_func.sh'; \ diff --git a/test/test_sql.sh b/test/test_sql.sh new file mode 100644 index 00000000..a4a6494a --- /dev/null +++ b/test/test_sql.sh @@ -0,0 +1,104 @@ +#! /bin/bash + +lnav_test="${top_builddir}/src/lnav-test" + + +run_test ${lnav_test} -n \ + -c ";select * from access_log" \ + -c ':write-csv-to -' \ + ${test_dir}/logfile_access_log.0 + +check_output "access_log table is not working" <,/vmw/cgi/tramp,gPXE/0.9.7,-,HTTP/1.0,134,200 +1,p.0,2009-07-20 22:59:29.000,3000,error,0,192.168.202.254,GET,-,,/vmw/vSphere/default/vmkboot.gz,gPXE/0.9.7,-,HTTP/1.0,46210,404 +2,p.0,2009-07-20 22:59:29.000,0,info,0,192.168.202.254,GET,-,,/vmw/vSphere/default/vmkernel.gz,gPXE/0.9.7,-,HTTP/1.0,78929,200 +EOF + + +run_test ${lnav_test} -n \ + -c ";select * from access_log where log_level >= 'warning'" \ + -c ':write-csv-to -' \ + ${test_dir}/logfile_access_log.0 + +check_output "loglevel collator is not working" <,/vmw/vSphere/default/vmkboot.gz,gPXE/0.9.7,-,HTTP/1.0,46210,404 +EOF + + +run_test ${lnav_test} -n \ + -c ";select * from syslog_log" \ + -c ':write-csv-to -' \ + ${test_dir}/logfile_syslog.0 + +check_output "syslog_log table is not working" <,sudo +EOF + + +run_test ${lnav_test} -n \ + -c ";select * from syslog_log where log_time >= datetime('2013-11-03T09:47:02.000')" \ + -c ':write-csv-to -' \ + ${test_dir}/logfile_syslog.0 + +check_output "log_time collation is wrong" <,sudo +EOF + + +run_test ${lnav_test} -n \ + -c ':filter-in sudo' \ + -c ";select * from logline" \ + -c ':write-csv-to -' \ + ${test_dir}/logfile_syslog.0 + +check_output "logline table is not working" <,sudo,timstack,pts/6,/auto/wstimstack/rpms/lbuild/test,root,/usr/bin/tail /var/log/messages +EOF + + +run_test ${lnav_test} -n \ + -c ";update access_log set log_mark = 1 where sc_bytes > 60000" \ + -c ':write-to -' \ + ${test_dir}/logfile_access_log.0 + +check_output "setting log_mark is not working" <