diff --git a/tests/bluechi_test/bluechi_is_online.py b/tests/bluechi_test/bluechi_is_online.py index 2a9b4e67b5..b9382be072 100644 --- a/tests/bluechi_test/bluechi_is_online.py +++ b/tests/bluechi_test/bluechi_is_online.py @@ -26,10 +26,15 @@ def run( ) return result, output - def agent_is_online(self) -> bool: + def agent_is_online(self, wait: int = None) -> bool: + cmd = ["agent"] + + if wait: + cmd.extend(["--wait", str(wait)]) + result, output = self.run( "Checking if agent is active.", - "agent", + " ".join(cmd), False, 0, ) diff --git a/tests/tests/tier0/bluechi-is-online-agent-wait/main.fmf b/tests/tests/tier0/bluechi-is-online-agent-wait/main.fmf new file mode 100644 index 0000000000..f1c4ec2162 --- /dev/null +++ b/tests/tests/tier0/bluechi-is-online-agent-wait/main.fmf @@ -0,0 +1,2 @@ +summary: test bluechi-is-online agent --wait command +id: afde18db-d923-4596-8a2f-c0eb8fab2a15 diff --git a/tests/tests/tier0/bluechi-is-online-agent-wait/test_bluechi_is_online_agent_wait.py b/tests/tests/tier0/bluechi-is-online-agent-wait/test_bluechi_is_online_agent_wait.py new file mode 100644 index 0000000000..72c295fb47 --- /dev/null +++ b/tests/tests/tier0/bluechi-is-online-agent-wait/test_bluechi_is_online_agent_wait.py @@ -0,0 +1,140 @@ +# +# Copyright Contributors to the Eclipse BlueChi project +# +# SPDX-License-Identifier: LGPL-2.1-or-later + +import logging +import os +import threading +import time +from typing import Dict + +from bluechi_test.bluechi_is_online import BluechiIsOnline +from bluechi_test.config import BluechiAgentConfig, BluechiControllerConfig +from bluechi_test.machine import BluechiAgentMachine, BluechiControllerMachine +from bluechi_test.test import BluechiTest +from bluechi_test.util import Timeout + +LOGGER = logging.getLogger(__name__) + +NODE_FOO = "node-foo" +IMMEDIATE_RETURN_TIMEOUT = int(os.getenv("IMMEDIATE_RETURN_TIMEOUT", 1000)) +WAIT_PARAM_VALUE = int(os.getenv("WAIT_PARAM_VALUE", 5000)) +MAX_WAIT_PARAM_VALUE = int(os.getenv("MAX_WAIT_PARAM_VALUE", 20000)) +SLEEP_DURATION = int(os.getenv("SLEEP_DURATION", 2)) + + +class ResultFuture: + def __init__(self): + self.result = None + self.output = "" + + +def check_agent( + bluechi_is_online: BluechiIsOnline, + future: ResultFuture, + description: str, + wait_time: int = 0, + expected_result: int = 0, +): + result = bluechi_is_online.agent_is_online( + wait_time, + ) + future.result = result + + +def exec(ctrl: BluechiControllerMachine, nodes: Dict[str, BluechiAgentMachine]): + + node_foo = nodes[NODE_FOO] + + # Test 1: Start agent and verify --wait option returns 0 immediately + with Timeout( + IMMEDIATE_RETURN_TIMEOUT, "bluechi-is-online didn't return immediately" + ): + LOGGER.debug("Starting test number 1 - agent should be online.") + assert node_foo.bluechi_is_online.agent_is_online(wait=IMMEDIATE_RETURN_TIMEOUT) + + # Test 2: Stop agent and verify --wait returns 1 after wait time is expired + node_foo.systemctl.stop_unit("bluechi-agent") + assert node_foo.wait_for_unit_state_to_be("bluechi-agent", "inactive") + LOGGER.debug("Starting test number 2 - agent should remain offline.") + + start_time = time.time() + # extend the test timeout here a bit since we are wrapping bluechi-is-online + with Timeout( + WAIT_PARAM_VALUE + 1, "bluechi-is-online did not exit within the specified time" + ): + result = node_foo.bluechi_is_online.agent_is_online(WAIT_PARAM_VALUE) + + assert ( + not result + ), f"Expected bluechi-is-online with --wait={WAIT_PARAM_VALUE} on node {NODE_FOO} to exit false" + assert ( + start_time - time.time() <= WAIT_PARAM_VALUE + 1 + ), "Expected around 5 second for bluechi-is-online to exit" + + # Test 3: Stop agent, run 'bluechi-is-online agent --wait', start agent and verify --wait returns 0 before the + # wait time expires + try: + with Timeout(MAX_WAIT_PARAM_VALUE, "Timeout during Test 3"): + LOGGER.debug( + "Starting test number 3, stopping agent to ensure it is inactive before starting `bluechi-is-online`." + ) + node_foo.systemctl.stop_unit("bluechi-agent") + assert node_foo.wait_for_unit_state_to_be("bluechi-agent", "inactive") + LOGGER.debug("Agent confirmed inactive after stopping.") + + result_future_wait = ResultFuture() + start_time = time.time() + LOGGER.debug( + "Starting `bluechi-is-online` thread with wait time of 20 seconds." + ) + check_thread_wait = threading.Thread( + target=check_agent, + args=( + node_foo.bluechi_is_online, + result_future_wait, + "Checking if agent comes online", + MAX_WAIT_PARAM_VALUE, + 0, + ), + ) + check_thread_wait.start() + time.sleep(SLEEP_DURATION) + node_foo.systemctl.start_unit("bluechi-agent") + assert node_foo.wait_for_unit_state_to_be("bluechi-agent", "active") + LOGGER.debug("Agent confirmed active after starting.") + + # Wait for the thread to complete + with Timeout(2, "Thread didn't stop within 2s of starting bluechi-agent"): + check_thread_wait.join() + + elapsed_time = time.time() - start_time + LOGGER.debug( + f"Test 3 result: {result_future_wait.result}, Elapsed time: {elapsed_time:.2f} seconds" + ) + assert ( + result_future_wait.result + ), "Expected agent to come online before wait expired" + assert ( + 2 < elapsed_time < 3 + ), f"Command took too long to complete: {elapsed_time:.2f} seconds" + except TimeoutError as ex: + LOGGER.error(f"Test 3 failed: {ex}") + return + + +def test_bluechi_is_online_agent_wait( + bluechi_test: BluechiTest, + bluechi_node_default_config: BluechiAgentConfig, + bluechi_ctrl_default_config: BluechiControllerConfig, +): + node_bar_cfg = bluechi_node_default_config.deep_copy() + node_bar_cfg.node_name = NODE_FOO + + bluechi_ctrl_default_config.allowed_node_names = [NODE_FOO] + + bluechi_test.set_bluechi_controller_config(bluechi_ctrl_default_config) + bluechi_test.add_bluechi_agent_config(node_bar_cfg) + + bluechi_test.run(exec)