vlib: improve test coverage 89/23889/2
authorDave Barach <dave@barachs.net>
Mon, 9 Dec 2019 15:45:47 +0000 (10:45 -0500)
committerFlorin Coras <florin.coras@gmail.com>
Mon, 9 Dec 2019 21:19:47 +0000 (21:19 +0000)
Add tests.
Remove unused rule-based parser code.

Type: test

Signed-off-by: Dave Barach <dave@barachs.net>
Change-Id: I3ca3a9dd9da8ee8f3a47004c98f2f5644db10057

src/plugins/unittest/vlib_test.c
src/vlib/CMakeLists.txt
src/vlib/cli.c
src/vlib/cli.h
src/vlib/cli_funcs.h [deleted file]
src/vlib/vlib.h
test/test_vlib.py

index 49c2ef2..3154b7d 100644 (file)
@@ -133,7 +133,7 @@ test_vlib_command_fn (vlib_main_t * vm,
 VLIB_CLI_COMMAND (test_vlib_command, static) =
 {
   .path = "test vlib",
-  .short_help = "vlib code coverate unit test",
+  .short_help = "vlib code coverage unit test",
   .function = test_vlib_command_fn,
 };
 /* *INDENT-ON* */
@@ -189,6 +189,45 @@ VLIB_CLI_COMMAND (test_format_vlib_command, static) =
 };
 /* *INDENT-ON* */
 
+static clib_error_t *
+test_vlib2_command_fn (vlib_main_t * vm,
+                      unformat_input_t * input, vlib_cli_command_t * cmd)
+{
+  u8 *s;
+  u8 **result;
+
+  s = format (0, "show       ");
+  result = vlib_cli_get_possible_completions (s);
+  vec_free (result);
+  vec_free (s);
+
+  s = 0;
+  vec_add1 (s, 0);
+  result = vlib_cli_get_possible_completions (s);
+  vec_free (result);
+  vec_free (s);
+
+  s = format (0, "show            ?");
+  result = vlib_cli_get_possible_completions (s);
+  vec_free (result);
+  vec_free (s);
+
+  return 0;
+}
+
+/* *INDENT-OFF* */
+VLIB_CLI_COMMAND (test_vlib2_command, static) =
+{
+  .path = "test vlib2",
+  .short_help = "vlib code coverage unit test #2",
+  .function = test_vlib2_command_fn,
+};
+/* *INDENT-ON* */
+
+
+
+
+
 /*
  * fd.io coding-style-patch-verification: ON
  *
index 1cecf5f..cc27d88 100644 (file)
@@ -86,7 +86,6 @@ add_vpp_library(vlib
   buffer_funcs.h
   buffer.h
   buffer_node.h
-  cli_funcs.h
   cli.h
   counter.h
   counter_types.h
index ec27c48..0ecdac3 100644 (file)
@@ -210,29 +210,6 @@ unformat_vlib_cli_sub_command (unformat_input_t * i, va_list * args)
   vlib_cli_main_t *cm = &vm->cli_main;
   uword *match_bitmap, is_unique, index;
 
-  {
-    vlib_cli_sub_rule_t *sr;
-    vlib_cli_parse_rule_t *r;
-    vec_foreach (sr, c->sub_rules)
-    {
-      void **d;
-      r = vec_elt_at_index (cm->parse_rules, sr->rule_index);
-      vec_add2 (cm->parse_rule_data, d, 1);
-      vec_reset_length (d[0]);
-      if (r->data_size)
-       d[0] = _vec_resize (d[0],
-                           /* length increment */ 1,
-                           r->data_size,
-                           /* header_bytes */ 0,
-                           /* data align */ sizeof (uword));
-      if (unformat_user (i, r->unformat_function, vm, d[0]))
-       {
-         *result = vec_elt_at_index (cm->commands, sr->command_index);
-         return 1;
-       }
-    }
-  }
-
   match_bitmap = vlib_cli_sub_command_match (c, i);
   is_unique = clib_bitmap_count_set_bits (match_bitmap) == 1;
   index = ~0;
@@ -362,50 +339,12 @@ format_vlib_cli_command_help (u8 * s, va_list * args)
   return s;
 }
 
-static u8 *
-format_vlib_cli_parse_rule_name (u8 * s, va_list * args)
-{
-  vlib_cli_parse_rule_t *r = va_arg (*args, vlib_cli_parse_rule_t *);
-  return format (s, "<%U>", format_c_identifier, r->name);
-}
-
 static u8 *
 format_vlib_cli_path (u8 * s, va_list * args)
 {
   u8 *path = va_arg (*args, u8 *);
-  int i, in_rule;
-  in_rule = 0;
-  for (i = 0; i < vec_len (path); i++)
-    {
-      switch (path[i])
-       {
-       case '%':
-         in_rule = 1;
-         vec_add1 (s, '<');    /* start of <RULE> */
-         break;
-
-       case '_':
-         /* _ -> space in rules. */
-         vec_add1 (s, in_rule ? ' ' : '_');
-         break;
-
-       case ' ':
-         if (in_rule)
-           {
-             vec_add1 (s, '>');        /* end of <RULE> */
-             in_rule = 0;
-           }
-         vec_add1 (s, ' ');
-         break;
-
-       default:
-         vec_add1 (s, path[i]);
-         break;
-       }
-    }
 
-  if (in_rule)
-    vec_add1 (s, '>');         /* terminate <RULE> */
+  s = format (s, "%v", path);
 
   return s;
 }
@@ -415,13 +354,10 @@ all_subs (vlib_cli_main_t * cm, vlib_cli_command_t * subs, u32 command_index)
 {
   vlib_cli_command_t *c = vec_elt_at_index (cm->commands, command_index);
   vlib_cli_sub_command_t *sc;
-  vlib_cli_sub_rule_t *sr;
 
   if (c->function)
     vec_add1 (subs, c[0]);
 
-  vec_foreach (sr, c->sub_rules)
-    subs = all_subs (cm, subs, sr->command_index);
   vec_foreach (sc, c->sub_commands) subs = all_subs (cm, subs, sc->index);
 
   return subs;
@@ -456,6 +392,8 @@ vlib_cli_dispatch_sub_commands (vlib_main_t * vm,
   unformat_input_t sub_input;
   u8 *string;
   uword is_main_dispatch = cm == &vm->cli_main;
+  uword value;
+  u8 *key;
 
   parent = vec_elt_at_index (cm->commands, parent_command_index);
   if (is_main_dispatch && unformat (input, "help"))
@@ -484,49 +422,34 @@ vlib_cli_dispatch_sub_commands (vlib_main_t * vm,
        vlib_cli_output (vm, "%U", format_vlib_cli_command_help, c,
                         /* is_long */ 1);
 
-      else if (vec_len (c->sub_commands) + vec_len (c->sub_rules) == 0)
+      else if (vec_len (c->sub_commands) == 0)
        vlib_cli_output (vm, "%v: no sub-commands", c->path);
 
       else
        {
-         vlib_cli_sub_command_t *sc;
-         vlib_cli_sub_rule_t *sr, *subs;
-
-         subs = vec_dup (c->sub_rules);
-
-         /* Add in rules if any. */
-         vec_foreach (sc, c->sub_commands)
-         {
-           vec_add2 (subs, sr, 1);
-           sr->name = sc->name;
-           sr->command_index = sc->index;
-           sr->rule_index = ~0;
-         }
+         vlib_cli_sub_rule_t *sr, *subs = 0;
+
+          /* *INDENT-OFF* */
+          hash_foreach_mem (key, value, c->sub_command_index_by_name,
+          ({
+            (void) key;
+            vec_add2 (subs, sr, 1);
+            sr->name = c->sub_commands[value].name;
+            sr->command_index = value;
+            sr->rule_index = ~0;
+          }));
+          /* *INDENT-ON* */
 
          vec_sort_with_function (subs, vlib_cli_cmp_rule);
 
          for (i = 0; i < vec_len (subs); i++)
            {
              vlib_cli_command_t *d;
-             vlib_cli_parse_rule_t *r;
 
              d = vec_elt_at_index (cm->commands, subs[i].command_index);
-             r =
-               subs[i].rule_index != ~0 ? vec_elt_at_index (cm->parse_rules,
-                                                            subs
-                                                            [i].rule_index) :
-               0;
-
-             if (r)
-               vlib_cli_output
-                 (vm, "  %-30U %U",
-                  format_vlib_cli_parse_rule_name, r,
-                  format_vlib_cli_command_help, d, /* is_long */ 0);
-             else
-               vlib_cli_output
-                 (vm, "  %-30v %U",
-                  subs[i].name,
-                  format_vlib_cli_command_help, d, /* is_long */ 0);
+             vlib_cli_output
+               (vm, "  %-30v %U", subs[i].name,
+                format_vlib_cli_command_help, d, /* is_long */ 0);
            }
 
          vec_free (subs);
@@ -669,8 +592,8 @@ vlib_cli_dispatch_sub_commands (vlib_main_t * vm,
                  if (c_error)
                    {
                      vec_add1 (c_error->what, 0);
-                     ed->err = elog_string (&vm->elog_main,
-                                            (char *) c_error->what);
+                     ed->err =
+                       elog_string (&vm->elog_main, (char *) c_error->what);
                      _vec_len (c_error->what) -= 1;
                    }
                  else
@@ -732,7 +655,6 @@ vlib_cli_input (vlib_main_t * vm,
                vlib_cli_output_function_t * function, uword function_arg)
 {
   vlib_process_t *cp = vlib_get_current_process (vm);
-  vlib_cli_main_t *cm = &vm->cli_main;
   clib_error_t *error;
   vlib_cli_output_function_t *save_function;
   uword save_function_arg;
@@ -746,9 +668,8 @@ vlib_cli_input (vlib_main_t * vm,
 
   do
     {
-      vec_reset_length (cm->parse_rule_data);
-      error = vlib_cli_dispatch_sub_commands (vm, &vm->cli_main, input,        /* parent */
-                                             0);
+      error = vlib_cli_dispatch_sub_commands (vm, &vm->cli_main, input,
+                                             /* parent */ 0);
     }
   while (!error && !unformat (input, "%U", unformat_eof));
 
@@ -1296,14 +1217,6 @@ add_sub_command (vlib_cli_main_t * cm, uword parent_index, uword child_index)
          return;
        }
 
-      q = hash_get_mem (cm->parse_rule_index_by_name, sub_name);
-      if (!q)
-       {
-         clib_error ("reference to unknown rule `%%%v' in path `%v'",
-                     sub_name, c->path);
-         return;
-       }
-
       hash_set_mem (p->sub_rule_index_by_name, sub_name,
                    vec_len (p->sub_rules));
       vec_add2 (p->sub_rules, sr, 1);
@@ -1492,6 +1405,8 @@ vlib_cli_register (vlib_main_t * vm, vlib_cli_command_t * c)
   return 0;
 }
 
+#if 0
+/* $$$ turn back on again someday, maybe */
 clib_error_t *
 vlib_cli_register_parse_rule (vlib_main_t * vm, vlib_cli_parse_rule_t * r_reg)
 {
@@ -1524,8 +1439,6 @@ vlib_cli_register_parse_rule (vlib_main_t * vm, vlib_cli_parse_rule_t * r_reg)
   return error;
 }
 
-#if 0
-/* $$$ turn back on again someday, maybe */
 static clib_error_t *vlib_cli_register_parse_rules (vlib_main_t * vm,
                                                    vlib_cli_parse_rule_t *
                                                    lo,
index 6d6d7e2..df9ed72 100644 (file)
@@ -140,15 +140,6 @@ typedef struct
   /* Hash table mapping normalized path to index into all_commands. */
   uword *command_index_by_path;
 
-  /* Vector of all known parse rules. */
-  vlib_cli_parse_rule_t *parse_rules;
-
-  /* Hash table mapping parse rule name to index into parse_rule vector. */
-  uword *parse_rule_index_by_name;
-
-  /* Data parsed for rules. */
-  void **parse_rule_data;
-
   /* registration list added by constructors */
   vlib_cli_command_t *cli_command_registrations;
 
diff --git a/src/vlib/cli_funcs.h b/src/vlib/cli_funcs.h
deleted file mode 100644 (file)
index 78aef73..0000000
+++ /dev/null
@@ -1,58 +0,0 @@
-/*
- * Copyright (c) 2015 Cisco and/or its affiliates.
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at:
- *
- *     http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-/*
- * cli_funcs.h: VLIB CLI related functions/inlines
- *
- * Copyright (c) 2008 Eliot Dresselhaus
- *
- * Permission is hereby granted, free of charge, to any person obtaining
- * a copy of this software and associated documentation files (the
- * "Software"), to deal in the Software without restriction, including
- * without limitation the rights to use, copy, modify, merge, publish,
- * distribute, sublicense, and/or sell copies of the Software, and to
- * permit persons to whom the Software is furnished to do so, subject to
- * the following conditions:
- *
- * The above copyright notice and this permission notice shall be
- * included in all copies or substantial portions of the Software.
- *
- *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
- *  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
- *  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
- *  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
- *  LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
- *  OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
- *  WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
- */
-
-#ifndef included_vlib_cli_funcs_h
-#define included_vlib_cli_funcs_h
-
-always_inline void *
-vlib_cli_get_parse_rule_result (vlib_main_t * vm, uword index)
-{
-  vlib_cli_main_t *cm = &vm->cli_main;
-  return vec_elt (cm->parse_rule_data, index);
-}
-
-#endif /* included_vlib_cli_funcs_h */
-
-/*
- * fd.io coding-style-patch-verification: ON
- *
- * Local Variables:
- * eval: (c-set-style "gnu")
- * End:
- */
index 8f59cae..8f0ea83 100644 (file)
@@ -70,7 +70,6 @@ typedef u32 vlib_log_class_t;
 #include <vlib/threads.h>
 #include <vlib/physmem_funcs.h>
 #include <vlib/buffer_funcs.h>
-#include <vlib/cli_funcs.h>
 #include <vlib/error_funcs.h>
 #include <vlib/format_funcs.h>
 #include <vlib/node_funcs.h>
index aa58667..eedfb2f 100644 (file)
@@ -100,6 +100,7 @@ class TestVlib(VppTestCase):
                 "set node function ethernet-input default",
                 "set node function ethernet-input bozo",
                 "set node function ethernet-input",
+                "show \t",
                 ]
 
         for cmd in cmds:
@@ -126,10 +127,35 @@ class TestVlib(VppTestCase):
                 "   incrementing 30\n"
                 "   }\n"
                 "}\n",
+                "elog trace",
+                "elog trace enable",
+                "elog trace api cli barrier",
                 "pa en",
+                "show interface bogus",
+                "elog trace disable api cli barrier",
+                "elog trace circuit-node ethernet-input",
+                "elog trace circuit-node ethernet-input disable",
                 "clear interfaces",
                 "test vlib",
+                "test vlib2",
+                "show memory api-segment stats-segment main-heap verbose",
+                "leak-check { show memory }",
+                "show cpu",
+                "memory-trace main-heap",
+                "memory-trace main-heap api-segment stats-segment",
+                "leak-check { show version }",
+                "show version ?",
+                "comment { show version }",
+                "uncomment { show version }",
+                "show memory main-heap",
+                "show memory bogus",
+                "choices",
+                "test heap-validate",
+                "memory-trace main-heap disable",
                 "show buffers",
+                "show eve",
+                "show help",
+                "show ip ",
                 ]
 
         for cmd in cmds: