Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions behave_framework/pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
[project]
name = "minifi-test-framework"
version = "0.1.0"
version = "0.2.0"
requires-python = ">= 3.10"
description = "A testing framework for MiNiFi extensions."
dependencies = [
"behavex==4.6.0",
"docker==7.1.0",
"PyYAML==6.0.3",
"humanfriendly==10.0",
"m2crypto==0.46.2",
"pyopenssl==26.0.0",
"cryptography==46.0.5",
"pyjks==20.0.0"
]

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,29 +15,31 @@
# limitations under the License.
#
from __future__ import annotations
from typing import TYPE_CHECKING

import json
import logging
import os
import shlex
import tempfile
import tarfile
import tempfile
import uuid

import docker
from docker.models.networks import Network

from minifi_test_framework.containers.container_protocol import ContainerProtocol
from minifi_test_framework.containers.directory import Directory
from minifi_test_framework.containers.file import File
from minifi_test_framework.containers.host_file import HostFile
from typing import TYPE_CHECKING

import docker

if TYPE_CHECKING:
from minifi_test_framework.core.minifi_test_context import MinifiTestContext


class Container:
def __init__(self, image_name: str, container_name: str, network: Network, command: str | None = None, entrypoint: str | None = None):
class LinuxContainer(ContainerProtocol):
def __init__(self, image_name: str, container_name: str, network: Network, command: str | None = None,
entrypoint: str | None = None):
super().__init__()
self.image_name: str = image_name
self.container_name: str = container_name
self.network: Network = network
Expand Down Expand Up @@ -121,10 +123,11 @@ def deploy(self, context: MinifiTestContext | None) -> bool:
pass
try:
logging.info(f"Creating and starting container '{self.container_name}'...")
self.container = self.client.containers.run(
image=self.image_name, name=self.container_name, ports=self.ports,
environment=self.environment, volumes=self.volumes, network=self.network.name,
command=self.command, entrypoint=self.entrypoint, user=self.user, detach=True)
self.container = self.client.containers.run(image=self.image_name, name=self.container_name,
ports=self.ports, environment=self.environment,
volumes=self.volumes, network=self.network.name,
command=self.command, entrypoint=self.entrypoint,
user=self.user, detach=True)
except Exception as e:
logging.error(f"Error starting container: {e}")
raise
Expand Down Expand Up @@ -212,7 +215,8 @@ def directory_contains_file_with_regex(self, directory_path: str, regex_str: str
exit_code, output = self.exec_run("sh -c {}".format(shlex.quote(command)))

if exit_code != 0:
logging.debug("While looking for regex %s in directory %s, grep returned exit code %d, output: %s", regex_str, directory_path, exit_code, output)
logging.debug("While looking for regex %s in directory %s, grep returned exit code %d, output: %s",
regex_str, directory_path, exit_code, output)
return exit_code == 0

def path_with_content_exists(self, path: str, content: str) -> bool:
Expand Down Expand Up @@ -376,7 +380,8 @@ def _extract_directory_from_container(self, directory_path: str, temp_dir: str)

return os.path.join(temp_dir, os.path.basename(directory_path.strip('/')))
except Exception as e:
logging.error(f"Error extracting files from directory path '{directory_path}' from container '{self.container_name}': {e}")
logging.error(
f"Error extracting files from directory path '{directory_path}' from container '{self.container_name}': {e}")
return None

def _read_files_from_directory(self, directory_path: str) -> list[str] | None:
Expand Down Expand Up @@ -479,3 +484,11 @@ def directory_contains_file_with_minimum_size(self, directory_path: str, expecte
return True

return False

def get_memory_usage(self) -> int | None:
exit_code, output = self.exec_run(["awk", "/VmRSS/ { printf \"%d\\n\", $2 }", "/proc/1/status"])
if exit_code != 0:
return None
memory_usage_in_bytes = int(output.strip()) * 1024
logging.info(f"{self.container_name} memory usage: {memory_usage_in_bytes} bytes")
return memory_usage_in_bytes
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements. See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
# the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from typing import Protocol, List

from minifi_test_framework.containers.directory import Directory
from minifi_test_framework.containers.file import File
from minifi_test_framework.containers.host_file import HostFile


class ContainerProtocol(Protocol):
image_name: str
container_name: str
dirs: List[Directory]
files: List[File]
host_files: List[HostFile]

def deploy(self, context) -> bool:
...

def clean_up(self):
...

def exec_run(self, command):
...

def directory_contains_file_with_content(self, directory_path: str, expected_content: str) -> bool:
...

def directory_contains_file_with_regex(self, directory_path: str, regex_str: str) -> bool:
...

def path_with_content_exists(self, path: str, content: str) -> bool:
...

def get_logs(self) -> str:
...

@property
def exited(self) -> bool:
...

def get_number_of_files(self, directory_path: str) -> int:
...

def verify_file_contents(self, directory_path: str, expected_contents: list[str]) -> bool:
...

def log_app_output(self) -> bool:
...
Loading
Loading