canonical / cloud-init

Official upstream for the cloud-init: cloud instance initialization

Home Page:https://cloud-init.io/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

tests/unittests/test_gpg.py::TestReceiveKeys::test_kill_gpg_succeeds fails if gpg not installed

dermotbradley opened this issue · comments

Bug report

Testcase tests/unittests/test_gpg.py::TestReceiveKeys::test_kill_gpg_succeeds fails on Alpine unless gpg package is installed.

It seems like either gpg or gpgconf is not mocked correctly.

Steps to reproduce the problem

Environment details

  • Cloud-init version: git master
  • Operating System Distribution: Alpine
  • Cloud provider, platform or installer type:

cloud-init logs

____________________ TestReceiveKeys.test_kill_gpg_succeeds ____________________

self = <MagicMock name='subp' id='140364442842128'>
calls = [call(['gpgconf', '--kill', 'all'], capture=True, update_env={'GNUPGHOME': '/tmp/tmpwaoimtvr'})]
any_order = False

    def assert_has_calls(self, calls, any_order=False):
        """assert the mock has been called with the specified calls.
        The `mock_calls` list is checked for the calls.
    
        If `any_order` is False (the default) then the calls must be
        sequential. There can be extra calls before or after the
        specified calls.
    
        If `any_order` is True then the calls can be in any order, but
        they must all appear in `mock_calls`."""
        expected = [self._call_matcher(c) for c in calls]
        cause = next((e for e in expected if isinstance(e, Exception)), None)
        all_calls = _CallList(self._call_matcher(c) for c in self.mock_calls)
        if not any_order:
            if expected not in all_calls:
                if cause is None:
                    problem = 'Calls not found.'
                else:
                    problem = ('Error processing expected calls.\n'
                               'Errors: {}').format(
                                   [e if isinstance(e, Exception) else None
                                    for e in expected])
>               raise AssertionError(
                    f'{problem}\n'
                    f'Expected: {_CallList(calls)}'
                    f'{self._calls_repr(prefix="  Actual").rstrip(".")}'
                ) from cause
E               AssertionError: Calls not found.
E               Expected: [call(['gpgconf', '--kill', 'all'], capture=True, update_env={'GNUPGHOME': '/tmp/tmpwaoimtvr'})]
E                 Actual: [call(['gpg', '--no-options', '--with-fingerprint', '--no-default-keyring', '--list-keys', '--keyring', '--with-colons', ''], update_env={'GNUPGHOME': '/tmp/tmpwaoimtvr'}, capture=True),
E                call(['ps', '-o', 'ppid,pid', '-C', 'keyboxd', '-C', 'dirmngr', '-C', 'gpg-agent'], capture=True, rcs=[0, 1])]

/usr/lib/python3.11/unittest/mock.py:976: AssertionError

During handling of the above exception, another exception occurred:

self = <tests.unittests.test_gpg.TestReceiveKeys object at 0x7fa92f6c4f90>
m_subp = <MagicMock name='subp' id='140364442842128'>

    def test_kill_gpg_succeeds(self, m_subp):
        """ensure that when gpgconf isn't found, processes are manually
        cleaned up. Also test that the context manager does cleanup
    
        """
        with pytest.raises(ZeroDivisionError):
            with gpg.GPG() as gpg_context:
    
                # run a gpg command so that we have "started" gpg
                gpg_context.list_keys("")
                1 / 0  # pylint: disable=pointless-statement
>       m_subp.assert_has_calls(
            [
                mock.call(
                    ["gpgconf", "--kill", "all"],
                    capture=True,
                    update_env=gpg_context.env,
                )
            ]
        )
E       AssertionError: Calls not found.
E       Expected: [call(['gpgconf', '--kill', 'all'], capture=True, update_env={'GNUPGHOME': '/tmp/tmpwaoimtvr'})]
E         Actual: [call(['gpg', '--no-options', '--with-fingerprint', '--no-default-keyring', '--list-keys', '--keyring', '--with-colons', ''], update_env={'GNUPGHOME': '/tmp/tmpwaoimtvr'}, capture=True),
E        call(['ps', '-o', 'ppid,pid', '-C', 'keyboxd', '-C', 'dirmngr', '-C', 'gpg-agent'], capture=True, rcs=[0, 1])]
E       
E       pytest introspection follows:
E       
E       Args:
E       assert (['ps', '-o',..., '-C', ...],) == ([call(['gpgc...waoimtvr'})],)
E         
E         At index 0 diff: ['ps', '-o', 'ppid,pid', '-C', 'keyboxd', '-C', 'dirmngr', '-C', 'gpg-agent'] != [call(['gpgconf', '--kill', 'all'], capture=True, update_env={'GNUPGHOME': '/tmp/tmpwaoimtvr'})]
E         Use -v to get more diff
E       Kwargs:
E       assert {'capture': T...'rcs': [0, 1]} == {}
E         
E         Left contains 2 more items:
E         {'capture': True, 'rcs': [0, 1]}
E         Use -v to get more diff

tests/unittests/test_gpg.py:182: AssertionError
=========================== short test summary info ============================
FAILED tests/unittests/test_gpg.py::TestReceiveKeys::test_kill_gpg_succeeds
= 3 failed, 5243 passed, 7 skipped, 8 xfailed, 302 warnings in 74.92s (0:01:14) =

Thanks @dermotbradley brett's going to peek at fixing this.