Make container-specific methods static

Make these methods callable without having to create an instance of the
Container class. This was needed to make pytest-wrapper.py cleaner.
This commit is contained in:
deeplow 2023-01-18 17:02:21 +00:00
parent f5c4847af2
commit 724dd2a71f
No known key found for this signature in database
GPG key ID: 577982871529A52A
2 changed files with 17 additions and 13 deletions

View file

@ -39,7 +39,8 @@ class Container(IsolationProvider):
def __init__(self) -> None: def __init__(self) -> None:
pass pass
def get_runtime_name(self) -> str: @staticmethod
def get_runtime_name() -> str:
if platform.system() == "Linux": if platform.system() == "Linux":
runtime_name = "podman" runtime_name = "podman"
else: else:
@ -47,25 +48,27 @@ class Container(IsolationProvider):
runtime_name = "docker" runtime_name = "docker"
return runtime_name return runtime_name
def get_runtime(self) -> str: @staticmethod
container_tech = self.get_runtime_name() def get_runtime() -> str:
container_tech = Container.get_runtime_name()
runtime = shutil.which(container_tech) runtime = shutil.which(container_tech)
if runtime is None: if runtime is None:
raise NoContainerTechException(container_tech) raise NoContainerTechException(container_tech)
return runtime return runtime
def install(self) -> bool: @staticmethod
def install() -> bool:
""" """
Make sure the podman container is installed. Linux only. Make sure the podman container is installed. Linux only.
""" """
if self.is_container_installed(): if Container.is_container_installed():
return True return True
# Load the container into podman # Load the container into podman
log.info("Installing Dangerzone container image...") log.info("Installing Dangerzone container image...")
p = subprocess.Popen( p = subprocess.Popen(
[self.get_runtime(), "load"], [Container.get_runtime(), "load"],
stdin=subprocess.PIPE, stdin=subprocess.PIPE,
startupinfo=get_subprocess_startupinfo(), startupinfo=get_subprocess_startupinfo(),
) )
@ -82,14 +85,15 @@ class Container(IsolationProvider):
break break
p.communicate() p.communicate()
if not self.is_container_installed(): if not Container.is_container_installed():
log.error("Failed to install the container image") log.error("Failed to install the container image")
return False return False
log.info("Container image installed") log.info("Container image installed")
return True return True
def is_container_installed(self) -> bool: @staticmethod
def is_container_installed() -> bool:
""" """
See if the podman container is installed. Linux only. See if the podman container is installed. Linux only.
""" """
@ -101,12 +105,12 @@ class Container(IsolationProvider):
installed = False installed = False
found_image_id = subprocess.check_output( found_image_id = subprocess.check_output(
[ [
self.get_runtime(), Container.get_runtime(),
"image", "image",
"list", "list",
"--format", "--format",
"{{.ID}}", "{{.ID}}",
self.CONTAINER_NAME, Container.CONTAINER_NAME,
], ],
text=True, text=True,
startupinfo=get_subprocess_startupinfo(), startupinfo=get_subprocess_startupinfo(),
@ -122,7 +126,7 @@ class Container(IsolationProvider):
try: try:
subprocess.check_output( subprocess.check_output(
[self.get_runtime(), "rmi", "--force", found_image_id], [Container.get_runtime(), "rmi", "--force", found_image_id],
startupinfo=get_subprocess_startupinfo(), startupinfo=get_subprocess_startupinfo(),
) )
except: except:

View file

@ -17,7 +17,7 @@ import sys
import pytest import pytest
from pkg_resources import parse_version from pkg_resources import parse_version
from dangerzone.container import get_runtime_name from dangerzone.isolation_provider.container import Container
PODMAN_MIN_VERSION = "4.3.0" PODMAN_MIN_VERSION = "4.3.0"
@ -44,7 +44,7 @@ if __name__ == "__main__":
pytest_args = sys.argv[1:] # exclude program names pytest_args = sys.argv[1:] # exclude program names
if get_runtime_name() == "docker": if Container.get_runtime_name() == "docker":
run_tests_in_parallel(pytest_args) run_tests_in_parallel(pytest_args)
else: else:
podman_ver_minimum_parallel = parse_version(PODMAN_MIN_VERSION) podman_ver_minimum_parallel = parse_version(PODMAN_MIN_VERSION)