6 |
6 |
from django.test import override_settings
|
7 |
7 |
|
8 |
8 |
from hobo.agent.common.management.commands.import_template import Command, UnknownTemplateError
|
|
9 |
from django.core.management import load_command_class
|
9 |
10 |
|
10 |
11 |
|
11 |
12 |
@mock.patch('hobo.agent.common.management.commands.import_template.os.path.exists')
|
12 |
13 |
@mock.patch('hobo.agent.common.management.commands.import_template.call_command')
|
13 |
14 |
@mock.patch('hobo.agent.common.management.commands.import_template.get_commands')
|
14 |
|
def test_import_template_default(get_commands, call_command, os_path_exists):
|
|
15 |
def test_import_template_default(mocked_get_commands, mocked_call_command, mocked_exists):
|
15 |
16 |
"""do import a template from default path"""
|
16 |
|
expected = [mock.call('import_site', '/var/lib/bobo/skeletons/import_me.json')]
|
17 |
|
|
18 |
|
get_commands.return_value = 'import_site'
|
19 |
|
call_command.return_value = mock.MagicMock()
|
20 |
|
os_path_exists.return_value = True
|
|
17 |
mocked_get_commands.return_value = 'import_site'
|
|
18 |
mocked_call_command.return_value = mock.MagicMock()
|
|
19 |
mocked_exists.return_value = True
|
21 |
20 |
|
22 |
21 |
# simulate:
|
23 |
22 |
# $ XXX-manage import-template import_me
|
... | ... | |
26 |
25 |
command.handle(template_name='import_me')
|
27 |
26 |
|
28 |
27 |
# assert 'import_site' command is run
|
29 |
|
assert call_command.mock_calls == expected
|
|
28 |
assert mocked_call_command.mock_calls == [
|
|
29 |
mock.call('import_site', '/var/lib/bobo/skeletons/import_me.json')]
|
30 |
30 |
|
31 |
31 |
@mock.patch('hobo.agent.common.management.commands.import_template.os.path.exists')
|
32 |
32 |
@mock.patch('hobo.agent.common.management.commands.import_template.call_command')
|
33 |
33 |
@mock.patch('hobo.agent.common.management.commands.import_template.get_commands')
|
34 |
|
def test_import_template_basepath(get_commands, call_command, os_path_exists):
|
|
34 |
def test_import_template_basepath(mocked_get_commands, mocked_call_command, mocked_exists):
|
35 |
35 |
"""do import a template having basepath parameter provided"""
|
36 |
|
expected = [mock.call('import_site', '/tmp/import_me.json')]
|
37 |
|
|
38 |
|
get_commands.return_value = 'import_site'
|
39 |
|
call_command.return_value = mock.MagicMock()
|
40 |
|
os_path_exists.return_value = True
|
|
36 |
mocked_get_commands.return_value = 'import_site'
|
|
37 |
mocked_call_command.return_value = mock.MagicMock()
|
|
38 |
mocked_exists.return_value = True
|
41 |
39 |
|
42 |
40 |
# simulate:
|
43 |
41 |
# $ XXX-manage import-template import_me --basepath /tmp
|
... | ... | |
46 |
44 |
command.handle(template_name='import_me', basepath='/tmp')
|
47 |
45 |
|
48 |
46 |
# assert 'import_site' command is run
|
49 |
|
assert call_command.mock_calls == expected
|
|
47 |
assert mocked_call_command.mock_calls == [
|
|
48 |
mock.call('import_site', '/tmp/import_me.json')]
|
50 |
49 |
|
51 |
50 |
|
52 |
51 |
@mock.patch('hobo.agent.common.management.commands.import_template.os.path.exists')
|
53 |
52 |
@mock.patch('hobo.agent.common.management.commands.import_template.call_command')
|
54 |
53 |
@mock.patch('hobo.agent.common.management.commands.import_template.get_commands')
|
55 |
|
def test_import_template_warning(get_commands, call_command, os_path_exists):
|
|
54 |
def test_import_template_warning(mocked_get_commands, mocked_call_command, mocked_exists):
|
56 |
55 |
"""returns if template is not found"""
|
57 |
|
|
58 |
|
get_commands.return_value = 'import_site'
|
59 |
|
call_command.return_value = mock.MagicMock()
|
60 |
|
os_path_exists.return_value = False
|
|
56 |
mocked_get_commands.return_value = 'import_site'
|
|
57 |
mocked_call_command.return_value = mock.MagicMock()
|
|
58 |
mocked_exists.return_value = False
|
61 |
59 |
|
62 |
60 |
# simulate:
|
63 |
61 |
# $ XXX-manage import-template import_me
|
... | ... | |
67 |
65 |
command.handle(template_name='import_me')
|
68 |
66 |
|
69 |
67 |
# assert 'import_site' command is not run
|
70 |
|
assert call_command.mock_calls == []
|
|
68 |
assert mocked_call_command.mock_calls == []
|
|
69 |
|
|
70 |
|
|
71 |
@mock.patch('hobo.agent.common.management.commands.import_template.Command.handle')
|
|
72 |
def test_import_template_on_combo_agent(mocked_super):
|
|
73 |
"""overloaded import-template for combo"""
|
|
74 |
command = load_command_class('hobo.agent.combo', 'import_template')
|
|
75 |
|
|
76 |
# template specified and found
|
|
77 |
command.handle(template_name='my-template')
|
|
78 |
assert mocked_super.mock_calls == [mock.call(template_name='my-template')]
|
|
79 |
|
|
80 |
# do nothing on internal templates
|
|
81 |
mocked_super.side_effect = UnknownTemplateError
|
|
82 |
for template in 'portal-user', 'portal-agent':
|
|
83 |
command.handle(template_name=template)
|
|
84 |
|
|
85 |
# template no found
|
|
86 |
with pytest.raises(UnknownTemplateError):
|
|
87 |
command.handle(template_name='unknown-template')
|