Skip to content

Commit

Permalink
chore: add is_docker_rootless() in tutor-discovery
Browse files Browse the repository at this point in the history
- move is_docker_rootless method from tutor to tutor-discovery
- move is_docker_rootless related tests from tutor to tutor-discovery and modify makefile according to it.
  • Loading branch information
Muhammad Faraz Maqsood authored and DawoudSheraz committed Nov 29, 2024
1 parent 403a5e2 commit b05698b
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 2 deletions.
7 changes: 5 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
.DEFAULT_GOAL := help
.PHONY: docs
SRC_DIRS = ./tutordiscovery
SRC_DIRS = ./tutordiscovery ./tests
BLACK_OPTS = --exclude templates ${SRC_DIRS}

# Warning: These checks are run on every PR.
test: test-lint test-types test-format # Run some static checks.
test: test-lint test-types test-format test-unit # Run some static checks.

test-format: ## Run code formatting tests.
black --check --diff $(BLACK_OPTS)
Expand All @@ -15,6 +15,9 @@ test-lint: ## Run code linting tests
test-types: ## Run type checks.
mypy --exclude=templates --ignore-missing-imports --implicit-reexport --strict ${SRC_DIRS}

test-unit: ## Run unit tests
python -m unittest discover tests

format: ## Format code automatically.
black $(BLACK_OPTS)

Expand Down
1 change: 1 addition & 0 deletions changelog.d/20241122_121506_faraz.maqsood_sumac.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- [Improvement] Move is_docker_rootless method related to elasticsearch from tutor core to tutor-discovery. (by @Faraz32123)
Empty file added tests/__init__.py
Empty file.
26 changes: 26 additions & 0 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import subprocess
import unittest
from unittest.mock import MagicMock, patch

from tutordiscovery import utils


class UtilsTests(unittest.TestCase):
@patch("subprocess.run")
def test_is_docker_rootless(self, mock_run: MagicMock) -> None:
# Mock rootless `docker info` output
utils.is_docker_rootless.cache_clear()
mock_run.return_value.stdout = "some prefix\n rootless foo bar".encode("utf-8")
self.assertTrue(utils.is_docker_rootless())

# Mock regular `docker info` output
utils.is_docker_rootless.cache_clear()
mock_run.return_value.stdout = "some prefix, regular docker".encode("utf-8")
self.assertFalse(utils.is_docker_rootless())

@patch("subprocess.run")
def test_is_docker_rootless_podman(self, mock_run: MagicMock) -> None:
"""Test the `is_docker_rootless` when podman is used or any other error with `docker info`"""
utils.is_docker_rootless.cache_clear()
mock_run.side_effect = subprocess.CalledProcessError(1, "docker info")
self.assertFalse(utils.is_docker_rootless())
5 changes: 5 additions & 0 deletions tutordiscovery/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from tutor.__about__ import __version_suffix__

from .__about__ import __version__
from .utils import is_docker_rootless

# Handle version suffix in main mode, just like tutor core
if __version_suffix__:
Expand Down Expand Up @@ -127,6 +128,10 @@ def _mount_course_discovery_on_build(
("discovery/apps", "plugins"),
],
)
# Template variables
tutor_hooks.Filters.ENV_TEMPLATE_VARIABLES.add_item(
("is_docker_rootless", is_docker_rootless),
)
# Load patches from files
for path in glob(
os.path.join(
Expand Down
16 changes: 16 additions & 0 deletions tutordiscovery/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import subprocess
from functools import lru_cache


@lru_cache(maxsize=None)
def is_docker_rootless() -> bool:
"""
A helper function to determine if Docker is running in rootless mode.
- https://docs.docker.com/engine/security/rootless/
"""
try:
results = subprocess.run(["docker", "info"], capture_output=True, check=True)
return "rootless" in results.stdout.decode()
except subprocess.CalledProcessError:
return False

0 comments on commit b05698b

Please sign in to comment.