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/>.
22 #include "testutils.h"
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
);
47 testLogParseOutputs(const void *opaque
)
51 virLogOutputPtr
*outputs
= NULL
;
52 const struct testLogData
*data
= opaque
;
54 noutputs
= virLogParseOutputs(data
->str
, &outputs
);
57 VIR_TEST_DEBUG("Got expected error: %s\n",
58 virGetLastErrorMessage());
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
);
67 } else if (!data
->pass
) {
68 VIR_TEST_DEBUG("Test should have failed\n");
74 virLogOutputListFree(outputs
, noutputs
);
79 testLogParseFilters(const void *opaque
)
83 virLogFilterPtr
*filters
= NULL
;
84 const struct testLogData
*data
= opaque
;
86 nfilters
= virLogParseFilters(data
->str
, &filters
);
89 VIR_TEST_DEBUG("Got expected error: %s\n",
90 virGetLastErrorMessage());
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
);
99 } else if (!data
->pass
) {
100 VIR_TEST_DEBUG("Test should have failed\n");
106 virLogFilterListFree(filters
, nfilters
);
115 #define DO_TEST_FULL(name, test, str, count, pass) \
117 struct testLogData data = { \
120 if (virTestRun(name, test, &data) < 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);
157 VIR_TEST_MAIN(mymain
)