summaryrefslogtreecommitdiffstats
path: root/src/lib/config/tests
diff options
context:
space:
mode:
authorMarcin Siodelski <marcin@isc.org>2017-07-18 11:53:13 +0200
committerMarcin Siodelski <marcin@isc.org>2017-07-18 11:53:13 +0200
commitcdc3d714e354dd015e7a613ca89167171f3896fe (patch)
treea5ec44745ff167d6071c94bc8bb189fc79143641 /src/lib/config/tests
parent[master] Added ChangeLog entry for #5329. (diff)
downloadkea-cdc3d714e354dd015e7a613ca89167171f3896fe.tar.xz
kea-cdc3d714e354dd015e7a613ca89167171f3896fe.zip
[5330] CommandMgrs now use hooks to process commands in hooks libraries.
Diffstat (limited to 'src/lib/config/tests')
-rw-r--r--src/lib/config/tests/command_mgr_unittests.cc158
1 files changed, 20 insertions, 138 deletions
diff --git a/src/lib/config/tests/command_mgr_unittests.cc b/src/lib/config/tests/command_mgr_unittests.cc
index 9bff3f9839..db251c0b23 100644
--- a/src/lib/config/tests/command_mgr_unittests.cc
+++ b/src/lib/config/tests/command_mgr_unittests.cc
@@ -48,8 +48,6 @@ public:
CommandMgr::instance().deregisterAll();
CommandMgr::instance().closeCommandSocket();
resetCalloutIndicators();
- HooksManager::preCalloutsLibraryHandle().deregisterAllCallouts(
- "control_command_receive");
}
/// @brief Returns socket path (using either hardcoded path or env variable)
@@ -67,9 +65,18 @@ public:
}
/// @brief Resets indicators related to callout invocation.
+ ///
+ /// It also removes any registered callouts.
static void resetCalloutIndicators() {
callout_name = "";
callout_argument_names.clear();
+
+ // Iterate over existing hook points and for each of them remove
+ // callouts registered.
+ std::vector<std::string> hooks = ServerHooks::getServerHooksPtr()->getHookNames();
+ for (auto h = hooks.cbegin(); h != hooks.cend(); ++h) {
+ HooksManager::preCalloutsLibraryHandle().deregisterAllCallouts(*h);
+ }
}
/// @brief A simple command handler that always returns an eror
@@ -92,76 +99,14 @@ public:
return (createAnswer(234, "text generated by hook handler"));
}
- /// @brief Test callback which stores callout name and passed arguments.
- ///
- /// This callout doesn't indicate that the command has been processed,
- /// allowing the Command Manager to process it.
- ///
- /// @param callout_handle Handle passed by the hooks framework.
- /// @return Always 0.
- static int
- control_command_receive_callout(CalloutHandle& callout_handle) {
- callout_name = "control_command_receive";
-
- ConstElementPtr response;
- callout_handle.setArgument("response", response);
-
- callout_argument_names = callout_handle.getArgumentNames();
- // Sort arguments alphabetically, so as we can access them on
- // expected positions and verify.
- std::sort(callout_argument_names.begin(), callout_argument_names.end());
- return (0);
- }
-
/// @brief Test callback which stores callout name and passed arguments and
/// which handles the command.
///
- /// This callout returns the skip status to indicate the the command has
- /// been handled.
- ///
/// @param callout_handle Handle passed by the hooks framework.
/// @return Always 0.
static int
control_command_receive_handle_callout(CalloutHandle& callout_handle) {
- callout_name = "control_command_receive";
-
- // Create a hooks specific command manager.
- BaseCommandMgr callout_command_mgr;
- callout_command_mgr.registerCommand("my-command", my_hook_handler);
-
- ConstElementPtr command;
- callout_handle.getArgument("command", command);
-
- ConstElementPtr arg;
- std::string command_name = parseCommand(arg, command);
-
- ConstElementPtr response = callout_command_mgr.processCommand(command);
- callout_handle.setArgument("response", response);
-
- // Set 'skip' status to indicate that the command has been handled.
- if (command_name != "list-commands") {
- callout_handle.setStatus(CalloutHandle::NEXT_STEP_SKIP);
- }
-
- callout_argument_names = callout_handle.getArgumentNames();
- // Sort arguments alphabetically, so as we can access them on
- // expected positions and verify.
- std::sort(callout_argument_names.begin(), callout_argument_names.end());
- return (0);
- }
-
- /// @brief Test callback which modifies parameters of the command and
- /// does not return skip status.
- ///
- /// This callout is used to test the case when the callout modifies the
- /// received command and does not set next state SKIP to propagate the
- /// command with modified parameters to the local command handler.
- ///
- /// @param callout_handle Handle passed by the hooks framework.
- /// @return Always 0.
- static int
- control_command_receive_modify_callout(CalloutHandle& callout_handle) {
- callout_name = "control_command_receive";
+ callout_name = "control_command_receive_handle";
ConstElementPtr command;
callout_handle.getArgument("command", command);
@@ -169,11 +114,8 @@ public:
ConstElementPtr arg;
std::string command_name = parseCommand(arg, command);
- ElementPtr new_arg = Element::createList();
- new_arg->add(Element::create("hook-param"));
- command = createCommand(command_name, new_arg);
-
- callout_handle.setArgument("command", command);
+ callout_handle.setArgument("response",
+ createAnswer(234, "text generated by hook handler"));
callout_argument_names = callout_handle.getArgumentNames();
// Sort arguments alphabetically, so as we can access them on
@@ -338,12 +280,6 @@ TEST_F(CommandMgrTest, deregisterAll) {
// Test checks whether a command handler can be installed and then
// runs through processCommand to check that it's indeed called.
TEST_F(CommandMgrTest, processCommand) {
-
- // Register callout so as we can check that it is called before
- // processing the command by the manager.
- HooksManager::preCalloutsLibraryHandle().registerCallout(
- "control_command_receive", control_command_receive_callout);
-
// Install my handler
EXPECT_NO_THROW(CommandMgr::instance().registerCommand("my-command",
my_handler));
@@ -371,21 +307,17 @@ TEST_F(CommandMgrTest, processCommand) {
ASSERT_TRUE(handler_params);
EXPECT_EQ("[ \"just\", \"some\", \"data\" ]", handler_params->str());
- EXPECT_EQ("control_command_receive", callout_name);
-
- // Check that the appropriate arguments have been set. Include the
- // 'response' which should have been set by the callout.
- ASSERT_EQ(2, callout_argument_names.size());
- EXPECT_EQ("command", callout_argument_names[0]);
- EXPECT_EQ("response", callout_argument_names[1]);
+ // Command handlers not installed so expecting that callouts weren't
+ // called.
+ EXPECT_TRUE(callout_name.empty());
}
// Verify that processing a command can be delegated to a hook library.
TEST_F(CommandMgrTest, delegateProcessCommand) {
// Register callout so as we can check that it is called before
// processing the command by the manager.
- HooksManager::preCalloutsLibraryHandle().registerCallout(
- "control_command_receive", control_command_receive_handle_callout);
+ HooksManager::preCalloutsLibraryHandle().registerCommandHandler(
+ "my-command", control_command_receive_handle_callout);
// Install local handler
EXPECT_NO_THROW(CommandMgr::instance().registerCommand("my-command",
@@ -411,7 +343,7 @@ TEST_F(CommandMgrTest, delegateProcessCommand) {
ASSERT_NO_THROW(answer_arg = parseAnswer(status_code, answer));
EXPECT_EQ(234, status_code);
- EXPECT_EQ("control_command_receive", callout_name);
+ EXPECT_EQ("control_command_receive_handle", callout_name);
// Check that the appropriate arguments have been set. Include the
// 'response' which should have been set by the callout.
@@ -425,8 +357,8 @@ TEST_F(CommandMgrTest, delegateProcessCommand) {
TEST_F(CommandMgrTest, delegateListCommands) {
// Register callout so as we can check that it is called before
// processing the command by the manager.
- HooksManager::preCalloutsLibraryHandle().registerCallout(
- "control_command_receive", control_command_receive_handle_callout);
+ HooksManager::preCalloutsLibraryHandle().registerCommandHandler(
+ "my-command", control_command_receive_handle_callout);
// Create my-command-bis which is unique for the local Command Manager,
// i.e. not supported by the hook library. This command should also
@@ -464,56 +396,6 @@ TEST_F(CommandMgrTest, delegateListCommands) {
EXPECT_EQ("my-command-bis", command_names_list[2]);
}
-// This test verifies the scenario in which the hook library influences the
-// command processing by the Kea server. In this test, the callout modifies
-// the arguments of the command and passes the command on to the Command
-// Manager for processing.
-TEST_F(CommandMgrTest, modifyCommandArgsInHook) {
- // Register callout so as we can check that it is called before
- // processing the command by the manager.
- HooksManager::preCalloutsLibraryHandle().registerCallout(
- "control_command_receive", control_command_receive_modify_callout);
-
- // Install local handler
- EXPECT_NO_THROW(CommandMgr::instance().registerCommand("my-command",
- my_handler));
-
- // Now tell CommandMgr to process a command 'my-command' with the
- // specified parameter.
- ElementPtr my_params = Element::fromJSON("[ \"just\", \"some\", \"data\" ]");
- ConstElementPtr command = createCommand("my-command", my_params);
- ConstElementPtr answer;
- ASSERT_NO_THROW(answer = CommandMgr::instance().processCommand(command));
-
- // There should be an answer.
- ASSERT_TRUE(answer);
-
- // Returned status should be unique for the my_handler.
- ConstElementPtr answer_arg;
- int status_code;
- ASSERT_NO_THROW(answer_arg = parseAnswer(status_code, answer));
- EXPECT_EQ(123, status_code);
-
- // Local handler should have been called after the callout.
- ASSERT_TRUE(handler_called);
- EXPECT_EQ("my-command", handler_name);
- ASSERT_TRUE(handler_params);
- // Check that the local handler received the command with arguments
- // set by the callout.
- EXPECT_EQ("[ \"hook-param\" ]", handler_params->str());
-
-
- // Check that the callout has been called with appropriate parameters.
- EXPECT_EQ("control_command_receive", callout_name);
-
- // Check that the appropriate arguments have been set. Include the
- // 'response' which should have been set by the callout.
- ASSERT_EQ(2, callout_argument_names.size());
- EXPECT_EQ("command", callout_argument_names[0]);
- EXPECT_EQ("response", callout_argument_names[1]);
-
-}
-
// This test verifies that a Unix socket can be opened properly and that input
// parameters (socket-type and socket-name) are verified.
TEST_F(CommandMgrTest, unixCreate) {