2025-07-14 18:47:25 +02:00
|
|
|
import unittest
|
|
|
|
|
from unittest.mock import patch
|
|
|
|
|
|
2025-12-26 18:13:26 +01:00
|
|
|
from baudolo.backup.app import requires_stop
|
|
|
|
|
|
|
|
|
|
|
2026-07-11 08:34:36 +02:00
|
|
|
@patch("baudolo.backup.app.is_swarm_task", return_value=False)
|
2025-12-26 18:13:26 +01:00
|
|
|
class TestRequiresStop(unittest.TestCase):
|
|
|
|
|
@patch("baudolo.backup.app.get_image_info")
|
2025-12-28 22:12:31 +01:00
|
|
|
def test_requires_stop_false_when_all_images_are_whitelisted(
|
2026-07-11 08:34:36 +02:00
|
|
|
self, mock_get_image_info, _mock_is_swarm_task
|
2025-12-28 22:12:31 +01:00
|
|
|
):
|
2025-12-26 18:13:26 +01:00
|
|
|
mock_get_image_info.side_effect = [
|
|
|
|
|
"repo/mastodon:v4",
|
|
|
|
|
"repo/wordpress:latest",
|
|
|
|
|
]
|
|
|
|
|
containers = ["c1", "c2"]
|
2026-07-12 18:19:20 +02:00
|
|
|
whitelist = ["repo/mastodon:v4", "repo/wordpress:latest"]
|
2025-12-26 18:13:26 +01:00
|
|
|
self.assertFalse(requires_stop(containers, whitelist))
|
|
|
|
|
|
|
|
|
|
@patch("baudolo.backup.app.get_image_info")
|
2025-12-28 22:12:31 +01:00
|
|
|
def test_requires_stop_true_when_any_image_is_not_whitelisted(
|
2026-07-11 08:34:36 +02:00
|
|
|
self, mock_get_image_info, _mock_is_swarm_task
|
2025-12-28 22:12:31 +01:00
|
|
|
):
|
2025-12-26 18:13:26 +01:00
|
|
|
mock_get_image_info.side_effect = [
|
|
|
|
|
"repo/mastodon:v4",
|
|
|
|
|
"repo/nginx:latest",
|
|
|
|
|
]
|
|
|
|
|
containers = ["c1", "c2"]
|
2026-07-12 18:19:20 +02:00
|
|
|
whitelist = ["repo/mastodon:v4", "repo/wordpress:latest"]
|
2025-12-26 18:13:26 +01:00
|
|
|
self.assertTrue(requires_stop(containers, whitelist))
|
|
|
|
|
|
2026-07-12 18:19:20 +02:00
|
|
|
@patch("baudolo.backup.app.get_image_info")
|
|
|
|
|
def test_requires_stop_true_on_substring_only_match(
|
|
|
|
|
self, mock_get_image_info, _mock_is_swarm_task
|
|
|
|
|
):
|
|
|
|
|
mock_get_image_info.return_value = "reg:5000/repo/mastodon:v4"
|
|
|
|
|
self.assertTrue(requires_stop(["c1"], ["mastodon"]))
|
|
|
|
|
self.assertTrue(requires_stop(["c1"], ["repo/mastodon:v4"]))
|
|
|
|
|
|
2025-12-26 18:13:26 +01:00
|
|
|
@patch("baudolo.backup.app.get_image_info")
|
2026-07-11 08:34:36 +02:00
|
|
|
def test_requires_stop_true_when_whitelist_empty(
|
|
|
|
|
self, mock_get_image_info, _mock_is_swarm_task
|
|
|
|
|
):
|
2025-12-26 18:13:26 +01:00
|
|
|
mock_get_image_info.return_value = "repo/anything:latest"
|
|
|
|
|
self.assertTrue(requires_stop(["c1"], []))
|
|
|
|
|
|
|
|
|
|
|
2026-07-11 08:34:36 +02:00
|
|
|
class TestRequiresStopSwarm(unittest.TestCase):
|
|
|
|
|
@patch("baudolo.backup.app.get_image_info")
|
|
|
|
|
@patch("baudolo.backup.app.is_swarm_task", return_value=True)
|
|
|
|
|
def test_swarm_tasks_never_require_stop(
|
|
|
|
|
self, _mock_is_swarm_task, mock_get_image_info
|
|
|
|
|
):
|
|
|
|
|
mock_get_image_info.return_value = "repo/not-whitelisted:latest"
|
|
|
|
|
self.assertFalse(requires_stop(["c1"], []))
|
|
|
|
|
mock_get_image_info.assert_not_called()
|
|
|
|
|
|
|
|
|
|
|
2025-12-26 18:13:26 +01:00
|
|
|
if __name__ == "__main__":
|
2025-07-14 18:47:25 +02:00
|
|
|
unittest.main()
|