-
Notifications
You must be signed in to change notification settings - Fork 190
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add unit tests to container_runtime salt module
- Loading branch information
1 parent
0bbca36
commit 2d82fd4
Showing
1 changed file
with
39 additions
and
0 deletions.
There are no files selected for viewing
39 changes: 39 additions & 0 deletions
39
susemanager-utils/susemanager-sls/src/tests/test_module_container_runtime.py
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,39 @@ | ||
""" | ||
Unit tests for the container_runtime module | ||
""" | ||
from ..modules import container_runtime | ||
|
||
from unittest.mock import patch, mock_open, MagicMock | ||
import pytest | ||
|
||
@pytest.mark.parametrize( | ||
"mock_read_file_return, mock_exists_return, expected_result", | ||
[ | ||
("docker", {"/proc/self/cgroup": True, "/.dockerenv": True}, "docker"), | ||
("", {"/proc/vz": True, "/proc/bc": False}, "openvz"), | ||
("podman", {"/run/.containerenv": True}, "podman"), | ||
("", {"/__runsc_containers__": True}, "gvisor"), | ||
("", {}, None), | ||
], | ||
) | ||
def test_get_container_runtime(mock_read_file_return, mock_exists_return, expected_result): | ||
mock_read_file = MagicMock(return_value=mock_read_file_return) | ||
mock_exists = MagicMock(side_effect=lambda path: mock_exists_return.get(path, False)) | ||
container_runtime._read_file = mock_read_file | ||
with patch("os.path.exists", mock_exists): | ||
assert container_runtime.get_container_runtime() == expected_result | ||
|
||
@pytest.mark.parametrize("file_name, expected_result", [ | ||
("/run/.containerenv", "podman"), | ||
("/.dockerenv", "docker"), | ||
("/var/run/secrets/kubernetes.io/serviceaccount", "kube"), | ||
]) | ||
def test_detect_container_files(file_name, expected_result): | ||
mock_exists = MagicMock(side_effect=lambda path: path == file_name) | ||
with patch("os.path.exists", mock_exists): | ||
assert container_runtime._detect_container_files() == expected_result | ||
|
||
def test_detect_container_files_not_found(): | ||
mock_exists = MagicMock(side_effect=lambda path: False) | ||
with patch("os.path.exists", mock_exists): | ||
assert container_runtime._detect_container_files() == "not-found" |