diff --git a/CHANGELOG.md b/CHANGELOG.md index 7169c97..f9ac2da 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,11 @@ # Change Log +## [0.7.1] + +### Fixes + +- [cli] missing inline override for setting executor and set default for the executor on program config + ## [0.7.0] ### Features and Enhancements diff --git a/README.md b/README.md index 7428d5d..e19a5a3 100644 --- a/README.md +++ b/README.md @@ -228,7 +228,7 @@ You can find the some of the samples in [examples](./examples/). | .programs[name].image | | container image that will be loaded, this also will be set as a reference of image name for the build/custom one | `yes` | | .programs[name].exec | | path of executeable inside container that will be ran as **entrypoint**, this is will be the main one | `yes` | | .programs[name].extends | `[]` | extending from another program config, ensure the config name is exists | `no` | -| .programs[name].executor | | set the container executor | `no` | +| .programs[name].executor | `default` | set the container executor | `no` | | .programs[name].aliases | `{}` | the maps of any other executeable inside container, during subcommand **alias** by the argument **--generate**, this will generate alias by pattern "[program_name]-[alias]" | `no` | | .programs[name].interactive | `True` | interactive mode (**-it** ~> keep STDIN and provide pseudo TTY ) | `no` | | .programs[name].allow_home_dir | `False` | allow ran in (top of) home directory if auto sandbox mount enabled | `no` | diff --git a/sandock/cli.py b/sandock/cli.py index 3f8afd8..fe3b1fb 100644 --- a/sandock/cli.py +++ b/sandock/cli.py @@ -8,7 +8,7 @@ from importlib.metadata import metadata from .config import MainConfig, load_config_file, main_config_finder from .config.program import Program -from .shared import log, SANDBOX_DEBUG_ENV, CONFIG_PATH_ENV, KV, run_shell +from .shared import log, SANDBOX_DEBUG_ENV, CONFIG_PATH_ENV, KV, run_shell, EXECUTOR_DEFAULT from .sandbox import SandboxExec from .volume import VolumeMgr from .exceptions import SandboxBaseException, SandboxExecConfig, SandboxVolumeExec @@ -358,6 +358,10 @@ def overrides_args(self) -> ArgumentParser: self.override_arg(name="network"), default=None, help="override network" ) + oparser.add_argument( + self.override_arg(name="executor"), default=None, help="override executor" + ) + oparser.add_argument( self.override_arg(name="allow-home-dir"), action="store_true", @@ -459,7 +463,7 @@ def executor_cls(self) -> Type[SandboxExec]: return sandbox class that will be use """ program_exec = self.program_cfg.executor - if not program_exec: + if program_exec == EXECUTOR_DEFAULT: return SandboxExec executor = self.config.executors.get(program_exec) diff --git a/sandock/config/program.py b/sandock/config/program.py index 29e8531..a0084da 100644 --- a/sandock/config/program.py +++ b/sandock/config/program.py @@ -1,6 +1,7 @@ from dataclasses import dataclass, field from typing import List, Optional, Dict from .image import ImageBuild +from ..shared import EXECUTOR_DEFAULT from ._helpers import build_if_set @@ -43,13 +44,13 @@ class Program(object): interactive: bool = True allow_home_dir: bool = False name: Optional[str] = None - executor: Optional[str] = None network: Optional[str] = None hostname: Optional[str] = None build: Optional[ImageBuild] = None user: Optional[ContainerUser] = None workdir: Optional[str] = None platform: Optional[str] = None + executor: str = EXECUTOR_DEFAULT persist: PersistContainer = field(default_factory=PersistContainer) sandbox_mount: SandboxMount = field(default_factory=SandboxMount) env: Dict[str, str] = field(default_factory=dict) diff --git a/sandock/sandbox.py b/sandock/sandbox.py index f6ee2bc..056dfe6 100644 --- a/sandock/sandbox.py +++ b/sandock/sandbox.py @@ -8,7 +8,7 @@ from .config import MainConfig from .config.program import Program from .config.image import ImageBuild, DEFAULT_DUMP_IMAGE_STORE -from .shared import log, run_shell, file_hash, ensure_home_dir_special_prefix, KV +from .shared import log, run_shell, file_hash, ensure_home_dir_special_prefix, KV, EXECUTOR_DEFAULT from .exceptions import SandboxExecution VOL_LABEL_CREATED_BY = "created_by.sandock" @@ -58,15 +58,15 @@ def hook_recreate_img(self, execute: bool=False) -> None: @property def docker_bin(self) -> str: default_bin = self.cfg.execution.docker_bin - custom_executor = self.program.executor - if not custom_executor: + executor = self.program.executor + if executor == EXECUTOR_DEFAULT: return default_bin - executor = self.cfg.executors.get(custom_executor) - if not executor: - raise SandboxExecution(f"Executor `{custom_executor}` is not defined") + executor_cfg = self.cfg.executors.get(executor) + if not executor_cfg: + raise SandboxExecution(f"Executor `{executor}` is not defined") - return executor.bin_path if executor.bin_path else default_bin + return executor_cfg.bin_path if executor_cfg.bin_path else default_bin @property def current_timestamp(self) -> float: diff --git a/sandock/shared.py b/sandock/shared.py index 1f13ddf..6b84122 100644 --- a/sandock/shared.py +++ b/sandock/shared.py @@ -7,6 +7,7 @@ KV = Dict[str, Any] +EXECUTOR_DEFAULT = "default" CONFIG_PATH_ENV = "SNDK_CFG" SANDBOX_DEBUG_ENV = "SNDK_DEBUG" FETCH_PROP_ENABLE_ENV = "SNDK_FETCH_PROP" diff --git a/tests/test_cli.py b/tests/test_cli.py index 5097a39..b09ea61 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -211,7 +211,6 @@ def test_executor_cls_custom_cls(self) -> None: ) as o: self.assertEqual(o.executor_cls, AppleContainerExec) - # TODO: [x] validation: program is not defined def test_init_program_not_defined(self) -> None: """ the provided program is not listed on configuration @@ -220,7 +219,6 @@ def test_init_program_not_defined(self) -> None: with self.obj(args=Namespace(program="another_app")): pass - # TODO: [x] validation: name of persist command canot be overrided def test_apply_overrides_persist_cannot_overrided(self) -> None: with self.assertRaisesRegex( SandboxExecConfig, "name of persist program cannot be overrided" @@ -232,7 +230,6 @@ def test_apply_overrides_persist_cannot_overrided(self) -> None: o.apply_overrides() - # TODO: [x] running hook test (recreate_img) @mock.patch("sandock.cli.SandboxExec") def test_main_hook_recreate_img(self, sandbox_exec_mock: MagicMock) -> None: remote = MagicMock() @@ -259,13 +256,16 @@ def test_main_overrided_params(self, sandbox_exec_mock: MagicMock) -> None: "--sandbox-arg-hostname=change_host", "--sandbox-arg-ports=8080:8080", "--sandbox-arg-ports=8081:8081", + "--sandbox-arg-executor=podman", "--version"] with self.obj( ns_props=dict(program_args=provided_args), + cfg=dummy_main_cfg( + executors={"podman": {"bin_path": "podman"}} + ) ) as o: o.main() - # TODO: [x] testing override parameters self.assertEqual( sandbox_exec_mock.call_args[1]["program"], dummy_program_cfg( @@ -273,7 +273,8 @@ def test_main_overrided_params(self, sandbox_exec_mock: MagicMock) -> None: ports=[ "8080:8080", "8081:8081" - ] + ], + executor="podman" ), ) diff --git a/tests/test_config.py b/tests/test_config.py index 945359d..077f879 100644 --- a/tests/test_config.py +++ b/tests/test_config.py @@ -216,7 +216,7 @@ def test_defaults(self) -> None: self.assertTrue(o.interactive, True) self.assertFalse(o.allow_home_dir) self.assertIsNone(o.name) - self.assertIsNone(o.executor) + self.assertEqual(o.executor, "default") self.assertIsNone(o.network) self.assertIsNone(o.hostname) self.assertIsNone(o.user) diff --git a/tests/test_sandbox.py b/tests/test_sandbox.py index 8b9ddd7..849d0d8 100644 --- a/tests/test_sandbox.py +++ b/tests/test_sandbox.py @@ -58,8 +58,6 @@ def obj(self, program_kwargs: dict = {}, **kwargs) -> SandboxExec: return self.exec_cls(**kwargs) - # TODO: move this to cli test - # TODO: [x] keep to validate directory execution check def test_init_validations(self) -> None: with self.assertRaisesRegex( SandboxExecution,