Skip to content

Instantly share code, notes, and snippets.

@mgedmin
Last active June 10, 2021 14:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mgedmin/1a8eaf34cddc07cd8a7c29f13a7b654c to your computer and use it in GitHub Desktop.
Save mgedmin/1a8eaf34cddc07cd8a7c29f13a7b654c to your computer and use it in GitHub Desktop.
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'
)
@mgedmin
Copy link
Author

mgedmin commented Jun 10, 2021

Put this in test/units/modules/test_apt_key.py in a checkout of your ansible source tree. Run the test with

ansible-test units --docker -v apt_key

@mgedmin
Copy link
Author

mgedmin commented Jun 10, 2021

Here's how this test fails on the devel branch (which is expected):

mg@blynas: ~/src/ansible [git:devel=] $ ansible-test units --docker -v apt_key
Run command: docker -v
Detected "docker" container runtime version: Docker version 20.10.2, build 20.10.2-0ubuntu2
Run command: docker image inspect quay.io/ansible/ansible-core-test-container:3.5.1
Run command: docker image inspect quay.io/ansible/pypi-test-container:1.0.0
Run command: docker run --detach quay.io/ansible/pypi-test-container:1.0.0
Run command: docker inspect 4ab9856f2ef329f101c7fb8c0cd7145887cdb14986981a4f5150fffc881d8016
Creating a payload archive containing 5066 files...
Created a 6812423 byte payload archive containing 5066 files in 0 seconds.
Assuming Docker is available on localhost.
Run command: docker run --detach --volume /sys/fs/cgroup:/sys/fs/cgroup:ro --privileged=false --security-opt seccomp=unconfined --volum ...
Run command: docker exec -i 5bf7e2d5c8f1f7639bc90ada4be1e3fff8291a15fcfdeebecb9cc5082bddae57 /bin/sh
Run command: docker exec -i 5bf7e2d5c8f1f7639bc90ada4be1e3fff8291a15fcfdeebecb9cc5082bddae57 dd of=/root/test.tgz bs=65536
Run command: docker exec 5bf7e2d5c8f1f7639bc90ada4be1e3fff8291a15fcfdeebecb9cc5082bddae57 tar oxzf /root/test.tgz -C /root
Run command: docker exec 5bf7e2d5c8f1f7639bc90ada4be1e3fff8291a15fcfdeebecb9cc5082bddae57 mkdir -p /root/ansible/test/results/junit /ro ...
Run command: docker exec 5bf7e2d5c8f1f7639bc90ada4be1e3fff8291a15fcfdeebecb9cc5082bddae57 chmod 777 /root/ansible/test/results/junit /r ...
Run command: docker exec 5bf7e2d5c8f1f7639bc90ada4be1e3fff8291a15fcfdeebecb9cc5082bddae57 chmod 755 /root
Run command: docker exec 5bf7e2d5c8f1f7639bc90ada4be1e3fff8291a15fcfdeebecb9cc5082bddae57 chmod 644 /root/ansible/test/results/.tmp/met ...
Run command: docker exec 5bf7e2d5c8f1f7639bc90ada4be1e3fff8291a15fcfdeebecb9cc5082bddae57 useradd pytest --create-home
Run command: docker exec 5bf7e2d5c8f1f7639bc90ada4be1e3fff8291a15fcfdeebecb9cc5082bddae57 /usr/bin/env ANSIBLE_TEST_CONTENT_ROOT=/root/ ...
Injecting custom PyPI hosts entries: /etc/hosts
Injecting custom PyPI config: /root/.pip/pip.conf
Injecting custom PyPI config: /root/.pydistutils.cfg
Run command: /usr/bin/python2.6 /root/ansible/test/lib/ansible_test/_data/quiet_pip.py install --disable-pip-version-check -r /root/ans ...
Run command: /usr/bin/python2.6 /root/ansible/test/lib/ansible_test/_data/quiet_pip.py install --disable-pip-version-check setuptools - ...
Run command: /usr/bin/python2.6 -c 'import cryptography'
Run command: /usr/bin/python2.6 /root/ansible/test/lib/ansible_test/_data/quiet_pip.py install --disable-pip-version-check -r /root/ans ...
Run command: /usr/bin/python2.6 /root/ansible/test/lib/ansible_test/_data/quiet_pip.py check --disable-pip-version-check
Run command: /usr/bin/python2.6 /root/ansible/test/lib/ansible_test/_data/yamlcheck.py
Run command: /usr/bin/python2.7 /root/ansible/test/lib/ansible_test/_data/quiet_pip.py install --disable-pip-version-check -r /root/ans ...
Run command: /usr/bin/python2.7 /root/ansible/test/lib/ansible_test/_data/quiet_pip.py install --disable-pip-version-check setuptools - ...
Run command: /usr/bin/python2.7 -c 'import cryptography'
Run command: /usr/bin/python2.7 /root/ansible/test/lib/ansible_test/_data/quiet_pip.py install --disable-pip-version-check -r /root/ans ...
Run command: /usr/bin/python2.7 /root/ansible/test/lib/ansible_test/_data/quiet_pip.py check --disable-pip-version-check
Run command: /usr/bin/python2.7 /root/ansible/test/lib/ansible_test/_data/yamlcheck.py
Run command: /usr/bin/python3.5 /root/ansible/test/lib/ansible_test/_data/quiet_pip.py install --disable-pip-version-check -r /root/ans ...
Run command: /usr/bin/python3.5 /root/ansible/test/lib/ansible_test/_data/quiet_pip.py install --disable-pip-version-check setuptools - ...
Run command: /usr/bin/python3.5 -c 'import cryptography'
Run command: /usr/bin/python3.5 /root/ansible/test/lib/ansible_test/_data/quiet_pip.py install --disable-pip-version-check -r /root/ans ...
Run command: /usr/bin/python3.5 /root/ansible/test/lib/ansible_test/_data/quiet_pip.py check --disable-pip-version-check
Run command: /usr/bin/python3.5 /root/ansible/test/lib/ansible_test/_data/yamlcheck.py
Run command: /usr/bin/python3.6 /root/ansible/test/lib/ansible_test/_data/quiet_pip.py install --disable-pip-version-check -r /root/ans ...
Run command: /usr/bin/python3.6 /root/ansible/test/lib/ansible_test/_data/quiet_pip.py install --disable-pip-version-check setuptools - ...
Run command: /usr/bin/python3.6 -c 'import cryptography'
Run command: /usr/bin/python3.6 /root/ansible/test/lib/ansible_test/_data/quiet_pip.py install --disable-pip-version-check -r /root/ans ...
Run command: /usr/bin/python3.6 /root/ansible/test/lib/ansible_test/_data/quiet_pip.py check --disable-pip-version-check
Run command: /usr/bin/python3.6 /root/ansible/test/lib/ansible_test/_data/yamlcheck.py
Run command: /usr/bin/python3.7 /root/ansible/test/lib/ansible_test/_data/quiet_pip.py install --disable-pip-version-check -r /root/ans ...
Run command: /usr/bin/python3.7 /root/ansible/test/lib/ansible_test/_data/quiet_pip.py install --disable-pip-version-check setuptools - ...
Run command: /usr/bin/python3.7 -c 'import cryptography'
Run command: /usr/bin/python3.7 /root/ansible/test/lib/ansible_test/_data/quiet_pip.py install --disable-pip-version-check -r /root/ans ...
Run command: /usr/bin/python3.7 /root/ansible/test/lib/ansible_test/_data/quiet_pip.py check --disable-pip-version-check
Run command: /usr/bin/python3.7 /root/ansible/test/lib/ansible_test/_data/yamlcheck.py
Run command: /usr/bin/python3.8 /root/ansible/test/lib/ansible_test/_data/quiet_pip.py install --disable-pip-version-check -r /root/ans ...
Run command: /usr/bin/python3.8 /root/ansible/test/lib/ansible_test/_data/quiet_pip.py install --disable-pip-version-check setuptools - ...
Run command: /usr/bin/python3.8 -c 'import cryptography'
Run command: /usr/bin/python3.8 /root/ansible/test/lib/ansible_test/_data/quiet_pip.py install --disable-pip-version-check -r /root/ans ...
Run command: /usr/bin/python3.8 /root/ansible/test/lib/ansible_test/_data/quiet_pip.py check --disable-pip-version-check
Run command: /usr/bin/python3.8 /root/ansible/test/lib/ansible_test/_data/yamlcheck.py
Run command: /usr/bin/python3.9 /root/ansible/test/lib/ansible_test/_data/quiet_pip.py install --disable-pip-version-check -r /root/ans ...
Run command: /usr/bin/python3.9 /root/ansible/test/lib/ansible_test/_data/quiet_pip.py install --disable-pip-version-check setuptools - ...
Run command: /usr/bin/python3.9 -c 'import cryptography'
Run command: /usr/bin/python3.9 /root/ansible/test/lib/ansible_test/_data/quiet_pip.py install --disable-pip-version-check -r /root/ans ...
Run command: /usr/bin/python3.9 /root/ansible/test/lib/ansible_test/_data/quiet_pip.py check --disable-pip-version-check
Run command: /usr/bin/python3.9 /root/ansible/test/lib/ansible_test/_data/yamlcheck.py
Run command: /usr/bin/python3.10 /root/ansible/test/lib/ansible_test/_data/quiet_pip.py install --disable-pip-version-check -r /root/an ...
Run command: /usr/bin/python3.10 /root/ansible/test/lib/ansible_test/_data/quiet_pip.py install --disable-pip-version-check setuptools  ...
Run command: /usr/bin/python3.10 -c 'import cryptography'
Run command: /usr/bin/python3.10 /root/ansible/test/lib/ansible_test/_data/quiet_pip.py install --disable-pip-version-check -r /root/an ...
Run command: /usr/bin/python3.10 /root/ansible/test/lib/ansible_test/_data/quiet_pip.py check --disable-pip-version-check
Run command: /usr/bin/python3.10 /root/ansible/test/lib/ansible_test/_data/yamlcheck.py
Removing custom PyPI config: /root/.pydistutils.cfg
Removing custom PyPI config: /root/.pip/pip.conf
Removing custom PyPI hosts entries: /etc/hosts
Run command: docker inspect 5bf7e2d5c8f1f7639bc90ada4be1e3fff8291a15fcfdeebecb9cc5082bddae57
Run command: docker network disconnect bridge 5bf7e2d5c8f1f7639bc90ada4be1e3fff8291a15fcfdeebecb9cc5082bddae57
Run command: docker exec --user pytest 5bf7e2d5c8f1f7639bc90ada4be1e3fff8291a15fcfdeebecb9cc5082bddae57 /usr/bin/env ANSIBLE_TEST_CONTE ...
Injecting custom PyPI config: /home/pytest/.pip/pip.conf
WARNING: Skipping custom hosts block for PyPI for non-root user.
Injecting custom PyPI config: /home/pytest/.pydistutils.cfg
Unit test modules with Python 2.6
Injecting "/tmp/python-ety7_5km-ansible/python" as a execv wrapper for the "/usr/bin/python2.6" interpreter.
Run command: pytest --boxed -r a -n auto --color yes -p no:cacheprovider -c /root/ansible/test/lib/ansible_test/_data/pytest.ini --juni ...
============================= test session starts ==============================
platform linux4 -- Python 2.6.9, pytest-3.2.5, py-1.4.34, pluggy-0.4.0 -- /usr/bin/python2.6
rootdir: /root/ansible/test/units/modules, inifile: /root/ansible/test/lib/ansible_test/_data/pytest.ini
plugins: xdist-1.20.1, mock-1.6.3, forked-1.0.1
gw0 I / gw1 I / gw2 I / gw3 I / gw4 I / gw5 I / gw6 I / gw7 I
[gw0] linux4 Python 2.6.9 cwd: /root/ansible
[gw1] linux4 Python 2.6.9 cwd: /root/ansible
[gw2] linux4 Python 2.6.9 cwd: /root/ansible
[gw3] linux4 Python 2.6.9 cwd: /root/ansible
[gw4] linux4 Python 2.6.9 cwd: /root/ansible
[gw5] linux4 Python 2.6.9 cwd: /root/ansible
[gw6] linux4 Python 2.6.9 cwd: /root/ansible
[gw7] linux4 Python 2.6.9 cwd: /root/ansible
[gw0] Python 2.6.9 (default, Apr 29 2018, 12:56:59)  -- [GCC 7.3.0]
[gw1] Python 2.6.9 (default, Apr 29 2018, 12:56:59)  -- [GCC 7.3.0]
[gw2] Python 2.6.9 (default, Apr 29 2018, 12:56:59)  -- [GCC 7.3.0]
[gw3] Python 2.6.9 (default, Apr 29 2018, 12:56:59)  -- [GCC 7.3.0]
[gw4] Python 2.6.9 (default, Apr 29 2018, 12:56:59)  -- [GCC 7.3.0]
[gw5] Python 2.6.9 (default, Apr 29 2018, 12:56:59)  -- [GCC 7.3.0]
[gw6] Python 2.6.9 (default, Apr 29 2018, 12:56:59)  -- [GCC 7.3.0]
[gw7] Python 2.6.9 (default, Apr 29 2018, 12:56:59)  -- [GCC 7.3.0]
gw0 [1] / gw1 [1] / gw2 [1] / gw3 [1] / gw4 [1] / gw5 [1] / gw6 [1] / gw7 [1]

scheduling tests via LoadScheduling
[gw5] FAILED test/units/modules/test_apt_key.py::AptKeyTestCase::test_import_key_with_http_proxy 

 generated xml file: /root/ansible/test/results/junit/python2.6-modules-units.xml 
=========================== short test summary info ============================
FAIL test/units/modules/test_apt_key.py::AptKeyTestCase::test_import_key_with_http_proxy
========================== slowest 25 test durations ===========================
0.00s call     test_apt_key.py::AptKeyTestCase::test_import_key_with_http_proxy
0.00s setup    test_apt_key.py::AptKeyTestCase::test_import_key_with_http_proxy
0.00s teardown test_apt_key.py::AptKeyTestCase::test_import_key_with_http_proxy
=================================== FAILURES ===================================
________________ AptKeyTestCase.test_import_key_with_http_proxy ________________
[gw5] linux4 -- Python 2.6.9 /usr/bin/python2.6
self = <units.modules.test_apt_key.AptKeyTestCase testMethod=test_import_key_with_http_proxy>

    @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'
        )
E       AssertionError: '/usr[57 chars]om --recv 0xDEADBEEF --keyserver-options http-[19 chars].com' != '/usr[57 chars]om --keyserver-options http-proxy=proxy.exampl[19 chars]BEEF'
E       - /usr/bin/apt-key adv --no-tty --keyserver keyserver.example.com --recv 0xDEADBEEF --keyserver-options http-proxy=proxy.example.com
E       ?                                                                 ------------------
E       + /usr/bin/apt-key adv --no-tty --keyserver keyserver.example.com --keyserver-options http-proxy=proxy.example.com --recv 0xDEADBEEF
E       ?                                                                                                                 ++++++++++++++++++

test/units/modules/test_apt_key.py:21: AssertionError
=========================== 1 failed in 0.73 seconds ===========================
ERROR: Command "pytest --boxed -r a -n auto --color yes -p no:cacheprovider -c /root/ansible/test/lib/ansible_test/_data/pytest.ini --junit-xml /root/ansible/test/results/junit/python2.6-modules-units.xml --durations=25 -v test/units/modules/test_apt_key.py" returned exit status 1.
Removing custom PyPI config: /home/pytest/.pydistutils.cfg
Removing custom PyPI config: /home/pytest/.pip/pip.conf
Run command: docker exec 5bf7e2d5c8f1f7639bc90ada4be1e3fff8291a15fcfdeebecb9cc5082bddae57 tar czf /root/results.tgz --exclude .tmp -C / ...
Run command: docker exec -i 5bf7e2d5c8f1f7639bc90ada4be1e3fff8291a15fcfdeebecb9cc5082bddae57 dd if=/root/results.tgz bs=65536
Run command: tar oxzf /tmp/ansible-result-luukdcqc.tgz -C /home/mg/src/ansible/test
Run command: docker rm -f 4ab9856f2ef329f101c7fb8c0cd7145887cdb14986981a4f5150fffc881d8016
Run command: docker rm -f 5bf7e2d5c8f1f7639bc90ada4be1e3fff8291a15fcfdeebecb9cc5082bddae57
ERROR: Command "docker exec --user pytest 5bf7e2d5c8f1f7639bc90ada4be1e3fff8291a15fcfdeebecb9cc5082bddae57 /usr/bin/env ANSIBLE_TEST_CONTENT_ROOT=/root/ansible LC_ALL=en_US.UTF-8 /usr/bin/python3.9 /root/ansible/bin/ansible-test units -v apt_key --metadata test/results/.tmp/metadata-d55gnfy4.json --truncate 140 --redact --color yes --requirements --pypi-endpoint http://172.17.0.2:3141/root/pypi/+simple/ --python default --requirements-mode skip" returned exit status 1.

For some reason I'm unable to run it at all on the PR branch:

mg@blynas: ~/src/ansible [git:apt_key=] $ ansible-test units --docker -v apt_key
Run command: docker images quay.io/ansible/ansible-core-test-container:3.2.0 --format '{{json .}}'
Run command: docker images quay.io/ansible/pypi-test-container:1.0.0 --format '{{json .}}'
Run command: docker run --detach quay.io/ansible/pypi-test-container:1.0.0
Run command: docker inspect 11e1c1efd7e3b4bf24bf9ca89ccd45ba8b2066cf3815f0bce81c3d0da88d1729
Creating a payload archive containing 738 files...
Created a 1210820 byte payload archive containing 738 files in 0 seconds.
Assuming Docker is available on localhost.
Run command: docker run --detach --volume /sys/fs/cgroup:/sys/fs/cgroup:ro --privileged=false --security-opt seccomp=unconfined --volum ...
Run command: docker exec -i 2f9541fd623e4a9ea09d3323f6b90c85f987a14bfc705d8dfc7f8212ba10480e /bin/sh
Run command: docker exec -i 2f9541fd623e4a9ea09d3323f6b90c85f987a14bfc705d8dfc7f8212ba10480e dd of=/root/test.tgz bs=65536
Run command: docker exec 2f9541fd623e4a9ea09d3323f6b90c85f987a14bfc705d8dfc7f8212ba10480e tar oxzf /root/test.tgz -C /root
Run command: docker exec 2f9541fd623e4a9ea09d3323f6b90c85f987a14bfc705d8dfc7f8212ba10480e mkdir -p /root/ansible/test/results/junit /ro ...
Run command: docker exec 2f9541fd623e4a9ea09d3323f6b90c85f987a14bfc705d8dfc7f8212ba10480e chmod 777 /root/ansible/test/results/junit /r ...
Run command: docker exec 2f9541fd623e4a9ea09d3323f6b90c85f987a14bfc705d8dfc7f8212ba10480e chmod 755 /root
Run command: docker exec 2f9541fd623e4a9ea09d3323f6b90c85f987a14bfc705d8dfc7f8212ba10480e chmod 644 /root/ansible/test/results/.tmp/met ...
chmod: cannot access '/root/ansible/test/results/.tmp/metadata-oxp2_r27.json': No such file or directory
Run command: docker rm -f 11e1c1efd7e3b4bf24bf9ca89ccd45ba8b2066cf3815f0bce81c3d0da88d1729
Run command: docker rm -f 2f9541fd623e4a9ea09d3323f6b90c85f987a14bfc705d8dfc7f8212ba10480e
ERROR: Command "docker exec 2f9541fd623e4a9ea09d3323f6b90c85f987a14bfc705d8dfc7f8212ba10480e chmod 644 /root/ansible/test/results/.tmp/metadata-oxp2_r27.json" returned exit status 1.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment