Skip to content

Commit

Permalink
Test for -c option check if port is change
Browse files Browse the repository at this point in the history
Added an option to add open ports for spawned containers.
And also added a test to check if conf file change with
-c option will change the ports for agent and ctrl.

Related-to: #668
Signed-off-by: Artiom Divak <adivak@redhat.com>
  • Loading branch information
ArtiomDivak committed Jan 9, 2024
1 parent cbcd280 commit 4805b3d
Show file tree
Hide file tree
Showing 6 changed files with 89 additions and 3 deletions.
9 changes: 8 additions & 1 deletion tests/bluechi_test/fixtures.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,11 @@ def bluechi_node_default_config(bluechi_ctrl_svc_port: str):
manager_port=bluechi_ctrl_svc_port)


@pytest.fixture(scope='function')
def additional_ports():
return None


@pytest.fixture(scope='function')
def bluechi_test(
podman_client: PodmanClient,
Expand All @@ -109,7 +114,8 @@ def bluechi_test(
tmt_test_serial_number: str,
tmt_test_data_dir: str,
run_with_valgrind: bool,
run_with_coverage: bool):
run_with_coverage: bool,
additional_ports: dict):

return BluechiTest(
podman_client,
Expand All @@ -120,4 +126,5 @@ def bluechi_test(
tmt_test_data_dir,
run_with_valgrind,
run_with_coverage,
additional_ports
)
9 changes: 7 additions & 2 deletions tests/bluechi_test/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ def __init__(
tmt_test_serial_number: str,
tmt_test_data_dir: str,
run_with_valgrind: bool,
run_with_coverage: bool) -> None:
run_with_coverage: bool,
additional_ports: dict) -> None:

self.podman_client = podman_client
self.bluechi_image_id = bluechi_image_id
Expand All @@ -38,6 +39,7 @@ def __init__(
self.tmt_test_data_dir = tmt_test_data_dir
self.run_with_valgrind = run_with_valgrind
self.run_with_coverage = run_with_coverage
self.additional_ports = additional_ports

self.bluechi_controller_config: BluechiControllerConfig = None
self.bluechi_node_configs: List[BluechiNodeConfig] = []
Expand All @@ -61,11 +63,14 @@ def setup(self) -> Tuple[bool, Tuple[BluechiControllerContainer, Dict[str, Bluec
LOGGER.debug(f"Starting container for bluechi-controller with config:\
\n{self.bluechi_controller_config.serialize()}")

ports = {self.bluechi_ctrl_svc_port: self.bluechi_ctrl_host_port}
if self.additional_ports:
ports.update(self.additional_ports)
c = self.podman_client.containers.run(
name=f"{self.bluechi_controller_config.name}-{self.tmt_test_serial_number}",
image=self.bluechi_image_id,
detach=True,
ports={self.bluechi_ctrl_svc_port: self.bluechi_ctrl_host_port},
ports=ports,
)
c.wait(condition="running")

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[bluechi-agent]
ManagerPort=8421
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[bluechi-controller]
ManagerPort=8421
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
summary: Test chenging port with cmd -c option
id: 5645dcdf-acaa-4a04-8c0d-d478c8a6f2a3
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
# SPDX-License-Identifier: LGPL-2.1-or-later

from typing import Dict
import os
import time

from bluechi_test.test import BluechiTest
from bluechi_test.container import BluechiControllerContainer, BluechiNodeContainer
from bluechi_test.config import BluechiControllerConfig, BluechiNodeConfig
from bluechi_test.util import read_file

NODE_FOO = "node-foo"


def check_if_node_and_ctrl_connected(ctrl, node):
assert os.popen("lsof -i:8421").read()
assert "Registered managed node from" in ctrl.exec_run("systemctl status bluechi-controller.service")[1]
assert f"Connected to manager as '{NODE_FOO}'" in node.exec_run("systemctl status bluechi-agent.service")[1]


def check_if_node_disconnected(node):
time.sleep(1)
assert not os.popen("lsof -i:8420 | grep slirp4net").read()
assert "Disconnected from manager" in node.exec_run("systemctl status bluechi-agent.service")[1]



def exec(ctrl: BluechiControllerContainer, nodes: Dict[str, BluechiNodeContainer]):
node_foo = nodes[NODE_FOO]
config_file_location = "/var/tmp"
bluechi_agent_str = "bluechi-agent"
bluechi_controller_str = "bluechi-controller"
file_location_ctrl = os.path.join("config-files", "ctrl_port_8421.conf")
file_location_agent = os.path.join("config-files", "agent_port_8421.conf")

# Copying relevant config files into the nodes container
content = read_file(file_location_agent)
node_foo.create_file(config_file_location, file_location_agent, content)
content = read_file(file_location_ctrl)
ctrl.create_file(config_file_location, file_location_ctrl, content)

ctrl.restart_with_config_file(
os.path.join(config_file_location, "ctrl_port_8421.conf"), bluechi_controller_str)
assert ctrl.wait_for_unit_state_to_be(bluechi_controller_str, "active")
check_if_node_disconnected(node_foo)


node_foo.restart_with_config_file(
os.path.join(config_file_location, "agent_port_8421.conf"), bluechi_agent_str)
assert node_foo.wait_for_unit_state_to_be(bluechi_agent_str, "active")
check_if_node_and_ctrl_connected(ctrl, node_foo)


def test_agent_invalid_port_configuration(
bluechi_test: BluechiTest,
bluechi_node_default_config: BluechiNodeConfig, bluechi_ctrl_default_config: BluechiControllerConfig):

node_foo_cfg = bluechi_node_default_config.deep_copy()
node_foo_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_node_config(node_foo_cfg)

bluechi_test.additional_ports = {"8421": "8421"}

bluechi_test.run(exec)

0 comments on commit 4805b3d

Please sign in to comment.