Azure/WALinuxAgent

[BUG] test test_exthandlers_exthandlerinstance failling when building WALinuxAgent package in Ubuntu 24.10 (Python 3.12)

Closed this issue · 1 comments

Hi team,

Describe the bug: A clear and concise description of what the bug is.

When building WALinuxAgent 2.11.1.4 from source code on Ubuntu 24.10 with Python 3.12, we got the following output for the test part (preventing the building):

========================================= test session starts =========================================
platform linux -- Python 3.12.4, pytest-8.2.2, pluggy-1.5.0
rootdir: /build/walinuxagent-g550Hv/walinuxagent-2.11.1.4
collected 5 items                                                                                     

tests/ga/test_exthandlers_exthandlerinstance.py ....F                                           [100%]

============================================== FAILURES ===============================================
_ ExtHandlerInstanceTestCase.test_rm_ext_handler_dir_should_report_an_event_if_an_error_occurs_while_deleting_the_extension_directory _

self = <tests.ga.test_exthandlers_exthandlerinstance.ExtHandlerInstanceTestCase testMethod=test_rm_ext_handler_dir_should_report_an_event_if_an_error_occurs_while_deleting_the_extension_directory>

        def test_rm_ext_handler_dir_should_report_an_event_if_an_error_occurs_while_deleting_the_extension_directory(self):
            os.mkdir(self.extension_directory)
            os.mknod(os.path.join(self.extension_directory, "extension_file1"))
            os.mknod(os.path.join(self.extension_directory, "extension_file2"))
            os.mknod(os.path.join(self.extension_directory, "extension_file3"))
    
            # The mock below relies on the knowledge that remove_ext_handler invokes Pyhon's shutil.rmtree,
            # which in turn invokes os.unlink (Python 3) or os.remove (Python 2)
            remove_api_name = "unlink" if sys.version_info >= (3, 0) else "remove"
    
            original_remove_api = getattr(shutil.os, remove_api_name)
    
            def mock_remove(path, dir_fd=None):  # pylint: disable=unused-argument
                #pdb.set_trace()
                if path.endswith("extension_file2"):
                    raise IOError("A mocked error")
                original_remove_api(path)
    
            with patch.object(shutil.os, remove_api_name, mock_remove):
                with patch.object(self.ext_handler_instance, "report_event") as mock_report_event:
                    self.ext_handler_instance.remove_ext_handler()
                    args, kwargs = mock_report_event.call_args  # pylint: disable=unused-variable
    
    #        args, kwargs = mock_report_event.call_args  # pylint: disable=unused-variable
            print(kwargs)
>           self.assertTrue("A mocked error" in kwargs["message"])
E           AssertionError: False is not true

tests/ga/test_exthandlers_exthandlerinstance.py:132: AssertionError

Digging into the test, the output we got that breaks the assertion is

{'message': "Failed to remove extension handler directory: [Errno None] None: '/tmp/ExtHandlerInstanceTestCase_e8jtflwt/extension_directory'", 'is_success': False}

instead of the expected one:

{'message': 'Failed to remove extension handler directory: A mocked error', 'is_success': False}

Distro and WALinuxAgent details (please complete the following information):

  • Distro and Version: Ubuntu 24.10 (Development)
  • WALinuxAgent version 2.11.1.4

Additional context
Changes around os.shutil.rmtree in python 3.12 (https://docs.python.org/3/whatsnew/3.12.html) cause a second exception that causes the mocked one

raise IOError("A mocked error")
to be lost.

I already made a patch for this that is working. I open PR #3149 to forward it to you, for your consideration.

Thanks!

fixed by #3149