Skip to content

Commit

Permalink
Add integration tests for heartbeat
Browse files Browse the repository at this point in the history
An integration test is to verify if default configuration of controller
disables periodic heartbeat of the controller, and the other is to
verify if the node gets disconnected when did not receive heartbeat
since threshold from node.

Signed-off-by: Joonyoung Shim <[email protected]>
  • Loading branch information
dofmind committed Jun 26, 2024
1 parent de69391 commit 37269b6
Show file tree
Hide file tree
Showing 7 changed files with 163 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 @@ -32,6 +32,8 @@ def __init__(
name: str = "bluechi-controller",
port: str = "8420",
allowed_node_names: List[str] = [],
heartbeat_interval: str = "0",
node_heartbeat_threshold: str = "6000",
log_level: str = "DEBUG",
log_target: str = "journald",
log_is_quiet: bool = False,
Expand All @@ -41,6 +43,8 @@ def __init__(
self.name = name
self.port = port
self.allowed_node_names = allowed_node_names
self.heartbeat_interval = heartbeat_interval
self.node_heartbeat_threshold = node_heartbeat_threshold
self.log_level = log_level
self.log_target = log_target
self.log_is_quiet = log_is_quiet
Expand All @@ -53,6 +57,8 @@ def serialize(self) -> str:
return f"""[bluechi-controller]
ControllerPort={self.port}
AllowedNodeNames={allowed_node_names}
HeartbeatInterval={self.heartbeat_interval}
NodeHeartbeatThreshold={self.node_heartbeat_threshold}
LogLevel={self.log_level}
LogTarget={self.log_target}
LogIsQuiet={self.log_is_quiet}
Expand Down
3 changes: 3 additions & 0 deletions tests/tests/tier0/bluechi-heartbeat-disabled/main.fmf
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
summary: Test if default configuration of controller disables periodic heartbeat of
the controller
id: a04517f6-8be1-468b-8dc0-f9674690f73f
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#
# Copyright Contributors to the Eclipse BlueChi project
#
# SPDX-License-Identifier: LGPL-2.1-or-later

import time
import unittest

from dasbus.typing import Variant

from bluechi.api import Node


class TestNodeIsConnected(unittest.TestCase):
def test_node_is_connected(self):

def on_state_change(state: Variant):
assert False

n = Node("node-foo")
assert n.status == "online"

n.on_status_changed(on_state_change)
timestamp = n.last_seen_timestamp

# verify that the node remains connected and LastSeenTimespamp is not
# updated after more than NodeHeartbeatThreshold seconds have elapsed
time.sleep(10)

assert n.status == "online"
assert n.last_seen_timestamp == timestamp


if __name__ == "__main__":
unittest.main()
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#
# 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]):
result, output = ctrl.run_python(os.path.join("python", "is_node_connected.py"))
if result != 0:
raise Exception(output)


def test_bluechi_heartbeat_disabled(
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.heartbeat_interval = "0"

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

bluechi_test.run(exec)
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
summary: Test if the node gets disconnected when did not receive heartbeat since
threshold from node
id: 0b087ba3-25fc-46b0-9c01-31bc2edf5209
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 Node


class TestNodeIsDisconnected(unittest.TestCase):

node_state = None
timestamp = None

def test_node_is_disconnected(self):
loop = EventLoop()

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

n = Node("node-foo")
assert n.status == "online"

n.on_status_changed(on_state_change)
timestamp = n.last_seen_timestamp

loop.run()

# verify that the node is disconnected after more than
# NodeHeartbeatThreshold seconds have elapsed
assert self.timestamp - timestamp > 6000000
assert self.node_state == "offline"


if __name__ == "__main__":
unittest.main()
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#
# 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]):
result, output = ctrl.run_python(os.path.join("python", "is_node_disconnected.py"))
if result != 0:
raise Exception(output)


def test_bluechi_heartbeat_node_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_ctrl_default_config.heartbeat_interval = "2000"

bluechi_node_default_config.node_name = node_foo_name
bluechi_node_default_config.heartbeat_interval = "0"

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 37269b6

Please sign in to comment.