From 9788ab3f167a7d5c3dec5469a68ca40f2cd55a4f Mon Sep 17 00:00:00 2001 From: James Falcon Date: Thu, 12 Aug 2021 08:04:08 -0500 Subject: [PATCH 1/2] Replace broken httpretty tests with mock Certain versions of python/httpretty don't work correctly using https URIs. #960 recently added httpretty tests using https. This commit replaces the httpretty tests that were failing on https with mocks of readurl instead. --- .../test_handler/test_handler_puppet.py | 58 ++++++------------- 1 file changed, 17 insertions(+), 41 deletions(-) diff --git a/tests/unittests/test_handler/test_handler_puppet.py b/tests/unittests/test_handler/test_handler_puppet.py index c0ba2e3c0a9..214c1832c58 100644 --- a/tests/unittests/test_handler/test_handler_puppet.py +++ b/tests/unittests/test_handler/test_handler_puppet.py @@ -309,62 +309,45 @@ def test_puppet_runs_puppet_with_args_string_if_requested(self, m_subp.call_args_list) -class TestInstallPuppetAio(HttprettyTestCase): +URL_MOCK = mock.Mock() +URL_MOCK.contents = b'#!/bin/bash\necho "Hi Mom"' - @mock.patch('cloudinit.config.cc_puppet.subp.subp', - return_value=(None, None)) - def test_install_with_default_arguments(self, m_subp): - """Install AIO with no arguments""" - response = b'#!/bin/bash\necho "Hi Mom"' - httpretty.register_uri( - httpretty.GET, cc_puppet.AIO_INSTALL_URL, - body=response, status=200) +@mock.patch('cloudinit.config.cc_puppet.subp.subp', return_value=(None, None)) +@mock.patch( + 'cloudinit.config.cc_puppet.url_helper.readurl', + return_value=URL_MOCK, autospec=True, +) +class TestInstallPuppetAio(HttprettyTestCase): + def test_install_with_default_arguments(self, m_readurl, m_subp): + """Install AIO with no arguments""" cc_puppet.install_puppet_aio() self.assertEqual( [mock.call([mock.ANY, '--cleanup'], capture=False)], m_subp.call_args_list) - @mock.patch('cloudinit.config.cc_puppet.subp.subp', - return_value=(None, None)) - def test_install_with_custom_url(self, m_subp): + def test_install_with_custom_url(self, m_readurl, m_subp): """Install AIO from custom URL""" - response = b'#!/bin/bash\necho "Hi Mom"' - url = 'http://custom.url/path/to/script.sh' - httpretty.register_uri( - httpretty.GET, url, body=response, status=200) - cc_puppet.install_puppet_aio('http://custom.url/path/to/script.sh') + m_readurl.assert_called_with( + url='http://custom.url/path/to/script.sh', + retries=5) self.assertEqual( [mock.call([mock.ANY, '--cleanup'], capture=False)], m_subp.call_args_list) - @mock.patch('cloudinit.config.cc_puppet.subp.subp', - return_value=(None, None)) - def test_install_with_version(self, m_subp): + def test_install_with_version(self, m_readurl, m_subp): """Install AIO with specific version""" - response = b'#!/bin/bash\necho "Hi Mom"' - httpretty.register_uri( - httpretty.GET, cc_puppet.AIO_INSTALL_URL, - body=response, status=200) - cc_puppet.install_puppet_aio(cc_puppet.AIO_INSTALL_URL, '7.6.0') self.assertEqual( [mock.call([mock.ANY, '-v', '7.6.0', '--cleanup'], capture=False)], m_subp.call_args_list) - @mock.patch('cloudinit.config.cc_puppet.subp.subp', - return_value=(None, None)) - def test_install_with_collection(self, m_subp): + def test_install_with_collection(self, m_readurl, m_subp): """Install AIO with specific collection""" - response = b'#!/bin/bash\necho "Hi Mom"' - httpretty.register_uri( - httpretty.GET, cc_puppet.AIO_INSTALL_URL, - body=response, status=200) - cc_puppet.install_puppet_aio( cc_puppet.AIO_INSTALL_URL, None, 'puppet6-nightly') @@ -373,15 +356,8 @@ def test_install_with_collection(self, m_subp): capture=False)], m_subp.call_args_list) - @mock.patch('cloudinit.config.cc_puppet.subp.subp', - return_value=(None, None)) - def test_install_with_no_cleanup(self, m_subp): + def test_install_with_no_cleanup(self, m_readurl, m_subp): """Install AIO with no cleanup""" - response = b'#!/bin/bash\necho "Hi Mom"' - httpretty.register_uri( - httpretty.GET, cc_puppet.AIO_INSTALL_URL, - body=response, status=200) - cc_puppet.install_puppet_aio( cc_puppet.AIO_INSTALL_URL, None, None, False) From a4693b8ee61e5d24815f8f2d0964eef8a72a3370 Mon Sep 17 00:00:00 2001 From: James Falcon Date: Thu, 12 Aug 2021 08:30:35 -0500 Subject: [PATCH 2/2] flake8 --- tests/unittests/test_handler/test_handler_puppet.py | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/unittests/test_handler/test_handler_puppet.py b/tests/unittests/test_handler/test_handler_puppet.py index 214c1832c58..b7891ab4774 100644 --- a/tests/unittests/test_handler/test_handler_puppet.py +++ b/tests/unittests/test_handler/test_handler_puppet.py @@ -5,7 +5,6 @@ from cloudinit import (distros, helpers, cloud, util) from cloudinit.tests.helpers import CiTestCase, HttprettyTestCase, mock -import httpretty import logging import textwrap