From: Marcin Siodelski Date: Fri, 17 Jan 2020 19:03:18 +0000 (+0100) Subject: [#1024] Improved processMaintenanceNotify test X-Git-Tag: Kea-1.7.4~56 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=94deb851e0d1cb00d8f6cb5ccb1cdac8aa9d2b58;p=thirdparty%2Fkea.git [#1024] Improved processMaintenanceNotify test Apart from checking the transition from the invalid states also check that transitions from all of the supported states is possible. --- diff --git a/src/hooks/dhcp/high_availability/tests/ha_service_unittest.cc b/src/hooks/dhcp/high_availability/tests/ha_service_unittest.cc index 79ab4749fc..abbe94fd81 100644 --- a/src/hooks/dhcp/high_availability/tests/ha_service_unittest.cc +++ b/src/hooks/dhcp/high_availability/tests/ha_service_unittest.cc @@ -2578,67 +2578,64 @@ TEST_F(HAServiceTest, processContinue) { TEST_F(HAServiceTest, processMaintenanceNotify) { HAConfigPtr config_storage = createValidConfiguration(); - TestHAService service(io_service_, network_state_, config_storage); - EXPECT_NO_THROW(service.transition(HA_WAITING_ST, HAService::NOP_EVT)); - - // Process ha-maintenance-notify command that should transition the - // state machine to the maintained state. - ConstElementPtr rsp; - ASSERT_NO_THROW(rsp = service.processMaintenanceNotify(false)); + std::set valid_states = { + HA_WAITING_ST, + HA_READY_ST, + HA_SYNCING_ST, + HA_PARTNER_DOWN_ST, + HA_LOAD_BALANCING_ST, + HA_HOT_STANDBY_ST, + }; - // The server should have responded. - ASSERT_TRUE(rsp); - checkAnswer(rsp, CONTROL_RESULT_SUCCESS, "Server is in maintained state."); + TestHAService service(io_service_, network_state_, config_storage); - // The state machine should have been transitioned to the maintained state. - EXPECT_EQ(HA_MAINTAINED_ST, service.getCurrState()); + // Test transition from the states for which it is allowed. + for (auto state : valid_states) { + EXPECT_NO_THROW(service.transition(state, HAService::NOP_EVT)); - // Try to cancel the maintenance. - ASSERT_NO_THROW(rsp = service.processMaintenanceNotify(true)); + // Process ha-maintenance-notify command that should transition the + // state machine to the maintained state. + ConstElementPtr rsp; + ASSERT_NO_THROW(rsp = service.processMaintenanceNotify(false)); - // The server should have responded. - ASSERT_TRUE(rsp); - checkAnswer(rsp, CONTROL_RESULT_SUCCESS, "Server maintenance canceled."); + // The server should have responded. + ASSERT_TRUE(rsp); + checkAnswer(rsp, CONTROL_RESULT_SUCCESS, "Server is in maintained state."); - // The state machine should have been transitioned to the state it was in - // prior to transitioning to the maintained state. - EXPECT_EQ(HA_WAITING_ST, service.getCurrState()); + // The state machine should have been transitioned to the maintained state. + EXPECT_EQ(HA_MAINTAINED_ST, service.getCurrState()); - // Make sure that the transition from the terminated state is not allowed. - EXPECT_NO_THROW(service.transition(HA_TERMINATED_ST, HAService::NOP_EVT)); - EXPECT_NO_THROW(service.runModel(HAService::NOP_EVT)); + // Try to cancel the maintenance. + ASSERT_NO_THROW(rsp = service.processMaintenanceNotify(true)); - ASSERT_NO_THROW(rsp = service.processMaintenanceNotify(false)); + // The server should have responded. + ASSERT_TRUE(rsp); + checkAnswer(rsp, CONTROL_RESULT_SUCCESS, "Server maintenance canceled."); - ASSERT_TRUE(rsp); - checkAnswer(rsp, TestHAService::HA_CONTROL_RESULT_MAINTENANCE_NOT_ALLOWED, - "Unable to transition the server from the terminated to" - " maintained state."); - - // The transition from the backup state is also not allowed. - EXPECT_NO_THROW(service.transition(HA_BACKUP_ST, HAService::NOP_EVT)); - EXPECT_NO_THROW(service.runModel(HAService::NOP_EVT)); - - ASSERT_NO_THROW(rsp = service.processMaintenanceNotify(false)); + // The state machine should have been transitioned to the state it was in + // prior to transitioning to the maintained state. + EXPECT_EQ(state, service.getCurrState()); + } - // The server should have responded. - ASSERT_TRUE(rsp); - checkAnswer(rsp, TestHAService::HA_CONTROL_RESULT_MAINTENANCE_NOT_ALLOWED, - "Unable to transition the server from the backup to maintained" - " state."); + std::set invalid_states = { + HA_TERMINATED_ST, + HA_BACKUP_ST, + HA_PARTNER_MAINTAINED_ST + }; - // Finally, it is not allowed to transition the server from the partner-maintained - // to the maintained state. - EXPECT_NO_THROW(service.transition(HA_PARTNER_MAINTAINED_ST, HAService::NOP_EVT)); - EXPECT_NO_THROW(service.runModel(HAService::NOP_EVT)); + // Make sure that the transition from the other states is not allowed. + for (auto state : invalid_states) { + EXPECT_NO_THROW(service.transition(state, HAService::NOP_EVT)); + EXPECT_NO_THROW(service.runModel(HAService::NOP_EVT)); - ASSERT_NO_THROW(rsp = service.processMaintenanceNotify(false)); + ConstElementPtr rsp; + ASSERT_NO_THROW(rsp = service.processMaintenanceNotify(false)); - // The server should have responded. - ASSERT_TRUE(rsp); - checkAnswer(rsp, TestHAService::HA_CONTROL_RESULT_MAINTENANCE_NOT_ALLOWED, - "Unable to transition the server from the partner-maintained to" - " maintained state."); + ASSERT_TRUE(rsp); + checkAnswer(rsp, TestHAService::HA_CONTROL_RESULT_MAINTENANCE_NOT_ALLOWED, + "Unable to transition the server from the " + + stateToString(state) + " to maintained state."); + } } // This test verifies the case when the server receiving the ha-maintenance-start