[svn-commits] dvossel: branch dvossel/test_api r234375 - in /team/dvossel/test_api: include...

SVN commits to the Digium repositories svn-commits at lists.digium.com
Fri Dec 11 15:02:44 CST 2009


Author: dvossel
Date: Fri Dec 11 15:02:40 2009
New Revision: 234375

URL: http://svnview.digium.com/svn/asterisk?view=rev&rev=234375
Log:
start of text xml and text result generation

Modified:
    team/dvossel/test_api/include/asterisk/test.h
    team/dvossel/test_api/main/test.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=234375&r1=234374&r2=234375
==============================================================================
--- team/dvossel/test_api/include/asterisk/test.h (original)
+++ team/dvossel/test_api/include/asterisk/test.h Fri Dec 11 15:02:40 2009
@@ -118,7 +118,8 @@
  * 
  * \param name of test result to generate (optional)
  * \param test catagory to generate (optional)
- * \param path to xml file to generate, default if NULL (optional)
+ * \param path to xml file to generate. (optional)
+ * \param path to txt file to generate, (optional)
  *
  * \return 0 if results were generated, 1 if error  
  *
@@ -127,8 +128,11 @@
  * 2. Given only a catagory results for every test within the catagory will be generated
  * 3. If given no name or catagory 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
+ * 
  */
-int ast_test_results(const char *name, const char *catagory, const char *xml_path);
+int ast_test_generate_results(const char *name, const char *catagory, const char *xml_path, const char *txt_path);
+
 
 #endif
 

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=234375&r1=234374&r2=234375
==============================================================================
--- team/dvossel/test_api/main/test.c (original)
+++ team/dvossel/test_api/main/test.c Fri Dec 11 15:02:40 2009
@@ -36,6 +36,10 @@
 #include "asterisk/utils.h"
 #include "asterisk/cli.h"
 #include "asterisk/term.h"
+
+
+#define DEFAULT_XML_PATH "asterisk_test_result.xml"
+#define DEFAULT_TXT_PATH "asterisk_test_result.txt"
 
 enum ast_test_result_state {
 	AST_TEST_NOT_RUN = 0,
@@ -74,7 +78,6 @@
 /*! List of registered test definitions */
 static AST_LIST_HEAD_STATIC(tests, ast_test);
 
-
 /*! static function prototypes */
 static struct ast_test *test_alloc(const char *name, const char *catagory, const char *summary, const char *description, ast_test_cb_t *cb);
 static struct ast_test *test_free(struct ast_test *test);
@@ -82,7 +85,6 @@
 static struct ast_test *test_remove(ast_test_cb_t *cb);
 /*static int test_generate_xml(void);*/
 
-
 int ast_test_register(const char *name, const char *catagory, const char *summary, const char *description, ast_test_cb_t *cb)
 {
 	struct ast_test *test;
@@ -129,6 +131,39 @@
 	begin = ast_tvnow();
 	test->result.state = test->cb(test->result.error, ARRAY_LEN(test->result.error));
 	test->result.time = ast_tvdiff_ms(ast_tvnow(), begin);
+}
+
+static void __test_xml_entry(struct ast_test *test, FILE *f)
+{
+	if (!f || !test) {
+		return;
+	}
+
+	fprintf(f, "\n<test>\n");
+	fprintf(f, "<name>%s</name>\n", S_OR(test->name, "NA"));
+	fprintf(f, "<catagory>%s</catagory>\n", S_OR(test->catagory, "NA"));
+	fprintf(f, "<summary>%s</summary>\n", S_OR(test->summary, "NA"));
+	fprintf(f, "<description>%s</description>\n", S_OR(test->description, "NA"));
+	fprintf(f, "<result>%s</result>\n", test_result2str[test->result.state]);
+	fprintf(f, "<error>%s</error>\n", S_OR(test->result.error, "NA"));
+	fprintf(f, "<time>%d</time>\n", test->result.time);
+	fprintf(f, "</test>\n");
+}
+
+static void __test_txt_entry(struct ast_test *test, FILE *f)
+{
+	if (!f || !test) {
+		return;
+	}
+
+	fprintf(f, "\nName: %s\n", S_OR(test->name, "NA"));
+	fprintf(f, "Catagory: %s\n", S_OR(test->catagory, "NA"));
+	fprintf(f, "Summary: %s\n", S_OR(test->summary, "NA"));
+	fprintf(f, "Description: %s\n", S_OR(test->description, "NA"));
+	fprintf(f, "Result: %s\n", test_result2str[test->result.state]);
+	fprintf(f, "Error Description: %s\n", S_OR(test->result.error, "NA"));
+	fprintf(f, "Time: %d\n", test->result.time);
+
 }
 
 int ast_test_execute(const char *name, const char *catagory, struct ast_cli_args *a)
@@ -183,10 +218,77 @@
 	return last_results.count;
 }
 
-int ast_test_results(const char *name, const char *catagory, const char *xml_path)
-{
-	//todohere
-	return 0;
+int ast_test_generate_results(const char *name, const char *catagory, const char *xml_path, const char *txt_path)
+{
+	char mode = 0;  /* 0 generate all, 1 generate by catagory only, 2 generate by name and catagory */
+	FILE *f_xml = NULL, *f_txt = NULL;
+	int res = 0;
+	struct ast_test *test = NULL;
+
+	/* verify at least one output file was given */
+	if (ast_strlen_zero(xml_path) && ast_strlen_zero(txt_path)) {
+		return -1;
+	}
+
+	/* define what mode is to be used */
+	if (!ast_strlen_zero(catagory)) {
+		if (!ast_strlen_zero(name)) {
+			mode = 2;
+		} else {
+			mode = 1;
+		}
+	}
+
+	/* open files for writing */
+	if (!ast_strlen_zero(xml_path)) {
+		if (!(f_xml = fopen(xml_path, "w"))) {
+			ast_log(LOG_WARNING, "Could not open file %s for xml test results\n", xml_path);
+			res = -1;
+			goto done;
+		}
+	}
+	if (!ast_strlen_zero(txt_path)) {
+		if (!(f_txt = fopen(txt_path, "w"))) {
+			ast_log(LOG_WARNING, "Could not open file %s for text output of test results\n", txt_path);
+			res = -1;
+			goto done;
+		}
+	}
+
+	/* todohere xml and txt headers */
+	if (f_xml) {
+		fprintf(f_xml, "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n");
+		fprintf(f_xml, "\n<results>\n");
+	//	fprintf(f_xml, "<version>%s</version>\n"); //todohere version!?
+	//	fprintf(f_xml, "<version>%s</version>\n");
+
+		fprintf(f_xml, "\n</results>\n");
+	}
+
+	if (f_txt) {
+
+//todohere txt header info
+
+	}
+
+	/* export each individual test */
+	AST_LIST_LOCK(&tests);
+	AST_LIST_TRAVERSE_SAFE_BEGIN(&tests, test, entry) {
+		__test_xml_entry(test, f_xml);
+		__test_txt_entry(test, f_txt);
+	}
+	AST_LIST_TRAVERSE_SAFE_END;
+	AST_LIST_UNLOCK(&tests);
+
+done:
+	if (f_xml) {
+		fclose(f_xml);
+	}
+	if (f_txt) {
+		fclose(f_txt);
+	}
+
+	return res;
 }
 
 /*!
@@ -468,12 +570,54 @@
 
 	return CLI_SUCCESS;
 }
+
+static char *test_cli_generate_results(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
+{
+	static const char * const option[] = { "xml", "txt", NULL };
+	int res = 0;
+	switch (cmd) {
+	case CLI_INIT:
+		e->command = "test generate results";
+		e->usage =
+			"Usage: test generate results\n"
+			"       Generates test results for tests that has been run.\n";
+		return NULL;
+	case CLI_GENERATE:
+		if (a->pos == 3) {
+			return ast_cli_complete(a->word, option, a->n);
+		}
+		return NULL;
+	case CLI_HANDLER:
+
+		/* verify input */
+		if (a->argc < 4 || a->argc > 5) {
+			return CLI_SHOWUSAGE;
+		} else if (!strcmp(a->argv[3], "xml")) {
+			res = ast_test_generate_results(NULL, NULL, (a->argc == 5) ? a->argv[4] : DEFAULT_XML_PATH, NULL);
+		} else if (!strcmp(a->argv[3], "txt")) {
+			res = ast_test_generate_results(NULL, NULL, NULL, (a->argc == 5) ? a->argv[4] : DEFAULT_TXT_PATH);
+		} else {
+			return CLI_SHOWUSAGE;
+		}
+
+		if (!res) {
+			ast_cli(a->fd, "Results Generated Successfully\n");
+		} else {
+			ast_cli(a->fd, "Results Could Not Be Generated\n");
+		}
+	default:
+		return NULL;
+	}
+
+	return CLI_SUCCESS;
+}
+
 static struct ast_cli_entry test_cli[] = {
 	AST_CLI_DEFINE(test_cli_show_registered,           "show registered tests"),
 	AST_CLI_DEFINE(test_cli_execute_registered,        "execute registered tests"),
-	AST_CLI_DEFINE(test_cli_show_results,          "show last test results"),
+	AST_CLI_DEFINE(test_cli_show_results,              "show last test results"),
+	AST_CLI_DEFINE(test_cli_generate_results,          "generate test results"),
 };
-
 
 /* Sample Test */
 AST_TEST_DEFINE(sample_test1,




More information about the svn-commits mailing list