Skip to content

Commit

Permalink
Add an integration test for heartbeat
Browse files Browse the repository at this point in the history
An integration test is to verify if the agent gets disconnected when did
not receive heartbeat since threshold from controller.

Signed-off-by: Joonyoung Shim <[email protected]>
  • Loading branch information
dofmind committed Aug 13, 2024
1 parent a6e8137 commit f188374
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 0 deletions.
6 changes: 6 additions & 0 deletions tests/bluechi_test/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ def deep_copy(self) -> "BluechiControllerConfig":
cfg.name = self.name
cfg.port = self.port
cfg.allowed_node_names = self.allowed_node_names
cfg.heartbeat_interval = self.heartbeat_interval
cfg.node_heartbeat_threshold = self.node_heartbeat_threshold
cfg.log_level = self.log_level
cfg.log_target = self.log_target
cfg.log_is_quiet = self.log_is_quiet
Expand All @@ -90,6 +92,7 @@ def __init__(
controller_port: str = "8420",
controller_address: str = "",
heartbeat_interval: str = "2000",
controller_heartbeat_threshold: str = "0",
log_level: str = "DEBUG",
log_target: str = "journald",
log_is_quiet: bool = False,
Expand All @@ -101,6 +104,7 @@ def __init__(
self.controller_port = controller_port
self.controller_address = controller_address
self.heartbeat_interval = heartbeat_interval
self.controller_heartbeat_threshold = controller_heartbeat_threshold
self.log_level = log_level
self.log_target = log_target
self.log_is_quiet = log_is_quiet
Expand All @@ -112,6 +116,7 @@ def serialize(self) -> str:
ControllerPort={self.controller_port}
ControllerAddress={self.controller_address}
HeartbeatInterval={self.heartbeat_interval}
ControllerHeartbeatThreshold={self.controller_heartbeat_threshold}
LogLevel={self.log_level}
LogTarget={self.log_target}
LogIsQuiet={self.log_is_quiet}
Expand All @@ -127,6 +132,7 @@ def deep_copy(self) -> "BluechiAgentConfig":
cfg.controller_port = self.controller_port
cfg.controller_address = self.controller_address
cfg.heartbeat_interval = self.heartbeat_interval
cfg.controller_heartbeat_threshold = self.controller_heartbeat_threshold
cfg.log_level = self.log_level
cfg.log_target = self.log_target
cfg.log_is_quiet = self.log_is_quiet
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
summary: Test if the agent gets disconnected when did not receive heartbeat since
threshold from controller
id: 9ea390ed-9ba9-4961-838b-ad44cbe9f0b1
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#
# Copyright Contributors to the Eclipse BlueChi project
#
# SPDX-License-Identifier: LGPL-2.1-or-later

import time
import unittest

from dasbus.loop import EventLoop
from dasbus.typing import Variant

from bluechi.api import Agent


class TestAgentIsDisconnected(unittest.TestCase):

state = None
timestamp = None

def test_agent_is_disconnected(self):
loop = EventLoop()

def on_state_change(state: Variant):
self.timestamp = round(time.time() * 1000000)
self.state = state.get_string()
loop.quit()

agent = Agent()
assert agent.status == "online"

agent.on_status_changed(on_state_change)
timestamp = agent.last_seen_timestamp

loop.run()

# verify that the agent is disconnected after more than
# ControllerHeartbeatThreshold seconds have elapsed
assert self.timestamp - timestamp > 6000000
assert self.state == "offline"


if __name__ == "__main__":
unittest.main()
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#
# Copyright Contributors to the Eclipse BlueChi project
#
# SPDX-License-Identifier: LGPL-2.1-or-later

import os
from typing import Dict

from bluechi_test.config import BluechiAgentConfig, BluechiControllerConfig
from bluechi_test.machine import BluechiAgentMachine, BluechiControllerMachine
from bluechi_test.test import BluechiTest

node_foo_name = "node-foo"


def exec(ctrl: BluechiControllerMachine, nodes: Dict[str, BluechiAgentMachine]):
node_foo = nodes[node_foo_name]

result, output = node_foo.run_python(
os.path.join("python", "is_agent_disconnected.py")
)
if result != 0:
raise Exception(output)


def test_bluechi_heartbeat_agent_disconnected(
bluechi_test: BluechiTest,
bluechi_ctrl_default_config: BluechiControllerConfig,
bluechi_node_default_config: BluechiAgentConfig,
):

bluechi_ctrl_default_config.allowed_node_names = [node_foo_name]

bluechi_node_default_config.node_name = node_foo_name
bluechi_node_default_config.controller_heartbeat_threshold = "6000"

bluechi_test.set_bluechi_controller_config(bluechi_ctrl_default_config)
bluechi_test.add_bluechi_agent_config(bluechi_node_default_config)

bluechi_test.run(exec)

0 comments on commit f188374

Please sign in to comment.