2013-10-11 16:07:54 +00:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2013 Red Hat, Inc.
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library 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
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library. If not, see
|
|
|
|
* <http://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
|
|
|
|
#include "testutils.h"
|
|
|
|
|
|
|
|
#include "virlog.h"
|
|
|
|
|
2016-03-16 09:55:38 +00:00
|
|
|
struct testLogData {
|
2013-10-11 16:07:54 +00:00
|
|
|
const char *str;
|
2016-03-16 09:58:32 +00:00
|
|
|
int count;
|
2016-03-16 09:55:38 +00:00
|
|
|
bool pass;
|
2013-10-11 16:07:54 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static int
|
|
|
|
testLogMatch(const void *opaque)
|
|
|
|
{
|
2016-03-16 09:55:38 +00:00
|
|
|
const struct testLogData *data = opaque;
|
2013-10-11 16:07:54 +00:00
|
|
|
|
|
|
|
bool got = virLogProbablyLogMessage(data->str);
|
2016-03-16 09:55:38 +00:00
|
|
|
if (got != data->pass) {
|
2019-05-03 08:31:02 +00:00
|
|
|
VIR_TEST_DEBUG("Expected '%d' but got '%d' for '%s'",
|
2016-03-16 09:55:38 +00:00
|
|
|
data->pass, got, data->str);
|
2013-10-11 16:07:54 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-03-16 09:58:32 +00:00
|
|
|
static int
|
|
|
|
testLogParseOutputs(const void *opaque)
|
|
|
|
{
|
|
|
|
int ret = -1;
|
2016-03-17 09:16:19 +00:00
|
|
|
int noutputs;
|
2021-03-11 07:16:13 +00:00
|
|
|
virLogOutput **outputs = NULL;
|
2016-03-16 09:58:32 +00:00
|
|
|
const struct testLogData *data = opaque;
|
|
|
|
|
2016-03-17 14:26:40 +00:00
|
|
|
noutputs = virLogParseOutputs(data->str, &outputs);
|
2016-03-17 09:16:19 +00:00
|
|
|
if (noutputs < 0) {
|
2016-03-16 09:58:32 +00:00
|
|
|
if (!data->pass) {
|
2019-05-03 08:31:02 +00:00
|
|
|
VIR_TEST_DEBUG("Got expected error: %s",
|
2016-03-16 09:58:32 +00:00
|
|
|
virGetLastErrorMessage());
|
|
|
|
virResetLastError();
|
|
|
|
ret = 0;
|
|
|
|
goto cleanup;
|
|
|
|
}
|
2016-03-17 09:16:19 +00:00
|
|
|
} else if (noutputs != data->count) {
|
2016-03-16 09:58:32 +00:00
|
|
|
VIR_TEST_DEBUG("Expected number of parsed outputs is %d, "
|
2019-05-03 08:31:02 +00:00
|
|
|
"but got %d", data->count, noutputs);
|
2016-03-16 09:58:32 +00:00
|
|
|
goto cleanup;
|
|
|
|
} else if (!data->pass) {
|
2019-05-03 08:31:02 +00:00
|
|
|
VIR_TEST_DEBUG("Test should have failed");
|
2016-03-16 09:58:32 +00:00
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = 0;
|
|
|
|
cleanup:
|
2016-03-17 14:26:40 +00:00
|
|
|
virLogOutputListFree(outputs, noutputs);
|
2016-03-16 09:58:32 +00:00
|
|
|
return ret;
|
|
|
|
}
|
2013-10-11 16:07:54 +00:00
|
|
|
|
2016-03-17 08:37:24 +00:00
|
|
|
static int
|
|
|
|
testLogParseFilters(const void *opaque)
|
|
|
|
{
|
|
|
|
int ret = -1;
|
|
|
|
int nfilters;
|
2021-03-11 07:16:13 +00:00
|
|
|
virLogFilter **filters = NULL;
|
2016-03-17 08:37:24 +00:00
|
|
|
const struct testLogData *data = opaque;
|
|
|
|
|
2016-03-29 14:48:08 +00:00
|
|
|
nfilters = virLogParseFilters(data->str, &filters);
|
2016-03-17 08:37:24 +00:00
|
|
|
if (nfilters < 0) {
|
|
|
|
if (!data->pass) {
|
2019-05-03 08:31:02 +00:00
|
|
|
VIR_TEST_DEBUG("Got expected error: %s",
|
2016-03-17 08:37:24 +00:00
|
|
|
virGetLastErrorMessage());
|
|
|
|
virResetLastError();
|
|
|
|
ret = 0;
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
} else if (nfilters != data->count) {
|
|
|
|
VIR_TEST_DEBUG("Expected number of parsed outputs is %d, "
|
2019-05-03 08:31:02 +00:00
|
|
|
"but got %d", data->count, nfilters);
|
2016-03-17 08:37:24 +00:00
|
|
|
goto cleanup;
|
|
|
|
} else if (!data->pass) {
|
2019-05-03 08:31:02 +00:00
|
|
|
VIR_TEST_DEBUG("Test should have failed");
|
2016-03-17 08:37:24 +00:00
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = 0;
|
|
|
|
cleanup:
|
2016-03-29 14:48:08 +00:00
|
|
|
virLogFilterListFree(filters, nfilters);
|
2016-03-17 08:37:24 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2013-10-11 16:07:54 +00:00
|
|
|
static int
|
|
|
|
mymain(void)
|
|
|
|
{
|
|
|
|
int ret = 0;
|
|
|
|
|
2017-11-03 12:09:47 +00:00
|
|
|
#define DO_TEST_FULL(name, test, str, count, pass) \
|
|
|
|
do { \
|
|
|
|
struct testLogData data = { \
|
|
|
|
str, count, pass \
|
|
|
|
}; \
|
|
|
|
if (virTestRun(name, test, &data) < 0) \
|
|
|
|
ret = -1; \
|
2013-10-11 16:07:54 +00:00
|
|
|
} while (0)
|
|
|
|
|
2017-11-03 12:09:47 +00:00
|
|
|
#define TEST_LOG_MATCH_FAIL(str) \
|
2016-03-16 09:58:32 +00:00
|
|
|
DO_TEST_FULL("testLogMatch " # str, testLogMatch, str, 0, false)
|
2017-11-03 12:09:47 +00:00
|
|
|
#define TEST_LOG_MATCH(str) \
|
2016-03-16 09:58:32 +00:00
|
|
|
DO_TEST_FULL("testLogMatch " # str, testLogMatch, str, 0, true)
|
|
|
|
|
2017-11-03 12:09:47 +00:00
|
|
|
#define TEST_PARSE_OUTPUTS_FAIL(str, count) \
|
2016-03-16 09:58:32 +00:00
|
|
|
DO_TEST_FULL("testLogParseOutputs " # str, testLogParseOutputs, str, count, false)
|
2017-11-03 12:09:47 +00:00
|
|
|
#define TEST_PARSE_OUTPUTS(str, count) \
|
2016-03-16 09:58:32 +00:00
|
|
|
DO_TEST_FULL("testLogParseOutputs " # str, testLogParseOutputs, str, count, true)
|
2017-11-03 12:09:47 +00:00
|
|
|
#define TEST_PARSE_FILTERS_FAIL(str, count) \
|
2016-03-17 08:37:24 +00:00
|
|
|
DO_TEST_FULL("testLogParseFilters " # str, testLogParseFilters, str, count, false)
|
2017-11-03 12:09:47 +00:00
|
|
|
#define TEST_PARSE_FILTERS(str, count) \
|
2016-03-17 08:37:24 +00:00
|
|
|
DO_TEST_FULL("testLogParseFilters " # str, testLogParseFilters, str, count, true)
|
2016-03-16 09:58:32 +00:00
|
|
|
|
2013-10-11 16:07:54 +00:00
|
|
|
|
2016-03-16 09:55:38 +00:00
|
|
|
TEST_LOG_MATCH("2013-10-11 15:43:43.866+0000: 28302: info : libvirt version: 1.1.3");
|
|
|
|
|
|
|
|
TEST_LOG_MATCH_FAIL("libvirt: error : cannot execute binary /usr/libexec/libvirt_lxc: No such file or directory");
|
2016-03-16 09:58:32 +00:00
|
|
|
TEST_PARSE_OUTPUTS("1:file:/dev/null", 1);
|
|
|
|
TEST_PARSE_OUTPUTS("1:file:/dev/null 2:stderr", 2);
|
|
|
|
TEST_PARSE_OUTPUTS_FAIL("foo:stderr", 1);
|
|
|
|
TEST_PARSE_OUTPUTS_FAIL("1:bar", 1);
|
|
|
|
TEST_PARSE_OUTPUTS_FAIL("1:stderr:foobar", 1);
|
2016-03-17 08:37:24 +00:00
|
|
|
TEST_PARSE_FILTERS("1:foo", 1);
|
|
|
|
TEST_PARSE_FILTERS("1:foo 2:bar 3:foobar", 3);
|
|
|
|
TEST_PARSE_FILTERS_FAIL("5:foo", 1);
|
|
|
|
TEST_PARSE_FILTERS_FAIL("1:", 1);
|
|
|
|
TEST_PARSE_FILTERS_FAIL(":foo", 1);
|
|
|
|
TEST_PARSE_FILTERS_FAIL("1:+", 1);
|
2013-10-11 16:07:54 +00:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2017-03-29 14:45:42 +00:00
|
|
|
VIR_TEST_MAIN(mymain)
|