From: Tomek Mrugalski Date: Wed, 21 Jun 2023 10:46:56 +0000 (+0200) Subject: [#2707] config-set in v4 now returns hash X-Git-Tag: Kea-2.4.0~130 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=abe38a4e5ced1cca3ddc4e7e71dfca4039f4724f;p=thirdparty%2Fkea.git [#2707] config-set in v4 now returns hash --- diff --git a/src/bin/dhcp4/json_config_parser.cc b/src/bin/dhcp4/json_config_parser.cc index 98dd3f379d..0de041bdf4 100644 --- a/src/bin/dhcp4/json_config_parser.cc +++ b/src/bin/dhcp4/json_config_parser.cc @@ -974,8 +974,14 @@ configureDhcp4Server(Dhcpv4Srv& server, isc::data::ConstElementPtr config_set, .arg(CfgMgr::instance().getStagingCfg()-> getConfigSummary(SrvConfig::CFGSEL_ALL4)); + // Also calculate SHA256 hash of the config that was just set and append it to the response. + ElementPtr config = CfgMgr::instance().getCurrentCfg()->toElement(); + string hash = BaseCommandMgr::getHash(config); + ElementPtr hash_map = Element::createMap(); + hash_map->set("hash", Element::create(hash)); + // Everything was fine. Configuration is successful. - answer = isc::config::createAnswer(CONTROL_RESULT_SUCCESS, "Configuration successful."); + answer = isc::config::createAnswer(CONTROL_RESULT_SUCCESS, "Configuration successful.", hash_map); return (answer); } diff --git a/src/bin/dhcp4/tests/ctrl_dhcp4_srv_unittest.cc b/src/bin/dhcp4/tests/ctrl_dhcp4_srv_unittest.cc index f912c17ab1..5cf130618f 100644 --- a/src/bin/dhcp4/tests/ctrl_dhcp4_srv_unittest.cc +++ b/src/bin/dhcp4/tests/ctrl_dhcp4_srv_unittest.cc @@ -816,9 +816,13 @@ TEST_F(CtrlChannelDhcpv4SrvTest, configSet) { std::string response; sendUnixCommand(os.str(), response); - // Verify the configuration was successful. - EXPECT_EQ("{ \"result\": 0, \"text\": \"Configuration successful.\" }", - response); + // Verify the configuration was successful. The config contains random + // socket name (/tmp/kea-/kea6.sock), so the + // hash will be different each time. As such, we can do simplified checks: + // - verify the "result": 0 is there + // - verify the "text": "Configuration successful." is there + EXPECT_NE(response.find("\"result\": 0"), std::string::npos); + EXPECT_NE(response.find("\"text\": \"Configuration successful.\""), std::string::npos); // Check that the config was indeed applied. const Subnet4Collection* subnets = @@ -880,9 +884,13 @@ TEST_F(CtrlChannelDhcpv4SrvTest, configSet) { // Verify the control channel socket no longer exists. EXPECT_FALSE(fileExists(socket_path_)); - // With no command channel, should still receive the response. - EXPECT_EQ("{ \"result\": 0, \"text\": \"Configuration successful.\" }", - response); + // With no command channel, should still receive the response. The config contains random + // socket name (/tmp/kea-/kea6.sock), so the + // hash will be different each time. As such, we can do simplified checks: + // - verify the "result": 0 is there + // - verify the "text": "Configuration successful." is there + EXPECT_NE(response.find("\"result\": 0"), std::string::npos); + EXPECT_NE(response.find("\"text\": \"Configuration successful.\""), std::string::npos); // Check that the config was not lost subnets = CfgMgr::instance().getCurrentCfg()->getCfgSubnets4()->getAll(); @@ -1021,9 +1029,13 @@ TEST_F(CtrlChannelDhcpv4SrvTest, configTest) { std::string response; sendUnixCommand(os.str(), response); - // Verify the configuration was successful. - EXPECT_EQ("{ \"result\": 0, \"text\": \"Configuration successful.\" }", - response); + // Verify the configuration was successful. The config contains random + // socket name (/tmp/kea-/kea6.sock), so the + // hash will be different each time. As such, we can do simplified checks: + // - verify the "result": 0 is there + // - verify the "text": "Configuration successful." is there + EXPECT_NE(response.find("\"result\": 0"), std::string::npos); + EXPECT_NE(response.find("\"text\": \"Configuration successful.\""), std::string::npos); // Check that the config was indeed applied. const Subnet4Collection* subnets = @@ -1579,9 +1591,13 @@ TEST_F(CtrlChannelDhcpv4SrvTest, configReloadValid) { // This command should reload test8.json config. sendUnixCommand("{ \"command\": \"config-reload\" }", response); - // Verify the configuration was successful. - EXPECT_EQ("{ \"result\": 0, \"text\": \"Configuration successful.\" }", - response); + // Verify the configuration was successful. The config contains random + // socket name (/tmp/kea-/kea6.sock), so the + // hash will be different each time. As such, we can do simplified checks: + // - verify the "result": 0 is there + // - verify the "text": "Configuration successful." is there + EXPECT_NE(response.find("\"result\": 0"), std::string::npos); + EXPECT_NE(response.find("\"text\": \"Configuration successful.\""), std::string::npos); // Check that the config was indeed applied. const Subnet4Collection* subnets = diff --git a/src/bin/dhcp4/tests/hooks_unittest.cc b/src/bin/dhcp4/tests/hooks_unittest.cc index 6d5f8cb5cc..6622995dfa 100644 --- a/src/bin/dhcp4/tests/hooks_unittest.cc +++ b/src/bin/dhcp4/tests/hooks_unittest.cc @@ -3267,7 +3267,9 @@ TEST_F(LoadUnloadDhcpv4SrvTest, Dhcpv4SrvConfigured) { parseAnswer(status_code, answer); if (parameters.empty()) { EXPECT_EQ(0, status_code); - EXPECT_EQ(answer->str(), R"({ "result": 0, "text": "Configuration successful." })"); + EXPECT_EQ(answer->str(), "{ \"arguments\": { \"hash\": \"FA868B7A5983A80A83A7E9EFD8E" + "3DA4CC78B57A44817AEB9EF9DD6E4B9B0B116\" }, \"result\": 0, \"text\": " + "\"Configuration successful.\" }"); } else { EXPECT_EQ(1, status_code); if (parameters.find("fail-without-error") != string::npos) {