Projet

Général

Profil

0002-hobo_deploy-add-unit-tests-for-import_template.py-33.patch

Nicolas Roche, 29 mai 2019 14:14

Télécharger (7,43 ko)

Voir les différences:

Subject: [PATCH 2/2] hobo_deploy: add unit tests for import_template.py
 (#33333)

 tests/test_import_template.py         | 57 +++++++++++++++++----------
 tests_schemas/test_import_template.py | 42 ++++++++++++++++++++
 2 files changed, 79 insertions(+), 20 deletions(-)
 create mode 100644 tests_schemas/test_import_template.py
tests/test_import_template.py
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')
tests_schemas/test_import_template.py
1
import os
2

  
3
import mock
4
import pytest
5

  
6
from django.core.management import call_command, load_command_class
7
from hobo.agent.common.management.commands.import_template import UnknownTemplateError
8

  
9
@mock.patch('hobo.agent.common.management.commands.import_template.call_command')
10
@mock.patch('hobo.agent.common.management.commands.import_template.get_commands')
11
def test_import_template_on_common_agent(mocked_get_commands, mocked_call_command, tmpdir):
12
    """import-template basic case (for passerelle here):
13
    $ passerelle-manage import_template my-template  # simulate this bash query
14
    check this call result in '$ passerelle-manage import_site my-template'
15
    """
16
    command = load_command_class('hobo.agent.common', 'import_template')
17

  
18
    mocked_get_commands.return_value = ['import_site']
19
    template_path = os.path.join(str(tmpdir), 'my-template.json')
20
    with open(template_path, 'w') as handler:
21
        handler.write('...')
22

  
23
    command.handle(basepath=str(tmpdir), template_name='my-template')
24
    assert mocked_call_command.mock_calls == [mock.call('import_site', template_path)]
25

  
26

  
27
@mock.patch('hobo.agent.common.management.commands.import_template.call_command')
28
@mock.patch('hobo.agent.common.management.commands.import_template.get_commands')
29
def test_import_template_on_combo_agent(mocked_get_commands, mocked_call_command, tmpdir):
30
    """overloaded case for combo
31
    $ combo-manage import_template my-template  # simulate this bash query
32
    only check we reach the hobo.agent.common's code here
33
    """
34
    command = load_command_class('hobo.agent.combo', 'import_template')
35

  
36
    mocked_get_commands.return_value = ['import_site']
37
    template_path = os.path.join(str(tmpdir), 'my-template.json')
38
    with open(template_path, 'w') as handler:
39
        handler.write('...')
40

  
41
    command.handle(basepath=str(tmpdir), template_name='my-template')
42
    assert mocked_call_command.mock_calls == [mock.call('import_site', template_path)]
0
-