From 77851bb3655be34ad310e70b5c17d13f87da4449 Mon Sep 17 00:00:00 2001 From: Amit Uniyal Date: Mon, 12 Feb 2024 08:31:36 +0000 Subject: [PATCH] Separate OSError with ValueError OSError will only be raised, if file path is not readable because of permission issue. With this change we will get correct error msg. Change-Id: Iad3b0f2ab3e6eafd9f6c98477edfa35c4cd46ee8 --- nova/cmd/manage.py | 7 +++++-- nova/tests/unit/cmd/test_manage.py | 12 ++++++++++++ 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/nova/cmd/manage.py b/nova/cmd/manage.py index 2f432cf3e7..f6c7a8e420 100644 --- a/nova/cmd/manage.py +++ b/nova/cmd/manage.py @@ -3208,12 +3208,15 @@ class VolumeAttachmentCommands(object): ) as e: print(str(e)) return 4 - except (ValueError, OSError): + except ValueError as e: print( f'Failed to open {connector_path}. Does it contain valid ' - f'connector_info data?' + f'connector_info data?\nError: {str(e)}' ) return 3 + except OSError as e: + print(str(e)) + return 3 except exception.InvalidInput as e: print(str(e)) return 2 diff --git a/nova/tests/unit/cmd/test_manage.py b/nova/tests/unit/cmd/test_manage.py index f9142d37f6..2532638279 100644 --- a/nova/tests/unit/cmd/test_manage.py +++ b/nova/tests/unit/cmd/test_manage.py @@ -3488,6 +3488,18 @@ class VolumeAttachmentCommandsTestCase(test.NoDBTestCase): output = self.output.getvalue().strip() self.assertIn('Failed to open fake_path', output) + @mock.patch('os.path.exists') + def test_refresh_connector_file_oserr(self, mock_exists): + """Test refresh with connector file having no read permission. + """ + mock_exists.return_value = True + with self.patch_open('fake_path', b'invalid json') as mock_file: + mock_file.side_effect = OSError("Permission denied") + ret = self.commands.refresh( + uuidsentinel.volume, uuidsentinel.instance, 'fake_path' + ) + self.assertEqual(3, ret) + @mock.patch('os.path.exists') def _test_refresh(self, mock_exists): ctxt = context.get_admin_context()