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

View file

@ -17,7 +17,7 @@ import sys
import pytest
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"
@ -44,7 +44,7 @@ if __name__ == "__main__":
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)
else:
podman_ver_minimum_parallel = parse_version(PODMAN_MIN_VERSION)