Skip to content

Commit

Permalink
added tests to make sure that server regex only deletes bibigrid serv…
Browse files Browse the repository at this point in the history
…ers with fitting cluster id and same for volumes
  • Loading branch information
XaverStiensmeier committed Nov 30, 2024
1 parent adf1095 commit 0ffa578
Showing 1 changed file with 95 additions and 1 deletion.
96 changes: 95 additions & 1 deletion tests/unit_tests/test_terminate_cluster.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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))

0 comments on commit 0ffa578

Please sign in to comment.