From f81a2565e511d1ab7d10c6f1c9e7105c0e386bae Mon Sep 17 00:00:00 2001 From: Tomek Mrugalski Date: Wed, 9 Nov 2016 22:28:38 +0100 Subject: [PATCH] [5014] Unit-test loading example config files implemented. --- src/bin/dhcp6/parser_context.cc | 2 +- src/bin/dhcp6/tests/Makefile.am | 1 + src/bin/dhcp6/tests/parser_unittest.cc | 48 +++++++++++++++++++++----- 3 files changed, 42 insertions(+), 9 deletions(-) diff --git a/src/bin/dhcp6/parser_context.cc b/src/bin/dhcp6/parser_context.cc index e667a8e5e8..07fecd08b2 100644 --- a/src/bin/dhcp6/parser_context.cc +++ b/src/bin/dhcp6/parser_context.cc @@ -60,7 +60,7 @@ Parser6Context::parseFile(const std::string& filename) { std::string line; while (!f.eof()) { std::getline(f, line); - string_ = string_ + line; + string_ = string_ + line + "\n"; } f.close(); diff --git a/src/bin/dhcp6/tests/Makefile.am b/src/bin/dhcp6/tests/Makefile.am index 0fa0220f8d..9192f3dbca 100644 --- a/src/bin/dhcp6/tests/Makefile.am +++ b/src/bin/dhcp6/tests/Makefile.am @@ -24,6 +24,7 @@ AM_CPPFLAGS += -DTOP_BUILDDIR="\"$(top_builddir)\"" AM_CPPFLAGS += $(BOOST_INCLUDES) AM_CPPFLAGS += -DTEST_DATA_BUILDDIR=\"$(abs_top_builddir)/src/bin/dhcp6/tests\" AM_CPPFLAGS += -DINSTALL_PROG=\"$(abs_top_srcdir)/install-sh\" +AM_CPPFLAGS += -DCFG_EXAMPLES=\"$(abs_top_srcdir)/doc/examples/kea6\" CLEANFILES = $(builddir)/logger_lockfile CLEANFILES += $(builddir)/load_marker.txt $(builddir)/unload_marker.txt diff --git a/src/bin/dhcp6/tests/parser_unittest.cc b/src/bin/dhcp6/tests/parser_unittest.cc index 188c906d43..84b1be94b9 100644 --- a/src/bin/dhcp6/tests/parser_unittest.cc +++ b/src/bin/dhcp6/tests/parser_unittest.cc @@ -13,11 +13,13 @@ using namespace std; namespace { -void compareJSON(ConstElementPtr a, ConstElementPtr b) { +void compareJSON(ConstElementPtr a, ConstElementPtr b, bool print = true) { ASSERT_TRUE(a); ASSERT_TRUE(b); - std::cout << a->str() << std::endl; - std::cout << b->str() << std::endl; + if (print) { + std::cout << a->str() << std::endl; + std::cout << b->str() << std::endl; + } EXPECT_EQ(a->str(), b->str()); } @@ -164,23 +166,53 @@ TEST(ParserTest, multilineComments) { testParser2(txt); } -TEST(ParserTest, file) { +void testFile(const std::string& fname, bool print) { ElementPtr reference_json; ConstElementPtr test_json; - std::string fname = "test.json"; + cout << "Attempting to load file " << fname << endl; EXPECT_NO_THROW(reference_json = Element::fromJSONFile(fname, true)); - EXPECT_NO_THROW({ + + try { Parser6Context ctx; test_json = ctx.parseFile(fname); - }); + } catch (const std::exception &x) { + cout << "EXCEPTION: " << x.what() << endl; + } ASSERT_TRUE(reference_json); ASSERT_TRUE(test_json); - compareJSON(reference_json, test_json); + compareJSON(reference_json, test_json, print); + + +} + +// This test loads all available existing files. Each config is loaded +// twice: first with the existing Element::fromJSONFile() and then +// the second time with Parser6. Both JSON trees are then compared. +TEST(ParserTest, file) { + vector configs; + configs.push_back("advanced.json"); + configs.push_back("backends.json"); + configs.push_back("classify.json"); + configs.push_back("dhcpv4-over-dhcpv6.json"); + configs.push_back("duid.json"); + configs.push_back("hooks.json"); + configs.push_back("leases-expiration.json"); + configs.push_back("multiple-options.json"); + configs.push_back("mysql-reservations.json"); + configs.push_back("pgsql-reservations.json"); + configs.push_back("reservations.json"); + configs.push_back("several-subnets.json"); + configs.push_back("simple.json"); + configs.push_back("stateless.json"); + + for (int i = 0; i