From 0ffa5786528d2ccd90b933a2c003ddf7668bcc92 Mon Sep 17 00:00:00 2001 From: XaverStiensmeier Date: Sat, 30 Nov 2024 15:42:14 +0100 Subject: [PATCH] added tests to make sure that server regex only deletes bibigrid servers with fitting cluster id and same for volumes --- tests/unit_tests/test_terminate_cluster.py | 96 +++++++++++++++++++++- 1 file changed, 95 insertions(+), 1 deletion(-) diff --git a/tests/unit_tests/test_terminate_cluster.py b/tests/unit_tests/test_terminate_cluster.py index 4a9e2342..6a045934 100644 --- a/tests/unit_tests/test_terminate_cluster.py +++ b/tests/unit_tests/test_terminate_cluster.py @@ -2,7 +2,7 @@ Module to test terminate """ from unittest import TestCase -from unittest.mock import MagicMock, patch +from unittest.mock import MagicMock, patch, call from bibigrid.core import startup from bibigrid.core.actions import create @@ -52,3 +52,97 @@ def test_terminate_none(self, _, mock_local): provider.delete_server.assert_not_called() provider.delete_keypair.assert_called_with( create.KEY_NAME.format(cluster_id=str(cluster_id))) # since keypair is not called + + def test_delete_non_pemanent_volumes(self): + cluster_id = "1234" + provider = MagicMock() + log = MagicMock() + cluster_id=21 + + # List of test volumes + volumes = [ + # Should be captured by the regex + {"name": f"bibigrid-master-{cluster_id}-tmp-0"}, + {"name": f"bibigrid-master-{cluster_id}-semiperm-0"}, + {"name": f"bibigrid-master-{cluster_id}-tmp-0-na<-0med"}, + {"name": f"bibigrid-master-{cluster_id}-semiperm-0-na<-0med"}, + {"name": f"bibigrid-worker-{cluster_id}-0-tmp-0"}, + {"name": f"bibigrid-worker-{cluster_id}-11-semiperm-0"}, + {"name": f"bibigrid-worker-{cluster_id}-0-tmp-0-na<-0med"}, + {"name": f"bibigrid-worker-{cluster_id}-11-semiperm-0-na<-0med"}, + + # Should NOT be captured by the regex + {"name": f"bibigrid-master-{cluster_id}-perm-0"}, + {"name": f"bibigrid-master-{cluster_id}-perm-11-na<-0med"}, + {"name": f"bibigrid-worker-{cluster_id}-112-perm-0"}, + {"name": f"bibigrid-worker-{cluster_id}-112-perm-11-na<-0med"}, + {"name": "somevolume"}, + {"name": "bibigrid-master-4242-0-tmp-0"}, + {"name": "bibigrid-master-4242-0-semiperm-0"}, + {"name": "bibigrid-master-4242-0-perm-0"}, + {"name": "bibigrid-worker-4242-0-tmp-0"}, + {"name": "bibigrid-worker-4242-0-semiperm-0"}, + {"name": "bibigrid-worker-4242-0-perm-0"}, + {"name": f"master-{cluster_id}-0-tmp-0"}, + {"name": f"master-{cluster_id}-0-semiperm-0"}, + {"name": f"master-{cluster_id}-0-perm-0"}, + ] + + provider.list_volumes.return_value = volumes + + # Call the method under test + _ = terminate.delete_non_permanent_volumes(provider, cluster_id, log) + + # Expected captured volumes + expected_calls = [call({'name': 'bibigrid-master-21-tmp-0'}), + call({'name': 'bibigrid-master-21-semiperm-0'}), + call({'name': 'bibigrid-master-21-tmp-0-na<-0med'}), + call({'name': 'bibigrid-master-21-semiperm-0-na<-0med'}), + call({'name': 'bibigrid-worker-21-0-tmp-0'}), + call({'name': 'bibigrid-worker-21-11-semiperm-0'}), + call({'name': 'bibigrid-worker-21-0-tmp-0-na<-0med'}), + call({'name': 'bibigrid-worker-21-11-semiperm-0-na<-0med'})] + + # Assert that the regex only captured the expected volumes + self.assertEqual(expected_calls, provider.delete_volume.call_args_list) + + def test_terminate_servers(self): + cluster_id = "21" + provider = MagicMock() + log = MagicMock() + + # List of test servers + servers = [ + # Should be captured by the regex + {"name": f"bibigrid-master-{cluster_id}", "id": 42}, + {"name": f"bibigrid-worker-{cluster_id}-0", "id": 42}, + {"name": f"bibigrid-worker-{cluster_id}-11", "id": 42}, + {"name": f"bibigrid-vpngtw-{cluster_id}-222", "id": 42}, + + # Should NOT be captured by the regex + {"name": "some-other-server", "id": 42}, + {"name": "bibigrid-master-4242", "id": 42}, + {"name": "bibigrid-worker-4242-0", "id": 42}, + {"name": "bibigrid-vpngtw-4242-0", "id": 42}, + ] + + provider.list_servers.return_value = servers + + # Patch terminate_server from bibigrid.core.actions.terminate + with patch("bibigrid.core.actions.terminate.terminate_server") as mock_terminate_server: + # Call the method under test + _ = terminate.terminate_servers(cluster_id, provider, log) + + # Expected captured servers + expected_calls = [ + call(provider, {"name": f"bibigrid-master-{cluster_id}", "id": 42}, log), + call(provider, {"name": f"bibigrid-worker-{cluster_id}-0", "id": 42}, log), + call(provider, {"name": f"bibigrid-worker-{cluster_id}-11", "id": 42}, log), + call(provider, {"name": f"bibigrid-vpngtw-{cluster_id}-222", "id": 42}, log), + ] + + # Assert that terminate_server was called only for the expected servers + mock_terminate_server.assert_has_calls(expected_calls, any_order=False) + + # Assert that the total number of calls matches the expected calls + self.assertEqual(mock_terminate_server.call_count, len(expected_calls))