summaryrefslogtreecommitdiffstats
path: root/templates/tests/health-test.jinja2
blob: 6170522fb308695fdfc57e314d2b92d0e020b2ad (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
- test:
    timeout:
      minutes: 2
    definitions:
    - repository:
        metadata:
          format: Lava-Test Test Definition 1.0
          name: inline-test
          description: "Inline test to validate test framework health"
          os:
          - debian
          scope:
          - functional
        run:
          steps:
          - lava-test-set start set-pass
          - lava-test-case always-pass --shell true
          - lava-test-set stop set-pass
      from: inline
      name: health-test
      path: inline/health-test.yaml
lor: #ae81ff } /* Literal.Number.Float */ .highlight .mh { color: #ae81ff } /* Literal.Number.Hex */ .highlight .mi { color: #ae81ff } /* Literal.Number.Integer */ .highlight .mo { color: #ae81ff } /* Literal.Number.Oct */ .highlight .sa { color: #e6db74 } /* Literal.String.Affix */ .highlight .sb { color: #e6db74 } /* Literal.String.Backtick */ .highlight .sc { color: #e6db74 } /* Literal.String.Char */ .highlight .dl { color: #e6db74 } /* Literal.String.Delimiter */ .highlight .sd { color: #e6db74 } /* Literal.String.Doc */ .highlight .s2 { color: #e6db74 } /* Literal.String.Double */ .highlight .se { color: #ae81ff } /* Literal.String.Escape */ .highlight .sh { color: #e6db74 } /* Literal.String.Heredoc */ .highlight .si { color: #e6db74 } /* Literal.String.Interpol */ .highlight .sx { color: #e6db74 } /* Literal.String.Other */ .highlight .sr { color: #e6db74 } /* Literal.String.Regex */ .highlight .s1 { color: #e6db74 } /* Literal.String.Single */ .highlight .ss { color: #e6db74 } /* Literal.String.Symbol */ .highlight .bp { color: #f8f8f2 } /* Name.Builtin.Pseudo */ .highlight .fm { color: #a6e22e } /* Name.Function.Magic */ .highlight .vc { color: #f8f8f2 } /* Name.Variable.Class */ .highlight .vg { color: #f8f8f2 } /* Name.Variable.Global */ .highlight .vi { color: #f8f8f2 } /* Name.Variable.Instance */ .highlight .vm { color: #f8f8f2 } /* Name.Variable.Magic */ .highlight .il { color: #ae81ff } /* Literal.Number.Integer.Long */ } @media (prefers-color-scheme: light) { .highlight .hll { background-color: #ffffcc } .highlight .c { color: #888888 } /* Comment */ .highlight .err { color: #a61717; background-color: #e3d2d2 } /* Error */ .highlight .k { color: #008800; font-weight: bold } /* Keyword */ .highlight .ch { color: #888888 } /* Comment.Hashbang */ .highlight .cm { color: #888888 } /* Comment.Multiline */ .highlight .cp { color: #cc0000; font-weight: bold } /* Comment.Preproc */ .highlight .cpf { color: #888888 } /* Comment.PreprocFile */ .highlight .c1 { color: #888888 } /* Comment.Single */ .highlight .cs { color: #cc0000; font-weight: bold; background-color: #fff0f0 } /* Comment.Special */ .highlight .gd { color: #000000; background-color: #ffdddd } /* Generic.Deleted */ .highlight .ge { font-style: italic } /* Generic.Emph */ .highlight .gr { color: #aa0000 } /* Generic.Error */ .highlight .gh { color: #333333 } /* Generic.Heading */ .highlight .gi { color: #000000; background-color: #ddffdd } /* Generic.Inserted */ .highlight .go { color: #888888 } /* Generic.Output */ .highlight .gp { color: #555555 } /* Generic.Prompt */ .highlight .gs { font-weight: bold } /* Generic.Strong */ .highlight .gu { color: #666666 } /* Generic.Subheading */ .highlight .gt { color: #aa0000 } /* Generic.Traceback */ .highlight .kc { color: #008800; font-weight: bold } /* Keyword.Constant */ .highlight .kd { color: #008800; font-weight: bold } /* Keyword.Declaration */ .highlight .kn { color: #008800; font-weight: bold } /* Keyword.Namespace */ .highlight .kp { color: #008800 } /* Keyword.Pseudo */ .highlight .kr { color: #008800; font-weight: bold } /* Keyword.Reserved */ .highlight .kt { color: #888888; font-weight: bold } /* Keyword.Type */ .highlight .m { color: #0000DD; font-weight: bold } /* Literal.Number */ .highlight .s { color: #dd2200; background-color: #fff0f0 } /* Literal.String */ .highlight .na { color: #336699 } /* Name.Attribute */ .highlight .nb { color: #003388 } /* Name.Builtin */ .highlight .nc { color: #bb0066; font-weight: bold } /* Name.Class */ .highlight .no { color: #003366; font-weight: bold } /* Name.Constant */ .highlight .nd { color: #555555 } /* Name.Decorator */ .highlight .ne { color: #bb0066; font-weight: bold } /* Name.Exception */ .highlight .nf { color: #0066bb; font-weight: bold } /* Name.Function */ .highlight .nl { color: #336699; font-style: italic } /* Name.Label */ .highlight .nn { color: #bb0066; font-weight: bold } /* Name.Namespace */ .highlight .py { color: #336699; font-weight: bold } /* Name.Property */ .highlight .nt { color: #bb0066; font-weight: bold } /* Name.Tag */ .highlight .nv { color: #336699 } /* Name.Variable */ .highlight .ow { color: #008800 } /* Operator.Word */ .highlight .w { color: #bbbbbb } /* Text.Whitespace */ .highlight .mb { color: #0000DD; font-weight: bold } /* Literal.Number.Bin */ .highlight .mf { color: #0000DD; font-weight: bold } /* Literal.Number.Float */ .highlight .mh { color: #0000DD; font-weight: bold } /* Literal.Number.Hex */ .highlight .mi { color: #0000DD; font-weight: bold } /* Literal.Number.Integer */ .highlight .mo { color: #0000DD; font-weight: bold } /* Literal.Number.Oct */ .highlight .sa { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Affix */ .highlight .sb { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Backtick */ .highlight .sc { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Char */ .highlight .dl { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Delimiter */ .highlight .sd { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Doc */ .highlight .s2 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Double */ .highlight .se { color: #0044dd; background-color: #fff0f0 } /* Literal.String.Escape */ .highlight .sh { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Heredoc */ .highlight .si { color: #3333bb; background-color: #fff0f0 } /* Literal.String.Interpol */ .highlight .sx { color: #22bb22; background-color: #f0fff0 } /* Literal.String.Other */ .highlight .sr { color: #008800; background-color: #fff0ff } /* Literal.String.Regex */ .highlight .s1 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Single */ .highlight .ss { color: #aa6600; background-color: #fff0f0 } /* Literal.String.Symbol */ .highlight .bp { color: #003388 } /* Name.Builtin.Pseudo */ .highlight .fm { color: #0066bb; font-weight: bold } /* Name.Function.Magic */ .highlight .vc { color: #336699 } /* Name.Variable.Class */ .highlight .vg { color: #dd7700 } /* Name.Variable.Global */ .highlight .vi { color: #3333bb } /* Name.Variable.Instance */ .highlight .vm { color: #336699 } /* Name.Variable.Magic */ .highlight .il { color: #0000DD; font-weight: bold } /* Literal.Number.Integer.Long */ }
/*
 * Copyright (C) 2015 "IoT.bzh"
 * Author "Fulup Ar Foll"
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#define _GNU_SOURCE         /* See feature_test_macros(7) */
#include <stdio.h>

#include "local-def.h"

#include "utils-jbus.h"

static const char _id_[]    = "id";
static const char _runid_[] = "runid";
static char _runnables_[]   = "runnables";
static char _detail_[]      = "detail";
static char _start_[]       = "start";
static char _terminate_[]   = "terminate";
static char _stop_[]        = "stop";
static char _continue_[]    = "continue";
static char _runners_[]     = "runners";
static char _state_[]       = "state";
static char _install_[]     = "install";
static char _uninstall_[]   = "uninstall";
static const char _mode_[]  = "mode";
static const char _local_[] = "local";
static const char _remote_[]= "remote";
static const char _auto_[]  = "auto";
static const char _uri_[]   = "uri";

static struct jbus *jbus;

static struct json_object *embed(AFB_request *request, const char *tag, struct json_object *obj)
{
	struct json_object *result;

	if (obj == NULL)
		result = NULL;
	else if (!tag) {
		request->errcode = MHD_HTTP_OK;
		result = obj;
	}
	else {
		result = json_object_new_object();
		if (result == NULL) {
			/* can't embed */
			result = obj;
			request->errcode = MHD_HTTP_INTERNAL_SERVER_ERROR;
		}
		else {
			/* TODO why is json-c not returning a status? */
			json_object_object_add(result, tag, obj);
			request->errcode = MHD_HTTP_OK;
		}
	}
	return result;
}

static struct json_object *call_void(AFB_request *request, AFB_PostItem *item)
{
	struct json_object *obj = jbus_call_sj_sync(jbus, request->api, "true");
	if (verbose)
		fprintf(stderr, "(afm-main-plugin) call_void: true -> %s\n", obj ? json_object_to_json_string(obj) : "NULL");
	request->errcode = obj ? MHD_HTTP_OK : MHD_HTTP_FAILED_DEPENDENCY;
	return obj;
}

static struct json_object *call_appid(AFB_request *request, AFB_PostItem *item)
{
	struct json_object *obj;
	char *sid;
	const char *id = getQueryValue(request, _id_);
	if (id == NULL) {
		request->errcode = MHD_HTTP_BAD_REQUEST;
		return NULL;
	}
	if (0 >= asprintf(&sid, "\"%s\"", id)) {
		request->errcode = MHD_HTTP_INTERNAL_SERVER_ERROR;
		return NULL;
	}
	obj = jbus_call_sj_sync(jbus, request->api, sid);
	if (verbose)
		fprintf(stderr, "(afm-main-plugin) call_appid: %s -> %s\n", sid, obj ? json_object_to_json_string(obj) : "NULL");
	free(sid);
	request->errcode = obj ? MHD_HTTP_OK : MHD_HTTP_FAILED_DEPENDENCY;
	return obj;
}

static struct json_object *call_runid(AFB_request *request, AFB_PostItem *item)
{
	struct json_object *obj;
	const char *id = getQueryValue(request, _runid_);
	if (id == NULL) {
		request->errcode = MHD_HTTP_BAD_REQUEST;
		return NULL;
	}
	obj = jbus_call_sj_sync(jbus, request->api, id);
	if (verbose)
		fprintf(stderr, "(afm-main-plugin) call_runid: %s -> %s\n", id, obj ? json_object_to_json_string(obj) : "NULL");
	request->errcode = obj ? MHD_HTTP_OK : MHD_HTTP_FAILED_DEPENDENCY;
	return obj;
}

static struct json_object *call_void__runnables(AFB_request *request, AFB_PostItem *item)
{
	return embed(request, _runnables_, call_void(request, item));
}

static struct json_object *call_start(AFB_request *request, AFB_PostItem *item)
{
	struct json_object *resp;
	const char *id, *mode;
	char *query;
	int rc;

	/* get the id */
	id = getQueryValue(request, _id_);
	if (id == NULL) {
		request->errcode = MHD_HTTP_BAD_REQUEST;
		return NULL;
	}
	/* get the mode */
	mode = getQueryValue(request, _mode_);
	if (mode == NULL || !strcmp(mode, _auto_)) {
		mode = request->config->mode == AFB_MODE_REMOTE ? _remote_ : _local_;
	}

	/* create the query */
	rc = asprintf(&query, "{\"id\":\"%s\",\"mode\":\"%s\"}", id, mode);
	if (rc < 0) {
		request->errcode = MHD_HTTP_INTERNAL_SERVER_ERROR;
		return NULL;
	}

	/* calls the service */
	resp = jbus_call_sj_sync(jbus, _start_, query);
	if (verbose)
		fprintf(stderr, "(afm-main-plugin) call_start: %s -> %s\n", query, resp ? json_object_to_json_string(resp) : "NULL");
	free(query);

	/* embed if needed */
	if (json_object_get_type(resp) == json_type_int)
		resp = embed(request, _runid_, resp);
	request->errcode = resp ? MHD_HTTP_OK : MHD_HTTP_FAILED_DEPENDENCY;
	return resp;
}

static struct json_object *call_void__runners(AFB_request *request, AFB_PostItem *item)
{
	return embed(request, _runners_, call_void(request, item));
}

static struct json_object *call_file__appid(AFB_request *request, AFB_PostItem *item)
{
	if (item == NULL) {
		const char *filename = getPostPath(request);
		if (filename != NULL) {
			struct json_object *obj;
			char *query;
			request->jresp = NULL;
			if (0 >= asprintf(&query, "\"%s\"", filename))
				request->errcode = MHD_HTTP_INTERNAL_SERVER_ERROR;
			else {
				obj = jbus_call_sj_sync(jbus, request->api, query);
				if (verbose)
					fprintf(stderr, "(afm-main-plugin) call_file_appid: %s -> %s\n", query, obj ? json_object_to_json_string(obj) : "NULL");
				free(query);
				if (obj)
					request->jresp = embed(request, _id_, obj);
				else
					request->errcode = MHD_HTTP_FAILED_DEPENDENCY;
			}
			unlink(filename);
		}
	}
	return getPostFile (request, item, "/tmp/upload");
}

static AFB_restapi plug_apis[] =
{
	{_runnables_, AFB_SESSION_CHECK, (AFB_apiCB)call_void__runnables,  "Get list of runnable applications"},
	{_detail_   , AFB_SESSION_CHECK, (AFB_apiCB)call_appid, "Get the details for one application"},
	{_start_    , AFB_SESSION_CHECK, (AFB_apiCB)call_start, "Start an application"},
	{_terminate_, AFB_SESSION_CHECK, (AFB_apiCB)call_runid, "Terminate a running application"},
	{_stop_     , AFB_SESSION_CHECK, (AFB_apiCB)call_runid, "Stop (pause) a running application"},
	{_continue_ , AFB_SESSION_CHECK, (AFB_apiCB)call_runid, "Continue (resume) a stopped application"},
	{_runners_  , AFB_SESSION_CHECK, (AFB_apiCB)call_void__runners,  "Get the list of running applications"},
	{_state_    , AFB_SESSION_CHECK, (AFB_apiCB)call_runid, "Get the state of a running application"},
	{_install_  , AFB_SESSION_CHECK, (AFB_apiCB)call_file__appid,  "Install an application using a widget file"},
	{_uninstall_, AFB_SESSION_CHECK, (AFB_apiCB)call_appid, "Uninstall an application"},
	{ NULL, 0, NULL, NULL }
};

static AFB_plugin plug_desc = {
	.type = AFB_PLUGIN_JSON,
	.info = "Application Framework Master Service",
	.prefix = "afm-main",
	.apis = plug_apis
};

AFB_plugin *pluginRegister()
{
	jbus = create_jbus_session("/org/AGL/afm/user");
        if (jbus)
		return &plug_desc;
	fprintf(stderr, "ERROR: %s:%d: can't connect to DBUS session\n", __FILE__, __LINE__);
	return NULL;
}