btrfs-progs: tests: add json formatter test coverage

Add a simple framework to exercise the json formatter and add testing
target that validates the output.

Run 'make test-json' to execute all available tests, requires 'jq'
utility for validation (https://github.com/stedolan/jq).

Signed-off-by: David Sterba <dsterba@suse.com>
This commit is contained in:
David Sterba 2020-12-16 19:14:00 +01:00
parent 29e2dd7a23
commit 9bf6b5c837
3 changed files with 113 additions and 1 deletions

1
.gitignore vendored
View File

@ -47,6 +47,7 @@
/ioctl-test
/send-test
/quick-test
/json-formatter-test
/aclocal.m4
/autom4te.cache

View File

@ -442,6 +442,17 @@ test-inst: all
$(MAKE) $(MAKEOPTS) DESTDIR=$$tmpdest install && \
$(RM) -rf -- $$tmpdest
test-json: json-formatter-test
@echo " [TEST] json formatting"
@echo | jq
@{ \
max=`./json-formatter-test`; \
for testno in `seq 1 $$max`; do \
echo " [TEST/json] $$testno"; \
./json-formatter-test $$testno | jq >& /dev/null; \
done \
}
test: test-check test-check-lowmem test-mkfs test-misc test-cli test-convert test-fuzz
testsuite: btrfs-corrupt-block btrfs-find-root btrfs-select-super fssum
@ -679,6 +690,10 @@ hash-speedtest: crypto/hash-speedtest.c $(objects) $(libs_static)
@echo " [LD] $@"
$(Q)$(CC) $(CFLAGS) -o $@ $^ $(LDFLAGS) $(LIBS)
json-formatter-test: tests/json-formatter-test.c $(objects) $(libs_static)
@echo " [LD] $@"
$(Q)$(CC) $(CFLAGS) -o $@ $^ $(LDFLAGS) $(LIBS)
test-build: test-build-pre test-build-real
test-build-pre:
@ -731,7 +746,7 @@ clean: $(CLEANDIRS)
crypto/*.o crypto/*.o.d \
ioctl-test quick-test library-test library-test-static \
mktables btrfs.static mkfs.btrfs.static fssum \
btrfs.box btrfs.box.static \
btrfs.box btrfs.box.static json-formatter-test \
$(check_defs) \
$(libs) $(lib_links) \
$(progs_static) \

View File

@ -0,0 +1,96 @@
/*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public
* License v2 as published by the Free Software Foundation.
*
* This program 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
* General Public License for more details.
*
* You should have received a copy of the GNU General Public
* License along with this program; if not, write to the
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 021110-1307, USA.
*/
/*
* Test json output formatter
*
* Usage:
*
* $ ./json-formatter-test
* 123
*
* Without arguments returns the number of tests.
*
* $ ./json-formatter-test 17
* ...
*
* Run the given test, print formatted json for further processing or validation.
*/
#include <stdio.h>
#include <stdlib.h>
#include "common/utils.h"
#include "common/format-output.h"
#include "cmds/commands.h"
/* Default empty output */
void test_simple_empty()
{
static const struct rowspec rows[] = {
ROWSPEC_END
};
struct format_ctx fctx;
fmt_start(&fctx, rows, 32, 0);
fmt_end(&fctx);
}
/* Single object with a few members */
void test1()
{
static const struct rowspec rows1[] = {
{ .key = "device", .fmt = "%s", .out_text = "device", .out_json = "device" },
{ .key = "devid", .fmt = "%llu", .out_text = "devid", .out_json = "devid" },
ROWSPEC_END
};
struct format_ctx fctx;
fmt_start(&fctx, rows1, 32, 0);
fmt_print_start_group(&fctx, "device-info", JSON_TYPE_MAP);
fmt_print(&fctx, "device", "/dev/sda");
fmt_print(&fctx, "devid", 1);
fmt_print_end_group(&fctx, NULL);
fmt_end(&fctx);
}
int main(int argc, char **argv)
{
int testno;
static void (*tests[])() = {
test_simple_empty,
test1,
};
btrfs_config_init();
bconf.output_format = CMD_FORMAT_JSON;
/* Without arguments, print the number of tests available */
if (argc == 1) {
printf("%zu\n", ARRAY_SIZE(tests));
return 0;
}
testno = atoi(argv[1]);
testno--;
if (testno < 0 || testno >= ARRAY_SIZE(tests)) {
fprintf(stderr, "ERROR: test number %d is out of range (max %zu)\n",
testno + 1, ARRAY_SIZE(tests));
return 1;
}
tests[testno]();
return 0;
}