mirror of
				https://github.com/asterisk/asterisk.git
				synced 2025-10-31 02:37:10 +00:00 
			
		
		
		
	Add support for a realtime sorcery module.
This change does the following: 1. Adds the sorcery realtime module 2. Adds unit tests for the sorcery realtime module 3. Changes the realtime core to use an ast_variable list instead of variadic arguments 4. Changes all realtime drivers to accept an ast_variable list Review: https://reviewboard.asterisk.org/r/2424/ git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@386731 65c4cc65-6c06-0410-ace0-fbb531ad65f3
This commit is contained in:
		
							
								
								
									
										791
									
								
								tests/test_sorcery_realtime.c
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										791
									
								
								tests/test_sorcery_realtime.c
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,791 @@ | ||||
| /* | ||||
|  * Asterisk -- An open source telephony toolkit. | ||||
|  * | ||||
|  * Copyright (C) 2013, Digium, Inc. | ||||
|  * | ||||
|  * Joshua Colp <jcolp@digium.com> | ||||
|  * | ||||
|  * See http://www.asterisk.org for more information about | ||||
|  * the Asterisk project. Please do not directly contact | ||||
|  * any of the maintainers of this project for assistance; | ||||
|  * the project provides a web site, mailing lists and IRC | ||||
|  * channels for your use. | ||||
|  * | ||||
|  * This program is free software, distributed under the terms of | ||||
|  * the GNU General Public License Version 2. See the LICENSE file | ||||
|  * at the top of the source tree. | ||||
|  */ | ||||
|  | ||||
| /*! | ||||
|  * \file | ||||
|  * \brief Sorcery Unit Tests | ||||
|  * | ||||
|  * \author Joshua Colp <jcolp@digium.com> | ||||
|  * | ||||
|  */ | ||||
|  | ||||
| /*** MODULEINFO | ||||
| 	<depend>TEST_FRAMEWORK</depend> | ||||
| 	<support_level>core</support_level> | ||||
|  ***/ | ||||
|  | ||||
| #include "asterisk.h" | ||||
|  | ||||
| ASTERISK_FILE_VERSION(__FILE__, "") | ||||
|  | ||||
| #include "asterisk/test.h" | ||||
| #include "asterisk/module.h" | ||||
| #include "asterisk/sorcery.h" | ||||
| #include "asterisk/astdb.h" | ||||
| #include "asterisk/logger.h" | ||||
|  | ||||
| /*! \brief Configuration structure which contains all stored objects */ | ||||
| static struct ast_config *realtime_objects; | ||||
|  | ||||
| /*! \brief Helper function which finds a given variable */ | ||||
| static const struct ast_variable *realtime_find_variable(const struct ast_variable *fields, const char *name) | ||||
| { | ||||
| 	const struct ast_variable *variable; | ||||
|  | ||||
| 	for (variable = fields; variable; variable = variable->next) { | ||||
| 		if (!strcmp(variable->name, name)) { | ||||
| 			return variable; | ||||
| 		} | ||||
| 	} | ||||
|  | ||||
| 	return NULL; | ||||
| } | ||||
|  | ||||
| /*! \brief Helper function which returns if an object is matching or not */ | ||||
| static int realtime_is_object_matching(const char *object_id, const struct ast_variable *fields) | ||||
| { | ||||
| 	const struct ast_variable *field; | ||||
|  | ||||
| 	for (field = fields; field; field = field->next) { | ||||
| 		char *name = ast_strdupa(field->name), *like; | ||||
| 		const char *value; | ||||
|  | ||||
| 		/* If we are doing a pattern matching we need to remove the LIKE from the name */ | ||||
| 		if ((like = strstr(name, " LIKE"))) { | ||||
| 			char *pattern, *field_value = ast_strdupa(field->value); | ||||
|  | ||||
| 			*like = '\0'; | ||||
|  | ||||
| 			value = ast_strdupa(ast_variable_retrieve(realtime_objects, object_id, name)); | ||||
|  | ||||
| 			if (!(pattern = strchr(field_value, '%'))) { | ||||
| 				return 0; | ||||
| 			} | ||||
|  | ||||
| 			*pattern = '\0'; | ||||
|  | ||||
| 			if (strncmp(value, field_value, strlen(field_value))) { | ||||
| 				return 0; | ||||
| 			} | ||||
| 		} else { | ||||
| 			value = ast_variable_retrieve(realtime_objects, object_id, name); | ||||
|  | ||||
| 			if (ast_strlen_zero(value) || strcmp(value, field->value)) { | ||||
| 				return 0; | ||||
| 			} | ||||
| 		} | ||||
| 	} | ||||
|  | ||||
| 	return 1; | ||||
| } | ||||
|  | ||||
| static struct ast_variable *realtime_sorcery(const char *database, const char *table, const struct ast_variable *fields) | ||||
| { | ||||
| 	char *object_id = NULL; | ||||
|  | ||||
| 	while ((object_id = ast_category_browse(realtime_objects, object_id))) { | ||||
| 		if (!realtime_is_object_matching(object_id, fields)) { | ||||
| 			continue; | ||||
| 		} | ||||
|  | ||||
| 		return ast_variables_dup(ast_category_root(realtime_objects, object_id)); | ||||
| 	} | ||||
|  | ||||
| 	return NULL; | ||||
| } | ||||
|  | ||||
| static struct ast_config *realtime_sorcery_multi(const char *database, const char *table, const struct ast_variable *fields) | ||||
| { | ||||
| 	struct ast_config *objects; | ||||
| 	char *object_id = NULL; | ||||
|  | ||||
| 	if (!(objects = ast_config_new())) { | ||||
| 		return NULL; | ||||
| 	} | ||||
|  | ||||
| 	while ((object_id = ast_category_browse(realtime_objects, object_id))) { | ||||
| 		struct ast_category *object; | ||||
|  | ||||
| 		if (!realtime_is_object_matching(object_id, fields)) { | ||||
| 			continue; | ||||
| 		} | ||||
|  | ||||
| 		if (!(object = ast_category_new("", "", 0))) { | ||||
| 			ast_config_destroy(objects); | ||||
| 			return NULL; | ||||
| 		} | ||||
|  | ||||
| 		ast_variable_append(object, ast_variables_dup(ast_category_root(realtime_objects, object_id))); | ||||
| 		ast_category_append(objects, object); | ||||
| 	} | ||||
|  | ||||
| 	return objects; | ||||
| } | ||||
|  | ||||
| static int realtime_sorcery_update(const char *database, const char *table, const char *keyfield, const char *entity, const struct ast_variable *fields) | ||||
| { | ||||
| 	struct ast_category *object; | ||||
|  | ||||
| 	if (!ast_category_exist(realtime_objects, entity)) { | ||||
| 		return 0; | ||||
| 	} else if (!(object = ast_category_new(entity, "", 0))) { | ||||
| 		return -1; | ||||
| 	} | ||||
|  | ||||
| 	ast_category_delete(realtime_objects, entity); | ||||
| 	ast_variable_append(object, ast_variables_dup((struct ast_variable*)fields)); | ||||
| 	ast_variable_append(object, ast_variable_new(keyfield, entity, "")); | ||||
| 	ast_category_append(realtime_objects, object); | ||||
|  | ||||
| 	return 1; | ||||
| } | ||||
|  | ||||
| static int realtime_sorcery_store(const char *database, const char *table, const struct ast_variable *fields) | ||||
| { | ||||
| 	/* The key field is explicit within res_sorcery_realtime */ | ||||
| 	const struct ast_variable *keyfield = realtime_find_variable(fields, "id"); | ||||
| 	struct ast_category *object; | ||||
|  | ||||
| 	if (!keyfield || ast_category_exist(realtime_objects, keyfield->value) || !(object = ast_category_new(keyfield->value, "", 0))) { | ||||
| 		return -1; | ||||
| 	} | ||||
|  | ||||
| 	ast_variable_append(object, ast_variables_dup((struct ast_variable*)fields)); | ||||
| 	ast_category_append(realtime_objects, object); | ||||
|  | ||||
| 	return 0; | ||||
| } | ||||
|  | ||||
| static int realtime_sorcery_destroy(const char *database, const char *table, const char *keyfield, const char *entity, const struct ast_variable *fields) | ||||
| { | ||||
| 	if (!ast_category_exist(realtime_objects, entity)) { | ||||
| 		return 0; | ||||
| 	} | ||||
|  | ||||
| 	ast_category_delete(realtime_objects, entity); | ||||
|  | ||||
| 	return 1; | ||||
| } | ||||
|  | ||||
| struct ast_config_engine sorcery_config_engine = { | ||||
| 	.name = "sorcery_realtime_test", | ||||
| 	.realtime_func = realtime_sorcery, | ||||
| 	.realtime_multi_func = realtime_sorcery_multi, | ||||
| 	.update_func = realtime_sorcery_update, | ||||
| 	.store_func = realtime_sorcery_store, | ||||
| 	.destroy_func = realtime_sorcery_destroy, | ||||
| }; | ||||
|  | ||||
| /*! \brief Dummy sorcery object */ | ||||
| struct test_sorcery_object { | ||||
| 	SORCERY_OBJECT(details); | ||||
| 	unsigned int bob; | ||||
| 	unsigned int joe; | ||||
| }; | ||||
|  | ||||
| /*! \brief Internal function to allocate a test object */ | ||||
| static void *test_sorcery_object_alloc(const char *id) | ||||
| { | ||||
| 	return ao2_alloc(sizeof(struct test_sorcery_object), NULL); | ||||
| } | ||||
|  | ||||
| static struct ast_sorcery *alloc_and_initialize_sorcery(void) | ||||
| { | ||||
| 	struct ast_sorcery *sorcery; | ||||
|  | ||||
| 	if (!(sorcery = ast_sorcery_open())) { | ||||
| 		return NULL; | ||||
| 	} | ||||
|  | ||||
| 	if (ast_sorcery_apply_default(sorcery, "test", "realtime", "sorcery_realtime_test") || | ||||
| 		ast_sorcery_object_register(sorcery, "test", test_sorcery_object_alloc, NULL, NULL) || | ||||
| 		!(realtime_objects = ast_config_new())) { | ||||
| 		ast_sorcery_unref(sorcery); | ||||
| 		return NULL; | ||||
| 	} | ||||
|  | ||||
| 	ast_sorcery_object_field_register(sorcery, "test", "bob", "5", OPT_UINT_T, 0, FLDSET(struct test_sorcery_object, bob)); | ||||
| 	ast_sorcery_object_field_register(sorcery, "test", "joe", "10", OPT_UINT_T, 0, FLDSET(struct test_sorcery_object, joe)); | ||||
|  | ||||
| 	return sorcery; | ||||
| } | ||||
|  | ||||
| static void deinitialize_sorcery(struct ast_sorcery *sorcery) | ||||
| { | ||||
| 	ast_config_destroy(realtime_objects); | ||||
| 	realtime_objects = NULL; | ||||
| 	ast_sorcery_unref(sorcery); | ||||
| } | ||||
|  | ||||
| AST_TEST_DEFINE(object_create) | ||||
| { | ||||
| 	RAII_VAR(struct ast_sorcery *, sorcery, NULL, deinitialize_sorcery); | ||||
| 	RAII_VAR(struct test_sorcery_object *, obj, NULL, ao2_cleanup); | ||||
|  | ||||
| 	switch (cmd) { | ||||
| 	case TEST_INIT: | ||||
| 		info->name = "object_create"; | ||||
| 		info->category = "/res/sorcery_realtime/"; | ||||
| 		info->summary = "sorcery realtime object creation unit test"; | ||||
| 		info->description = | ||||
| 			"Test object creation in sorcery using realtime wizard"; | ||||
| 		return AST_TEST_NOT_RUN; | ||||
| 	case TEST_EXECUTE: | ||||
| 		break; | ||||
| 	} | ||||
|  | ||||
| 	if (!(sorcery = alloc_and_initialize_sorcery())) { | ||||
| 		ast_test_status_update(test, "Failed to open sorcery structure\n"); | ||||
| 		return AST_TEST_FAIL; | ||||
| 	} | ||||
|  | ||||
| 	if (!(obj = ast_sorcery_alloc(sorcery, "test", "blah"))) { | ||||
| 		ast_test_status_update(test, "Failed to allocate a known object type\n"); | ||||
| 		return AST_TEST_FAIL; | ||||
| 	} | ||||
|  | ||||
| 	if (ast_sorcery_create(sorcery, obj)) { | ||||
| 		ast_test_status_update(test, "Failed to create object using realtime wizard\n"); | ||||
| 		return AST_TEST_FAIL; | ||||
| 	} | ||||
|  | ||||
| 	return AST_TEST_PASS; | ||||
| } | ||||
|  | ||||
| AST_TEST_DEFINE(object_retrieve_id) | ||||
| { | ||||
| 	RAII_VAR(struct ast_sorcery *, sorcery, NULL, deinitialize_sorcery); | ||||
| 	RAII_VAR(struct test_sorcery_object *, obj, NULL, ao2_cleanup); | ||||
|  | ||||
| 	switch (cmd) { | ||||
| 	case TEST_INIT: | ||||
| 		info->name = "object_retrieve_id"; | ||||
| 		info->category = "/res/sorcery_realtime/"; | ||||
| 		info->summary = "sorcery object retrieval using id unit test"; | ||||
| 		info->description = | ||||
| 			"Test object retrieval using id in sorcery with realtime wizard"; | ||||
| 		return AST_TEST_NOT_RUN; | ||||
| 	case TEST_EXECUTE: | ||||
| 		break; | ||||
| 	} | ||||
|  | ||||
| 	if (!(sorcery = alloc_and_initialize_sorcery())) { | ||||
| 		ast_test_status_update(test, "Failed to open sorcery structure\n"); | ||||
| 		return AST_TEST_FAIL; | ||||
| 	} | ||||
|  | ||||
| 	if (!(obj = ast_sorcery_alloc(sorcery, "test", "blah"))) { | ||||
| 		ast_test_status_update(test, "Failed to allocate a known object type\n"); | ||||
| 		return AST_TEST_FAIL; | ||||
| 	} | ||||
|  | ||||
| 	if (ast_sorcery_create(sorcery, obj)) { | ||||
| 		ast_test_status_update(test, "Failed to create object using astdb wizard\n"); | ||||
| 		return AST_TEST_FAIL; | ||||
| 	} | ||||
|  | ||||
| 	ao2_cleanup(obj); | ||||
|  | ||||
| 	if (!(obj = ast_sorcery_alloc(sorcery, "test", "blah2"))) { | ||||
| 		ast_test_status_update(test, "Failed to allocate second instance of a known object type\n"); | ||||
| 		return AST_TEST_FAIL; | ||||
| 	} | ||||
|  | ||||
| 	if (ast_sorcery_create(sorcery, obj)) { | ||||
| 		ast_test_status_update(test, "Failed to create second object using astdb wizard\n"); | ||||
| 		return AST_TEST_FAIL; | ||||
| 	} | ||||
|  | ||||
| 	ao2_cleanup(obj); | ||||
|  | ||||
| 	if (!(obj = ast_sorcery_retrieve_by_id(sorcery, "test", "blah"))) { | ||||
| 		ast_test_status_update(test, "Failed to retrieve properly created object using id of 'blah'\n"); | ||||
| 		return AST_TEST_FAIL; | ||||
| 	} else if (strcmp(ast_sorcery_object_get_id(obj), "blah")) { | ||||
| 		ast_test_status_update(test, "Retrieved object does not have correct id\n"); | ||||
| 		return AST_TEST_FAIL; | ||||
| 	} | ||||
|  | ||||
| 	return AST_TEST_PASS; | ||||
| } | ||||
|  | ||||
| AST_TEST_DEFINE(object_retrieve_field) | ||||
| { | ||||
| 	RAII_VAR(struct ast_sorcery *, sorcery, NULL, deinitialize_sorcery); | ||||
| 	RAII_VAR(struct test_sorcery_object *, obj, NULL, ao2_cleanup); | ||||
| 	RAII_VAR(struct ast_variable *, fields, ast_variable_new("joe", "42", ""), ast_variables_destroy); | ||||
|  | ||||
| 	switch (cmd) { | ||||
| 	case TEST_INIT: | ||||
| 		info->name = "object_retrieve_field"; | ||||
| 		info->category = "/res/sorcery_realtime/"; | ||||
| 		info->summary = "sorcery object retrieval using a specific field unit test"; | ||||
| 		info->description = | ||||
| 			"Test object retrieval using a specific field in sorcery with realtime wizard"; | ||||
| 		return AST_TEST_NOT_RUN; | ||||
| 	case TEST_EXECUTE: | ||||
| 		break; | ||||
| 	} | ||||
|  | ||||
| 	if (!fields) { | ||||
| 		ast_test_status_update(test, "Failed to create fields for object retrieval attempt\n"); | ||||
| 		return AST_TEST_FAIL; | ||||
| 	} | ||||
|  | ||||
| 	if (!(sorcery = alloc_and_initialize_sorcery())) { | ||||
| 		ast_test_status_update(test, "Failed to open sorcery structure\n"); | ||||
| 		return AST_TEST_FAIL; | ||||
| 	} | ||||
|  | ||||
| 	if (!(obj = ast_sorcery_alloc(sorcery, "test", "blah"))) { | ||||
| 		ast_test_status_update(test, "Failed to allocate a known object type\n"); | ||||
| 		return AST_TEST_FAIL; | ||||
| 	} | ||||
|  | ||||
| 	obj->joe = 42; | ||||
|  | ||||
| 	if (ast_sorcery_create(sorcery, obj)) { | ||||
| 		ast_test_status_update(test, "Failed to create object using realtime wizard\n"); | ||||
| 		return AST_TEST_FAIL; | ||||
| 	} | ||||
|  | ||||
| 	ao2_cleanup(obj); | ||||
|  | ||||
| 	if (!(obj = ast_sorcery_retrieve_by_fields(sorcery, "test", AST_RETRIEVE_FLAG_DEFAULT, fields))) { | ||||
| 		ast_test_status_update(test, "Failed to retrieve properly created object using 'joe' field\n"); | ||||
| 		return AST_TEST_FAIL; | ||||
| 	} | ||||
|  | ||||
| 	ao2_cleanup(obj); | ||||
| 	ast_variables_destroy(fields); | ||||
|  | ||||
| 	if (!(fields = ast_variable_new("joe", "49", ""))) { | ||||
| 		ast_test_status_update(test, "Failed to create fields for object retrieval attempt\n"); | ||||
| 		return AST_TEST_FAIL; | ||||
| 	} | ||||
|  | ||||
| 	if ((obj = ast_sorcery_retrieve_by_fields(sorcery, "test", AST_RETRIEVE_FLAG_DEFAULT, fields))) { | ||||
| 		ast_test_status_update(test, "Retrieved an object using a field with an in-correct value... that should not happen\n"); | ||||
| 		return AST_TEST_FAIL; | ||||
| 	} | ||||
|  | ||||
| 	return AST_TEST_PASS; | ||||
| } | ||||
|  | ||||
| AST_TEST_DEFINE(object_retrieve_multiple_all) | ||||
| { | ||||
| 	RAII_VAR(struct ast_sorcery *, sorcery, NULL, deinitialize_sorcery); | ||||
| 	RAII_VAR(struct test_sorcery_object *, obj, NULL, ao2_cleanup); | ||||
| 	RAII_VAR(struct ao2_container *, objects, NULL, ao2_cleanup); | ||||
|  | ||||
| 	switch (cmd) { | ||||
| 	case TEST_INIT: | ||||
| 		info->name = "object_retrieve_multiple_all"; | ||||
| 		info->category = "/res/sorcery_realtime/"; | ||||
| 		info->summary = "sorcery multiple object retrieval unit test"; | ||||
| 		info->description = | ||||
| 			"Test multiple object retrieval in sorcery using realtime wizard"; | ||||
| 		return AST_TEST_NOT_RUN; | ||||
| 	case TEST_EXECUTE: | ||||
| 		break; | ||||
| 	} | ||||
|  | ||||
| 	if (!(sorcery = alloc_and_initialize_sorcery())) { | ||||
| 		ast_test_status_update(test, "Failed to open sorcery structure\n"); | ||||
| 		return AST_TEST_FAIL; | ||||
| 	} | ||||
|  | ||||
| 	if (!(obj = ast_sorcery_alloc(sorcery, "test", "blah"))) { | ||||
| 		ast_test_status_update(test, "Failed to allocate a known object type\n"); | ||||
| 		return AST_TEST_FAIL; | ||||
| 	} | ||||
|  | ||||
| 	if (ast_sorcery_create(sorcery, obj)) { | ||||
| 		ast_test_status_update(test, "Failed to create object using realtime wizard\n"); | ||||
| 		return AST_TEST_FAIL; | ||||
| 	} | ||||
|  | ||||
| 	ao2_cleanup(obj); | ||||
|  | ||||
| 	if (!(obj = ast_sorcery_alloc(sorcery, "test", "blah2"))) { | ||||
| 		ast_test_status_update(test, "Failed to allocate second instance of a known object type\n"); | ||||
| 		return AST_TEST_FAIL; | ||||
| 	} | ||||
|  | ||||
| 	if (ast_sorcery_create(sorcery, obj)) { | ||||
| 		ast_test_status_update(test, "Failed to create second object using realtime wizard\n"); | ||||
| 		return AST_TEST_FAIL; | ||||
| 	} | ||||
|  | ||||
| 	if (!(objects = ast_sorcery_retrieve_by_fields(sorcery, "test", AST_RETRIEVE_FLAG_MULTIPLE | AST_RETRIEVE_FLAG_ALL, NULL))) { | ||||
| 		ast_test_status_update(test, "Failed to retrieve a container of all objects\n"); | ||||
| 		return AST_TEST_FAIL; | ||||
| 	} else if (ao2_container_count(objects) != 2) { | ||||
| 		ast_test_status_update(test, "Received a container with no objects in it when there should be some\n"); | ||||
| 		return AST_TEST_FAIL; | ||||
| 	} | ||||
|  | ||||
| 	return AST_TEST_PASS; | ||||
| } | ||||
|  | ||||
| AST_TEST_DEFINE(object_retrieve_multiple_field) | ||||
| { | ||||
| 	RAII_VAR(struct ast_sorcery *, sorcery, NULL, deinitialize_sorcery); | ||||
| 	RAII_VAR(struct test_sorcery_object *, obj, NULL, ao2_cleanup); | ||||
| 	RAII_VAR(struct ao2_container *, objects, NULL, ao2_cleanup); | ||||
| 	RAII_VAR(struct ast_variable *, fields, ast_variable_new("joe", "6", ""), ast_variables_destroy); | ||||
|  | ||||
| 	switch (cmd) { | ||||
| 	case TEST_INIT: | ||||
| 		info->name = "object_retrieve_multiple_field"; | ||||
| 		info->category = "/res/sorcery_realtime/"; | ||||
| 		info->summary = "sorcery multiple object retrieval unit test"; | ||||
| 		info->description = | ||||
| 			"Test multiple object retrieval in sorcery using fields using realtime wizard"; | ||||
| 		return AST_TEST_NOT_RUN; | ||||
| 	case TEST_EXECUTE: | ||||
| 		break; | ||||
| 	} | ||||
|  | ||||
| 	if (!fields) { | ||||
| 		ast_test_status_update(test, "Failed to create fields for multiple retrieve\n"); | ||||
| 		return AST_TEST_FAIL; | ||||
| 	} | ||||
|  | ||||
| 	if (!(sorcery = alloc_and_initialize_sorcery())) { | ||||
| 		ast_test_status_update(test, "Failed to open sorcery structure\n"); | ||||
| 		return AST_TEST_FAIL; | ||||
| 	} | ||||
|  | ||||
| 	if (!(obj = ast_sorcery_alloc(sorcery, "test", "blah"))) { | ||||
| 		ast_test_status_update(test, "Failed to allocate a known object type\n"); | ||||
| 		return AST_TEST_FAIL; | ||||
| 	} | ||||
|  | ||||
| 	obj->joe = 6; | ||||
|  | ||||
| 	if (ast_sorcery_create(sorcery, obj)) { | ||||
| 		ast_test_status_update(test, "Failed to create object using realtime wizard\n"); | ||||
| 		return AST_TEST_FAIL; | ||||
| 	} | ||||
|  | ||||
| 	if (!(objects = ast_sorcery_retrieve_by_fields(sorcery, "test", AST_RETRIEVE_FLAG_MULTIPLE, fields))) { | ||||
| 		ast_test_status_update(test, "Failed to retrieve a container of all objects\n"); | ||||
| 		return AST_TEST_FAIL; | ||||
| 	} else if (ao2_container_count(objects) != 1) { | ||||
| 		ast_test_status_update(test, "Received a container with no objects in it when there should be some\n"); | ||||
| 		return AST_TEST_FAIL; | ||||
| 	} | ||||
|  | ||||
| 	ao2_cleanup(objects); | ||||
| 	ast_variables_destroy(fields); | ||||
|  | ||||
| 	if (!(fields = ast_variable_new("joe", "7", ""))) { | ||||
| 		ast_test_status_update(test, "Failed to create fields for multiple retrieval\n"); | ||||
| 		return AST_TEST_FAIL; | ||||
| 	} else if (!(objects = ast_sorcery_retrieve_by_fields(sorcery, "test", AST_RETRIEVE_FLAG_MULTIPLE, fields))) { | ||||
| 		ast_test_status_update(test, "Failed to retrieve an empty container when retrieving multiple\n"); | ||||
| 		return AST_TEST_FAIL; | ||||
| 	} else if (ao2_container_count(objects)) { | ||||
| 		ast_test_status_update(test, "Received a container with objects when there should be none in it\n"); | ||||
| 		return AST_TEST_FAIL; | ||||
| 	} | ||||
|  | ||||
| 	return AST_TEST_PASS; | ||||
| } | ||||
|  | ||||
| AST_TEST_DEFINE(object_retrieve_regex) | ||||
| { | ||||
| 	RAII_VAR(struct ast_sorcery *, sorcery, NULL, deinitialize_sorcery); | ||||
| 	RAII_VAR(struct test_sorcery_object *, obj, NULL, ao2_cleanup); | ||||
| 	RAII_VAR(struct ao2_container *, objects, NULL, ao2_cleanup); | ||||
|  | ||||
| 	switch (cmd) { | ||||
| 	case TEST_INIT: | ||||
| 		info->name = "object_retrieve_regex"; | ||||
| 		info->category = "/res/sorcery_realtime/"; | ||||
| 		info->summary = "sorcery multiple object retrieval using regex unit test"; | ||||
| 		info->description = | ||||
| 			"Test multiple object retrieval in sorcery using regular expression for matching using realtime wizard"; | ||||
| 		return AST_TEST_NOT_RUN; | ||||
| 	case TEST_EXECUTE: | ||||
| 		break; | ||||
| 	} | ||||
|  | ||||
| 	if (!(sorcery = alloc_and_initialize_sorcery())) { | ||||
| 		ast_test_status_update(test, "Failed to open sorcery structure\n"); | ||||
| 		return AST_TEST_FAIL; | ||||
| 	} | ||||
|  | ||||
| 	if (!(obj = ast_sorcery_alloc(sorcery, "test", "blah-98joe"))) { | ||||
| 		ast_test_status_update(test, "Failed to allocate a known object type\n"); | ||||
| 		return AST_TEST_FAIL; | ||||
| 	} | ||||
|  | ||||
| 	if (ast_sorcery_create(sorcery, obj)) { | ||||
| 		ast_test_status_update(test, "Failed to create object using realtime wizard\n"); | ||||
| 		return AST_TEST_FAIL; | ||||
| 	} | ||||
|  | ||||
| 	ao2_cleanup(obj); | ||||
|  | ||||
| 	if (!(obj = ast_sorcery_alloc(sorcery, "test", "blah-93joe"))) { | ||||
| 		ast_test_status_update(test, "Failed to allocate second instance of a known object type\n"); | ||||
| 		return AST_TEST_FAIL; | ||||
| 	} | ||||
|  | ||||
| 	if (ast_sorcery_create(sorcery, obj)) { | ||||
| 		ast_test_status_update(test, "Failed to create second object using astdb wizard\n"); | ||||
| 		return AST_TEST_FAIL; | ||||
| 	} | ||||
|  | ||||
| 	ao2_cleanup(obj); | ||||
|  | ||||
| 	if (!(obj = ast_sorcery_alloc(sorcery, "test", "neener-93joe"))) { | ||||
| 		ast_test_status_update(test, "Failed to allocate third instance of a known object type\n"); | ||||
| 		return AST_TEST_FAIL; | ||||
| 	} | ||||
|  | ||||
| 	if (ast_sorcery_create(sorcery, obj)) { | ||||
| 		ast_test_status_update(test, "Failed to create third object using astdb wizard\n"); | ||||
| 		return AST_TEST_FAIL; | ||||
| 	} | ||||
|  | ||||
| 	if (!(objects = ast_sorcery_retrieve_by_regex(sorcery, "test", "^blah-"))) { | ||||
| 		ast_test_status_update(test, "Failed to retrieve a container of objects\n"); | ||||
| 		return AST_TEST_FAIL; | ||||
| 	} else if (ao2_container_count(objects) != 2) { | ||||
| 		ast_test_status_update(test, "Received a container with incorrect number of objects in it\n"); | ||||
| 		return AST_TEST_FAIL; | ||||
| 	} | ||||
|  | ||||
| 	return AST_TEST_PASS; | ||||
| } | ||||
|  | ||||
| AST_TEST_DEFINE(object_update) | ||||
| { | ||||
| 	RAII_VAR(struct ast_sorcery *, sorcery, NULL, deinitialize_sorcery); | ||||
| 	RAII_VAR(struct test_sorcery_object *, obj, NULL, ao2_cleanup); | ||||
| 	RAII_VAR(struct test_sorcery_object *, obj2, NULL, ao2_cleanup); | ||||
|  | ||||
| 	switch (cmd) { | ||||
| 	case TEST_INIT: | ||||
| 		info->name = "object_update"; | ||||
| 		info->category = "/res/sorcery_realtime/"; | ||||
| 		info->summary = "sorcery object update unit test"; | ||||
| 		info->description = | ||||
| 			"Test object updating in sorcery using realtime wizard"; | ||||
| 		return AST_TEST_NOT_RUN; | ||||
| 	case TEST_EXECUTE: | ||||
| 		break; | ||||
| 	} | ||||
|  | ||||
| 	if (!(sorcery = alloc_and_initialize_sorcery())) { | ||||
| 		ast_test_status_update(test, "Failed to open sorcery structure\n"); | ||||
| 		return AST_TEST_FAIL; | ||||
| 	} | ||||
|  | ||||
| 	if (!(obj = ast_sorcery_alloc(sorcery, "test", "blah"))) { | ||||
| 		ast_test_status_update(test, "Failed to allocate a known object type\n"); | ||||
| 		return AST_TEST_FAIL; | ||||
| 	} | ||||
|  | ||||
| 	if (ast_sorcery_create(sorcery, obj)) { | ||||
| 		ast_test_status_update(test, "Failed to create object using realtime wizard\n"); | ||||
| 		return AST_TEST_FAIL; | ||||
| 	} | ||||
|  | ||||
| 	if (!(obj2 = ast_sorcery_copy(sorcery, obj))) { | ||||
| 		ast_test_status_update(test, "Failed to allocate a known object type for updating\n"); | ||||
| 		return AST_TEST_FAIL; | ||||
| 	} | ||||
|  | ||||
| 	ao2_cleanup(obj); | ||||
|  | ||||
| 	obj2->bob = 1000; | ||||
| 	obj2->joe = 2000; | ||||
|  | ||||
| 	if (ast_sorcery_update(sorcery, obj2)) { | ||||
| 		ast_test_status_update(test, "Failed to update sorcery with new object\n"); | ||||
| 		return AST_TEST_FAIL; | ||||
| 	} | ||||
|  | ||||
| 	if (!(obj = ast_sorcery_retrieve_by_id(sorcery, "test", "blah"))) { | ||||
| 		ast_test_status_update(test, "Failed to retrieve properly updated object\n"); | ||||
| 		return AST_TEST_FAIL; | ||||
| 	} else if ((obj->bob != obj2->bob) || (obj->joe != obj2->joe)) { | ||||
| 		ast_test_status_update(test, "Object retrieved is not the updated object\n"); | ||||
| 		return AST_TEST_FAIL; | ||||
| 	} | ||||
|  | ||||
| 	return AST_TEST_PASS; | ||||
| } | ||||
|  | ||||
| AST_TEST_DEFINE(object_update_uncreated) | ||||
| { | ||||
| 	RAII_VAR(struct ast_sorcery *, sorcery, NULL, deinitialize_sorcery); | ||||
| 	RAII_VAR(struct test_sorcery_object *, obj, NULL, ao2_cleanup); | ||||
|  | ||||
| 	switch (cmd) { | ||||
| 	case TEST_INIT: | ||||
| 		info->name = "object_update_uncreated"; | ||||
| 		info->category = "/res/sorcery_realtime/"; | ||||
| 		info->summary = "sorcery object update unit test"; | ||||
| 		info->description = | ||||
| 			"Test updating of an uncreated object in sorcery using realtime wizard"; | ||||
| 		return AST_TEST_NOT_RUN; | ||||
| 	case TEST_EXECUTE: | ||||
| 		break; | ||||
| 	} | ||||
|  | ||||
| 	if (!(sorcery = alloc_and_initialize_sorcery())) { | ||||
| 		ast_test_status_update(test, "Failed to open sorcery structure\n"); | ||||
| 		return AST_TEST_FAIL; | ||||
| 	} | ||||
|  | ||||
| 	if (!(obj = ast_sorcery_alloc(sorcery, "test", "blah"))) { | ||||
| 		ast_test_status_update(test, "Failed to allocate a known object type\n"); | ||||
| 		return AST_TEST_FAIL; | ||||
| 	} | ||||
|  | ||||
| 	if (!ast_sorcery_update(sorcery, obj)) { | ||||
| 		ast_test_status_update(test, "Successfully updated an object which has not been created yet\n"); | ||||
| 		return AST_TEST_FAIL; | ||||
| 	} | ||||
|  | ||||
| 	return AST_TEST_PASS; | ||||
| } | ||||
|  | ||||
| AST_TEST_DEFINE(object_delete) | ||||
| { | ||||
| 	RAII_VAR(struct ast_sorcery *, sorcery, NULL, deinitialize_sorcery); | ||||
| 	RAII_VAR(struct test_sorcery_object *, obj, NULL, ao2_cleanup); | ||||
|  | ||||
| 	switch (cmd) { | ||||
| 	case TEST_INIT: | ||||
| 		info->name = "object_delete"; | ||||
| 		info->category = "/res/sorcery_realtime/"; | ||||
| 		info->summary = "sorcery object deletion unit test"; | ||||
| 		info->description = | ||||
| 			"Test object deletion in sorcery using realtime wizard"; | ||||
| 		return AST_TEST_NOT_RUN; | ||||
| 	case TEST_EXECUTE: | ||||
| 		break; | ||||
| 	} | ||||
|  | ||||
| 	if (!(sorcery = alloc_and_initialize_sorcery())) { | ||||
| 		ast_test_status_update(test, "Failed to open sorcery structure\n"); | ||||
| 		return AST_TEST_FAIL; | ||||
| 	} | ||||
|  | ||||
| 	if (!(obj = ast_sorcery_alloc(sorcery, "test", "blah"))) { | ||||
| 		ast_test_status_update(test, "Failed to allocate a known object type\n"); | ||||
| 		return AST_TEST_FAIL; | ||||
| 	} | ||||
|  | ||||
| 	if (ast_sorcery_create(sorcery, obj)) { | ||||
| 		ast_test_status_update(test, "Failed to create object using realtime wizard\n"); | ||||
| 		return AST_TEST_FAIL; | ||||
| 	} | ||||
|  | ||||
| 	if (ast_sorcery_delete(sorcery, obj)) { | ||||
| 		ast_test_status_update(test, "Failed to delete object using realtime wizard\n"); | ||||
| 		return AST_TEST_FAIL; | ||||
| 	} | ||||
|  | ||||
| 	ao2_cleanup(obj); | ||||
|  | ||||
| 	if ((obj = ast_sorcery_retrieve_by_id(sorcery, "test", "blah"))) { | ||||
| 		ast_test_status_update(test, "Retrieved deleted object that should not be there\n"); | ||||
| 		return AST_TEST_FAIL; | ||||
| 	} | ||||
|  | ||||
| 	return AST_TEST_PASS; | ||||
| } | ||||
|  | ||||
| AST_TEST_DEFINE(object_delete_uncreated) | ||||
| { | ||||
| 	RAII_VAR(struct ast_sorcery *, sorcery, NULL, deinitialize_sorcery); | ||||
| 	RAII_VAR(struct test_sorcery_object *, obj, NULL, ao2_cleanup); | ||||
|  | ||||
| 	switch (cmd) { | ||||
| 	case TEST_INIT: | ||||
| 		info->name = "object_delete_uncreated"; | ||||
| 		info->category = "/res/sorcery_realtime/"; | ||||
| 		info->summary = "sorcery object deletion unit test"; | ||||
| 		info->description = | ||||
| 			"Test object deletion of an uncreated object in sorcery using realtime wizard"; | ||||
| 		return AST_TEST_NOT_RUN; | ||||
| 	case TEST_EXECUTE: | ||||
| 		break; | ||||
| 	} | ||||
|  | ||||
| 	if (!(sorcery = alloc_and_initialize_sorcery())) { | ||||
| 		ast_test_status_update(test, "Failed to open sorcery structure\n"); | ||||
| 		return AST_TEST_FAIL; | ||||
| 	} | ||||
|  | ||||
| 	if (!(obj = ast_sorcery_alloc(sorcery, "test", "blah"))) { | ||||
| 		ast_test_status_update(test, "Failed to allocate a known object type\n"); | ||||
| 		return AST_TEST_FAIL; | ||||
| 	} | ||||
|  | ||||
| 	if (!ast_sorcery_delete(sorcery, obj)) { | ||||
| 		ast_test_status_update(test, "Successfully deleted an object which was never created\n"); | ||||
| 		return AST_TEST_FAIL; | ||||
| 	} | ||||
|  | ||||
| 	return AST_TEST_PASS; | ||||
| } | ||||
|  | ||||
| static int unload_module(void) | ||||
| { | ||||
| 	ast_config_engine_deregister(&sorcery_config_engine); | ||||
| 	AST_TEST_UNREGISTER(object_create); | ||||
| 	AST_TEST_UNREGISTER(object_retrieve_id); | ||||
| 	AST_TEST_UNREGISTER(object_retrieve_field); | ||||
| 	AST_TEST_UNREGISTER(object_retrieve_multiple_all); | ||||
| 	AST_TEST_UNREGISTER(object_retrieve_multiple_field); | ||||
| 	AST_TEST_UNREGISTER(object_retrieve_regex); | ||||
| 	AST_TEST_UNREGISTER(object_update); | ||||
| 	AST_TEST_UNREGISTER(object_update_uncreated); | ||||
| 	AST_TEST_UNREGISTER(object_delete); | ||||
| 	AST_TEST_UNREGISTER(object_delete_uncreated); | ||||
|  | ||||
| 	return 0; | ||||
| } | ||||
|  | ||||
| static int load_module(void) | ||||
| { | ||||
| 	ast_config_engine_register(&sorcery_config_engine); | ||||
| 	ast_realtime_append_mapping("sorcery_realtime_test", "sorcery_realtime_test", "test", "test", 1); | ||||
| 	AST_TEST_REGISTER(object_create); | ||||
| 	AST_TEST_REGISTER(object_retrieve_id); | ||||
| 	AST_TEST_REGISTER(object_retrieve_field); | ||||
| 	AST_TEST_REGISTER(object_retrieve_multiple_all); | ||||
| 	AST_TEST_REGISTER(object_retrieve_multiple_field); | ||||
| 	AST_TEST_REGISTER(object_retrieve_regex); | ||||
| 	AST_TEST_REGISTER(object_update); | ||||
| 	AST_TEST_REGISTER(object_update_uncreated); | ||||
| 	AST_TEST_REGISTER(object_delete); | ||||
| 	AST_TEST_REGISTER(object_delete_uncreated); | ||||
|  | ||||
| 	return AST_MODULE_LOAD_SUCCESS; | ||||
| } | ||||
|  | ||||
| AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Sorcery Realtime Wizard test module"); | ||||
		Reference in New Issue
	
	Block a user