2013-04-08 13:27:45 +00:00
|
|
|
/*
|
|
|
|
* Asterisk -- An open source telephony toolkit.
|
|
|
|
*
|
|
|
|
* Copyright (C) 2013, Digium, Inc.
|
|
|
|
*
|
|
|
|
* David M. Lee, II <dlee@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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*!
|
2015-05-17 12:15:15 +00:00
|
|
|
* \file
|
|
|
|
* \brief Test Stasis Application API.
|
2013-04-08 13:27:45 +00:00
|
|
|
* \author\verbatim David M. Lee, II <dlee@digium.com> \endverbatim
|
|
|
|
*
|
|
|
|
* \ingroup tests
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*** MODULEINFO
|
|
|
|
<depend>TEST_FRAMEWORK</depend>
|
2013-04-15 16:43:47 +00:00
|
|
|
<depend>res_stasis</depend>
|
2013-04-08 13:27:45 +00:00
|
|
|
<support_level>core</support_level>
|
|
|
|
***/
|
|
|
|
|
|
|
|
#include "asterisk.h"
|
|
|
|
|
git migration: Refactor the ASTERISK_FILE_VERSION macro
Git does not support the ability to replace a token with a version
string during check-in. While it does have support for replacing a
token on clone, this is somewhat sub-optimal: the token is replaced
with the object hash, which is not particularly easy for human
consumption. What's more, in practice, the source file version was often
not terribly useful. Generally, when triaging bugs, the overall version
of Asterisk is far more useful than an individual SVN version of a file. As a
result, this patch removes Asterisk's support for showing source file
versions.
Specifically, it does the following:
* Rename ASTERISK_FILE_VERSION macro to ASTERISK_REGISTER_FILE, and
remove passing the version in with the macro. Other facilities
than 'core show file version' make use of the file names, such as
setting a debug level only on a specific file. As such, the act of
registering source files with the Asterisk core still has use. The
macro rename now reflects the new macro purpose.
* main/asterisk:
- Refactor the file_version structure to reflect that it no longer
tracks a version field.
- Remove the "core show file version" CLI command. Without the file
version, it is no longer useful.
- Remove the ast_file_version_find function. The file version is no
longer tracked.
- Rename ast_register_file_version/ast_unregister_file_version to
ast_register_file/ast_unregister_file, respectively.
* main/manager: Remove value from the Version key of the ModuleCheck
Action. The actual key itself has not been removed, as doing so would
absolutely constitute a backwards incompatible change. However, since
the file version is no longer tracked, there is no need to attempt to
include it in the Version key.
* UPGRADE: Add notes for:
- Modification to the ModuleCheck AMI Action
- Removal of the "core show file version" CLI command
Change-Id: I6cf0ff280e1668bf4957dc21f32a5ff43444a40e
2015-04-12 02:38:22 +00:00
|
|
|
ASTERISK_REGISTER_FILE()
|
2013-04-08 13:27:45 +00:00
|
|
|
|
|
|
|
#include "asterisk/module.h"
|
|
|
|
#include "asterisk/test.h"
|
2013-04-15 16:43:47 +00:00
|
|
|
#include "asterisk/stasis_app.h"
|
2013-04-08 13:27:45 +00:00
|
|
|
|
2013-04-15 16:43:47 +00:00
|
|
|
static const char *test_category = "/stasis/res/";
|
2013-04-08 13:27:45 +00:00
|
|
|
|
|
|
|
AST_TEST_DEFINE(app_invoke_dne)
|
|
|
|
{
|
|
|
|
int res;
|
|
|
|
|
|
|
|
switch (cmd) {
|
|
|
|
case TEST_INIT:
|
|
|
|
info->name = __func__;
|
|
|
|
info->category = test_category;
|
|
|
|
info->summary = "Test stasis app invocation.";
|
|
|
|
info->description = "Test stasis app invocation.";
|
|
|
|
return AST_TEST_NOT_RUN;
|
|
|
|
case TEST_EXECUTE:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
res = stasis_app_send("i-am-not-an-app", ast_json_null());
|
|
|
|
ast_test_validate(test, -1 == res);
|
|
|
|
|
|
|
|
return AST_TEST_PASS;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct app_data {
|
|
|
|
int invocations;
|
|
|
|
struct ast_json *messages;
|
|
|
|
};
|
|
|
|
|
|
|
|
static void app_data_dtor(void *obj)
|
|
|
|
{
|
|
|
|
struct app_data *actual = obj;
|
|
|
|
|
|
|
|
ast_json_unref(actual->messages);
|
|
|
|
actual->messages = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct app_data *app_data_create(void)
|
|
|
|
{
|
|
|
|
struct app_data *res = ao2_alloc(sizeof(struct app_data), app_data_dtor);
|
|
|
|
|
|
|
|
if (!res) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
res->messages = ast_json_array_create();
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void test_handler(void *data, const char *app_name, struct ast_json *message)
|
|
|
|
{
|
|
|
|
struct app_data *actual = data;
|
|
|
|
int res;
|
|
|
|
++(actual->invocations);
|
|
|
|
res = ast_json_array_append(actual->messages, ast_json_copy(message));
|
|
|
|
ast_assert(res == 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
AST_TEST_DEFINE(app_invoke_one)
|
|
|
|
{
|
|
|
|
RAII_VAR(struct app_data *, app_data, NULL, ao2_cleanup);
|
|
|
|
RAII_VAR(char *, app_name, NULL, stasis_app_unregister);
|
|
|
|
RAII_VAR(struct ast_json *, expected_message, NULL, ast_json_unref);
|
|
|
|
RAII_VAR(struct ast_json *, message, NULL, ast_json_unref);
|
|
|
|
int res;
|
|
|
|
|
|
|
|
switch (cmd) {
|
|
|
|
case TEST_INIT:
|
|
|
|
info->name = __func__;
|
|
|
|
info->category = test_category;
|
|
|
|
info->summary = "Test stasis app invocation.";
|
|
|
|
info->description = "Test stasis app invocation.";
|
|
|
|
return AST_TEST_NOT_RUN;
|
|
|
|
case TEST_EXECUTE:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
app_name = "test-handler";
|
|
|
|
|
|
|
|
app_data = app_data_create();
|
|
|
|
|
|
|
|
stasis_app_register(app_name, test_handler, app_data);
|
|
|
|
message = ast_json_pack("{ s: o }", "test-message", ast_json_null());
|
|
|
|
expected_message = ast_json_pack("[o]", ast_json_ref(message));
|
|
|
|
|
|
|
|
res = stasis_app_send(app_name, message);
|
|
|
|
ast_test_validate(test, 0 == res);
|
|
|
|
ast_test_validate(test, 1 == app_data->invocations);
|
|
|
|
ast_test_validate(test, ast_json_equal(expected_message, app_data->messages));
|
|
|
|
|
|
|
|
return AST_TEST_PASS;
|
|
|
|
}
|
|
|
|
|
|
|
|
AST_TEST_DEFINE(app_replaced)
|
|
|
|
{
|
|
|
|
RAII_VAR(struct app_data *, app_data1, NULL, ao2_cleanup);
|
|
|
|
RAII_VAR(struct app_data *, app_data2, NULL, ao2_cleanup);
|
|
|
|
RAII_VAR(char *, app_name, NULL, stasis_app_unregister);
|
|
|
|
RAII_VAR(struct ast_json *, expected_message1, NULL, ast_json_unref);
|
|
|
|
RAII_VAR(struct ast_json *, message, NULL, ast_json_unref);
|
|
|
|
RAII_VAR(struct ast_json *, expected_message2, NULL, ast_json_unref);
|
|
|
|
int res;
|
|
|
|
|
|
|
|
switch (cmd) {
|
|
|
|
case TEST_INIT:
|
|
|
|
info->name = __func__;
|
|
|
|
info->category = test_category;
|
|
|
|
info->summary = "Test stasis app invocation.";
|
|
|
|
info->description = "Test stasis app invocation.";
|
|
|
|
return AST_TEST_NOT_RUN;
|
|
|
|
case TEST_EXECUTE:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
app_name = "test-handler";
|
|
|
|
|
|
|
|
app_data1 = app_data_create();
|
|
|
|
app_data2 = app_data_create();
|
|
|
|
|
|
|
|
stasis_app_register(app_name, test_handler, app_data1);
|
|
|
|
stasis_app_register(app_name, test_handler, app_data2);
|
Update events to use Swagger 1.3 subtyping, and related aftermath
This patch started with the simple idea of changing the /events data
model to be more sane. The original model would send out events like:
{ "stasis_start": { "args": [], "channel": { ... } } }
The event discriminator was the field name instead of being a value in
the object, due to limitations in how Swagger 1.1 could model objects.
While technically sufficient in communicating event information, it was
really difficult to deal with in terms of client side JSON handling.
This patch takes advantage of a proposed extension[1] to Swagger which
allows type variance through the use of a discriminator field. This had
a domino effect that made this a surprisingly large patch.
[1]: https://groups.google.com/d/msg/wordnik-api/EC3rGajE0os/ey_5dBI_jWcJ
In changing the models, I also had to change the swagger_model.py
processor so it can handle the type discriminator and subtyping. I took
that a big step forward, and using that information to generate an
ari_model module, which can validate a JSON object against the Swagger
model.
The REST and WebSocket generators were changed to take advantage of the
validators. If compiled with AST_DEVMODE enabled, JSON objects that
don't match their corresponding models will not be sent out. For REST
API calls, a 500 Internal Server response is sent. For WebSockets, the
invalid JSON message is replaced with an error message.
Since this took over about half of the job of the existing JSON
generators, and the .to_json virtual function on messages took over the
other half, I reluctantly removed the generators.
The validators turned up all sorts of errors and inconsistencies in our
data models, and the code. These were cleaned up, with checks in the
code generator avoid some of the consistency problems in the future.
* The model for a channel snapshot was trimmed down to match the
information sent via AMI. Many of the field being sent were not
useful in the general case.
* The model for a bridge snapshot was updated to be more consistent
with the other ARI models.
Another impact of introducing subtyping was that the swagger-codegen
documentation generator was insufficient (at least until it catches up
with Swagger 1.2). I wanted it to be easier to generate docs for the API
anyways, so I ported the wiki pages to use the Asterisk Swagger
generator. In the process, I was able to clean up many of the model
links, which would occasionally give inconsistent results on the wiki. I
also added error responses to the wiki docs, making the wiki
documentation more complete.
Finally, since Stasis-HTTP will now be named Asterisk REST Interface
(ARI), any new functions and files I created carry the ari_ prefix. I
changed a few stasis_http references to ari where it was non-intrusive
and made sense.
(closes issue ASTERISK-21885)
Review: https://reviewboard.asterisk.org/r/2639/
git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@393529 65c4cc65-6c06-0410-ace0-fbb531ad65f3
2013-07-03 16:32:41 +00:00
|
|
|
expected_message1 = ast_json_pack("[{s: s, s: s}]",
|
|
|
|
"type", "ApplicationReplaced",
|
|
|
|
"application", app_name);
|
2013-04-08 13:27:45 +00:00
|
|
|
message = ast_json_pack("{ s: o }", "test-message", ast_json_null());
|
|
|
|
expected_message2 = ast_json_pack("[o]", ast_json_ref(message));
|
|
|
|
|
|
|
|
res = stasis_app_send(app_name, message);
|
|
|
|
ast_test_validate(test, 0 == res);
|
|
|
|
ast_test_validate(test, 1 == app_data1->invocations);
|
|
|
|
ast_test_validate(test, ast_json_equal(expected_message1, app_data1->messages));
|
|
|
|
ast_test_validate(test, 1 == app_data2->invocations);
|
|
|
|
ast_test_validate(test, ast_json_equal(expected_message2, app_data2->messages));
|
|
|
|
|
|
|
|
return AST_TEST_PASS;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int unload_module(void)
|
|
|
|
{
|
|
|
|
AST_TEST_UNREGISTER(app_invoke_dne);
|
|
|
|
AST_TEST_UNREGISTER(app_invoke_one);
|
|
|
|
AST_TEST_UNREGISTER(app_replaced);
|
2013-05-10 17:12:57 +00:00
|
|
|
stasis_app_unref();
|
2013-04-08 13:27:45 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int load_module(void)
|
|
|
|
{
|
2013-05-10 17:12:57 +00:00
|
|
|
stasis_app_ref();
|
2013-04-08 13:27:45 +00:00
|
|
|
AST_TEST_REGISTER(app_replaced);
|
|
|
|
AST_TEST_REGISTER(app_invoke_one);
|
|
|
|
AST_TEST_REGISTER(app_invoke_dne);
|
|
|
|
return AST_MODULE_LOAD_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2013-04-15 16:43:47 +00:00
|
|
|
AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT, "Stasis Core testing",
|
|
|
|
.load = load_module,
|
|
|
|
.unload = unload_module,
|
|
|
|
.nonoptreq = "res_stasis",
|
2015-05-06 00:49:04 +00:00
|
|
|
);
|