2015-01-13 17:19:34 +00:00
|
|
|
/*
|
2016-05-31 21:35:14 +00:00
|
|
|
* Copyright (C) 2015-2016 Red Hat, Inc.
|
2015-01-13 17:19:34 +00:00
|
|
|
*
|
|
|
|
* 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 "util/virjson.h"
|
2016-05-31 21:35:14 +00:00
|
|
|
#include "util/virqemu.h"
|
2015-01-13 17:19:34 +00:00
|
|
|
#include "testutils.h"
|
|
|
|
#include "testutilsqemu.h"
|
|
|
|
|
|
|
|
#define VIR_FROM_THIS VIR_FROM_NONE
|
|
|
|
|
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
const char *props;
|
|
|
|
const char *expectprops;
|
2016-07-25 17:47:40 +00:00
|
|
|
virQEMUBuildCommandLineJSONArrayFormatFunc arrayfunc;
|
2015-01-13 17:19:34 +00:00
|
|
|
} testQemuCommandBuildObjectFromJSONData;
|
|
|
|
|
|
|
|
static int
|
2016-07-22 13:54:57 +00:00
|
|
|
testQemuCommandBuildFromJSON(const void *opaque)
|
2015-01-13 17:19:34 +00:00
|
|
|
{
|
|
|
|
const testQemuCommandBuildObjectFromJSONData *data = opaque;
|
|
|
|
virJSONValuePtr val = NULL;
|
2016-07-22 13:54:57 +00:00
|
|
|
virBuffer buf = VIR_BUFFER_INITIALIZER;
|
2015-01-13 17:19:34 +00:00
|
|
|
char *result = NULL;
|
|
|
|
int ret = -1;
|
|
|
|
|
|
|
|
if (!(val = virJSONValueFromString(data->props))) {
|
|
|
|
fprintf(stderr, "Failed to parse JSON string '%s'", data->props);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2016-07-25 17:47:40 +00:00
|
|
|
if (virQEMUBuildCommandLineJSON(val, &buf, data->arrayfunc) < 0) {
|
2016-07-22 13:54:57 +00:00
|
|
|
fprintf(stderr,
|
|
|
|
"\nvirQEMUBuildCommandlineJSON failed process JSON:\n%s\n",
|
|
|
|
data->props);
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
result = virBufferContentAndReset(&buf);
|
2015-01-13 17:19:34 +00:00
|
|
|
|
2016-07-25 12:59:19 +00:00
|
|
|
if (STRNEQ_NULLABLE(data->expectprops, result)) {
|
2015-01-13 17:19:34 +00:00
|
|
|
fprintf(stderr, "\nFailed to create object string. "
|
|
|
|
"\nExpected:\n'%s'\nGot:\n'%s'",
|
2016-07-25 12:59:19 +00:00
|
|
|
NULLSTR(data->expectprops), NULLSTR(result));
|
2015-01-13 17:19:34 +00:00
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = 0;
|
|
|
|
cleanup:
|
|
|
|
virJSONValueFree(val);
|
|
|
|
VIR_FREE(result);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
mymain(void)
|
|
|
|
{
|
|
|
|
int ret = 0;
|
|
|
|
testQemuCommandBuildObjectFromJSONData data1;
|
|
|
|
|
2018-08-13 11:40:18 +00:00
|
|
|
#if !WITH_YAJL
|
2018-05-09 14:42:43 +00:00
|
|
|
fputs("libvirt not compiled with JSON support, skipping this test\n", stderr);
|
2015-01-13 17:19:34 +00:00
|
|
|
return EXIT_AM_SKIP;
|
|
|
|
#endif
|
|
|
|
|
2016-07-22 13:54:57 +00:00
|
|
|
virTestCounterReset("testQemuCommandBuildFromJSON");
|
2015-01-13 17:19:34 +00:00
|
|
|
|
2017-11-03 12:09:47 +00:00
|
|
|
#define DO_TEST_COMMAND_FROM_JSON(PROPS, ARRAYFUNC, EXPECT) \
|
|
|
|
do { \
|
|
|
|
data1.props = PROPS; \
|
|
|
|
data1.expectprops = EXPECT; \
|
|
|
|
data1.arrayfunc = ARRAYFUNC; \
|
|
|
|
if (virTestRun(virTestCounterNext(), \
|
|
|
|
testQemuCommandBuildFromJSON, \
|
|
|
|
&data1) < 0) \
|
|
|
|
ret = -1; \
|
2015-01-13 17:19:34 +00:00
|
|
|
} while (0)
|
|
|
|
|
2017-11-03 12:09:47 +00:00
|
|
|
#define DO_TEST_COMMAND_OBJECT_FROM_JSON(PROPS, EXPECT) \
|
2016-07-25 17:47:40 +00:00
|
|
|
DO_TEST_COMMAND_FROM_JSON(PROPS, virQEMUBuildCommandLineJSONArrayBitmap, EXPECT)
|
|
|
|
|
2017-11-03 12:09:47 +00:00
|
|
|
#define DO_TEST_COMMAND_DRIVE_FROM_JSON(PROPS, EXPECT) \
|
2016-07-25 17:47:40 +00:00
|
|
|
DO_TEST_COMMAND_FROM_JSON(PROPS, virQEMUBuildCommandLineJSONArrayNumbered, EXPECT)
|
|
|
|
|
2015-01-13 17:19:34 +00:00
|
|
|
DO_TEST_COMMAND_OBJECT_FROM_JSON("{}", NULL);
|
|
|
|
DO_TEST_COMMAND_OBJECT_FROM_JSON("{\"string\":\"qwer\"}", "string=qwer");
|
2016-07-25 12:37:47 +00:00
|
|
|
DO_TEST_COMMAND_OBJECT_FROM_JSON("{\"string\":\"qw,e,r\"}", "string=qw,,e,,r");
|
2015-01-13 17:19:34 +00:00
|
|
|
DO_TEST_COMMAND_OBJECT_FROM_JSON("{\"number\":1234}", "number=1234");
|
|
|
|
DO_TEST_COMMAND_OBJECT_FROM_JSON("{\"boolean\":true}", "boolean=yes");
|
|
|
|
DO_TEST_COMMAND_OBJECT_FROM_JSON("{\"boolean\":false}", "boolean=no");
|
|
|
|
DO_TEST_COMMAND_OBJECT_FROM_JSON("{\"bitmap\":[]}", NULL);
|
|
|
|
DO_TEST_COMMAND_OBJECT_FROM_JSON("{\"bitmap\":[0]}", "bitmap=0");
|
|
|
|
DO_TEST_COMMAND_OBJECT_FROM_JSON("{\"bitmap\":[1,3,5]}",
|
|
|
|
"bitmap=1,bitmap=3,bitmap=5");
|
|
|
|
DO_TEST_COMMAND_OBJECT_FROM_JSON("{\"bitmap\":[0,1,2,3]}", "bitmap=0-3");
|
|
|
|
DO_TEST_COMMAND_OBJECT_FROM_JSON("{\"bitmap\":[1,2,3,5]}",
|
|
|
|
"bitmap=1-3,bitmap=5");
|
|
|
|
DO_TEST_COMMAND_OBJECT_FROM_JSON("{\"bitmap\":[1,2,3,5,7,8,9]}",
|
|
|
|
"bitmap=1-3,bitmap=5,bitmap=7-9");
|
|
|
|
DO_TEST_COMMAND_OBJECT_FROM_JSON("{\"array\":[\"bleah\",\"qwerty\",1]}",
|
|
|
|
"array=bleah,array=qwerty,array=1");
|
|
|
|
DO_TEST_COMMAND_OBJECT_FROM_JSON("{\"boolean\":true,\"hyphen-name\":1234,\"some_string\":\"bleah\"}",
|
|
|
|
"boolean=yes,hyphen-name=1234,some_string=bleah");
|
2016-07-22 15:19:28 +00:00
|
|
|
DO_TEST_COMMAND_OBJECT_FROM_JSON("{\"nest\": {\"boolean\":true,"
|
|
|
|
"\"hyphen-name\":1234,"
|
|
|
|
"\"some_string\":\"bleah\","
|
|
|
|
"\"bleah\":\"bl,eah\""
|
|
|
|
"}"
|
|
|
|
"}",
|
|
|
|
"nest.boolean=yes,nest.hyphen-name=1234,"
|
|
|
|
"nest.some_string=bleah,nest.bleah=bl,,eah");
|
2016-07-25 17:47:40 +00:00
|
|
|
DO_TEST_COMMAND_DRIVE_FROM_JSON("{\"driver\":\"gluster\","
|
|
|
|
"\"volume\":\"test\","
|
|
|
|
"\"path\":\"img\","
|
|
|
|
"\"server\":[ { \"type\":\"tcp\","
|
|
|
|
"\"host\":\"example.com\","
|
|
|
|
"\"port\":\"1234\""
|
|
|
|
"},"
|
|
|
|
"{ \"type\":\"unix\","
|
|
|
|
"\"socket\":\"/path/socket\""
|
|
|
|
"},"
|
|
|
|
"{ \"type\":\"tcp\","
|
|
|
|
"\"host\":\"example.com\""
|
|
|
|
"}"
|
|
|
|
"]"
|
|
|
|
"}",
|
|
|
|
"driver=gluster,volume=test,path=img,"
|
|
|
|
"server.0.type=tcp,"
|
|
|
|
"server.0.host=example.com,"
|
|
|
|
"server.0.port=1234,"
|
|
|
|
"server.1.type=unix,"
|
|
|
|
"server.1.socket=/path/socket,"
|
|
|
|
"server.2.type=tcp,"
|
|
|
|
"server.2.host=example.com");
|
2015-01-13 17:19:34 +00:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2017-03-29 14:45:42 +00:00
|
|
|
VIR_TEST_MAIN(mymain)
|