Skip to content

Commit

Permalink
Replace broken httpretty tests with mock (SC-324) (#973)
Browse files Browse the repository at this point in the history
* 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.
  • Loading branch information
TheRealFalcon committed Aug 13, 2021
1 parent e119cec commit 1c3b10b
Showing 1 changed file with 17 additions and 42 deletions.
59 changes: 17 additions & 42 deletions tests/unittests/test_handler/test_handler_puppet.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -309,62 +308,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')

Expand All @@ -373,15 +355,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)

Expand Down

0 comments on commit 1c3b10b

Please sign in to comment.