tests: add unit tests for import_template.py (#33333)

This commit is contained in:
Nicolas Roche 2019-05-24 18:15:30 +02:00
parent 7dcc824782
commit 76ce7c38f3
2 changed files with 79 additions and 20 deletions

View File

@ -6,18 +6,17 @@ from django.core.management.base import CommandError
from django.test import override_settings
from hobo.agent.common.management.commands.import_template import Command, UnknownTemplateError
from django.core.management import load_command_class
@mock.patch('hobo.agent.common.management.commands.import_template.os.path.exists')
@mock.patch('hobo.agent.common.management.commands.import_template.call_command')
@mock.patch('hobo.agent.common.management.commands.import_template.get_commands')
def test_import_template_default(get_commands, call_command, os_path_exists):
def test_import_template_default(mocked_get_commands, mocked_call_command, mocked_exists):
"""do import a template from default path"""
expected = [mock.call('import_site', '/var/lib/bobo/skeletons/import_me.json')]
get_commands.return_value = 'import_site'
call_command.return_value = mock.MagicMock()
os_path_exists.return_value = True
mocked_get_commands.return_value = 'import_site'
mocked_call_command.return_value = mock.MagicMock()
mocked_exists.return_value = True
# simulate:
# $ XXX-manage import-template import_me
@ -26,18 +25,17 @@ def test_import_template_default(get_commands, call_command, os_path_exists):
command.handle(template_name='import_me')
# assert 'import_site' command is run
assert call_command.mock_calls == expected
assert mocked_call_command.mock_calls == [
mock.call('import_site', '/var/lib/bobo/skeletons/import_me.json')]
@mock.patch('hobo.agent.common.management.commands.import_template.os.path.exists')
@mock.patch('hobo.agent.common.management.commands.import_template.call_command')
@mock.patch('hobo.agent.common.management.commands.import_template.get_commands')
def test_import_template_basepath(get_commands, call_command, os_path_exists):
def test_import_template_basepath(mocked_get_commands, mocked_call_command, mocked_exists):
"""do import a template having basepath parameter provided"""
expected = [mock.call('import_site', '/tmp/import_me.json')]
get_commands.return_value = 'import_site'
call_command.return_value = mock.MagicMock()
os_path_exists.return_value = True
mocked_get_commands.return_value = 'import_site'
mocked_call_command.return_value = mock.MagicMock()
mocked_exists.return_value = True
# simulate:
# $ XXX-manage import-template import_me --basepath /tmp
@ -46,18 +44,18 @@ def test_import_template_basepath(get_commands, call_command, os_path_exists):
command.handle(template_name='import_me', basepath='/tmp')
# assert 'import_site' command is run
assert call_command.mock_calls == expected
assert mocked_call_command.mock_calls == [
mock.call('import_site', '/tmp/import_me.json')]
@mock.patch('hobo.agent.common.management.commands.import_template.os.path.exists')
@mock.patch('hobo.agent.common.management.commands.import_template.call_command')
@mock.patch('hobo.agent.common.management.commands.import_template.get_commands')
def test_import_template_warning(get_commands, call_command, os_path_exists):
def test_import_template_warning(mocked_get_commands, mocked_call_command, mocked_exists):
"""returns if template is not found"""
get_commands.return_value = 'import_site'
call_command.return_value = mock.MagicMock()
os_path_exists.return_value = False
mocked_get_commands.return_value = 'import_site'
mocked_call_command.return_value = mock.MagicMock()
mocked_exists.return_value = False
# simulate:
# $ XXX-manage import-template import_me
@ -67,4 +65,23 @@ def test_import_template_warning(get_commands, call_command, os_path_exists):
command.handle(template_name='import_me')
# assert 'import_site' command is not run
assert call_command.mock_calls == []
assert mocked_call_command.mock_calls == []
@mock.patch('hobo.agent.common.management.commands.import_template.Command.handle')
def test_import_template_on_combo_agent(mocked_super):
"""overloaded import-template for combo"""
command = load_command_class('hobo.agent.combo', 'import_template')
# template specified and found
command.handle(template_name='my-template')
assert mocked_super.mock_calls == [mock.call(template_name='my-template')]
# do nothing on internal templates
mocked_super.side_effect = UnknownTemplateError
for template in 'portal-user', 'portal-agent':
command.handle(template_name=template)
# template no found
with pytest.raises(UnknownTemplateError):
command.handle(template_name='unknown-template')

View File

@ -0,0 +1,42 @@
import os
import mock
import pytest
from django.core.management import call_command, load_command_class
from hobo.agent.common.management.commands.import_template import UnknownTemplateError
@mock.patch('hobo.agent.common.management.commands.import_template.call_command')
@mock.patch('hobo.agent.common.management.commands.import_template.get_commands')
def test_import_template_on_common_agent(mocked_get_commands, mocked_call_command, tmpdir):
"""import-template basic case (for passerelle here):
$ passerelle-manage import_template my-template # simulate this bash query
check this call result in '$ passerelle-manage import_site my-template'
"""
command = load_command_class('hobo.agent.common', 'import_template')
mocked_get_commands.return_value = ['import_site']
template_path = os.path.join(str(tmpdir), 'my-template.json')
with open(template_path, 'w') as handler:
handler.write('...')
command.handle(basepath=str(tmpdir), template_name='my-template')
assert mocked_call_command.mock_calls == [mock.call('import_site', template_path)]
@mock.patch('hobo.agent.common.management.commands.import_template.call_command')
@mock.patch('hobo.agent.common.management.commands.import_template.get_commands')
def test_import_template_on_combo_agent(mocked_get_commands, mocked_call_command, tmpdir):
"""overloaded case for combo
$ combo-manage import_template my-template # simulate this bash query
only check we reach the hobo.agent.common's code here
"""
command = load_command_class('hobo.agent.combo', 'import_template')
mocked_get_commands.return_value = ['import_site']
template_path = os.path.join(str(tmpdir), 'my-template.json')
with open(template_path, 'w') as handler:
handler.write('...')
command.handle(basepath=str(tmpdir), template_name='my-template')
assert mocked_call_command.mock_calls == [mock.call('import_site', template_path)]