1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
|
/*
* Copyright (C) 2015-2016 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 "util/virjson.h"
#include "util/virqemu.h"
#include "testutils.h"
#define VIR_FROM_THIS VIR_FROM_NONE
typedef struct
{
const char *props;
const char *expectprops;
virQEMUBuildCommandLineJSONArrayFormatFunc arrayfunc;
} testQemuCommandBuildObjectFromJSONData;
static int
testQemuCommandBuildFromJSON(const void *opaque)
{
const testQemuCommandBuildObjectFromJSONData *data = opaque;
g_autoptr(virJSONValue) val = NULL;
g_auto(virBuffer) buf = VIR_BUFFER_INITIALIZER;
g_autofree char *result = NULL;
if (!(val = virJSONValueFromString(data->props))) {
fprintf(stderr, "Failed to parse JSON string '%s'", data->props);
return -1;
}
if (virQEMUBuildCommandLineJSON(val, &buf, NULL, data->arrayfunc) < 0) {
fprintf(stderr,
"\nvirQEMUBuildCommandlineJSON failed process JSON:\n%s\n",
data->props);
return -1;
}
result = virBufferContentAndReset(&buf);
if (STRNEQ_NULLABLE(data->expectprops, result)) {
fprintf(stderr, "\nFailed to create object string. "
"\nExpected:\n'%s'\nGot:\n'%s'",
NULLSTR(data->expectprops), NULLSTR(result));
return -1;
}
return 0;
}
static int
mymain(void)
{
int ret = 0;
testQemuCommandBuildObjectFromJSONData data1;
virTestCounterReset("testQemuCommandBuildFromJSON");
#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; \
} while (0)
#define DO_TEST_COMMAND_DRIVE_FROM_JSON(PROPS, EXPECT) \
DO_TEST_COMMAND_FROM_JSON(PROPS, virQEMUBuildCommandLineJSONArrayNumbered, EXPECT)
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");
return ret == 0 ? EXIT_SUCCESS : EXIT_FAILURE;
}
VIR_TEST_MAIN(mymain)
|