Browse Source

[3422] Implemented the status command for keactrl.

Marcin Siodelski 11 years ago
parent
commit
c3baf284d7

+ 24 - 2
src/bin/keactrl/keactrl.in

@@ -105,10 +105,11 @@ as another instance is already running."
 
 command=
 if [ $# -ne 1 ]; then
-    log_error "No command specified. Use: start, stop, commit."
+    log_error "No command specified. Use: start, stop, commit or status."
     exit 1
 
-elif [ "${1}" != "start" -a "${1}" != "stop" -a "${1}" != "commit" ]; then
+elif [ "${1}" != "start" -a "${1}" != "stop" -a\
+ "${1}" != "commit" -a "${1}" != "status" ]; then
     log_error "Illegal command: ${1}"
     exit 1
 
@@ -195,6 +196,27 @@ case ${command} in
 
         exit 0 ;;
 
+    status)
+        kea4_status="inactive"
+        check_running $(basename ${dhcpv4_srv})
+        if [ ${_running} -eq 1 ]; then
+            kea4_status="active"
+        fi
+        printf "DHCPv4 server: %s\n" ${kea4_status}
+
+        kea6_status="inactive"
+        check_running $(basename ${dhcpv6_srv})
+        if [ ${_running} -eq 1 ]; then
+            kea6_status="active"
+        fi
+        printf "DHCPv6 server: %s\n" ${kea6_status}
+
+        printf "Kea configuration file: %s\n" ${kea_config_file}
+
+        printf "keactrl configuration file: %s\n" ${keactrl_conf}
+
+        exit 0 ;;
+
     # No other commands are supported.
     *)
         log_error "Invalid command:  ${command}."

+ 2 - 0
src/bin/keactrl/tests/Makefile.am

@@ -3,6 +3,8 @@ SUBDIRS = .
 SHTESTS = keactrl_tests.sh
 
 noinst_SCRIPTS = keactrl_tests.sh
+CLEANFILES = keactrl_tests.sh
+CLEANFILES = *.log *.json
 
 EXTRA_DIST = keactrl_tests.sh.in
 

+ 26 - 8
src/bin/keactrl/tests/keactrl_tests.sh.in

@@ -144,7 +144,7 @@ Expected wait_for_message return %d, returned %d."
     assert_eq 0 ${_GET_PIDS_NUM} \
         "Expected %d ${kea6_name} processes running, found %d processes running"
 
-    cleanup
+    test_finish 0
 }
 
 # This test checks that only DHCPv4 server can be started and that the DHCPv6
@@ -191,6 +191,16 @@ Expected wait_for_message return %d, returned %d."
     assert_eq 0 ${_GET_PIDS_NUM} \
         "Expected %d ${kea6_name} process running, found %d processes running"
 
+    # Make sure that the status command returns appropriate status.
+    printf "Getting status of Kea modules: %s\n" "${keactrl} status"
+    output=$( ${keactrl} status )
+    ret=${?}
+    assert_eq 0 ${ret} "Expected keactrl to return %d, returned %d"
+    assert_string_contains "DHCPv4 server: active" "${output}" \
+        "Expected keactrl status command return %s"
+    assert_string_contains "DHCPv6 server: inactive" "${output}" \
+        "Expected keactrl status command return %s"
+
     # Use keactrl stop to shutdown the servers.
     printf "Stopping Kea: ${keactrl} stop\n"
     ${keactrl} stop
@@ -212,7 +222,7 @@ Expected wait_for_message return %d, returned %d."
     assert_eq 0 ${_GET_PIDS_NUM} \
         "Expected %d ${kea6_name} processes running, found %d processes running"
 
-    cleanup
+    test_finish 0
 }
 
 # This test checks that only DHCPv6 server can be started and that the DHCPv4
@@ -237,7 +247,7 @@ start_v6_server_test() {
     printf "Starting Kea: ${keactrl} start\n"
     ${keactrl} start
     ret=${?}
-    assert_eq 0 ${ret} "Expected keactrl to retrun 0, returned value was ${ret}"
+    assert_eq 0 ${ret} "Expected keactrl to return %d, returned value was %d"
 
     # Wait up to 20s for the DHCPv6 server to configure.
     wait_for_message 20 "DHCP6_CONFIG_COMPLETE" 1
@@ -259,6 +269,16 @@ Expected wait_for_message return %d, returned %d."
     assert_eq 0 ${_GET_PIDS_NUM} \
         "Expected %d ${kea4_name} process running, found %d processes running"
 
+    # Make sure that the status command returns appropriate status.
+    printf "Getting status of Kea modules: %s\n" "${keactrl} status"
+    output=$( ${keactrl} status )
+    ret=${?}
+    assert_eq 0 ${ret} "Expected keactrl to return %d, returned %d"
+    assert_string_contains "DHCPv4 server: inactive" "${output}" \
+        "Expected keactrl status command return %s"
+    assert_string_contains "DHCPv6 server: active" "${output}" \
+        "Expected keactrl status command return %s"
+
     # Use keactrl stop to shutdown the servers.
     printf "Stopping Kea: ${keactrl} stop\n"
     ${keactrl} stop
@@ -280,7 +300,7 @@ Expected wait_for_message return %d, returned %d."
     assert_eq 0 ${_GET_PIDS_NUM} \
         "Expected %d ${kea6_name} processes running, found %d processes running"
 
-    cleanup
+    test_finish 0
 }
 
 # This test checks that the DHCPv6 server can be started first, and then the
@@ -408,7 +428,7 @@ Expected wait_for_message return %d, returned %d."
     assert_eq 0 ${_GET_PIDS_NUM} \
         "Expected %d ${kea6_name} processes running, found %d processes running"
 
-    cleanup
+    test_finish 0
 }
 
 # This test checks that the DHCPv4 server can be started first, and then the
@@ -536,7 +556,7 @@ Expected wait_for_message return %d, returned %d."
     assert_eq 0 ${_GET_PIDS_NUM} \
         "Expected %d ${kea6_name} processes running, found %d processes running"
 
-    cleanup
+    test_finish 0
 }
 
 start_both_servers_test
@@ -545,5 +565,3 @@ start_v6_server_test
 late_start_v4_server_test
 late_start_v6_server_test
 
-
-clean_exit 0

+ 48 - 15
src/lib/testutils/dhcp_test_lib.sh

@@ -21,10 +21,16 @@
 # A list of Kea processes, mainly used by the cleanup functions.
 KEA_PROCS="b10-dhcp4 b10-dhcp6 b10-dhcp-ddns"
 
+# Assertion that checks if two numbers are equal.
+# If numbers are not equal, the mismatched values are presented and the
+# detailed error is printed. The detailed error must use the printf
+# formatting like this:
+#    "Expected that some value 1 %d is equal to some other value %d".
 assert_eq() {
-    val1=${1}
-    val2=${2}
-    detailed_err=${3}
+    val1=${1}         # Reference value
+    val2=${2}         # Tested value
+    detailed_err=${3} # Detailed error format string
+    # If nothing found, present an error an exit.
     if [ ${val1} -ne ${val2} ]; then
         printf "Assertion failure: ${val1} != ${val2}, for val1=${val1}, val2=${val2}\n"
         printf "${detailed_err}\n" ${val1} ${val2}
@@ -32,12 +38,49 @@ assert_eq() {
     fi
 }
 
+# Assertion that checks if one string contains another string.
+# If assertion fails, both strings are displayed and the detailed
+# error is printed. The detailed error must use the printf formatting
+# like this:
+#    "Expected some string to contain this string: %s".
+assert_string_contains() {
+    pattern=${1}      # Substring or awk pattern
+    text=${2}         # Text to be searched for substring
+    detailed_err=${3} # Detailed error format string
+    # Search for a pattern
+    match=$( printf "%s" "${text}" | awk /"${pattern}"/ )
+    # If nothing found, present an error and exit.
+    if [ -z "${match}" ]; then
+        printf "Assertion failure: \n\"%s\"\n\ndoesn't contain pattern:\n
+\"%s\"\n\n" "${text}" "${pattern}"
+        printf "${detailed_err}\n" "\"${pattern}\""
+        clean_exit 1
+    fi
+}
+
 # Begins a test by prining its name.
 # It requires the ${TEST_NAME} variable to hold the test name.
 test_start() {
     printf "\nSTART TEST ${TEST_NAME}\n"
 }
 
+test_finish() {
+    local error_code=${1}
+    exit_code=${1}  # Exit code to be returned by the exit function.
+    if [ ${exit_code} -eq 0 ]; then
+        cleanup
+        printf "PASSED ${TEST_NAME}\n\n"
+    else
+        # Dump log file if exists for debugging purposes.
+        if [ -s ${LOG_FILE} ]; then
+            printf "Log file dump:\n"
+            cat ${LOG_FILE}
+        fi
+        cleanup
+        printf "FAILED ${TEST_NAME}\n\n"
+    fi
+}
+
 # Stores the configuration specified as a parameter in the configuration
 # file which name has been set in the ${CFG_FILE} variable.
 create_config() {
@@ -123,18 +166,8 @@ cleanup() {
 # If a test fails, the Kea log is dumped.
 clean_exit() {
     exit_code=${1}  # Exit code to be returned by the exit function.
-    if [ ${exit_code} -eq 0 ]; then
-        cleanup
-        printf "PASSED ${TEST_NAME}\n\n"
-    else
-        # Dump log file if exists for debugging purposes.
-        if [ -s ${LOG_FILE} ]; then
-            printf "Log file dump:\n"
-            cat ${LOG_FILE}
-        fi
-        cleanup
-        printf "FAILED ${TEST_NAME}\n\n"
-    fi
+    # Print test result and perform a cleanup
+    test_finish ${exit_code}
     exit ${exit_code}
 }