Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace broken httpretty tests with mock (SC-324) #973

Merged
merged 3 commits into from
Aug 13, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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