There were a number of places in the res_pjsip stack that were getting all endpoints or all aors, and then filtering them locally. A good example is pjsip_options which, on startup, retrieves all endpoints, then the aors for those endpoints, then tests the aors to see if the qualify_frequency is > 0. One issue was that it never did anything with the endpoints other than retrieve the aors so we probably could have skipped a step and just retrieved all aors. But nevermind. This worked reasonably well with local config files but with a realtime backend and thousands of objects, this was a nightmare. The issue really boiled down to the fact that while realtime supports predicates that are passed to the database engine, the non-realtime sorcery backends didn't. They do now. The realtime engines have a scheme for doing simple comparisons. They take in an ast_variable (or list) for matching, and the name of each variable can contain an operator. For instance, a name of "qualify_frequency >" and a value of "0" would create a SQL predicate that looks like "where qualify_frequency > '0'". If there's no operator after the name, the engines add an '=' so a simple name of "qualify_frequency" and a value of "10" would return exact matches. The non-realtime backends decide whether to include an object in a result set by calling ast_sorcery_changeset_create on every object in the internal container. However, ast_sorcery_changeset_create only does exact string matches though so a name of "qualify_frequency >" and a value of "0" returns nothing because the literal "qualify_frequency >" doesn't match any name in the objset set. So, the real task was to create a generic string matcher that can take a left value, operator and a right value and perform the match. To that end, strings.c has a new ast_strings_match(left, operator, right) function. Left and right are the strings to operate on and the operator can be a string containing any of the following: = (or NULL or ""), !=, >, >=, <, <=, like or regex. If the operator is like or regex, the right string should be a %-pattern or a regex expression. If both left and right can be converted to float, then a numeric comparison is performed, otherwise a string comparison is performed. To use this new function on ast_variables, 2 new functions were added to config.c. One that compares 2 ast_variables, and one that compares 2 ast_variable lists. The former is useful when you want to compare 2 ast_variables that happen to be in a list but don't want to traverse the list. The latter will traverse the right list and return true if all the variables in it match the left list. Now, the backends' fields_cmp functions call ast_variable_lists_match instead of ast_sorcery_changeset_create and they can now process the same syntax as the realtime engines. The realtime backend just passes the variable list unaltered to the engine. The only gotcha is that there's no common realtime engine support for regex so that's been noted in the api docs for ast_sorcery_retrieve_by_fields. Only one more change to sorcery was done... A new config flag "allow_unqualified_fetch" was added to reg_sorcery_realtime. "no": ignore fetches if no predicate fields were supplied. "error": same as no but emit an error. (good for testing) "yes": allow (the default); "warn": allow but emit a warning. (good for testing) Now on to res_pjsip... pjsip_options was modified to retrieve aors with qualify_frequency > 0 rather than all endpoints then all aors. Not only was this a big improvement in realtime retrieval but even for config files there's an improvement because we're not going through endpoints anymore. res_pjsip_mwi was modified to retieve only endpoints with something in the mailboxes field instead of all endpoints then testing mailboxes. res_pjsip_registrar_expire was completely refactored. It was retrieving all contacts then setting up scheduler entries to check for expiration. Now, it's a single thread (like keepalive) that periodically retrieves only contacts whose expiration time is < now and deletes them. A new contact_expiration_check_interval was added to global with a default of 30 seconds. Ross Beer reports that with this patch, his Asterisk startup time dropped from around an hour to under 30 seconds. There are still objects that can't be filtered at the database like identifies, transports, and registrations. These are not going to be anywhere near as numerous as endpoints, aors, auths, contacts however. Back to allow_unqualified_fetch. If this is set to yes and you have a very large number of objects in the database, the pjsip CLI commands will attempt to retrive ALL of them if not qualified with a LIKE. Worse, if you type "pjsip show endpoint <tab>" guess what's going to happen? :) Having a cache helps but all the objects will have to be retrieved at least once to fill the cache. Setting allow_unqualified_fetch=no prevents the mass retrieve and should be used on endpoints, auths, aors, and contacts. It should NOT be used for identifies, registrations and transports since these MUST be retrieved in bulk. Example sorcery.conf: [res_pjsip] endpoint=config,pjsip.conf,criteria=type=endpoint endpoint=realtime,ps_endpoints,allow_unqualified_fetch=error ASTERISK-25826 #close Reported-by: Ross Beer Tested-by: Ross Beer Change-Id: Id2691e447db90892890036e663aaf907b2dc1c67
639 lines
19 KiB
C
639 lines
19 KiB
C
/*
|
|
* 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_REGISTER_FILE()
|
|
|
|
#include "asterisk/test.h"
|
|
#include "asterisk/module.h"
|
|
#include "asterisk/sorcery.h"
|
|
#include "asterisk/astdb.h"
|
|
#include "asterisk/logger.h"
|
|
|
|
/*! \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 ast_sorcery_generic_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", "astdb", "test") != AST_SORCERY_APPLY_SUCCESS) ||
|
|
ast_sorcery_internal_object_register(sorcery, "test", test_sorcery_object_alloc, NULL, NULL)) {
|
|
ast_sorcery_unref(sorcery);
|
|
return NULL;
|
|
}
|
|
|
|
ast_sorcery_object_field_register_nodoc(sorcery, "test", "bob", "5", OPT_UINT_T, 0, FLDSET(struct test_sorcery_object, bob));
|
|
ast_sorcery_object_field_register_nodoc(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_db_deltree("test/test", 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);
|
|
char value[2];
|
|
|
|
switch (cmd) {
|
|
case TEST_INIT:
|
|
info->name = "object_create";
|
|
info->category = "/res/sorcery_astdb/";
|
|
info->summary = "sorcery astdb object creation unit test";
|
|
info->description =
|
|
"Test object creation in sorcery using astdb 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;
|
|
} else if (ast_db_get("test/test", "blah", value, sizeof(value))) {
|
|
ast_test_status_update(test, "Object was apparently created but does not actually exist in astdb\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_astdb/";
|
|
info->summary = "sorcery object retrieval using id unit test";
|
|
info->description =
|
|
"Test object retrieval using id in sorcery with astdb 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_astdb/";
|
|
info->summary = "sorcery object retrieval using a specific field unit test";
|
|
info->description =
|
|
"Test object retrieval using a specific field in sorcery with astdb 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 astdb 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_astdb/";
|
|
info->summary = "sorcery multiple object retrieval unit test";
|
|
info->description =
|
|
"Test multiple object retrieval in sorcery using astdb 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;
|
|
}
|
|
|
|
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_astdb/";
|
|
info->summary = "sorcery multiple object retrieval unit test";
|
|
info->description =
|
|
"Test multiple object retrieval in sorcery using fields using astdb 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 astdb 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 <", "6", ""))) {
|
|
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_astdb/";
|
|
info->summary = "sorcery multiple object retrieval using regex unit test";
|
|
info->description =
|
|
"Test multiple object retrieval in sorcery using regular expression for matching using astdb 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 astdb 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_astdb/";
|
|
info->summary = "sorcery object update unit test";
|
|
info->description =
|
|
"Test object updating in sorcery using astdb 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;
|
|
}
|
|
|
|
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_astdb/";
|
|
info->summary = "sorcery object update unit test";
|
|
info->description =
|
|
"Test updating of an uncreated object in sorcery using astdb 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_astdb/";
|
|
info->summary = "sorcery object deletion unit test";
|
|
info->description =
|
|
"Test object deletion in sorcery using astdb 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;
|
|
}
|
|
|
|
if (ast_sorcery_delete(sorcery, obj)) {
|
|
ast_test_status_update(test, "Failed to delete 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, "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_astdb/";
|
|
info->summary = "sorcery object deletion unit test";
|
|
info->description =
|
|
"Test object deletion of an uncreated object in sorcery using astdb 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_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_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 astdb Wizard test module");
|