Browse Source

[3770] Added -t to kea-dhcp4 too

Francis Dupont 8 years ago
parent
commit
ddf96f4f6d

+ 12 - 1
src/bin/d2/kea-dhcp-ddns.xml

@@ -52,7 +52,8 @@
       <arg><option>-V</option></arg>
       <arg><option>-W</option></arg>
       <arg><option>-d</option></arg>
-      <arg><option>-s</option></arg>
+      <!-- not yet <arg><option>-t</option></arg> -->
+      <arg><option>-c</option></arg>
     </cmdsynopsis>
   </refsynopsisdiv>
 
@@ -104,6 +105,16 @@
         </para></listitem>
       </varlistentry>
 
+<!-- not yet
+      <varlistentry>
+        <term><option>-t</option></term>
+        <listitem><para>
+          Check the syntax of the configuration file and report the first
+          error if any.
+        </para></listitem>
+      </varlistentry>
+-->
+
       <varlistentry>
         <term><option>-c</option></term>
         <listitem><para>

+ 9 - 0
src/bin/dhcp4/kea-dhcp4.xml

@@ -52,6 +52,7 @@
       <arg><option>-V</option></arg>
       <arg><option>-W</option></arg>
       <arg><option>-d</option></arg>
+      <arg><option>-t</option></arg>
       <arg><option>-c<replaceable class="parameter">config-file</replaceable></option></arg>
       <arg><option>-p<replaceable class="parameter">port-number</replaceable></option></arg>
     </cmdsynopsis>
@@ -102,6 +103,14 @@
       </varlistentry>
 
       <varlistentry>
+        <term><option>-t</option></term>
+        <listitem><para>
+          Check the syntax of the configuration file and report the first
+          error if any.
+        </para></listitem>
+      </varlistentry>
+
+      <varlistentry>
         <term><option>-c</option></term>
         <listitem><para>
           Configuration file including the configuration for DHCPv4 server.

+ 29 - 2
src/bin/dhcp4/main.cc

@@ -8,6 +8,7 @@
 
 #include <dhcp4/ctrl_dhcp4_srv.h>
 #include <dhcp4/dhcp4_log.h>
+#include <dhcp4/parser_context.h>
 #include <dhcpsrv/cfgmgr.h>
 #include <log/logger_support.h>
 #include <log/logger_manager.h>
@@ -17,6 +18,7 @@
 
 #include <iostream>
 
+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

+ 66 - 0
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

+ 1 - 2
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