diff --git a/simple_vm_client/openstack_connector/openstack_connector.py b/simple_vm_client/openstack_connector/openstack_connector.py index 66ef2eb..f1a084b 100644 --- a/simple_vm_client/openstack_connector/openstack_connector.py +++ b/simple_vm_client/openstack_connector/openstack_connector.py @@ -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: @@ -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) @@ -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) diff --git a/simple_vm_client/test_openstack_connector.py b/simple_vm_client/test_openstack_connector.py index 55ac4fd..c4d777b 100644 --- a/simple_vm_client/test_openstack_connector.py +++ b/simple_vm_client/test_openstack_connector.py @@ -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 diff --git a/simple_vm_client/test_virtualmachinehandler.py b/simple_vm_client/test_virtualmachinehandler.py index 4372b84..38da130 100644 --- a/simple_vm_client/test_virtualmachinehandler.py +++ b/simple_vm_client/test_virtualmachinehandler.py @@ -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(