From: Francis Dupont Date: Tue, 27 Dec 2016 17:17:16 +0000 (+0100) Subject: [3770] Added -t to kea-dhcp6 X-Git-Tag: trac1205_base~6^2~10 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=a4fd21ce1b605bfb32be7a8fa01eff672372396d;p=thirdparty%2Fkea.git [3770] Added -t to kea-dhcp6 --- diff --git a/src/bin/dhcp6/kea-dhcp6.xml b/src/bin/dhcp6/kea-dhcp6.xml index ae5a9680b1..6098af4b20 100644 --- a/src/bin/dhcp6/kea-dhcp6.xml +++ b/src/bin/dhcp6/kea-dhcp6.xml @@ -52,6 +52,7 @@ + @@ -102,6 +103,14 @@ + + + + Check the syntax of the configuration file and report the first + error if any. + + + diff --git a/src/bin/dhcp6/main.cc b/src/bin/dhcp6/main.cc index 3097e10ee6..653109c831 100644 --- a/src/bin/dhcp6/main.cc +++ b/src/bin/dhcp6/main.cc @@ -8,6 +8,7 @@ #include #include +#include #include #include #include @@ -18,6 +19,7 @@ #include +using namespace isc::data; using namespace isc::dhcp; using namespace std; @@ -43,11 +45,12 @@ usage() { cerr << "Kea DHCPv6 server, version " << VERSION << endl; cerr << endl; cerr << "Usage: " << DHCP6_NAME - << " -[v|V|W] [-d] [-c cfgfile] [-p port_number]" << endl; + << " -[v|V|W] [-d] [-t] [-c cfgfile] [-p port_number]" << endl; cerr << " -v: print version number and exit." << endl; cerr << " -V: print extended version and exit" << endl; cerr << " -W: display the configuration report and exit" << endl; cerr << " -d: debug mode with extra verbosity (former -v)" << endl; + cerr << " -t: check the configuration file syntax and exit" << endl; cerr << " -c file: specify configuration file" << endl; cerr << " -p number: specify non-standard port number 1-65535 " << "(useful for testing only)" << endl; @@ -61,16 +64,21 @@ main(int argc, char* argv[]) { int port_number = DHCP6_SERVER_PORT; // The default. Any other values are // useful for testing only. bool verbose_mode = false; // Should server be verbose? + bool check_mode = false; // Check syntax // The standard config file std::string config_file(""); - while ((ch = getopt(argc, argv, "dvVWc:p:")) != -1) { + while ((ch = getopt(argc, argv, "dtvVWc:p:")) != -1) { switch (ch) { case 'd': verbose_mode = true; break; + case 't': + check_mode = true; + break; + case 'v': cout << Dhcpv6Srv::getVersion(false) << endl; return (EXIT_SUCCESS); @@ -118,6 +126,24 @@ main(int argc, char* argv[]) { usage(); } + if (check_mode) { + try { + Parser6Context parser; + ConstElementPtr json; + json = parser.parseFile(config_file, Parser6Context::PARSER_DHCP6); + if (!json) { + cerr << "No configuration found" << endl; + return (EXIT_FAILURE); + } + if (verbose_mode) { + cerr << "Syntax check OK" << endl; + } + return (EXIT_SUCCESS); + } catch (const std::exception& ex) { + cerr << "Syntax check failed with " << ex.what() << endl; + } + return (EXIT_FAILURE); + } int ret = EXIT_SUCCESS; try { diff --git a/src/bin/dhcp6/tests/dhcp6_process_tests.sh.in b/src/bin/dhcp6/tests/dhcp6_process_tests.sh.in index 03cc4edde1..65b0ce12ca 100644 --- a/src/bin/dhcp6/tests/dhcp6_process_tests.sh.in +++ b/src/bin/dhcp6/tests/dhcp6_process_tests.sh.in @@ -59,12 +59,50 @@ CONFIG="{ ] } }" +# Invalid configuration (syntax error) to check that Kea can check syntax. +CONFIG_BAD_SYNTAX="{ + \"Dhcp6\": + { + \"interfaces\": [ ], + \"preferred-lifetime\": 3000, + \"valid-lifetime\": 4000, + \"renew-timer\": 1000, + \"rebind-timer\": 2000, + \"lease-database\": + { + \"type\": \"memfile\", + \"persist\": false + }, + \"subnet6\": [ + { + \"subnet\": \"2001:db8:1::/64\", + \"pool\": [ { \"pool\": \"2001:db8:1::10-2001:db8:1::100\" } ] + } ] + }, + + \"Logging\": + { + \"loggers\": [ + { + \"name\": \"kea-dhcp6\", + \"output_options\": [ + { + \"output\": \"$LOG_FILE\" + } + ], + \"severity\": \"INFO\" + } + ] + } +}" # Invalid configuration (negative preferred-lifetime) to check that Kea # gracefully handles reconfiguration errors. CONFIG_INVALID="{ \"Dhcp6\": { - \"interfaces\": [ ], + \"interfaces-config\": { + \"interfaces\": [ ] + }, \"preferred-lifetime\": -3, \"valid-lifetime\": 4000, \"renew-timer\": 1000, @@ -104,6 +142,36 @@ bin_path=@abs_top_builddir@/src/bin/dhcp6 # Import common test library. . @abs_top_builddir@/src/lib/testutils/dhcp_test_lib.sh +# This test verifies that syntax check works properly. +syntax_check_test() { + # Log the start of the test and print test name. + test_start "dhcpv6_srv.syntax_check" + # Remove dangling Kea instances and remove log files. + cleanup + # Create correct configuration file. + create_config "${CONFIG}" + # Check it + printf "Running command %s.\n" "\"${bin_path}/${bin} -t -c ${CFG_FILE}\"" + ${bin_path}/${bin} -t -c ${CFG_FILE} + exit_code=$? + if [ ${exit_code} -ne 0 ]; then + printf "ERROR: expected exit code 0, got ${exit_code}\n" + clean_exit 1 + fi + # Create incorrect configuration file. + create_config "${CONFIG_BAD_SYNTAX}" + # Check it + printf "Running command %s.\n" "\"${bin_path}/${bin} -t -c ${CFG_FILE}\"" + printf "A syntax error should be detected\n" + ${bin_path}/${bin} -t -c ${CFG_FILE} + if [ $? -eq 0 ]; then + printf "ERROR: expected exit code not 0, got 0\n" + clean_exit 1 + fi + # All ok + clean_exit 0 +} + # This test verifies that DHCPv6 can be reconfigured with a SIGHUP signal. dynamic_reconfiguration_test() { # Log the start of the test and print test name. @@ -381,6 +449,7 @@ dynamic_reconfiguration_test shutdown_test "dhcpv6.sigterm_test" 15 shutdown_test "dhcpv6.sigint_test" 2 version_test "dhcpv6.version" +syntax_check_test logger_vars_test "dhcpv6.variables" lfc_timer_test