Skip to content

Commit 4927305

Browse files
committed
MINIFICPP-2685 Move MQTT tests to modular docker tests
1 parent c85588c commit 4927305

20 files changed

Lines changed: 518 additions & 341 deletions

File tree

behave_framework/src/minifi_test_framework/containers/container.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import shlex
2121
import tempfile
2222
import tarfile
23+
import uuid
2324

2425
import docker
2526
from docker.models.networks import Network
@@ -50,6 +51,23 @@ def __init__(self, image_name: str, container_name: str, network: Network, comma
5051
def add_host_file(self, host_path: str, container_path: str, mode: str = "ro"):
5152
self.host_files.append(HostFile(container_path, host_path, mode))
5253

54+
def add_file_to_running_container(self, content: str, path: str):
55+
if not self.container:
56+
logging.error("Container is not running. Cannot add file.")
57+
raise RuntimeError("Container is not running. Cannot add file.")
58+
59+
mkdir_command = f"mkdir -p {shlex.quote(path)}"
60+
exit_code, output = self.exec_run(mkdir_command)
61+
if exit_code != 0:
62+
logging.error(f"Error creating directory '{path}' in container: {output}")
63+
raise RuntimeError(f"Error creating directory '{path}' in container: {output}")
64+
65+
file_name = str(uuid.uuid4())
66+
exit_code, output = self.exec_run(f"sh -c \"printf %s '{content}' > {os.path.join(path, file_name)}\"")
67+
if exit_code != 0:
68+
logging.error(f"Error adding file to running container: {output}")
69+
raise RuntimeError(f"Error adding file to running container: {output}")
70+
5371
def _write_content_to_file(self, filepath: str, permissions: int | None, content: str | bytes):
5472
write_mode = "w"
5573
if isinstance(content, bytes):
@@ -74,7 +92,13 @@ def _configure_volumes_of_container_dirs(self):
7492
self._write_content_to_file(file_path, None, content)
7593
self.volumes[temp_path] = {"bind": directory.path, "mode": directory.mode}
7694

95+
def is_deployed(self) -> bool:
96+
return self.container is not None
97+
7798
def deploy(self) -> bool:
99+
if self.is_deployed():
100+
logging.info(f"Container '{self.container_name}' is already deployed.")
101+
return True
78102
self._temp_dir = tempfile.TemporaryDirectory()
79103
self._configure_volumes_of_container_files()
80104
self._configure_volumes_of_container_dirs()

behave_framework/src/minifi_test_framework/steps/checking_steps.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,18 @@ def step_impl(context: MinifiTestContext, content: str, path: str, duration: str
3636
timeout_seconds=timeout_in_seconds, bail_condition=lambda: context.get_default_minifi_container().exited, context=context)
3737

3838

39-
@then('a single file with the content "{content}" is placed in the "{directory}" directory in less than {duration}')
40-
def step_impl(context: MinifiTestContext, content: str, directory: str, duration: str):
39+
@then('in the "{container_name}" container a single file with the content "{content}" is placed in the "{directory}" directory in less than {duration}')
40+
def step_impl(context: MinifiTestContext, container_name: str, content: str, directory: str, duration: str):
4141
new_content = content.replace("\\n", "\n")
4242
timeout_in_seconds = humanfriendly.parse_timespan(duration)
4343
assert wait_for_condition(
44-
condition=lambda: context.get_default_minifi_container().directory_has_single_file_with_content(directory, new_content),
45-
timeout_seconds=timeout_in_seconds, bail_condition=lambda: context.get_default_minifi_container().exited, context=context)
44+
condition=lambda: context.get_minifi_container(container_name).directory_has_single_file_with_content(directory, new_content),
45+
timeout_seconds=timeout_in_seconds, bail_condition=lambda: context.get_minifi_container(container_name).exited, context=context)
46+
47+
48+
@then('a single file with the content "{content}" is placed in the "{directory}" directory in less than {duration}')
49+
def step_impl(context: MinifiTestContext, content: str, directory: str, duration: str):
50+
context.execute_steps(f'then in the "{DEFAULT_MINIFI_CONTAINER_NAME}" container a single file with the content "{content}" is placed in the "{directory}" directory in less than {duration}')
4651

4752

4853
@then('in the "{container_name}" container at least one file with the content "{content}" is placed in the "{directory}" directory in less than {duration}')
@@ -147,6 +152,7 @@ def step_impl(context: MinifiTestContext, directory: str, regex_str: str, durati
147152

148153

149154
@then('files with contents "{content_one}" and "{content_two}" are placed in the "{directory}" directory in less than {timeout}')
155+
@then("files with contents '{content_one}' and '{content_two}' are placed in the '{directory}' directory in less than {timeout}")
150156
def step_impl(context: MinifiTestContext, directory: str, timeout: str, content_one: str, content_two: str):
151157
timeout_seconds = humanfriendly.parse_timespan(timeout)
152158
c1 = content_one.replace("\\n", "\n")

behave_framework/src/minifi_test_framework/steps/core_steps.py

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,10 +62,27 @@ def step_impl(context: MinifiTestContext, file_name: str, content: str, path: st
6262
context.get_or_create_default_minifi_container().files.append(File(os.path.join(path, file_name), new_content))
6363

6464

65-
@step('a file with the content "{content}" is present in "{path}"')
65+
@given('a file with the content "{content}" is present in "{path}" in the "{container_name}" flow')
66+
def step_impl(context: MinifiTestContext, content: str, path: str, container_name: str):
67+
new_content = content.replace("\\n", "\n")
68+
context.get_or_create_minifi_container(container_name).files.append(File(os.path.join(path, str(uuid.uuid4())), new_content))
69+
70+
71+
@given('a file with the content "{content}" is present in "{path}"')
72+
@given("a file with the content '{content}' is present in '{path}'")
6673
def step_impl(context: MinifiTestContext, content: str, path: str):
74+
context.execute_steps(f"given a file with the content \"{content}\" is present in \"{path}\" in the \"{DEFAULT_MINIFI_CONTAINER_NAME}\" flow")
75+
76+
77+
@when('a file with the content "{content}" is placed in "{path}" in the "{container_name}" flow')
78+
def step_impl(context: MinifiTestContext, content: str, path: str, container_name: str):
6779
new_content = content.replace("\\n", "\n")
68-
context.get_or_create_default_minifi_container().files.append(File(os.path.join(path, str(uuid.uuid4())), new_content))
80+
context.get_minifi_container(container_name).add_file_to_running_container(new_content, path)
81+
82+
83+
@when('a file with the content "{content}" is placed in "{path}"')
84+
def step_impl(context: MinifiTestContext, content: str, path: str):
85+
context.execute_steps(f"when a file with the content \"{content}\" is placed in \"{path}\" in the \"{DEFAULT_MINIFI_CONTAINER_NAME}\" flow")
6986

7087

7188
@given("an empty file is present in \"{path}\"")
@@ -95,11 +112,31 @@ def step_impl(context: MinifiTestContext):
95112
context.get_or_create_default_minifi_container().stop()
96113

97114

115+
@when("\"{container_name}\" flow is stopped")
116+
def step_impl(context: MinifiTestContext, container_name: str):
117+
context.get_or_create_minifi_container(container_name).stop()
118+
119+
98120
@when("MiNiFi is restarted")
99121
def step_impl(context: MinifiTestContext):
100122
context.get_or_create_default_minifi_container().restart()
101123

102124

125+
@when("\"{container_name}\" flow is restarted")
126+
def step_impl(context: MinifiTestContext, container_name: str):
127+
context.get_or_create_minifi_container(container_name).restart()
128+
129+
130+
@when("\"{container_name}\" flow is started")
131+
def step_impl(context: MinifiTestContext, container_name: str):
132+
context.get_or_create_minifi_container(container_name).start()
133+
134+
135+
@when("\"{container_name}\" flow is killed")
136+
def step_impl(context: MinifiTestContext, container_name: str):
137+
context.get_or_create_minifi_container(container_name).kill()
138+
139+
103140
@given("OpenSSL FIPS mode is enabled in MiNiFi")
104141
def step_impl(context: MinifiTestContext):
105142
context.get_or_create_default_minifi_container().enable_openssl_fips_mode()
@@ -145,3 +182,8 @@ def step_impl(context: MinifiTestContext):
145182
def step_impl(context: MinifiTestContext):
146183
context.containers["minifi-c2-server"] = MinifiC2Server(context)
147184
assert context.containers["minifi-c2-server"].deploy()
185+
186+
187+
@step("{duration} later")
188+
def step_impl(context: MinifiTestContext, duration: str):
189+
time.sleep(humanfriendly.parse_timespan(duration))

behave_framework/src/minifi_test_framework/steps/flow_building_steps.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,10 +66,16 @@ def step_impl(context: MinifiTestContext, processor_type: str, property_name: st
6666
context.get_or_create_minifi_container(minifi_container_name).flow_definition.add_processor(processor)
6767

6868

69+
@given('a {processor_type} processor with the name "{processor_name}" in the "{minifi_container_name}" flow')
70+
def step_impl(context: MinifiTestContext, processor_type: str, processor_name: str, minifi_container_name: str):
71+
processor = Processor(processor_type, processor_name)
72+
context.get_or_create_minifi_container(minifi_container_name).flow_definition.add_processor(processor)
73+
74+
6975
@given('a {processor_type} processor with the name "{processor_name}"')
7076
def step_impl(context: MinifiTestContext, processor_type: str, processor_name: str):
71-
processor = Processor(processor_type, processor_name)
72-
context.get_or_create_default_minifi_container().flow_definition.add_processor(processor)
77+
context.execute_steps(
78+
f'given a {processor_type} processor with the name "{processor_name}" in the "{DEFAULT_MINIFI_CONTAINER_NAME}" flow')
7379

7480

7581
@given("a {processor_type} processor in the \"{minifi_container_name}\" flow")

docker/requirements.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,3 @@ azure-storage-blob==12.24.1
1010
prometheus-api-client==0.5.5
1111
humanfriendly==10.0
1212
requests<2.29 # https://github.com/docker/docker-py/issues/3113
13-
paho-mqtt==2.1.0

docker/test/integration/cluster/ContainerStore.py

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
from .containers.AzureStorageServerContainer import AzureStorageServerContainer
2222
from .containers.HttpProxyContainer import HttpProxyContainer
2323
from .containers.PostgreSQLServerContainer import PostgreSQLServerContainer
24-
from .containers.MqttBrokerContainer import MqttBrokerContainer
2524
from .containers.SyslogUdpClientContainer import SyslogUdpClientContainer
2625
from .containers.SyslogTcpClientContainer import SyslogTcpClientContainer
2726
from .containers.MinifiAsPodInKubernetesCluster import MinifiAsPodInKubernetesCluster
@@ -128,14 +127,6 @@ def acquire_container(self, context, container_name: str, engine='minifi-cpp', c
128127
network=self.network,
129128
image_store=self.image_store,
130129
command=command))
131-
elif engine == 'mqtt-broker':
132-
return self.containers.setdefault(container_name,
133-
MqttBrokerContainer(feature_context=feature_context,
134-
name=container_name,
135-
vols=self.vols,
136-
network=self.network,
137-
image_store=self.image_store,
138-
command=command))
139130
elif engine == "syslog-udp-client":
140131
return self.containers.setdefault(container_name,
141132
SyslogUdpClientContainer(

docker/test/integration/cluster/DockerTestCluster.py

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
from .checkers.PostgresChecker import PostgresChecker
2424
from .checkers.PrometheusChecker import PrometheusChecker
2525
from .checkers.ModbusChecker import ModbusChecker
26-
from .checkers.MqttHelper import MqttHelper
2726
from utils import get_peak_memory_usage, get_minifi_pid, get_memory_usage
2827

2928

@@ -37,7 +36,6 @@ def __init__(self, context, feature_id):
3736
self.postgres_checker = PostgresChecker(self.container_communicator)
3837
self.prometheus_checker = PrometheusChecker()
3938
self.modbus_checker = ModbusChecker(self.container_communicator)
40-
self.mqtt_helper = MqttHelper()
4139

4240
def cleanup(self):
4341
self.container_store.cleanup()
@@ -278,6 +276,3 @@ def set_value_on_plc_with_modbus(self, container_name, modbus_cmd):
278276

279277
def enable_ssl_in_nifi(self):
280278
self.container_store.enable_ssl_in_nifi()
281-
282-
def publish_test_mqtt_message(self, topic: str, message: str):
283-
self.mqtt_helper.publish_test_mqtt_message(topic, message)

docker/test/integration/cluster/ImageStore.py

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,6 @@ def get_image(self, container_engine):
6363
image = self.__build_http_proxy_image()
6464
elif container_engine == "postgresql-server":
6565
image = self.__build_postgresql_server_image()
66-
elif container_engine == "mqtt-broker":
67-
image = self.__build_mqtt_broker_image()
6866
else:
6967
raise Exception("There is no associated image for " + container_engine)
7068

@@ -277,15 +275,6 @@ def __build_postgresql_server_image(self):
277275
""".format(base_image='postgres:17.4'))
278276
return self.__build_image(dockerfile)
279277

280-
def __build_mqtt_broker_image(self):
281-
dockerfile = dedent("""\
282-
FROM {base_image}
283-
RUN echo 'log_dest stderr' >> /mosquitto-no-auth.conf
284-
CMD ["/usr/sbin/mosquitto", "--verbose", "--config-file", "/mosquitto-no-auth.conf"]
285-
""".format(base_image='eclipse-mosquitto:2.0.14'))
286-
287-
return self.__build_image(dockerfile)
288-
289278
def __build_image(self, dockerfile, context_files=[]):
290279
conf_dockerfile_buffer = BytesIO()
291280
docker_context_buffer = BytesIO()

docker/test/integration/cluster/containers/MqttBrokerContainer.py

Lines changed: 0 additions & 40 deletions
This file was deleted.

docker/test/integration/features/MiNiFi_integration_test_driver.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -389,6 +389,3 @@ def set_value_on_plc_with_modbus(self, container_name, modbus_cmd):
389389

390390
def enable_ssl_in_nifi(self):
391391
self.cluster.enable_ssl_in_nifi()
392-
393-
def publish_test_mqtt_message(self, topic, message):
394-
self.cluster.publish_test_mqtt_message(topic, message)

0 commit comments

Comments
 (0)