lordgamez commented on code in PR #2059:
URL: https://github.com/apache/nifi-minifi-cpp/pull/2059#discussion_r2660973972


##########
behave_framework/src/minifi_test_framework/containers/container.py:
##########
@@ -254,6 +285,59 @@ def verify_file_contents(self, directory_path: str, 
expected_contents: list[str]
 
         return sorted(actual_file_contents) == sorted(expected_contents)
 
+    def _verify_file_contents_in_stopped_container(self, directory_path: str, 
expected_contents: list[str]) -> bool:
+        if not self.container:
+            return False
+
+        with tempfile.TemporaryDirectory() as temp_dir:
+            extracted_dir = 
self._extract_directory_from_container(directory_path, temp_dir)
+            if not extracted_dir:
+                return False
+
+            actual_file_contents = 
self._read_files_from_directory(extracted_dir)
+            if actual_file_contents is None:
+                return False
+
+            return sorted(actual_file_contents) == sorted(expected_contents)
+
+    def _extract_directory_from_container(self, directory_path: str, temp_dir: 
str) -> str | None:
+        try:
+            bits, _ = self.container.get_archive(directory_path)
+            temp_tar_path = os.path.join(temp_dir, "archive.tar")
+
+            with open(temp_tar_path, 'wb') as f:
+                for chunk in bits:
+                    f.write(chunk)
+
+            with tarfile.open(temp_tar_path) as tar:
+                tar.extractall(path=temp_dir)
+
+            return os.path.join(temp_dir, 
os.path.basename(directory_path.strip('/')))
+        except Exception as e:
+            logging.error(f"Error extracting files from container: {e}")
+            return None
+
+    def _read_files_from_directory(self, directory_path: str) -> list[str] | 
None:
+        try:
+            file_contents = []
+            for entry in os.scandir(directory_path):
+                if entry.is_file():
+                    with open(entry.path, 'r') as f:
+                        file_contents.append(f.read())
+            return file_contents
+        except Exception as e:
+            logging.error(f"Error reading extracted files: {e}")
+            return None
+
+    def verify_file_contents(self, directory_path: str, expected_contents: 
list[str]) -> bool:
+        if not self.container:
+            return False
+
+        if self.container.status == "running":
+            return 
self._verify_file_contents_in_running_container(directory_path, 
expected_contents)
+
+        return self._verify_file_contents_in_stopped_container(directory_path, 
expected_contents)

Review Comment:
   The directory scan will fail in that case as it cannot connect the 
non-running container. That should be okay, because we only expect the 
container to be stopped if we stopped it explicitly and that should had 
happened beforehand.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to