Projet

Général

Profil

0003-hobo_deploy-do-not-recall-import_template-when-it-pr.patch

Nicolas Roche, 21 juin 2019 16:27

Télécharger (5,6 ko)

Voir les différences:

Subject: [PATCH 3/3] hobo_deploy: do not recall import_template when it
 previously success (#33875)

 .../management/commands/import_template.py    |  5 ++++-
 tests/test_import_template.py                 | 19 +++++++++++++++----
 tests_schemas/test_import_template.py         | 12 ++++++++----
 3 files changed, 27 insertions(+), 9 deletions(-)
hobo/agent/common/management/commands/import_template.py
30 30
    def add_arguments(self, parser):
31 31
        parser.add_argument('template_name', type=str)
32 32
        parser.add_argument('--basepath', type=str)
33
        parser.add_argument('--again', action='store_true', default=False,
34
                            help='Force re-import site')
33 35

  
34 36
    def handle(self, *args, **kwargs):
35 37
        app_name = settings.PROJECT_NAME
38
        again = kwargs.get('again')
36 39
        basepath = kwargs.get('basepath') or '/var/lib/%s/skeletons' % app_name
37 40
        template = '%s/%s.json' % (basepath, kwargs.get('template_name'))
38 41

  
......
40 43
            if not os.path.exists(template):
41 44
                raise UnknownTemplateError('unknown template (%r)' % template)
42 45
            else:
43
                call_command('import_site', template)
46
                call_command('import_site', template, if_empty=not again)
tests/test_import_template.py
21 21
    # simulate:
22 22
    # $ XXX-manage import-template import_me
23 23
    command = Command()
24
    with override_settings(PROJECT_NAME='bobo'):
24
    with override_settings(PROJECT_NAME='hobo'):
25 25
        command.handle(template_name='import_me')
26 26

  
27 27
    # assert 'import_site' command is run
28 28
    assert mocked_call_command.mock_calls == [
29
        mock.call('import_site', '/var/lib/bobo/skeletons/import_me.json')]
29
        mock.call('import_site', '/var/lib/hobo/skeletons/import_me.json',
30
                  if_empty=True)]
31

  
32
    # set again flag
33
    mocked_call_command.reset_mock()
34
    kwargs = {'again': True}
35
    with override_settings(PROJECT_NAME='hobo'):
36
        command.handle(template_name='import_me', **kwargs)
37
    assert mocked_call_command.mock_calls == [
38
        mock.call('import_site', '/var/lib/hobo/skeletons/import_me.json',
39
                  if_empty=False)]
40

  
30 41

  
31 42
@mock.patch('hobo.agent.common.management.commands.import_template.os.path.exists')
32 43
@mock.patch('hobo.agent.common.management.commands.import_template.call_command')
......
40 51
    # simulate:
41 52
    # $ XXX-manage import-template import_me --basepath /tmp
42 53
    command = Command()
43
    with override_settings(PROJECT_NAME='bobo'):
54
    with override_settings(PROJECT_NAME='hobo'):
44 55
        command.handle(template_name='import_me', basepath='/tmp')
45 56

  
46 57
    # assert 'import_site' command is run
47 58
    assert mocked_call_command.mock_calls == [
48
        mock.call('import_site', '/tmp/import_me.json')]
59
        mock.call('import_site', '/tmp/import_me.json', if_empty=True)]
49 60

  
50 61

  
51 62
@mock.patch('hobo.agent.common.management.commands.import_template.os.path.exists')
tests_schemas/test_import_template.py
14 14
    check this call result in '$ passerelle-manage import_site my-template'
15 15
    """
16 16
    command = load_command_class('hobo.agent.common', 'import_template')
17
    kwargs = {'if_empty': False}
17 18

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

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

  
26 28

  
27 29
@mock.patch('hobo.agent.common.management.commands.import_template.call_command')
......
32 34
    only check we reach the hobo.agent.common's code here
33 35
    """
34 36
    command = load_command_class('hobo.agent.combo', 'import_template')
37
    kwargs = {'if_empty': False}
35 38

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

  
41
    command.handle(basepath=str(tmpdir), template_name='my-template')
42
    assert mocked_call_command.mock_calls == [mock.call('import_site', template_path)]
44
    command.handle(basepath=str(tmpdir), template_name='my-template', **kwargs)
45
    assert mocked_call_command.mock_calls == [
46
        mock.call('import_site', template_path, if_empty=True)]
43
-