[asterisk-commits] dvossel: branch dvossel/test_api r236021 - in /team/dvossel/test_api: include...

SVN commits to the Asterisk project asterisk-commits at lists.digium.com
Mon Dec 21 16:25:28 CST 2009


Author: dvossel
Date: Mon Dec 21 16:25:26 2009
New Revision: 236021

URL: http://svnview.digium.com/svn/asterisk?view=rev&rev=236021
Log:
addition of ast_test_status_update() function 

Modified:
    team/dvossel/test_api/include/asterisk/test.h
    team/dvossel/test_api/main/test.c
    team/dvossel/test_api/tests/test_heap.c

Modified: team/dvossel/test_api/include/asterisk/test.h
URL: http://svnview.digium.com/svn/asterisk/team/dvossel/test_api/include/asterisk/test.h?view=diff&rev=236021&r1=236020&r2=236021
==============================================================================
--- team/dvossel/test_api/include/asterisk/test.h (original)
+++ team/dvossel/test_api/include/asterisk/test.h Mon Dec 21 16:25:26 2009
@@ -55,7 +55,7 @@
 
 \code
    AST_TEST_DEFINE(sample_test_cb) \\The name of the callback function
-   {                                     \\The the function's body 
+   {                               \\The the function's body 
       switch (cmd) {
       case TEST_INIT:
           info->name = "sample_test";
@@ -130,14 +130,24 @@
 #endif
 
 enum ast_test_result_state {
-	AST_TEST_NOT_RUN = 0,
-	AST_TEST_PASS = 1,
-	AST_TEST_FAIL = 2,
+	AST_TEST_NOT_RUN,
+	AST_TEST_PASS,
+	AST_TEST_FAIL,
 };
 
 enum ast_test_command {
-	TEST_INIT = 0,
-	TEST_EXECUTE = 1,
+	TEST_INIT,
+	TEST_EXECUTE,
+};
+
+/*!
+ *  This struct is passed to ast_test_status_update() providing a place to push
+ *  the update to. In the future this structure may expand beyond simply being
+ *  a wrapper for cli args to including other status update options as well.
+ */
+struct ast_test_status_args {
+	/*! pointer to cli arg used for updating status */
+	struct ast_cli_args *cli;
 };
 
 /*!
@@ -145,6 +155,7 @@
  */
 struct ast_test_args {
 	struct ast_str *ast_test_error_str;  /*! optional error str to describe error result */
+	struct ast_test_status_args status_update;
 };
 
 /*!
@@ -157,15 +168,14 @@
 	const char *description;   /*! optional brief detailed description of test */
 };
 
-
-
 #ifdef TEST_FRAMEWORK
 /*!
  * \brief Generic test callback function
  *
  * \param error buffer string for failure results
  *
- * \return AST_TEST_PASS for pass, AST_TEST_FAIL for failure
+ * \retval AST_TEST_PASS for pass
+ * \retval AST_TEST_FAIL for failure
  */
 typedef enum ast_test_result_state (ast_test_cb_t)(struct ast_test_info *info, enum ast_test_command cmd, struct ast_test_args *args);
 
@@ -189,5 +199,16 @@
  */
 int ast_test_register(ast_test_cb_t *cb);
 
+/*!
+ * \brief update test's status during testing.
+ *
+ * \param ast_test_status_args defines everywhere the update should go.
+ *
+ * \retval 0 success
+ * \retval -1 failure
+ */
+int ast_test_status_update(struct ast_test_status_args *args, const char *fmt, ...)
+__attribute__((format(printf, 2, 3)));
+
 #endif /* TEST_FRAMEWORK */
 #endif /* _AST_TEST_H */

Modified: team/dvossel/test_api/main/test.c
URL: http://svnview.digium.com/svn/asterisk/team/dvossel/test_api/main/test.c?view=diff&rev=236021&r1=236020&r2=236021
==============================================================================
--- team/dvossel/test_api/main/test.c (original)
+++ team/dvossel/test_api/main/test.c Mon Dec 21 16:25:26 2009
@@ -37,28 +37,23 @@
 #include "asterisk/cli.h"
 #include "asterisk/term.h"
 #include "asterisk/version.h"
-
-#define DEFAULT_XML_PATH "asterisk_test_result.xml"
-#define DEFAULT_TXT_PATH "asterisk_test_result.txt"
+#include "asterisk/paths.h"
+#include "asterisk/time.h"
 
 /*! This array corrisponds to the values defined in the ast_test_state enum */
-static const char *test_result2str[] = {
+static const char * const test_result2str[] = {
 	[AST_TEST_NOT_RUN] = "NOT RUN",
 	[AST_TEST_PASS] = "PASS",
 	[AST_TEST_FAIL] = "FAIL",
 };
 
-/*! represents all the test result data for a single ast_test object */
-struct ast_test_result {
-};
-
 /*! holds all the information pertaining to a single defined test */
 struct ast_test {
-	struct ast_test_info info;       /*! holds test callback information */
-	struct ast_test_args args;       /*! function callback arguments */
-	enum ast_test_result_state state;       /*! current test state */
-	unsigned int time;               /*! time in ms test took */
-	ast_test_cb_t *cb;               /*! test callback function */
+	struct ast_test_info info;        /*! holds test callback information */
+	struct ast_test_args args;        /*! function callback arguments */
+	enum ast_test_result_state state; /*! current test state */
+	unsigned int time;                /*! time in ms test took */
+	ast_test_cb_t *cb;                /*! test callback function */
 	AST_LIST_ENTRY(ast_test) entry;
 };
 
@@ -89,6 +84,27 @@
 static struct ast_test *test_remove(ast_test_cb_t *cb);
 static int test_cat_cmp(const char *cat1, const char *cat2);
 
+int ast_test_status_update(struct ast_test_status_args *args, const char *fmt, ...)
+{
+	struct ast_str *buf = NULL;
+
+	if (args->cli) {
+		va_list ap;
+		if (!(buf = ast_str_create(128))) {
+			return -1;
+		}
+
+		va_start(ap, fmt);
+		ast_str_set_va(&buf, 0, fmt, ap);
+		va_end(ap);
+
+		ast_cli(args->cli->fd, "%s", ast_str_buffer(buf));
+	}
+
+	ast_free(buf);
+	return 0;
+}
+
 int ast_test_register(ast_test_cb_t *cb)
 {
 	struct ast_test *test;
@@ -147,6 +163,8 @@
 	test->state = test->cb(&test->info, TEST_EXECUTE, &test->args);
 	/* record the total time the test took */
 	test->time = ast_tvdiff_ms(ast_tvnow(), begin);
+	/* clear any status update args that may have been set */
+	memset(&test->args.status_update, 0, sizeof(struct ast_test_status_args));
 }
 
 static void test_xml_entry(struct ast_test *test, FILE *f)
@@ -198,16 +216,16 @@
  *
  * \param name of test to run (optional)
  * \param test category to run (optional)
- * \param fd for realtime cli test updates (optional)
+ * \param cli args for cli test updates (optional)
  *
  * \return number of tests executed.
  *
  * \note This function has three modes of operation
- * 1. When given a name and category, a matching individual test will execute if found.
- * 2. When given only a category all matching tests within that category will execute.
- * 3. If given no name or category all registered tests will execute.
- */
-static int test_execute_multiple(const char *name, const char *category, const int fd)
+ * -# When given a name and category, a matching individual test will execute if found.
+ * -# When given only a category all matching tests within that category will execute.
+ * -# If given no name or category all registered tests will execute.
+ */
+static int test_execute_multiple(const char *name, const char *category, struct ast_cli_args *cli)
 {
 	char result_buf[32] = { 0 };
 	struct ast_test *test = NULL;
@@ -245,9 +263,12 @@
 		}
 
 		if (execute) {
-			if (fd) {
-				ast_cli(fd, "START  %s - %s \n", test->info.category, test->info.name);
-			}
+			if (cli) {
+				ast_cli(cli->fd, "START  %s - %s \n", test->info.category, test->info.name);
+			}
+
+			/* set the test status update argument. it is ok if cli is NULL */
+			test->args.status_update.cli = cli;
 
 			/* execute the test and save results */
 			test_execute(test);
@@ -260,13 +281,13 @@
 				last_results.last_failed++;
 			}
 
-			if (fd) {
+			if (cli) {
 				term_color(result_buf,
 					test_result2str[test->state],
 					(test->state == AST_TEST_FAIL) ? COLOR_RED : COLOR_GREEN,
 					0,
 					sizeof(result_buf));
-				ast_cli(fd, "END    %s - %s Time: %dms Result: %s %s\n",
+				ast_cli(cli->fd, "END    %s - %s Time: %dms Result: %s %s\n",
 					test->info.category,
 					test->info.name,
 					test->time,
@@ -306,9 +327,9 @@
  * \retval -1 failure
  *
  * \note This function has three modes of operation.
- * 1. When given both a name and category, results will be generated for that single test.
- * 2. When given only a category, results for every test within the category will be generated.
- * 3. When given no name or category, results for every registered test will be generated.
+ * -# When given both a name and category, results will be generated for that single test.
+ * -# When given only a category, results for every test within the category will be generated.
+ * -# When given no name or category, results for every registered test will be generated.
  *
  * In order for the results to be generated, an xml and or txt file path must be provided.
  */
@@ -332,7 +353,6 @@
 			mode = TEST_CATEGORY;
 		}
 	}
-
 	/* open files for writing */
 	if (!ast_strlen_zero(xml_path)) {
 		if (!(f_xml = fopen(xml_path, "w"))) {
@@ -632,13 +652,13 @@
 
 		if ((a->argc == 3) && !strcmp(a->argv[2], "all")) { /* run all registered tests */
 			ast_cli(a->fd, "Running all available tests...\n\n");
-			test_execute_multiple(NULL, NULL, a->fd);
+			test_execute_multiple(NULL, NULL, a);
 		} else if (a->argc == 4) { /* run only tests within a category */
 			ast_cli(a->fd, "Running all available tests matching category %s\n\n", a->argv[3]);
-			test_execute_multiple(NULL, a->argv[3], a->fd);
+			test_execute_multiple(NULL, a->argv[3], a);
 		} else if (a->argc == 6) { /* run only a single test matching the category and name */
 			ast_cli(a->fd, "Running all available tests matching category %s and name %s\n\n", a->argv[5], a->argv[3]);
-			test_execute_multiple(a->argv[5], a->argv[3], a->fd);
+			test_execute_multiple(a->argv[5], a->argv[3], a);
 		} else {
 			return CLI_SHOWUSAGE;
 		}
@@ -732,7 +752,12 @@
 {
 	static const char * const option[] = { "xml", "txt", NULL };
 	const char *file = NULL;
+	const char *type = "";
+	int isxml = 0;
 	int res = 0;
+	struct ast_str *buf = NULL;
+	struct timeval time = ast_tvnow();
+
 	switch (cmd) {
 	case CLI_INIT:
 		e->command = "test generate results";
@@ -751,17 +776,34 @@
 		}
 		return NULL;
 	case CLI_HANDLER:
+
 		/* verify input */
 		if (a->argc < 4 || a->argc > 5) {
 			return CLI_SHOWUSAGE;
 		} else if (!strcmp(a->argv[3], "xml")) {
-			file = (a->argc == 5) ? a->argv[4] : DEFAULT_XML_PATH;
-			res = test_generate_results(NULL, NULL, file, NULL);
+			type = "xml";
+			isxml = 1;
 		} else if (!strcmp(a->argv[3], "txt")) {
-			file = (a->argc == 5) ? a->argv[4] : DEFAULT_TXT_PATH;
-			res = test_generate_results(NULL, NULL, NULL, (a->argc == 5) ? a->argv[4] : DEFAULT_TXT_PATH);
+			type = "txt";
 		} else {
 			return CLI_SHOWUSAGE;
+		}
+
+		if (a->argc == 5) {
+			file = a->argv[4];
+		} else {
+			if (!(buf = ast_str_create(256))) {
+				return NULL;
+			}
+			ast_str_set(&buf, 0, "%s/asterisk_test_results-%ld.%s", ast_config_AST_LOG_DIR, time.tv_sec, type);
+
+			file = ast_str_buffer(buf);
+		}
+
+		if (isxml) {
+			res = test_generate_results(NULL, NULL, file, NULL);
+		} else {
+			res = test_generate_results(NULL, NULL, NULL, file);
 		}
 
 		if (!res) {
@@ -769,6 +811,8 @@
 		} else {
 			ast_cli(a->fd, "Results Could Not Be Generated: %s\n", S_OR(file, ""));
 		}
+
+		ast_free(buf);
 	default:
 		return NULL;
 	}

Modified: team/dvossel/test_api/tests/test_heap.c
URL: http://svnview.digium.com/svn/asterisk/team/dvossel/test_api/tests/test_heap.c?view=diff&rev=236021&r1=236020&r2=236021
==============================================================================
--- team/dvossel/test_api/tests/test_heap.c (original)
+++ team/dvossel/test_api/tests/test_heap.c Mon Dec 21 16:25:26 2009
@@ -59,7 +59,11 @@
 {
 	struct ast_heap *h;
 	struct node *obj;
-	struct node nodes[3];
+	struct node nodes[3] = {
+		{ 1, } ,
+		{ 2, } ,
+		{ 3, } ,
+	};
 
 	switch (cmd) {
 	case TEST_INIT:
@@ -72,14 +76,12 @@
 		break;
 	}
 
-	nodes[0].val = nodes[0].index = 1;
-	nodes[1].val = nodes[1].index = 2;
-	nodes[2].val = nodes[2].index = 3;
-
 	if (!(h = ast_heap_create(8, node_cmp, offsetof(struct node, index)))) {
 		return AST_TEST_FAIL;
 	}
 
+	ast_test_status_update(&args->status_update, "pushing nodes\n");
+
 	ast_heap_push(h, &nodes[0]);
 
 	ast_heap_push(h, &nodes[1]);
@@ -91,6 +93,7 @@
 		return AST_TEST_FAIL;
 	}
 
+	ast_test_status_update(&args->status_update, "popping nodes\n");
 	obj = ast_heap_pop(h);
 	if (obj->val != 2) {
 		return AST_TEST_FAIL;




More information about the asterisk-commits mailing list