mirror of
https://dev.iopsys.eu/iopsys/imonitor.git
synced 2025-12-20 01:03:43 +08:00
989 lines
31 KiB
C
989 lines
31 KiB
C
/*
|
|
* Copyright (C) 2018 Inteno Broadband Technology AB. All rights reserved.
|
|
*
|
|
* Authors: Hrvoje Varga <hrvoje.varga@sartura.hr>
|
|
* Matija Amidzic <matija.amidzic@sartura.hr>
|
|
*
|
|
* This program is free software; you can redistribute it and/or
|
|
* modify it under the terms of the GNU General Public License
|
|
* version 2 as published by the Free Software Foundation.
|
|
*
|
|
* 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, write to the Free Software
|
|
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
|
|
* 02110-1301 USA
|
|
*/
|
|
|
|
#include <stdarg.h>
|
|
#include <stddef.h>
|
|
#include <setjmp.h>
|
|
#include <cmocka.h>
|
|
#include <libubox/list.h>
|
|
#include <pthread.h>
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <unistd.h>
|
|
#include <string.h>
|
|
#include "monitor.h"
|
|
|
|
static const char *ubus_socket_path = "/var/run/ubus/ubus.sock";
|
|
|
|
static int _counterTestCalled = 0;
|
|
static int _counterExecuteCalled = 0;
|
|
|
|
/** test_monitor_non_existing_ubus_socket
|
|
*
|
|
* Check that monitor will return error -1 in case when the path to non-existing
|
|
* ubus socket is passed as an argument.
|
|
*/
|
|
static void test_monitor_non_existing_ubus_socket(void **state)
|
|
{
|
|
struct list_head monitors = LIST_HEAD_INIT(monitors);
|
|
int error = monitor_start("invalid_ubus.sock", &monitors);
|
|
assert_int_equal(error, -1);
|
|
}
|
|
|
|
/**
|
|
* test_monitor_condition_not_met
|
|
*
|
|
* Check that monitor execute callback will not be called since the condition
|
|
* must not be met.
|
|
* Test uses stdout stream for condition.
|
|
* Test also checks that the monitor test callback is called with the correct
|
|
* command.
|
|
*/
|
|
static void *test_monitor_condition_stdout_not_met_start_monitor(void *vargp)
|
|
{
|
|
// initialize list and one monitor
|
|
struct list_head monitors = LIST_HEAD_INIT(monitors);
|
|
monitor_t *monitor = NULL;
|
|
monitor = calloc(1, sizeof(monitor_t));
|
|
monitor->app = "test";
|
|
monitor->test = "echo test";
|
|
monitor->stream = STREAM_STDOUT;
|
|
monitor->match_pattern = "a pattern that will not match";
|
|
monitor->pattern_type = PATTERN_MATCH;
|
|
monitor->execute = "echo 1";
|
|
monitor->interval = 1000;
|
|
monitor->nr_tests = 1;
|
|
list_add(&monitor->head, &monitors);
|
|
|
|
// start monitor
|
|
monitor_start(ubus_socket_path, &monitors);
|
|
free(monitor);
|
|
return NULL;
|
|
}
|
|
|
|
static void *test_monitor_condition_stdout_not_met_stop_monitor(void *vargp)
|
|
{
|
|
sleep(2);
|
|
monitor_stop();
|
|
return NULL;
|
|
}
|
|
|
|
static void test_monitor_condition_stdout_not_met_test_called(char *command)
|
|
{
|
|
// assert the command
|
|
assert_string_equal(command, "echo test");
|
|
// increment the global variable
|
|
_counterTestCalled++;
|
|
}
|
|
|
|
static void test_monitor_condition_stdout_not_met_execute_called(char *command)
|
|
{
|
|
// increment the global variable
|
|
_counterExecuteCalled++;
|
|
}
|
|
|
|
static void test_monitor_condition_stdout_not_met(void **state)
|
|
{
|
|
// reset global variables
|
|
_counterTestCalled = 0;
|
|
_counterExecuteCalled = 0;
|
|
|
|
// setup the monitor callbacks
|
|
monitor_test_cb = test_monitor_condition_stdout_not_met_test_called;
|
|
monitor_execute_cb = test_monitor_condition_stdout_not_met_execute_called;
|
|
|
|
// start a new thread which will call `monitor_start` from monitor.h
|
|
pthread_t start_monitor_tid;
|
|
pthread_create(&start_monitor_tid, NULL, test_monitor_condition_stdout_not_met_start_monitor, NULL);
|
|
|
|
// start a new thread which will call `monitor_stop` from monitor.h
|
|
pthread_t stop_monitor_tid;
|
|
pthread_create(&stop_monitor_tid, NULL, test_monitor_condition_stdout_not_met_stop_monitor, NULL);
|
|
|
|
// wait for `monitor_stop` to finish
|
|
pthread_join(stop_monitor_tid, NULL);
|
|
|
|
// wait for `monitor_start` to finish
|
|
pthread_join(start_monitor_tid, NULL);
|
|
|
|
// check that test callback is called
|
|
assert_int_equal(_counterTestCalled, 1);
|
|
// check that execute callback is not called
|
|
assert_int_equal(_counterExecuteCalled, 0);
|
|
}
|
|
|
|
/**
|
|
* test_monitor_condition_is_met
|
|
*
|
|
* Check that monitor execute callback will be called since the condition is met.
|
|
* Test uses stdout stream for condition.
|
|
* Test also checks that the monitor test and execute callback is called with
|
|
* the correct command.
|
|
*/
|
|
static void *test_monitor_condition_stdout_is_met_start_monitor(void *vargp)
|
|
{
|
|
// initialize list and one monitor
|
|
struct list_head monitors = LIST_HEAD_INIT(monitors);
|
|
monitor_t *monitor = NULL;
|
|
monitor = calloc(1, sizeof(monitor_t));
|
|
monitor->app = "test";
|
|
monitor->test = "echo test";
|
|
monitor->stream = STREAM_STDOUT;
|
|
monitor->match_pattern = "test";
|
|
monitor->pattern_type = PATTERN_MATCH;
|
|
monitor->execute = "echo execute callback called";
|
|
monitor->interval = 1000;
|
|
monitor->nr_tests = 1;
|
|
list_add(&monitor->head, &monitors);
|
|
|
|
// start monitor
|
|
monitor_start(ubus_socket_path, &monitors);
|
|
free(monitor);
|
|
return NULL;
|
|
}
|
|
|
|
static void *test_monitor_condition_stdout_is_met_stop_monitor(void *vargp)
|
|
{
|
|
sleep(2);
|
|
monitor_stop();
|
|
return NULL;
|
|
}
|
|
|
|
static void test_monitor_condition_stdout_is_met_test_called(char *command)
|
|
{
|
|
// assert the command
|
|
assert_string_equal(command, "echo test");
|
|
// increment the global variable
|
|
_counterTestCalled++;
|
|
}
|
|
|
|
static void test_monitor_condition_stdout_is_met_execute_called(char *command)
|
|
{
|
|
assert_string_equal(command, "echo execute callback called");
|
|
// increment the global variable
|
|
_counterExecuteCalled++;
|
|
}
|
|
|
|
static void test_monitor_condition_stdout_is_met(void **state)
|
|
{
|
|
// reset global variables
|
|
_counterTestCalled = 0;
|
|
_counterExecuteCalled = 0;
|
|
|
|
// setup the monitor callbacks
|
|
monitor_test_cb = test_monitor_condition_stdout_is_met_test_called;
|
|
monitor_execute_cb = test_monitor_condition_stdout_is_met_execute_called;
|
|
|
|
// start a new thread which will call `monitor_start` from monitor.h
|
|
pthread_t start_monitor_tid;
|
|
pthread_create(&start_monitor_tid, NULL, test_monitor_condition_stdout_is_met_start_monitor, NULL);
|
|
|
|
// start a new thread which will call `monitor_stop` from monitor.h
|
|
pthread_t stop_monitor_tid;
|
|
pthread_create(&stop_monitor_tid, NULL, test_monitor_condition_stdout_is_met_stop_monitor, NULL);
|
|
|
|
// wait for `monitor_stop` to finish
|
|
pthread_join(stop_monitor_tid, NULL);
|
|
|
|
// wait for `monitor_start` to finish
|
|
pthread_join(start_monitor_tid, NULL);
|
|
|
|
// check that test callback is called
|
|
assert_int_equal(_counterTestCalled, 1);
|
|
// check that execute callback is called
|
|
assert_int_equal(_counterExecuteCalled, 1);
|
|
}
|
|
|
|
/**
|
|
* test_monitor_condition_stderr_not_met
|
|
*
|
|
* Check that monitor execute callback will not be called since the condition
|
|
* must not be met.
|
|
* Test uses stderr stream for condition.
|
|
* Test also checks that the monitor test callback is called with the correct
|
|
* command.
|
|
*/
|
|
static void *test_monitor_condition_stderr_not_met_start_monitor(void *vargp)
|
|
{
|
|
// initialize list and one monitor
|
|
struct list_head monitors = LIST_HEAD_INIT(monitors);
|
|
monitor_t *monitor = NULL;
|
|
monitor = calloc(1, sizeof(monitor_t));
|
|
monitor->app = "test";
|
|
monitor->test = "echo test 1>&2";
|
|
monitor->stream = STREAM_STDERR;
|
|
monitor->match_pattern = "a pattern that will not match";
|
|
monitor->pattern_type = PATTERN_MATCH;
|
|
monitor->execute = "echo 1";
|
|
monitor->interval = 1000;
|
|
monitor->nr_tests = 1;
|
|
list_add(&monitor->head, &monitors);
|
|
|
|
// start monitor
|
|
monitor_start(ubus_socket_path, &monitors);
|
|
free(monitor);
|
|
return NULL;
|
|
}
|
|
|
|
static void *test_monitor_condition_stderr_not_met_stop_monitor(void *vargp)
|
|
{
|
|
sleep(2);
|
|
monitor_stop();
|
|
return NULL;
|
|
}
|
|
|
|
static void test_monitor_condition_stderr_not_met_test_called(char *command)
|
|
{
|
|
// assert the command
|
|
assert_string_equal(command, "echo test 1>&2");
|
|
// increment the global variable
|
|
_counterTestCalled++;
|
|
}
|
|
|
|
static void test_monitor_condition_stderr_not_met_execute_called(char *command)
|
|
{
|
|
// increment the global variable
|
|
_counterExecuteCalled++;
|
|
}
|
|
|
|
static void test_monitor_condition_stderr_not_met(void **state)
|
|
{
|
|
// reset global variables
|
|
_counterTestCalled = 0;
|
|
_counterExecuteCalled = 0;
|
|
|
|
// setup the monitor callbacks
|
|
monitor_test_cb = test_monitor_condition_stderr_not_met_test_called;
|
|
monitor_execute_cb = test_monitor_condition_stderr_not_met_execute_called;
|
|
|
|
// start a new thread which will call `monitor_start` from monitor.h
|
|
pthread_t start_monitor_tid;
|
|
pthread_create(&start_monitor_tid, NULL, test_monitor_condition_stderr_not_met_start_monitor, NULL);
|
|
|
|
// start a new thread which will call `monitor_stop` from monitor.h
|
|
pthread_t stop_monitor_tid;
|
|
pthread_create(&stop_monitor_tid, NULL, test_monitor_condition_stderr_not_met_stop_monitor, NULL);
|
|
|
|
// wait for `monitor_stop` to finish
|
|
pthread_join(stop_monitor_tid, NULL);
|
|
|
|
// wait for `monitor_start` to finish
|
|
pthread_join(start_monitor_tid, NULL);
|
|
|
|
// check that test callback is called
|
|
assert_int_equal(_counterTestCalled, 1);
|
|
// check that execute callback is not called
|
|
assert_int_equal(_counterExecuteCalled, 0);
|
|
}
|
|
|
|
/**
|
|
* test_monitor_condition_stderr_is_met
|
|
*
|
|
* Check that monitor execute callback will be called since the condition is met.
|
|
* Test uses stderr stream for condition.
|
|
* Test also checks that the monitor test and execute callback is called with
|
|
* the correct command.
|
|
*/
|
|
static void *test_monitor_condition_stderr_is_met_start_monitor(void *vargp)
|
|
{
|
|
// initialize list and one monitor
|
|
struct list_head monitors = LIST_HEAD_INIT(monitors);
|
|
monitor_t *monitor = NULL;
|
|
monitor = calloc(1, sizeof(monitor_t));
|
|
monitor->app = "test";
|
|
monitor->test = "echo test 1>&2";
|
|
monitor->stream = STREAM_STDERR;
|
|
monitor->match_pattern = "test";
|
|
monitor->pattern_type = PATTERN_MATCH;
|
|
monitor->execute = "echo execute callback called";
|
|
monitor->interval = 1000;
|
|
monitor->nr_tests = 1;
|
|
list_add(&monitor->head, &monitors);
|
|
|
|
// start monitor
|
|
monitor_start(ubus_socket_path, &monitors);
|
|
free(monitor);
|
|
return NULL;
|
|
}
|
|
|
|
static void *test_monitor_condition_stderr_is_met_stop_monitor(void *vargp)
|
|
{
|
|
sleep(2);
|
|
monitor_stop();
|
|
return NULL;
|
|
}
|
|
|
|
static void test_monitor_condition_stderr_is_met_test_called(char *command)
|
|
{
|
|
// assert the command
|
|
assert_string_equal(command, "echo test 1>&2");
|
|
// increment the global variable
|
|
_counterTestCalled++;
|
|
}
|
|
|
|
static void test_monitor_condition_stderr_is_met_execute_called(char *command)
|
|
{
|
|
assert_string_equal(command, "echo execute callback called");
|
|
// increment the global variable
|
|
_counterExecuteCalled++;
|
|
}
|
|
|
|
static void test_monitor_condition_stderr_is_met(void **state)
|
|
{
|
|
// reset global variables
|
|
_counterTestCalled = 0;
|
|
_counterExecuteCalled = 0;
|
|
|
|
// setup the monitor callbacks
|
|
monitor_test_cb = test_monitor_condition_stderr_is_met_test_called;
|
|
monitor_execute_cb = test_monitor_condition_stderr_is_met_execute_called;
|
|
|
|
// start a new thread which will call `monitor_start` from monitor.h
|
|
pthread_t start_monitor_tid;
|
|
pthread_create(&start_monitor_tid, NULL, test_monitor_condition_stderr_is_met_start_monitor, NULL);
|
|
|
|
// start a new thread which will call `monitor_stop` from monitor.h
|
|
pthread_t stop_monitor_tid;
|
|
pthread_create(&stop_monitor_tid, NULL, test_monitor_condition_stderr_is_met_stop_monitor, NULL);
|
|
|
|
// wait for `monitor_stop` to finish
|
|
pthread_join(stop_monitor_tid, NULL);
|
|
|
|
// wait for `monitor_start` to finish
|
|
pthread_join(start_monitor_tid, NULL);
|
|
|
|
// check that test callback is called
|
|
assert_int_equal(_counterTestCalled, 1);
|
|
// check that execute callback is called
|
|
assert_int_equal(_counterExecuteCalled, 1);
|
|
}
|
|
|
|
/**
|
|
* test_monitor_condition_met_nr_tests
|
|
*
|
|
* Check that monitor execute callback is called only once when a condition is
|
|
* met and that test callback is called nr_tests times.
|
|
*/
|
|
static void *test_monitor_condition_met_nr_tests_start_monitor(void *vargp)
|
|
{
|
|
// initialize list and one monitor
|
|
struct list_head monitors = LIST_HEAD_INIT(monitors);
|
|
monitor_t *monitor = NULL;
|
|
monitor = calloc(1, sizeof(monitor_t));
|
|
monitor->app = "test";
|
|
monitor->test = "echo test";
|
|
monitor->stream = STREAM_STDOUT;
|
|
monitor->match_pattern = "test";
|
|
monitor->pattern_type = PATTERN_MATCH;
|
|
monitor->execute = "echo execute callback called";
|
|
monitor->interval = 200;
|
|
monitor->nr_tests = 4;
|
|
list_add(&monitor->head, &monitors);
|
|
|
|
// start monitor
|
|
monitor_start(ubus_socket_path, &monitors);
|
|
free(monitor);
|
|
return NULL;
|
|
}
|
|
|
|
static void *test_monitor_condition_met_nr_tests_stop_monitor(void *vargp)
|
|
{
|
|
sleep(1);
|
|
monitor_stop();
|
|
return NULL;
|
|
}
|
|
|
|
static void test_monitor_condition_met_nr_tests_test_called(char *command)
|
|
{
|
|
// assert the command
|
|
assert_string_equal(command, "echo test");
|
|
// increment the global variable
|
|
if (_counterExecuteCalled == 0)
|
|
_counterTestCalled++;
|
|
}
|
|
|
|
static void test_monitor_condition_met_nr_tests_execute_called(char *command)
|
|
{
|
|
assert_string_equal(command, "echo execute callback called");
|
|
// increment the global variable
|
|
_counterExecuteCalled++;
|
|
}
|
|
|
|
static void test_monitor_condition_met_nr_tests(void **state)
|
|
{
|
|
// reset global variables
|
|
_counterTestCalled = 0;
|
|
_counterExecuteCalled = 0;
|
|
|
|
// setup the monitor callbacks
|
|
monitor_test_cb = test_monitor_condition_met_nr_tests_test_called;
|
|
monitor_execute_cb = test_monitor_condition_met_nr_tests_execute_called;
|
|
|
|
// start a new thread which will call `monitor_start` from monitor.h
|
|
pthread_t start_monitor_tid;
|
|
pthread_create(&start_monitor_tid, NULL, test_monitor_condition_met_nr_tests_start_monitor, NULL);
|
|
|
|
// start a new thread which will call `monitor_stop` from monitor.h
|
|
pthread_t stop_monitor_tid;
|
|
pthread_create(&stop_monitor_tid, NULL, test_monitor_condition_met_nr_tests_stop_monitor, NULL);
|
|
|
|
// wait for `monitor_stop` to finish
|
|
pthread_join(stop_monitor_tid, NULL);
|
|
|
|
// wait for `monitor_start` to finish
|
|
pthread_join(start_monitor_tid, NULL);
|
|
|
|
// check that test callback is called correct number of times
|
|
assert_int_equal(_counterTestCalled, 4);
|
|
// check that execute callback is called
|
|
assert_int_equal(_counterExecuteCalled, 1);
|
|
}
|
|
|
|
/**
|
|
* test_monitor_multiple_monitors
|
|
*
|
|
* Check that monitor test callback and execute callback will be called for
|
|
* every monitor.
|
|
*/
|
|
static void *test_monitor_multiple_monitors_start_monitor(void *vargp)
|
|
{
|
|
// initialize list and two monitors
|
|
struct list_head monitors = LIST_HEAD_INIT(monitors);
|
|
monitor_t *monitor_one = NULL;
|
|
monitor_one = calloc(1, sizeof(monitor_t));
|
|
monitor_one->app = "monitor_one";
|
|
monitor_one->test = "echo monitor_one";
|
|
monitor_one->stream = STREAM_STDOUT;
|
|
monitor_one->match_pattern = "monitor_one";
|
|
monitor_one->pattern_type = PATTERN_MATCH;
|
|
monitor_one->execute = "echo execute monitor_one";
|
|
monitor_one->interval = 1000;
|
|
monitor_one->nr_tests = 1;
|
|
list_add(&monitor_one->head, &monitors);
|
|
|
|
monitor_t *monitor_two = NULL;
|
|
monitor_two = calloc(1, sizeof(monitor_t));
|
|
monitor_two->app = "monitor_two";
|
|
monitor_two->test = "echo monitor_two";
|
|
monitor_two->stream = STREAM_STDOUT;
|
|
monitor_two->match_pattern = "monitor_two";
|
|
monitor_two->pattern_type = PATTERN_MATCH;
|
|
monitor_two->execute = "echo execute monitor_two";
|
|
monitor_two->interval = 1000;
|
|
monitor_two->nr_tests = 1;
|
|
list_add(&monitor_two->head, &monitors);
|
|
|
|
// start monitor
|
|
monitor_start(ubus_socket_path, &monitors);
|
|
free(monitor_one);
|
|
free(monitor_two);
|
|
return NULL;
|
|
}
|
|
|
|
static void *test_monitor_multiple_monitors_stop_monitor(void *vargp)
|
|
{
|
|
sleep(2);
|
|
monitor_stop();
|
|
return NULL;
|
|
}
|
|
|
|
static void test_monitor_multiple_monitors_test_called(char *command)
|
|
{
|
|
// check that the test callback is called
|
|
if (strcmp(command, "echo monitor_one") == 0 || strcmp(command, "echo monitor_two") == 0) {
|
|
_counterTestCalled++;
|
|
} else {
|
|
fail_msg("wrong test command: %s", command);
|
|
}
|
|
}
|
|
|
|
static void test_monitor_multiple_monitors_execute_called(char *command)
|
|
{
|
|
// check that the execute callback is called
|
|
if (strcmp(command, "echo execute monitor_one") == 0 || strcmp(command, "echo execute monitor_two") == 0) {
|
|
_counterExecuteCalled++;
|
|
} else {
|
|
fail_msg("wrong execute command: %s", command);
|
|
}
|
|
}
|
|
|
|
static void test_monitor_multiple_monitors(void **state)
|
|
{
|
|
// reset global variables
|
|
_counterTestCalled = 0;
|
|
_counterExecuteCalled = 0;
|
|
|
|
// setup the monitor callbacks
|
|
monitor_test_cb = test_monitor_multiple_monitors_test_called;
|
|
monitor_execute_cb = test_monitor_multiple_monitors_execute_called;
|
|
|
|
// start a new thread which will call `monitor_start` from monitor.h
|
|
pthread_t start_monitor_tid;
|
|
pthread_create(&start_monitor_tid, NULL, test_monitor_multiple_monitors_start_monitor, NULL);
|
|
|
|
// start a new thread which will call `monitor_stop` from monitor.h
|
|
pthread_t stop_monitor_tid;
|
|
pthread_create(&stop_monitor_tid, NULL, test_monitor_multiple_monitors_stop_monitor, NULL);
|
|
|
|
// wait for `monitor_stop` to finish
|
|
pthread_join(stop_monitor_tid, NULL);
|
|
|
|
// wait for `monitor_start` to finish
|
|
pthread_join(start_monitor_tid, NULL);
|
|
|
|
// check that test callback is called for both monitors
|
|
assert_int_equal(_counterTestCalled, 2);
|
|
// check that execute callback is called for both monitors
|
|
assert_int_equal(_counterExecuteCalled, 2);
|
|
}
|
|
|
|
/**
|
|
* test_monitor_test_output_null
|
|
*
|
|
* Check that monitor execute callback will not be called since the condition
|
|
* is not met due to the NULL output.
|
|
* Test also checks that the monitor will not crash.
|
|
*/
|
|
static void *test_monitor_test_output_null_start_monitor(void *vargp)
|
|
{
|
|
// initialize list and one monitor
|
|
struct list_head monitors = LIST_HEAD_INIT(monitors);
|
|
monitor_t *monitor = NULL;
|
|
monitor = calloc(1, sizeof(monitor_t));
|
|
monitor->app = "test";
|
|
monitor->test = "unknown command";
|
|
monitor->stream = STREAM_STDOUT;
|
|
monitor->match_pattern = "test";
|
|
monitor->pattern_type = PATTERN_MATCH;
|
|
monitor->execute = "echo execute callback called";
|
|
monitor->interval = 1000;
|
|
monitor->nr_tests = 1;
|
|
list_add(&monitor->head, &monitors);
|
|
|
|
// start monitor
|
|
monitor_start(ubus_socket_path, &monitors);
|
|
free(monitor);
|
|
return NULL;
|
|
}
|
|
|
|
static void *test_monitor_test_output_null_stop_monitor(void *vargp)
|
|
{
|
|
sleep(2);
|
|
monitor_stop();
|
|
return NULL;
|
|
}
|
|
|
|
static void test_monitor_test_output_null_test_called(char *command)
|
|
{
|
|
// assert the command
|
|
assert_string_equal(command, "unknown command");
|
|
// increment the global variable
|
|
_counterTestCalled++;
|
|
}
|
|
|
|
static void test_monitor_test_output_null_execute_called(char *command)
|
|
{
|
|
// increment the global variable
|
|
_counterExecuteCalled++;
|
|
}
|
|
|
|
static void test_monitor_test_output_null(void **state)
|
|
{
|
|
// reset global variables
|
|
_counterTestCalled = 0;
|
|
_counterExecuteCalled = 0;
|
|
|
|
// setup the monitor callbacks
|
|
monitor_test_cb = test_monitor_test_output_null_test_called;
|
|
monitor_execute_cb = test_monitor_test_output_null_execute_called;
|
|
|
|
// start a new thread which will call `monitor_start` from monitor.h
|
|
pthread_t start_monitor_tid;
|
|
pthread_create(&start_monitor_tid, NULL, test_monitor_test_output_null_start_monitor, NULL);
|
|
|
|
// start a new thread which will call `monitor_stop` from monitor.h
|
|
pthread_t stop_monitor_tid;
|
|
pthread_create(&stop_monitor_tid, NULL, test_monitor_test_output_null_stop_monitor, NULL);
|
|
|
|
// wait for `monitor_stop` to finish
|
|
pthread_join(stop_monitor_tid, NULL);
|
|
|
|
// wait for `monitor_start` to finish
|
|
pthread_join(start_monitor_tid, NULL);
|
|
|
|
// check that test callback is called
|
|
assert_int_equal(_counterTestCalled, 1);
|
|
// check that execute callback is not called
|
|
assert_int_equal(_counterExecuteCalled, 0);
|
|
}
|
|
|
|
/** test_monitor_empty_monitors
|
|
*
|
|
* Check that monitor will immediately return with no error in case when empty
|
|
* monitor list is passed as an argument.
|
|
*/
|
|
static void test_monitor_empty_monitors(void **state)
|
|
{
|
|
struct list_head monitors = LIST_HEAD_INIT(monitors);
|
|
|
|
// start monitor
|
|
int error = monitor_start(ubus_socket_path, &monitors);
|
|
assert_int_equal(error, 0);
|
|
monitor_stop();
|
|
}
|
|
|
|
/** test_monitor_string_not_match_is_met
|
|
*
|
|
* Check that monitor execute callback will be called since the condition string_not_match is met.
|
|
*/
|
|
static void *test_monitor_string_not_match_is_met_start_monitor(void *vargp)
|
|
{
|
|
// initialize list and one monitor
|
|
struct list_head monitors = LIST_HEAD_INIT(monitors);
|
|
monitor_t *monitor = NULL;
|
|
monitor = calloc(1, sizeof(monitor_t));
|
|
monitor->app = "echo";
|
|
monitor->test = "echo test";
|
|
monitor->stream = STREAM_STDOUT;
|
|
monitor->match_pattern = "some string that is different than test";
|
|
monitor->pattern_type = PATTERN_NOT_MATCH;
|
|
monitor->execute = "echo execute callback called";
|
|
monitor->interval = 1000;
|
|
monitor->nr_tests = 1;
|
|
list_add(&monitor->head, &monitors);
|
|
|
|
// start monitor
|
|
monitor_start(ubus_socket_path, &monitors);
|
|
free(monitor);
|
|
|
|
return NULL;
|
|
}
|
|
|
|
static void *test_monitor_string_not_match_is_met_stop_monitor(void *vargp)
|
|
{
|
|
sleep(2);
|
|
monitor_stop();
|
|
return NULL;
|
|
}
|
|
|
|
static void test_monitor_string_not_match_is_met_test_called(char *command)
|
|
{
|
|
// assert the command
|
|
assert_string_equal(command, "echo test");
|
|
// increment the global variable
|
|
_counterTestCalled++;
|
|
}
|
|
|
|
static void test_monitor_string_not_match_is_met_execute_called(char *command)
|
|
{
|
|
// assert the command
|
|
assert_string_equal(command, "echo execute callback called");
|
|
// increment the global variable
|
|
_counterExecuteCalled++;
|
|
}
|
|
|
|
static void test_monitor_string_not_match_is_met(void **state)
|
|
{
|
|
// reset global variables
|
|
_counterTestCalled = 0;
|
|
_counterExecuteCalled = 0;
|
|
|
|
// setup the monitor callbacks
|
|
monitor_test_cb = test_monitor_string_not_match_is_met_test_called;
|
|
monitor_execute_cb = test_monitor_string_not_match_is_met_execute_called;
|
|
|
|
// start a new thread which will call `monitor_start` from monitor.h
|
|
pthread_t start_monitor_tid;
|
|
pthread_create(&start_monitor_tid, NULL, test_monitor_string_not_match_is_met_start_monitor, NULL);
|
|
|
|
// start a new thread which will call `monitor_stop` from monitor.h
|
|
pthread_t stop_monitor_tid;
|
|
pthread_create(&stop_monitor_tid, NULL, test_monitor_string_not_match_is_met_stop_monitor, NULL);
|
|
|
|
// wait for `monitor_stop` to finish
|
|
pthread_join(stop_monitor_tid, NULL);
|
|
|
|
// wait for `monitor_start` to finish
|
|
pthread_join(start_monitor_tid, NULL);
|
|
|
|
// check that test callback is called
|
|
assert_int_equal(_counterTestCalled, 1);
|
|
// check that execute callback is called
|
|
assert_int_equal(_counterExecuteCalled, 1);
|
|
}
|
|
|
|
/** test_monitor_string_not_match_not_met
|
|
*
|
|
* Check that monitor execute callback will not be called since the condition string_not_match is not met.
|
|
*/
|
|
static void *test_monitor_string_not_match_not_met_start_monitor(void *vargp)
|
|
{
|
|
// initialize list and one monitor
|
|
struct list_head monitors = LIST_HEAD_INIT(monitors);
|
|
monitor_t *monitor = NULL;
|
|
monitor = calloc(1, sizeof(monitor_t));
|
|
monitor->app = "echo";
|
|
monitor->test = "echo test";
|
|
monitor->stream = STREAM_STDOUT;
|
|
monitor->match_pattern = "test";
|
|
monitor->pattern_type = PATTERN_NOT_MATCH;
|
|
monitor->execute = "echo execute callback called";
|
|
monitor->interval = 1000;
|
|
monitor->nr_tests = 1;
|
|
list_add(&monitor->head, &monitors);
|
|
|
|
// start monitor
|
|
monitor_start(ubus_socket_path, &monitors);
|
|
free(monitor);
|
|
|
|
return NULL;
|
|
}
|
|
|
|
static void *test_monitor_string_not_match_not_met_stop_monitor(void *vargp)
|
|
{
|
|
sleep(2);
|
|
monitor_stop();
|
|
return NULL;
|
|
}
|
|
|
|
static void test_monitor_string_not_match_not_met_test_called(char *command)
|
|
{
|
|
// assert the command
|
|
assert_string_equal(command, "echo test");
|
|
// increment the global variable
|
|
_counterTestCalled++;
|
|
}
|
|
|
|
static void test_monitor_string_not_match_not_met_execute_called(char *command)
|
|
{
|
|
// increment the global variable
|
|
_counterExecuteCalled++;
|
|
}
|
|
|
|
static void test_monitor_string_not_match_not_met(void **state)
|
|
{
|
|
// reset global variables
|
|
_counterTestCalled = 0;
|
|
_counterExecuteCalled = 0;
|
|
|
|
// setup the monitor callbacks
|
|
monitor_test_cb = test_monitor_string_not_match_not_met_test_called;
|
|
monitor_execute_cb = test_monitor_string_not_match_not_met_execute_called;
|
|
|
|
// start a new thread which will call `monitor_start` from monitor.h
|
|
pthread_t start_monitor_tid;
|
|
pthread_create(&start_monitor_tid, NULL, test_monitor_string_not_match_not_met_start_monitor, NULL);
|
|
|
|
// start a new thread which will call `monitor_stop` from monitor.h
|
|
pthread_t stop_monitor_tid;
|
|
pthread_create(&stop_monitor_tid, NULL, test_monitor_string_not_match_not_met_stop_monitor, NULL);
|
|
|
|
// wait for `monitor_stop` to finish
|
|
pthread_join(stop_monitor_tid, NULL);
|
|
|
|
// wait for `monitor_start` to finish
|
|
pthread_join(start_monitor_tid, NULL);
|
|
|
|
// check that test callback is called
|
|
assert_int_equal(_counterTestCalled, 1);
|
|
// check that execute callback is called
|
|
assert_int_equal(_counterExecuteCalled, 0);
|
|
}
|
|
|
|
/** test_monitor_regex_match_is_met
|
|
*
|
|
* Check that monitor execute callback will be called since the condition regex_match is met.
|
|
*/
|
|
static void *test_monitor_regex_match_is_met_start_monitor(void *vargp)
|
|
{
|
|
// initialize list and one monitor
|
|
struct list_head monitors = LIST_HEAD_INIT(monitors);
|
|
monitor_t *monitor = NULL;
|
|
monitor = calloc(1, sizeof(monitor_t));
|
|
monitor->app = "echo";
|
|
monitor->test = "echo some string that has 'test' in it";
|
|
monitor->stream = STREAM_STDOUT;
|
|
monitor->match_pattern = ".*test.*";
|
|
monitor->pattern_type = PATTERN_REGEX;
|
|
monitor->execute = "echo execute callback called";
|
|
monitor->interval = 1000;
|
|
monitor->nr_tests = 1;
|
|
list_add(&monitor->head, &monitors);
|
|
|
|
// start monitor
|
|
monitor_start(ubus_socket_path, &monitors);
|
|
free(monitor);
|
|
|
|
return NULL;
|
|
}
|
|
|
|
static void *test_monitor_regex_match_is_met_stop_monitor(void *vargp)
|
|
{
|
|
sleep(2);
|
|
monitor_stop();
|
|
return NULL;
|
|
}
|
|
|
|
static void test_monitor_regex_match_is_met_test_called(char *command)
|
|
{
|
|
// assert the command
|
|
assert_string_equal(command, "echo some string that has 'test' in it");
|
|
// increment the global variable
|
|
_counterTestCalled++;
|
|
}
|
|
|
|
static void test_monitor_regex_match_is_met_execute_called(char *command)
|
|
{
|
|
// assert the command
|
|
assert_string_equal(command, "echo execute callback called");
|
|
// increment the global variable
|
|
_counterExecuteCalled++;
|
|
}
|
|
|
|
static void test_monitor_regex_match_is_met(void **state)
|
|
{
|
|
// reset global variables
|
|
_counterTestCalled = 0;
|
|
_counterExecuteCalled = 0;
|
|
|
|
// setup the monitor callbacks
|
|
monitor_test_cb = test_monitor_regex_match_is_met_test_called;
|
|
monitor_execute_cb = test_monitor_regex_match_is_met_execute_called;
|
|
|
|
// start a new thread which will call `monitor_start` from monitor.h
|
|
pthread_t start_monitor_tid;
|
|
pthread_create(&start_monitor_tid, NULL, test_monitor_regex_match_is_met_start_monitor, NULL);
|
|
|
|
// start a new thread which will call `monitor_stop` from monitor.h
|
|
pthread_t stop_monitor_tid;
|
|
pthread_create(&stop_monitor_tid, NULL, test_monitor_regex_match_is_met_stop_monitor, NULL);
|
|
|
|
// wait for `monitor_stop` to finish
|
|
pthread_join(stop_monitor_tid, NULL);
|
|
|
|
// wait for `monitor_start` to finish
|
|
pthread_join(start_monitor_tid, NULL);
|
|
|
|
// check that test callback is called
|
|
assert_int_equal(_counterTestCalled, 1);
|
|
// check that execute callback is called
|
|
assert_int_equal(_counterExecuteCalled, 1);
|
|
}
|
|
|
|
/** test_monitor_regex_match_not_met
|
|
*
|
|
* Check that monitor execute callback will not be called since the condition regex_match is not met.
|
|
*/
|
|
static void *test_monitor_regex_match_not_met_start_monitor(void *vargp)
|
|
{
|
|
// initialize list and one monitor
|
|
struct list_head monitors = LIST_HEAD_INIT(monitors);
|
|
monitor_t *monitor = NULL;
|
|
monitor = calloc(1, sizeof(monitor_t));
|
|
monitor->app = "echo";
|
|
monitor->test = "echo some string that has 'experiment' in it";
|
|
monitor->stream = STREAM_STDOUT;
|
|
monitor->match_pattern = ".*test.*";
|
|
monitor->pattern_type = PATTERN_REGEX;
|
|
monitor->execute = "echo execute callback called";
|
|
monitor->interval = 1000;
|
|
monitor->nr_tests = 1;
|
|
list_add(&monitor->head, &monitors);
|
|
|
|
// start monitor
|
|
monitor_start(ubus_socket_path, &monitors);
|
|
free(monitor);
|
|
|
|
return NULL;
|
|
}
|
|
|
|
static void *test_monitor_regex_match_not_met_stop_monitor(void *vargp)
|
|
{
|
|
sleep(2);
|
|
monitor_stop();
|
|
return NULL;
|
|
}
|
|
|
|
static void test_monitor_regex_match_not_met_test_called(char *command)
|
|
{
|
|
// assert the command
|
|
assert_string_equal(command, "echo some string that has 'experiment' in it");
|
|
// increment the global variable
|
|
_counterTestCalled++;
|
|
}
|
|
|
|
static void test_monitor_regex_match_not_met_execute_called(char *command)
|
|
{
|
|
// increment the global variable
|
|
_counterExecuteCalled++;
|
|
}
|
|
|
|
static void test_monitor_regex_match_not_met(void **state)
|
|
{
|
|
// reset global variables
|
|
_counterTestCalled = 0;
|
|
_counterExecuteCalled = 0;
|
|
|
|
// setup the monitor callbacks
|
|
monitor_test_cb = test_monitor_regex_match_not_met_test_called;
|
|
monitor_execute_cb = test_monitor_regex_match_not_met_execute_called;
|
|
|
|
// start a new thread which will call `monitor_start` from monitor.h
|
|
pthread_t start_monitor_tid;
|
|
pthread_create(&start_monitor_tid, NULL, test_monitor_regex_match_not_met_start_monitor, NULL);
|
|
|
|
// start a new thread which will call `monitor_stop` from monitor.h
|
|
pthread_t stop_monitor_tid;
|
|
pthread_create(&stop_monitor_tid, NULL, test_monitor_regex_match_not_met_stop_monitor, NULL);
|
|
|
|
// wait for `monitor_stop` to finish
|
|
pthread_join(stop_monitor_tid, NULL);
|
|
|
|
// wait for `monitor_start` to finish
|
|
pthread_join(start_monitor_tid, NULL);
|
|
|
|
// check that test callback is called
|
|
assert_int_equal(_counterTestCalled, 1);
|
|
// check that execute callback is called
|
|
assert_int_equal(_counterExecuteCalled, 0);
|
|
}
|
|
|
|
int main(void)
|
|
{
|
|
const struct CMUnitTest tests[] = {
|
|
cmocka_unit_test(test_monitor_non_existing_ubus_socket),
|
|
cmocka_unit_test(test_monitor_condition_stdout_not_met),
|
|
cmocka_unit_test(test_monitor_condition_stdout_is_met),
|
|
cmocka_unit_test(test_monitor_condition_stderr_not_met),
|
|
cmocka_unit_test(test_monitor_condition_stderr_is_met),
|
|
cmocka_unit_test(test_monitor_condition_met_nr_tests),
|
|
cmocka_unit_test(test_monitor_multiple_monitors),
|
|
cmocka_unit_test(test_monitor_test_output_null),
|
|
cmocka_unit_test(test_monitor_empty_monitors),
|
|
cmocka_unit_test(test_monitor_string_not_match_is_met),
|
|
cmocka_unit_test(test_monitor_string_not_match_not_met),
|
|
cmocka_unit_test(test_monitor_regex_match_is_met),
|
|
cmocka_unit_test(test_monitor_regex_match_not_met),
|
|
|
|
};
|
|
return cmocka_run_group_tests(tests, NULL, NULL);
|
|
}
|