-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: add is_docker_rootless() in tutor-discovery
- 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
1 parent
403a5e2
commit b05698b
Showing
6 changed files
with
53 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |