apt_key: add --recv argument as last one (#74949)

* apt_key: add --recv argument as last one

* Add unit test

* Add the required boilerplate

Co-authored-by: Marius Gedminas <marius@gedmin.as>
pull/74928/head
Jonathan Kirszling 4 years ago committed by GitHub
parent 81ad125aa6
commit 50e998e303
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -0,0 +1,2 @@
bugfixes:
- apt_key - set --recv argument as last one in apt-key command when using env var HTTP_PROXY (https://github.com/ansible/ansible/issues/74946)

@ -313,13 +313,16 @@ def import_key(module, keyring, keyserver, key_id):
global lang_env global lang_env
if keyring: if keyring:
cmd = "%s --keyring %s adv --no-tty --keyserver %s --recv %s" % (apt_key_bin, keyring, keyserver, key_id) cmd = "%s --keyring %s adv --no-tty --keyserver %s" % (apt_key_bin, keyring, keyserver)
else: else:
cmd = "%s adv --no-tty --keyserver %s --recv %s" % (apt_key_bin, keyserver, key_id) cmd = "%s adv --no-tty --keyserver %s" % (apt_key_bin, keyserver)
# check for proxy # check for proxy
cmd = add_http_proxy(cmd) cmd = add_http_proxy(cmd)
# add recv argument as last one
cmd = "%s --recv %s" % (cmd, key_id)
for retry in range(5): for retry in range(5):
(rc, out, err) = module.run_command(cmd, environ_update=lang_env) (rc, out, err) = module.run_command(cmd, environ_update=lang_env)
if rc == 0: if rc == 0:

@ -0,0 +1,27 @@
from __future__ import (absolute_import, division, print_function)
__metaclass__ = type
import os
from units.compat import mock
from units.compat import unittest
from ansible.modules import apt_key
class AptKeyTestCase(unittest.TestCase):
@mock.patch.object(apt_key, 'apt_key_bin', '/usr/bin/apt-key')
@mock.patch.dict(os.environ, {'HTTP_PROXY': 'proxy.example.com'})
def test_import_key_with_http_proxy(self):
m_mock = mock.Mock()
m_mock.run_command.return_value = (0, '', '')
apt_key.import_key(
m_mock, keyring=None, keyserver='keyserver.example.com',
key_id='0xDEADBEEF')
self.assertEqual(
m_mock.run_command.call_args_list[0][0][0],
'/usr/bin/apt-key adv --no-tty --keyserver keyserver.example.com'
' --keyserver-options http-proxy=proxy.example.com'
' --recv 0xDEADBEEF'
)
Loading…
Cancel
Save