2013-09-09 17:22:19 +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 "virerror.h"
|
|
|
|
#include "virxml.h"
|
|
|
|
|
|
|
|
#define VIR_FROM_THIS VIR_FROM_NONE
|
|
|
|
|
|
|
|
static const char metadata1[] =
|
|
|
|
"<derp xmlns:foobar='http://foo.bar/'>\n"
|
|
|
|
" <bar>foobar</bar>\n"
|
|
|
|
" <foo fooish='blurb'>foofoo</foo>\n"
|
|
|
|
" <foobar:baz>zomg</foobar:baz>\n"
|
|
|
|
"</derp>";
|
|
|
|
|
|
|
|
|
|
|
|
static const char metadata1_ns[] =
|
|
|
|
"<herp:derp xmlns:foobar='http://foo.bar/' xmlns:herp='http://herp.derp/'>\n"
|
|
|
|
" <herp:bar>foobar</herp:bar>\n"
|
|
|
|
" <herp:foo fooish='blurb'>foofoo</herp:foo>\n"
|
|
|
|
" <foobar:baz>zomg</foobar:baz>\n"
|
|
|
|
"</herp:derp>";
|
|
|
|
|
|
|
|
|
|
|
|
static const char metadata2[] =
|
|
|
|
"<foo>\n"
|
|
|
|
" <bar>baz</bar>\n"
|
|
|
|
"</foo>";
|
|
|
|
|
|
|
|
|
|
|
|
static const char metadata2_ns[] =
|
|
|
|
"<blurb:foo xmlns:blurb='http://herp.derp/'>\n"
|
|
|
|
" <blurb:bar>baz</blurb:bar>\n"
|
|
|
|
"</blurb:foo>";
|
|
|
|
|
|
|
|
|
|
|
|
static char *
|
|
|
|
getMetadataFromXML(virDomainPtr dom)
|
|
|
|
{
|
2021-08-11 11:57:18 +00:00
|
|
|
g_autoptr(xmlDoc) doc = NULL;
|
2021-08-11 11:30:26 +00:00
|
|
|
g_autoptr(xmlXPathContext) ctxt = NULL;
|
2013-09-09 17:22:19 +00:00
|
|
|
xmlNodePtr node;
|
|
|
|
|
2021-08-17 10:21:56 +00:00
|
|
|
g_autofree char *xml = NULL;
|
2013-09-09 17:22:19 +00:00
|
|
|
|
|
|
|
if (!(xml = virDomainGetXMLDesc(dom, 0)))
|
2021-08-17 10:17:31 +00:00
|
|
|
return NULL;
|
2013-09-09 17:22:19 +00:00
|
|
|
|
|
|
|
if (!(doc = virXMLParseStringCtxt(xml, "(domain_definition)", &ctxt)))
|
2021-08-17 10:17:31 +00:00
|
|
|
return NULL;
|
2013-09-09 17:22:19 +00:00
|
|
|
|
|
|
|
if (!(node = virXPathNode("//metadata/*", ctxt)))
|
2021-08-17 10:17:31 +00:00
|
|
|
return NULL;
|
2013-09-09 17:22:19 +00:00
|
|
|
|
2021-08-17 10:17:31 +00:00
|
|
|
return virXMLNodeToString(node->doc, node);
|
2013-09-09 17:22:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
metadataXMLConvertApostrophe(char *str)
|
|
|
|
{
|
|
|
|
do {
|
|
|
|
if (*str == '\"')
|
|
|
|
*str = '\'';
|
|
|
|
} while ((*++str) != '\0');
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static bool
|
|
|
|
verifyMetadata(virDomainPtr dom,
|
|
|
|
const char *expectXML,
|
|
|
|
const char *expectAPI,
|
|
|
|
const char *uri)
|
|
|
|
{
|
2021-09-04 20:37:44 +00:00
|
|
|
g_autofree char *metadataXML = NULL;
|
|
|
|
g_autofree char *metadataAPI = NULL;
|
2013-09-09 17:22:19 +00:00
|
|
|
|
|
|
|
if (!expectAPI) {
|
|
|
|
if ((metadataAPI = virDomainGetMetadata(dom,
|
|
|
|
VIR_DOMAIN_METADATA_ELEMENT,
|
|
|
|
uri, 0))) {
|
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR,
|
|
|
|
"expected no metadata in API, but got:\n[%s]",
|
|
|
|
metadataAPI);
|
2021-09-04 20:41:46 +00:00
|
|
|
return false;
|
2013-09-09 17:22:19 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (!(metadataAPI = virDomainGetMetadata(dom,
|
|
|
|
VIR_DOMAIN_METADATA_ELEMENT,
|
|
|
|
uri, 0)))
|
2021-09-04 20:41:46 +00:00
|
|
|
return false;
|
2013-09-09 17:22:19 +00:00
|
|
|
|
|
|
|
metadataXMLConvertApostrophe(metadataAPI);
|
|
|
|
|
|
|
|
if (STRNEQ(metadataAPI, expectAPI)) {
|
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR,
|
|
|
|
"XML metadata in API doesn't match expected metadata: "
|
|
|
|
"expected:\n[%s]\ngot:\n[%s]",
|
|
|
|
expectAPI, metadataAPI);
|
2021-09-04 20:41:46 +00:00
|
|
|
return false;
|
2013-09-09 17:22:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!expectXML) {
|
|
|
|
if ((metadataXML = getMetadataFromXML(dom))) {
|
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR,
|
|
|
|
"expected no metadata in XML, but got:\n[%s]",
|
|
|
|
metadataXML);
|
2021-09-04 20:41:46 +00:00
|
|
|
return false;
|
2013-09-09 17:22:19 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (!(metadataXML = getMetadataFromXML(dom)))
|
2021-09-04 20:41:46 +00:00
|
|
|
return false;
|
2013-09-09 17:22:19 +00:00
|
|
|
|
|
|
|
metadataXMLConvertApostrophe(metadataXML);
|
|
|
|
|
|
|
|
if (STRNEQ(metadataXML, expectXML)) {
|
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR,
|
|
|
|
"XML in dump doesn't match expected metadata: "
|
|
|
|
"expected:\n[%s]\ngot:\n[%s]",
|
|
|
|
expectXML, metadataXML);
|
2021-09-04 20:41:46 +00:00
|
|
|
return false;
|
2013-09-09 17:22:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-04 20:41:46 +00:00
|
|
|
return true;
|
2013-09-09 17:22:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
struct metadataTest {
|
|
|
|
virConnectPtr conn;
|
|
|
|
virDomainPtr dom;
|
2014-07-17 14:24:23 +00:00
|
|
|
|
|
|
|
const char *data;
|
2019-02-27 14:52:45 +00:00
|
|
|
const char *expect;
|
2014-07-17 14:24:23 +00:00
|
|
|
int type;
|
|
|
|
bool fail;
|
2013-09-09 17:22:19 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
static int
|
|
|
|
testAssignMetadata(const void *data)
|
|
|
|
{
|
|
|
|
const struct metadataTest *test = data;
|
|
|
|
|
|
|
|
if (virDomainSetMetadata(test->dom, VIR_DOMAIN_METADATA_ELEMENT,
|
|
|
|
metadata1, "herp", "http://herp.derp/", 0) < 0)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
if (!verifyMetadata(test->dom, metadata1_ns, metadata1, "http://herp.derp/"))
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
testRewriteMetadata(const void *data)
|
|
|
|
{
|
|
|
|
const struct metadataTest *test = data;
|
|
|
|
|
|
|
|
if (virDomainSetMetadata(test->dom, VIR_DOMAIN_METADATA_ELEMENT,
|
|
|
|
metadata2, "blurb", "http://herp.derp/", 0) < 0)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
if (!verifyMetadata(test->dom, metadata2_ns, metadata2, "http://herp.derp/"))
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
testEraseMetadata(const void *data)
|
|
|
|
{
|
|
|
|
const struct metadataTest *test = data;
|
|
|
|
|
|
|
|
if (virDomainSetMetadata(test->dom, VIR_DOMAIN_METADATA_ELEMENT,
|
|
|
|
NULL, NULL, "http://herp.derp/", 0) < 0)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
if (!verifyMetadata(test->dom, NULL, NULL, "http://herp.derp/"))
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-07-17 14:24:23 +00:00
|
|
|
static int
|
|
|
|
testTextMetadata(const void *data)
|
|
|
|
{
|
|
|
|
const struct metadataTest *test = data;
|
2021-09-04 20:37:44 +00:00
|
|
|
g_autofree char *actual = NULL;
|
2014-07-17 14:24:23 +00:00
|
|
|
|
|
|
|
if (virDomainSetMetadata(test->dom, test->type, test->data, NULL, NULL, 0) < 0) {
|
|
|
|
if (test->fail)
|
|
|
|
return 0;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
actual = virDomainGetMetadata(test->dom, test->type, NULL, 0);
|
|
|
|
|
2019-02-27 14:52:45 +00:00
|
|
|
if (STRNEQ_NULLABLE(test->expect, actual)) {
|
2014-07-17 14:24:23 +00:00
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR,
|
|
|
|
"expected metadata doesn't match actual: "
|
|
|
|
"expected:'%s'\ngot: '%s'",
|
|
|
|
NULLSTR(test->data), NULLSTR(actual));
|
2021-09-04 20:41:46 +00:00
|
|
|
return -1;
|
2014-07-17 14:24:23 +00:00
|
|
|
}
|
|
|
|
|
2021-09-04 20:41:46 +00:00
|
|
|
return 0;
|
2014-07-17 14:24:23 +00:00
|
|
|
}
|
|
|
|
|
2019-02-27 14:52:45 +00:00
|
|
|
#define TEST_TEXT_METADATA(INDEX, TYPE, DATA, EXPECT, FAIL) \
|
2017-11-03 12:09:47 +00:00
|
|
|
do { \
|
|
|
|
test.type = VIR_DOMAIN_METADATA_ ## TYPE; \
|
|
|
|
test.data = DATA; \
|
2019-02-27 14:52:45 +00:00
|
|
|
test.expect = EXPECT; \
|
2017-11-03 12:09:47 +00:00
|
|
|
test.fail = FAIL; \
|
|
|
|
\
|
|
|
|
if (virTestRun("text metadata: " #TYPE " " INDEX " ", \
|
|
|
|
testTextMetadata, &test) < 0) \
|
|
|
|
ret = EXIT_FAILURE; \
|
2014-07-17 14:24:23 +00:00
|
|
|
} while (0)
|
|
|
|
|
2019-02-27 14:52:45 +00:00
|
|
|
#define TEST_TITLE(INDEX, DATA) \
|
|
|
|
TEST_TEXT_METADATA(INDEX, TITLE, DATA, DATA, false)
|
|
|
|
#define TEST_TITLE_EXPECT(INDEX, DATA, EXPECT) \
|
|
|
|
TEST_TEXT_METADATA(INDEX, TITLE, DATA, EXPECT, false)
|
|
|
|
#define TEST_TITLE_FAIL(INDEX, DATA) \
|
|
|
|
TEST_TEXT_METADATA(INDEX, TITLE, DATA, DATA, true)
|
|
|
|
#define TEST_DESCR(INDEX, DATA) \
|
|
|
|
TEST_TEXT_METADATA(INDEX, DESCRIPTION, DATA, DATA, false)
|
|
|
|
#define TEST_DESCR_EXPECT(INDEX, DATA, EXPECT) \
|
|
|
|
TEST_TEXT_METADATA(INDEX, DESCRIPTION, DATA, EXPECT, false)
|
2014-07-17 14:24:23 +00:00
|
|
|
|
2013-09-09 17:22:19 +00:00
|
|
|
static int
|
|
|
|
mymain(void)
|
|
|
|
{
|
2018-05-28 07:05:29 +00:00
|
|
|
struct metadataTest test = { 0 };
|
2013-09-09 17:22:19 +00:00
|
|
|
int ret = EXIT_SUCCESS;
|
|
|
|
|
|
|
|
if (!(test.conn = virConnectOpen("test:///default")))
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
|
|
|
|
if (!(test.dom = virDomainLookupByName(test.conn, "test"))) {
|
|
|
|
virConnectClose(test.conn);
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
|
2016-05-26 15:02:00 +00:00
|
|
|
virTestQuiesceLibvirtErrors(false);
|
2013-09-17 13:22:42 +00:00
|
|
|
|
2016-05-26 15:01:50 +00:00
|
|
|
if (virTestRun("Assign metadata ", testAssignMetadata, &test) < 0)
|
2013-09-09 17:22:19 +00:00
|
|
|
ret = EXIT_FAILURE;
|
2016-05-26 15:01:50 +00:00
|
|
|
if (virTestRun("Rewrite Metadata ", testRewriteMetadata, &test) < 0)
|
2013-09-09 17:22:19 +00:00
|
|
|
ret = EXIT_FAILURE;
|
2016-05-26 15:01:50 +00:00
|
|
|
if (virTestRun("Erase metadata ", testEraseMetadata, &test) < 0)
|
2013-09-09 17:22:19 +00:00
|
|
|
ret = EXIT_FAILURE;
|
|
|
|
|
2014-07-17 14:24:23 +00:00
|
|
|
TEST_TITLE("1", "qwert");
|
|
|
|
TEST_TITLE("2", NULL);
|
|
|
|
TEST_TITLE("3", "blah");
|
|
|
|
TEST_TITLE_FAIL("4", "qwe\nrt");
|
2019-02-27 14:52:45 +00:00
|
|
|
TEST_TITLE_EXPECT("5", "", NULL);
|
2014-07-17 14:24:23 +00:00
|
|
|
TEST_TITLE_FAIL("6", "qwert\n");
|
|
|
|
TEST_TITLE_FAIL("7", "\n");
|
|
|
|
|
|
|
|
TEST_DESCR("1", "qwert\nqwert");
|
|
|
|
TEST_DESCR("2", NULL);
|
|
|
|
TEST_DESCR("3", "qwert");
|
|
|
|
TEST_DESCR("4", "\n");
|
2019-02-27 14:52:45 +00:00
|
|
|
TEST_DESCR_EXPECT("5", "", NULL);
|
2014-07-17 14:24:23 +00:00
|
|
|
|
2013-09-09 17:22:19 +00:00
|
|
|
virDomainFree(test.dom);
|
|
|
|
virConnectClose(test.conn);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2017-03-29 14:45:42 +00:00
|
|
|
VIR_TEST_MAIN(mymain)
|