]> git.ipfire.org Git - thirdparty/kea.git/commitdiff
[#1024] Improved processMaintenanceNotify test
authorMarcin Siodelski <marcin@isc.org>
Fri, 17 Jan 2020 19:03:18 +0000 (20:03 +0100)
committerMarcin Siodelski <marcin@isc.org>
Mon, 27 Jan 2020 12:55:18 +0000 (13:55 +0100)
Apart from checking the transition from the invalid states also check that
transitions from all of the supported states is possible.

src/hooks/dhcp/high_availability/tests/ha_service_unittest.cc

index 79ab4749fc3b083b6d597b5bb4ea53f1bff7df8f..abbe94fd81ca3a79fa14755e1e27d1c921d9dff8 100644 (file)
@@ -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<int> 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<int> 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