From: Francis Dupont Date: Tue, 27 Dec 2016 17:52:39 +0000 (+0100) Subject: [3770] Added -t to kea-dhcp4 too X-Git-Tag: trac1205_base~6^2~9 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=ddf96f4f6d267b03537d8ade1aad6fee694b57d6;p=thirdparty%2Fkea.git [3770] Added -t to kea-dhcp4 too --- diff --git a/src/bin/d2/kea-dhcp-ddns.xml b/src/bin/d2/kea-dhcp-ddns.xml index 78650c097c..9974227256 100644 --- a/src/bin/d2/kea-dhcp-ddns.xml +++ b/src/bin/d2/kea-dhcp-ddns.xml @@ -52,7 +52,8 @@ - + + @@ -104,6 +105,16 @@ + + diff --git a/src/bin/dhcp4/kea-dhcp4.xml b/src/bin/dhcp4/kea-dhcp4.xml index 7a7b1256dd..722b3a398a 100644 --- a/src/bin/dhcp4/kea-dhcp4.xml +++ b/src/bin/dhcp4/kea-dhcp4.xml @@ -52,6 +52,7 @@ + @@ -101,6 +102,14 @@ + + + + Check the syntax of the configuration file and report the first + error if any. + + + diff --git a/src/bin/dhcp4/main.cc b/src/bin/dhcp4/main.cc index 553520a7c4..2283fb6196 100644 --- a/src/bin/dhcp4/main.cc +++ b/src/bin/dhcp4/main.cc @@ -8,6 +8,7 @@ #include #include +#include #include #include #include @@ -17,6 +18,7 @@ #include +using namespace isc::data; using namespace isc::dhcp; using namespace std; @@ -38,11 +40,12 @@ usage() { cerr << "Kea DHCPv4 server, version " << VERSION << endl; cerr << endl; cerr << "Usage: " << DHCP4_NAME - << " -[v|V|W] [-d] [-c cfgfile] [-p number]" << endl; + << " -[v|V|W] [-d] [-t] [-c cfgfile] [-p 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; @@ -56,16 +59,21 @@ main(int argc, char* argv[]) { int port_number = DHCP4_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 << Dhcpv4Srv::getVersion(false) << endl; return (EXIT_SUCCESS); @@ -114,6 +122,25 @@ main(int argc, char* argv[]) { usage(); } + if (check_mode) { + try { + Parser4Context parser; + ConstElementPtr json; + json = parser.parseFile(config_file, Parser4Context::PARSER_DHCP4); + 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 { // It is important that we set a default logger name because this name diff --git a/src/bin/dhcp4/tests/dhcp4_process_tests.sh.in b/src/bin/dhcp4/tests/dhcp4_process_tests.sh.in index 0fab7f40da..1313a04a7d 100644 --- a/src/bin/dhcp4/tests/dhcp4_process_tests.sh.in +++ b/src/bin/dhcp4/tests/dhcp4_process_tests.sh.in @@ -57,6 +57,41 @@ CONFIG="{ ] } }" +# Invalid configuration (syntax error) to check that Kea can check syntax. +CONFIG_BAD_SYNTAX="{ + \"Dhcp4\": + { + \"interfaces\": [ ], + \"valid-lifetime\": 4000, + \"renew-timer\": 1000, + \"rebind-timer\": 2000, + \"lease-database\": + { + \"type\": \"memfile\", + \"persist\": false + }, + \"subnet4\": [ + { + \"subnet\": \"10.0.0.0/8\", + \"pool\": [ { \"pool\": \"10.0.0.10-10.0.0.100\" } ] + } ] + }, + + \"Logging\": + { + \"loggers\": [ + { + \"name\": \"kea-dhcp4\", + \"output_options\": [ + { + \"output\": \"$LOG_FILE\" + } + ], + \"severity\": \"INFO\" + } + ] + } +}" # Invalid configuration (negative valid-lifetime) to check that Kea # gracefully handles reconfiguration errors. CONFIG_INVALID="{ @@ -103,6 +138,36 @@ bin_path=@abs_top_builddir@/src/bin/dhcp4 # 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 "dhcpv4_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 DHCPv4 can be reconfigured with a SIGHUP signal. dynamic_reconfiguration_test() { # Log the start of the test and print test name. @@ -380,3 +445,4 @@ shutdown_test "dhcpv4.sigint_test" 2 version_test "dhcpv4.version" logger_vars_test "dhcpv4.variables" lfc_timer_test +syntax_check_test diff --git a/src/bin/dhcp6/tests/dhcp6_process_tests.sh.in b/src/bin/dhcp6/tests/dhcp6_process_tests.sh.in index 65b0ce12ca..5ce8df00f9 100644 --- a/src/bin/dhcp6/tests/dhcp6_process_tests.sh.in +++ b/src/bin/dhcp6/tests/dhcp6_process_tests.sh.in @@ -449,7 +449,6 @@ 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 - +syntax_check_test