backup: Wire up qemu full pull backup commands over QMP
[libvirt/ericb.git] / tests / virlogtest.c
blob62b4b753004813a26c465dc8efcdde486f4187bd
1 /*
2 * Copyright (C) 2013 Red Hat, Inc.
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library. If not, see
16 * <http://www.gnu.org/licenses/>.
20 #include <config.h>
22 #include "testutils.h"
24 #include "virlog.h"
26 struct testLogData {
27 const char *str;
28 int count;
29 bool pass;
32 static int
33 testLogMatch(const void *opaque)
35 const struct testLogData *data = opaque;
37 bool got = virLogProbablyLogMessage(data->str);
38 if (got != data->pass) {
39 VIR_TEST_DEBUG("Expected '%d' but got '%d' for '%s'\n",
40 data->pass, got, data->str);
41 return -1;
43 return 0;
46 static int
47 testLogParseOutputs(const void *opaque)
49 int ret = -1;
50 int noutputs;
51 virLogOutputPtr *outputs = NULL;
52 const struct testLogData *data = opaque;
54 noutputs = virLogParseOutputs(data->str, &outputs);
55 if (noutputs < 0) {
56 if (!data->pass) {
57 VIR_TEST_DEBUG("Got expected error: %s\n",
58 virGetLastErrorMessage());
59 virResetLastError();
60 ret = 0;
61 goto cleanup;
63 } else if (noutputs != data->count) {
64 VIR_TEST_DEBUG("Expected number of parsed outputs is %d, "
65 "but got %d\n", data->count, noutputs);
66 goto cleanup;
67 } else if (!data->pass) {
68 VIR_TEST_DEBUG("Test should have failed\n");
69 goto cleanup;
72 ret = 0;
73 cleanup:
74 virLogOutputListFree(outputs, noutputs);
75 return ret;
78 static int
79 testLogParseFilters(const void *opaque)
81 int ret = -1;
82 int nfilters;
83 virLogFilterPtr *filters = NULL;
84 const struct testLogData *data = opaque;
86 nfilters = virLogParseFilters(data->str, &filters);
87 if (nfilters < 0) {
88 if (!data->pass) {
89 VIR_TEST_DEBUG("Got expected error: %s\n",
90 virGetLastErrorMessage());
91 virResetLastError();
92 ret = 0;
93 goto cleanup;
95 } else if (nfilters != data->count) {
96 VIR_TEST_DEBUG("Expected number of parsed outputs is %d, "
97 "but got %d\n", data->count, nfilters);
98 goto cleanup;
99 } else if (!data->pass) {
100 VIR_TEST_DEBUG("Test should have failed\n");
101 goto cleanup;
104 ret = 0;
105 cleanup:
106 virLogFilterListFree(filters, nfilters);
107 return ret;
110 static int
111 mymain(void)
113 int ret = 0;
115 #define DO_TEST_FULL(name, test, str, count, pass) \
116 do { \
117 struct testLogData data = { \
118 str, count, pass \
119 }; \
120 if (virTestRun(name, test, &data) < 0) \
121 ret = -1; \
122 } while (0)
124 #define TEST_LOG_MATCH_FAIL(str) \
125 DO_TEST_FULL("testLogMatch " # str, testLogMatch, str, 0, false)
126 #define TEST_LOG_MATCH(str) \
127 DO_TEST_FULL("testLogMatch " # str, testLogMatch, str, 0, true)
129 #define TEST_PARSE_OUTPUTS_FAIL(str, count) \
130 DO_TEST_FULL("testLogParseOutputs " # str, testLogParseOutputs, str, count, false)
131 #define TEST_PARSE_OUTPUTS(str, count) \
132 DO_TEST_FULL("testLogParseOutputs " # str, testLogParseOutputs, str, count, true)
133 #define TEST_PARSE_FILTERS_FAIL(str, count) \
134 DO_TEST_FULL("testLogParseFilters " # str, testLogParseFilters, str, count, false)
135 #define TEST_PARSE_FILTERS(str, count) \
136 DO_TEST_FULL("testLogParseFilters " # str, testLogParseFilters, str, count, true)
139 TEST_LOG_MATCH("2013-10-11 15:43:43.866+0000: 28302: info : libvirt version: 1.1.3");
141 TEST_LOG_MATCH_FAIL("libvirt: error : cannot execute binary /usr/libexec/libvirt_lxc: No such file or directory");
142 TEST_PARSE_OUTPUTS("1:file:/dev/null", 1);
143 TEST_PARSE_OUTPUTS("1:file:/dev/null 2:stderr", 2);
144 TEST_PARSE_OUTPUTS_FAIL("foo:stderr", 1);
145 TEST_PARSE_OUTPUTS_FAIL("1:bar", 1);
146 TEST_PARSE_OUTPUTS_FAIL("1:stderr:foobar", 1);
147 TEST_PARSE_FILTERS("1:foo", 1);
148 TEST_PARSE_FILTERS("1:foo 2:bar 3:foobar", 3);
149 TEST_PARSE_FILTERS_FAIL("5:foo", 1);
150 TEST_PARSE_FILTERS_FAIL("1:", 1);
151 TEST_PARSE_FILTERS_FAIL(":foo", 1);
152 TEST_PARSE_FILTERS_FAIL("1:+", 1);
154 return ret;
157 VIR_TEST_MAIN(mymain)