1616#
1717
1818import logging
19+ import os
20+ from pathlib import Path
1921from OpenSSL import crypto
2022
2123from minifi_test_framework .core .minifi_test_context import MinifiTestContext
2224from minifi_test_framework .containers .file import File
25+ from minifi_test_framework .containers .host_file import HostFile
2326from minifi_test_framework .minifi .minifi_flow_definition import MinifiFlowDefinition
2427from minifi_test_framework .core .ssl_utils import make_cert_without_extended_usage , make_client_cert , make_server_cert
28+ from minifi_test_framework .core .helpers import wait_for_condition , retry_check
2529from .container import Container
2630
2731
@@ -47,6 +51,10 @@ def __init__(self, container_name: str, test_context: MinifiTestContext):
4751 crypto .dump_certificate (type = crypto .FILETYPE_PEM , cert = minifi_server_cert ) + crypto .dump_privatekey (type = crypto .FILETYPE_PEM , pkey = minifi_server_key )))
4852
4953 self .is_fhs = 'MINIFI_INSTALLATION_TYPE=FHS' in str (self .client .images .get (test_context .minifi_container_image ).history ())
54+ if self .is_fhs :
55+ self .minifi_controller_path = '/usr/bin/minifi-controller'
56+ else :
57+ self .minifi_controller_path = '/opt/minifi/minifi-current/bin/minifi-controller'
5058
5159 self ._fill_default_properties ()
5260 self ._fill_default_log_properties ()
@@ -66,7 +74,18 @@ def deploy(self) -> bool:
6674 self .files .append (File ("/opt/minifi/minifi-current/conf/minifi-log.properties" ,
6775 self ._get_log_properties_file_content ()))
6876
69- return super ().deploy ()
77+ resource_dir = Path (__file__ ).resolve ().parent / "resources" / "minifi-controller"
78+ self .host_files .append (HostFile ("/tmp/resources/minifi-controller/config.yml" , os .path .join (resource_dir , "config.yml" )))
79+
80+ if not super ().deploy ():
81+ return False
82+
83+ finished_str = "MiNiFi started"
84+ return wait_for_condition (
85+ condition = lambda : finished_str in self .get_logs (),
86+ timeout_seconds = 15 ,
87+ bail_condition = lambda : self .exited ,
88+ context = None )
7089
7190 def set_property (self , key : str , value : str ):
7291 self .properties [key ] = value
@@ -111,3 +130,74 @@ def get_memory_usage(self) -> int | None:
111130 memory_usage_in_bytes = int (output .strip ()) * 1024
112131 logging .info (f"MiNiFi memory usage: { memory_usage_in_bytes } bytes" )
113132 return memory_usage_in_bytes
133+
134+ def set_controller_socket_properties (self ):
135+ self .properties ["controller.socket.enable" ] = "true"
136+ self .properties ["controller.socket.host" ] = "localhost"
137+ self .properties ["controller.socket.port" ] = "9998"
138+ self .properties ["controller.socket.local.any.interface" ] = "false"
139+
140+ def update_flow_config_through_controller (self ):
141+ self .exec_run ([self .minifi_controller_path , "--updateflow" , "/tmp/resources/minifi-controller/config.yml" ])
142+
143+ def updated_config_is_persisted (self ) -> bool :
144+ exit_code , output = self .exec_run (["cat" , "/opt/minifi/minifi-current/conf/config.yml" if not self .is_fhs else "/etc/nifi-minifi-cpp/config.yml" ])
145+ if exit_code != 0 :
146+ logging .error ("Failed to read MiNiFi config file to check if updated config is persisted" )
147+ return False
148+ return "2f2a3b47-f5ba-49f6-82b5-bc1c86b96f38" in output
149+
150+ def stop_component_through_controller (self , component : str ):
151+ self .exec_run ([self .minifi_controller_path , "--stop" , component ])
152+
153+ def start_component_through_controller (self , component : str ):
154+ self .exec_run ([self .minifi_controller_path , "--start" , component ])
155+
156+ @retry_check (10 , 1 )
157+ def is_component_running (self , component : str ) -> bool :
158+ (code , output ) = self .exec_run ([self .minifi_controller_path , "--list" , "components" ])
159+ return code == 0 and component + ", running: true" in output
160+
161+ def get_connections (self ):
162+ (_ , output ) = self .exec_run ([self .minifi_controller_path , "--list" , "connections" ])
163+ connections = []
164+ for line in output .split ('\n ' ):
165+ if not line .startswith ('[' ) and not line .startswith ('Connection Names' ):
166+ connections .append (line )
167+ return connections
168+
169+ @retry_check (10 , 1 )
170+ def connection_found_through_controller (self , connection : str ) -> bool :
171+ return connection in self .get_connections ()
172+
173+ def get_full_connection_count (self ) -> int :
174+ (_ , output ) = self .exec_run ([self .minifi_controller_path , "--getfull" ])
175+ for line in output .split ('\n ' ):
176+ if "are full" in line :
177+ return int (line .split (' ' )[0 ])
178+ return - 1
179+
180+ def get_connection_size (self , connection : str ):
181+ (_ , output ) = self .exec_run ([self .minifi_controller_path , "--getsize" , connection ])
182+ for line in output .split ('\n ' ):
183+ if "Size/Max of " + connection in line :
184+ size_and_max = line .split (connection )[1 ].split ('/' )
185+ return (int (size_and_max [0 ].strip ()), int (size_and_max [1 ].strip ()))
186+ return (- 1 , - 1 )
187+
188+ def get_manifest (self ) -> str :
189+ (_ , output ) = self .exec_run ([self .minifi_controller_path , "--manifest" ])
190+ manifest = ""
191+ for line in output .split ('\n ' ):
192+ if not line .startswith ('[' ):
193+ manifest += line
194+ return manifest
195+
196+ def create_debug_bundle (self ) -> bool :
197+ (code , _ ) = self .exec_run ([self .minifi_controller_path , "--debug" , "/tmp" ])
198+ if code != 0 :
199+ logging .error ("Minifi controller debug command failed with code: %d" , code )
200+ return False
201+
202+ (code , _ ) = self .exec_run (["test" , "-f" , "/tmp/debug.tar.gz" ])
203+ return code == 0
0 commit comments