From 72f989ec512e9358ead0b7b56dffc6ab0112a3dd Mon Sep 17 00:00:00 2001 From: qqmok Date: Wed, 6 Nov 2024 20:20:07 +0100 Subject: [PATCH 1/3] fix(rescue_instance): added tests --- simple_vm_client/test_openstack_connector.py | 65 +++++++++++++++++++ .../test_virtualmachinehandler.py | 12 ++++ 2 files changed, 77 insertions(+) diff --git a/simple_vm_client/test_openstack_connector.py b/simple_vm_client/test_openstack_connector.py index 0a7dd45..9d4a993 100644 --- a/simple_vm_client/test_openstack_connector.py +++ b/simple_vm_client/test_openstack_connector.py @@ -2308,6 +2308,71 @@ 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 + ) + + @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 214e53f..fe42851 100644 --- a/simple_vm_client/test_virtualmachinehandler.py +++ b/simple_vm_client/test_virtualmachinehandler.py @@ -163,6 +163,18 @@ 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 + ) + + 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( From 619450b1d95cad69cbc3c21e66f7c6f0b9895dfe Mon Sep 17 00:00:00 2001 From: qqmok Date: Thu, 21 Nov 2024 03:00:43 +0100 Subject: [PATCH 2/3] fix(tests): fixed rescue unit tests --- .../openstack_connector/openstack_connector.py | 15 +++++++-------- simple_vm_client/test_openstack_connector.py | 9 ++++++--- 2 files changed, 13 insertions(+), 11 deletions(-) diff --git a/simple_vm_client/openstack_connector/openstack_connector.py b/simple_vm_client/openstack_connector/openstack_connector.py index 1045ca1..da86003 100644 --- a/simple_vm_client/openstack_connector/openstack_connector.py +++ b/simple_vm_client/openstack_connector/openstack_connector.py @@ -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: @@ -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}") @@ -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 diff --git a/simple_vm_client/test_openstack_connector.py b/simple_vm_client/test_openstack_connector.py index 9d4a993..a6fdf7c 100644 --- a/simple_vm_client/test_openstack_connector.py +++ b/simple_vm_client/test_openstack_connector.py @@ -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 @@ -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") @@ -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 @@ -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") From fb40d35d58b04b88bd8fb85ada35679f0eb16937 Mon Sep 17 00:00:00 2001 From: qqmok Date: Tue, 26 Nov 2024 10:03:14 +0100 Subject: [PATCH 3/3] fix(rescue_test): adjusted vmhandler rescue test assertion --- simple_vm_client/test_virtualmachinehandler.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/simple_vm_client/test_virtualmachinehandler.py b/simple_vm_client/test_virtualmachinehandler.py index a640486..38da130 100644 --- a/simple_vm_client/test_virtualmachinehandler.py +++ b/simple_vm_client/test_virtualmachinehandler.py @@ -166,7 +166,9 @@ def test_resume_server(self) -> None: 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 + openstack_id=OPENSTACK_ID, + admin_pass=None, + image_ref=None ) def test_unrescue_server(self) -> None: