Projet

Général

Profil

0001-hobo_deploy-internally-call-import_template-using-te.patch

Nicolas Roche, 12 juin 2019 17:39

Télécharger (6,29 ko)

Voir les différences:

Subject: [PATCH] hobo_deploy: internally call import_template (using
 tenant_command)

 .../common/management/commands/hobo_deploy.py |  6 +++--
 tests/test_hobo_deploy.py                     | 19 +++++++++------
 tests_schemas/env.json                        |  4 ++--
 tests_schemas/test_hobo_deploy.py             | 23 +++++++------------
 4 files changed, 26 insertions(+), 26 deletions(-)
hobo/agent/common/management/commands/hobo_deploy.py
8 8

  
9 9
from django.conf import settings
10 10
from django.core.management.base import BaseCommand, CommandError
11
from django.core.management import call_command, get_commands
11
from django.core.management import call_command, execute_from_command_line, get_commands
12 12

  
13 13
from hobo.multitenant.middleware import TenantMiddleware, TenantNotFound
14 14
from hobo.theme.utils import get_theme
......
192 192
        me = [x for x in hobo_environment.get('services') if x.get('this') is True][0]
193 193

  
194 194
        if 'import_template' in get_commands() and me.get('template_name'):
195
            call_command('import_template', me['template_name'])
195
            execute_from_command_line([sys.argv[0], 'tenant_command',
196
                                       'import_template', me['template_name'],
197
                                       '--domain', tenant.domain_url])
tests/test_hobo_deploy.py
381 381
    assert os.path.exists(theme_path) is False
382 382

  
383 383

  
384
@patch('hobo.agent.common.management.commands.hobo_deploy.execute_from_command_line')
384 385
@patch('hobo.agent.common.management.commands.hobo_deploy.get_commands')
385
@patch('hobo.agent.common.management.commands.hobo_deploy.call_command')
386
def test_configure_template(mocked_call_command, mocked_get_commands):
386
def test_configure_template(mocked_get_commands, mocked_execute_from_command_line):
387 387
    command = Command()
388
    tenant = Mock()
389
    tenant.domain_url = 'combo.dev.publik.love'
388 390
    ENVIRONMENT = {'services': [{'service-id': 'combo',
389 391
                                 'template_name': 'my_template',
390 392
                                 'this': True}]}
......
394 396
    mocked_get_commands.return_value = ['import_template', '...']
395 397

  
396 398
    # main case
397
    command.configure_template(ENVIRONMENT, 'unused')  # TODO: dead tenant parameter
398
    assert mocked_call_command.mock_calls == [call('import_template', 'my_template')]
399
    sys.argv[0] = '.../manage.py'
400
    command.configure_template(ENVIRONMENT, tenant)
401
    assert mocked_execute_from_command_line.mock_calls == [
402
        call(['.../manage.py', 'tenant_command', 'import_template', 'my_template',
403
              '--domain', 'combo.dev.publik.love'])]
399 404

  
400 405
    # no template_name entry provided
401
    mocked_call_command.reset_mock()
406
    mocked_execute_from_command_line.reset_mock()
402 407
    del ENVIRONMENT['services'][0]['template_name']
403
    command.configure_template(ENVIRONMENT, 'unused')
404
    assert mocked_call_command.mock_calls == []
408
    command.configure_template(ENVIRONMENT, tenant)
409
    assert mocked_execute_from_command_line.mock_calls == []
405 410

  
406 411

  
407 412
def test_combo_get_theme(fake_themes):
tests_schemas/env.json
6 6
	    "slug": "agendas",
7 7
	    "title": "CHRONO",
8 8
	    "secret_key": "123",
9
	    "template_name": "import_me.txt"
9
	    "template_name": "import_me"
10 10
	},
11 11
	{
12 12
	    "service-id": "wcs",
......
27 27
	    "slug": "portal",
28 28
	    "title": "COMBO",
29 29
	    "secret_key": "123",
30
	    "template_name": "import_me.txt"
30
	    "template_name": "import_me"
31 31
	},
32 32
	{
33 33
	    "service-id": "authentic",
tests_schemas/test_hobo_deploy.py
1 1
import os
2
import sys
2 3

  
3 4
import mock
4 5
from django.core.management import call_command, get_commands, load_command_class
......
14 15
    assert os.path.exists(tenant_hobo_json)
15 16

  
16 17

  
17
@mock.patch('hobo.agent.common.management.commands.hobo_deploy.call_command')
18
@mock.patch('hobo.agent.common.management.commands.hobo_deploy.execute_from_command_line')
18 19
@mock.patch('hobo.agent.common.management.commands.hobo_deploy.get_commands')
19
def test_import_template(mocked_get_commands, mocked_call_command, db):
20
def test_import_template(mocked_get_commands, mocked_execute_from_command_line, db):
20 21
    """check 'hobo-deploy' result in '$ chrono-manage import_template' call
21 22
    $ chrono-manage hobo_deploy env.json  # simulate this bash query
22

  
23
    warning:
24
    this test must be the first one executed else 'get_commands' fails to be mocked
25 23
    """
26 24
    command = load_command_class('hobo.agent.common', 'hobo_deploy')
27 25
    domain = 'chrono.dev.publik.love'
28

  
29
    def my_call_command(command, parameter):
30
        if command == 'import_template':
31
            my_call_command.import_template_was_called = True
32
            return
33
        call_command(command, parameter)
34

  
35 26
    mocked_get_commands.return_value = ['import_template']
36
    mocked_call_command.side_effect = my_call_command
37
    my_call_command.import_template_was_called = False
27
    sys.argv[0] = '.../manage.py'
38 28

  
39 29
    command.handle('https://%s/' % domain, 'tests_schemas/env.json')
40 30
    assert_deployed(domain)
41 31

  
42 32
    # assert the 'import_template' command was called
43
    assert my_call_command.import_template_was_called is True
33
    assert mocked_execute_from_command_line.mock_calls == [
34
        mock.call(['.../manage.py', 'tenant_command',
35
                   'import_template', 'import_me',
36
                   '--domain', 'chrono.dev.publik.love'])]
44 37

  
45 38

  
46 39
def test_deploy_on_common_agent(db):
47
-