Skip to content

Commit

Permalink
fix(tests): fixed rescue unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
qqmok committed Nov 21, 2024
1 parent 72f989e commit 619450b
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 11 deletions.
15 changes: 7 additions & 8 deletions simple_vm_client/openstack_connector/openstack_connector.py
Original file line number Diff line number Diff line change
Expand Up @@ -1366,6 +1366,7 @@ def delete_server(self, openstack_id: str) -> None:

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 @@ -1374,12 +1375,11 @@ def rescue_server(self, openstack_id: str, admin_pass: str = None, image_ref: st
message=f"Instance {openstack_id} not found",
name_or_id=openstack_id,
)
self.openstack_connection.compute.rescue_server(server.id, admin_pass, image_ref)
self.openstack_connection.compute.rescue_server(server, admin_pass, image_ref)

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

raise OpenStackConflictException(message=e.message)
logger.exception(f"Rescue Server {openstack_id} failed!")
raise OpenStackConflictException(message=str(e))

def unrescue_server(self, openstack_id: str) -> None:
logger.info(f"Unrescue Server {openstack_id}")
Expand All @@ -1392,12 +1392,11 @@ def unrescue_server(self, openstack_id: str) -> None:
name_or_id=openstack_id,
)

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

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

raise OpenStackConflictException(message=e.message)
logger.exception(f"Unrescue Server {openstack_id} failed!")
raise OpenStackConflictException(message=str(e))

def _calculate_vm_ports(self, server: Server):
fixed_ip = server.private_v4
Expand Down
9 changes: 6 additions & 3 deletions simple_vm_client/test_openstack_connector.py
Original file line number Diff line number Diff line change
Expand Up @@ -2308,6 +2308,8 @@ 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
Expand All @@ -2319,7 +2321,7 @@ def test_rescue_server_success(self, mock_get_server):
# 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
server_mock, None, None
)

@patch.object(OpenStackConnector, "get_server")
Expand All @@ -2330,7 +2332,8 @@ def test_rescue_server_conflict_exception(
# 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(
self.openstack_connector.openstack_connection.compute.rescue_server.side_effect = ConflictException
(
"Unit Test"
)
# Act
Expand Down Expand Up @@ -2370,7 +2373,7 @@ def test_unrescue_server_conflict_exception(
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")
Expand Down

0 comments on commit 619450b

Please sign in to comment.