Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(rescue_instance): added tests #636

Merged
merged 11 commits into from
Dec 3, 2024
9 changes: 5 additions & 4 deletions simple_vm_client/openstack_connector/openstack_connector.py
Original file line number Diff line number Diff line change
Expand Up @@ -1375,6 +1375,7 @@ def rescue_server(
self, openstack_id: str, admin_pass: str = None, image_ref: str = None
) -> None:
logger.info(f"Rescue Server {openstack_id}")

try:
server: Server = self.get_server(openstack_id=openstack_id)
if not server:
Expand All @@ -1384,11 +1385,11 @@ def rescue_server(
name_or_id=openstack_id,
)
self.openstack_connection.compute.rescue_server(
server.id, admin_pass, image_ref
server, admin_pass, image_ref
)

except ConflictException as e:
logger.error(f"Rescue Server {openstack_id} failed!")
logger.exception(f"Rescue Server {openstack_id} failed!")

raise OpenStackConflictException(message=e.message)

Expand All @@ -1404,11 +1405,11 @@ def unrescue_server(self, openstack_id: str) -> None:
)

self.openstack_connection.compute.unrescue_server(
server.id,
server
)

except ConflictException as e:
logger.error(f"Unrescue Server {openstack_id} failed!")
logger.exception(f"Unrescue Server {openstack_id} failed!")

raise OpenStackConflictException(message=e.message)

Expand Down
68 changes: 68 additions & 0 deletions simple_vm_client/test_openstack_connector.py
Original file line number Diff line number Diff line change
Expand Up @@ -2324,6 +2324,74 @@ def test_get_server_openstack_exception(self):
with self.assertRaises(DefaultException):
self.openstack_connector.get_server("someid")



@patch.object(OpenStackConnector, "get_server")
def test_rescue_server_success(self, mock_get_server):
# Arrange
server_mock = fakes.generate_fake_resource(server.Server)
mock_get_server.return_value = server_mock
# Act
self.openstack_connector.rescue_server(openstack_id="some_openstack_id")

# Assert
# Ensure the stop_server method is called with the correct server
self.openstack_connector.openstack_connection.compute.rescue_server.assert_called_once_with(
server_mock, None, None
)

@patch.object(OpenStackConnector, "get_server")
@patch("simple_vm_client.openstack_connector.openstack_connector.logger.exception")
def test_rescue_server_conflict_exception(
self, mock_logger_exception, mock_get_server
):
# Arrange
server_mock = fakes.generate_fake_resource(server.Server)
mock_get_server.return_value = server_mock
self.openstack_connector.openstack_connection.compute.rescue_server.side_effect = ConflictException
(
"Unit Test"
)
# Act
with self.assertRaises(OpenStackConflictException):
self.openstack_connector.rescue_server(openstack_id="some_openstack_id")
mock_logger_exception.assert_called_once_with(
"Rescue Server some_openstack_id failed!"
)

@patch.object(OpenStackConnector, "get_server")
def test_unrescue_server_success(self, mock_get_server):
# Arrange
server_mock = fakes.generate_fake_resource(server.Server)
mock_get_server.return_value = server_mock
# Act
self.openstack_connector.unrescue_server(openstack_id="some_openstack_id")

# Assert
# Ensure the stop_server method is called with the correct server
self.openstack_connector.openstack_connection.compute.unrescue_server.assert_called_once_with(
server_mock
)

@patch.object(OpenStackConnector, "get_server")
@patch("simple_vm_client.openstack_connector.openstack_connector.logger.exception")
def test_unrescue_server_conflict_exception(
self, mock_logger_exception, mock_get_server
):
# Arrange
server_mock = fakes.generate_fake_resource(server.Server)
mock_get_server.return_value = server_mock
self.openstack_connector.openstack_connection.compute.unrescue_server.side_effect = ConflictException(
"Unit Test"
)
# Act
with self.assertRaises(OpenStackConflictException):
self.openstack_connector.unrescue_server(openstack_id="some_openstack_id")
mock_logger_exception.assert_called_once_with(
"Unrescue Server some_openstack_id failed!"
)


@patch.object(OpenStackConnector, "get_server")
def test_set_server_metadata_success(self, mock_get_server):
# Arrange
Expand Down
14 changes: 14 additions & 0 deletions simple_vm_client/test_virtualmachinehandler.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,20 @@ def test_resume_server(self) -> None:
openstack_id=OPENSTACK_ID
)

def test_rescue_server(self) -> None:
self.handler.rescue_server(openstack_id=OPENSTACK_ID)
self.handler.openstack_connector.rescue_server.assert_called_once_with(
openstack_id=OPENSTACK_ID,
admin_pass=None,
image_ref=None
)

def test_unrescue_server(self) -> None:
self.handler.unrescue_server(openstack_id=OPENSTACK_ID)
self.handler.openstack_connector.unrescue_server.assert_called_once_with(
openstack_id=OPENSTACK_ID
)

def test_set_server_metadata(self):
self.handler.set_server_metadata(openstack_id=OPENSTACK_ID, metadata=METADATA)
self.handler.openstack_connector.set_server_metadata.assert_called_once_with(
Expand Down