cook: simplify command call in tests (#72335)
This commit is contained in:
parent
50ef55637d
commit
bc8e7a3867
|
@ -59,6 +59,7 @@ def get_domain(url):
|
|||
|
||||
|
||||
class Command(BaseCommand):
|
||||
permissive = False
|
||||
must_notify = False
|
||||
verbosity = 1
|
||||
|
||||
|
|
|
@ -6,6 +6,7 @@ from unittest.mock import Mock, call, mock_open, patch
|
|||
import pytest
|
||||
from django.contrib.auth.models import User
|
||||
from django.contrib.contenttypes.models import ContentType
|
||||
from django.core.management import call_command
|
||||
from django.core.management.base import CommandError
|
||||
|
||||
from hobo.environment.management.commands.cook import Command
|
||||
|
@ -32,9 +33,27 @@ def mocked_notify_agents():
|
|||
yield mocked_notify_agents
|
||||
|
||||
|
||||
def test_check_action(monkeypatch):
|
||||
@pytest.fixture
|
||||
def command():
|
||||
return Command()
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def cook(command, tmpdir):
|
||||
class Cook:
|
||||
counter = 0
|
||||
|
||||
def __call__(self, recipe, **kwargs):
|
||||
path = tmpdir / f'recipe-{self.counter}.json'
|
||||
self.counter += 1
|
||||
path.write_text(json.dumps(recipe), 'utf-8')
|
||||
call_command(command, str(path), **kwargs)
|
||||
|
||||
return Cook()
|
||||
|
||||
|
||||
def test_check_action(command, monkeypatch):
|
||||
"""no exception raised if url are available"""
|
||||
command = Command()
|
||||
command.server_action = 'mock a server_action handler (ex: hobo-create)'
|
||||
action, action_args = 'server-action', {'url': 'https://test.org/'}
|
||||
|
||||
|
@ -44,9 +63,8 @@ def test_check_action(monkeypatch):
|
|||
assert True
|
||||
|
||||
|
||||
def test_check_action_unknown_action(monkeypatch):
|
||||
def test_check_action_unknown_action(command, monkeypatch):
|
||||
"""raise CommandError"""
|
||||
command = Command()
|
||||
command.server_action = 'mock a server_action handler (ex: hobo-create)'
|
||||
action, action_args = 'not-a-server-action', {'url': 'https://test.org/'}
|
||||
|
||||
|
@ -57,9 +75,8 @@ def test_check_action_unknown_action(monkeypatch):
|
|||
assert 'Unknown action not-a-server-action' in str(e_info.value)
|
||||
|
||||
|
||||
def test_check_action_unresolvable(monkeypatch):
|
||||
def test_check_action_unresolvable(command, monkeypatch):
|
||||
"""raise CommandError"""
|
||||
command = Command()
|
||||
command.server_action = 'mock a server_action handler (ex: hobo-create)'
|
||||
action, action_args = 'server-action', {'url': 'https://test.org/'}
|
||||
|
||||
|
@ -70,9 +87,8 @@ def test_check_action_unresolvable(monkeypatch):
|
|||
assert 'test.org is not resolvable in URL https://test.org/' in str(e_info.value)
|
||||
|
||||
|
||||
def test_check_action_invalid_certificat(monkeypatch):
|
||||
def test_check_action_invalid_certificat(command, monkeypatch):
|
||||
"""raise CommandError"""
|
||||
command = Command()
|
||||
command.server_action = 'mock a server_action handler (ex: hobo-create)'
|
||||
action, action_args = 'server-action', {'url': 'https://test.org/'}
|
||||
|
||||
|
@ -83,9 +99,8 @@ def test_check_action_invalid_certificat(monkeypatch):
|
|||
assert 'no valid certificate for https://test.org/' in str(e_info.value)
|
||||
|
||||
|
||||
def test_handle():
|
||||
def test_handle(command):
|
||||
kwargs = {'verbosity': 0, 'timeout': 'timeout value', 'permissive': 'permissive value'}
|
||||
command = Command()
|
||||
command.run_cook = Mock()
|
||||
|
||||
command.handle('recipe.json', **kwargs)
|
||||
|
@ -95,46 +110,30 @@ def test_handle():
|
|||
assert command.run_cook.mock_calls == [call('recipe.json')]
|
||||
|
||||
|
||||
def test_run_cook(mocked_notify_agents, tmpdir):
|
||||
command = Command()
|
||||
command.permissive = False
|
||||
def test_run_cook(command, cook, mocked_notify_agents):
|
||||
command.must_notify = True
|
||||
command.timeout = 42
|
||||
command.check_action = Mock()
|
||||
command.create_hobo = Mock()
|
||||
mocked_notify_agents = Mock()
|
||||
command.wait_operationals = Mock()
|
||||
|
||||
recipe = {'steps': [{'create-hobo': {'url': 'https://entrouvert.org/'}}]}
|
||||
recipe_path = os.path.join(str(tmpdir), 'recipe.json')
|
||||
with open(recipe_path, 'w') as handler:
|
||||
handler.write(json.dumps(recipe))
|
||||
cook(recipe, timeout=42, permissive=False)
|
||||
|
||||
command.run_cook(recipe_path)
|
||||
assert command.check_action.mock_calls == [call('create-hobo', {'url': 'https://entrouvert.org/'})]
|
||||
assert command.create_hobo.mock_calls == [call(url='https://entrouvert.org/')]
|
||||
assert mocked_notify_agents.mock_calls == []
|
||||
assert mocked_notify_agents.call_count == 2
|
||||
assert command.wait_operationals.mock_calls == [call(timeout=42)]
|
||||
|
||||
|
||||
def test_having_several_action_args(tmpdir):
|
||||
command = Command()
|
||||
command.permissive = True
|
||||
def test_having_several_action_args(command, cook):
|
||||
command.create_authentic = Mock()
|
||||
|
||||
recipe = {'steps': [{'create-authentic': {'url': 'https://entrouvert.org/', 'title': 'Connexion'}}]}
|
||||
recipe_path = os.path.join(str(tmpdir), 'recipe.json')
|
||||
with open(recipe_path, 'w') as handler:
|
||||
handler.write(json.dumps(recipe))
|
||||
|
||||
command.run_cook(recipe_path)
|
||||
cook(recipe, permissive=True)
|
||||
assert command.create_authentic.mock_calls == [call(title='Connexion', url='https://entrouvert.org/')]
|
||||
|
||||
|
||||
def test_load_variables_from(db, tmpdir):
|
||||
def test_load_variables_from(command, cook, tmpdir):
|
||||
"""load variables from a file"""
|
||||
command = Command()
|
||||
command.permissive = True
|
||||
command.create_hobo = Mock()
|
||||
command.create_authentic = Mock()
|
||||
command.create_combo = Mock()
|
||||
|
@ -153,17 +152,14 @@ def test_load_variables_from(db, tmpdir):
|
|||
{'create-combo': {'url': 'https://${bar}/', 'not_a_string': []}},
|
||||
],
|
||||
}
|
||||
recipe_path = os.path.join(str(tmpdir), 'recipe.json')
|
||||
with open(recipe_path, 'w') as handler:
|
||||
handler.write(json.dumps(recipe))
|
||||
cook(recipe, permissive=True)
|
||||
|
||||
command.run_cook(recipe_path)
|
||||
assert command.create_hobo.mock_calls == [call(url='https://entrouvert.org/')]
|
||||
assert command.create_authentic.mock_calls == [call(url='https://foo1/')]
|
||||
assert command.create_combo.mock_calls == [call(not_a_string=[], url='https://bar2/')]
|
||||
|
||||
|
||||
def test_wait_operationals(db, monkeypatch):
|
||||
def test_wait_operationals(command, db, monkeypatch):
|
||||
service1 = Mock()
|
||||
service2 = Mock()
|
||||
obj1 = Mock()
|
||||
|
@ -180,7 +176,6 @@ def test_wait_operationals(db, monkeypatch):
|
|||
service1.objects.all = Mock(return_value=[obj1])
|
||||
service2.objects.all = Mock(return_value=[obj2])
|
||||
monkeypatch.setattr('hobo.environment.management.commands.cook.AVAILABLE_SERVICES', [service1, service2])
|
||||
command = Command()
|
||||
|
||||
# already operational
|
||||
obj1.last_operational_success_timestamp = 'some date'
|
||||
|
@ -195,9 +190,7 @@ def test_wait_operationals(db, monkeypatch):
|
|||
command.wait_operationals(0.6)
|
||||
|
||||
|
||||
def test_set_variable(db):
|
||||
command = Command()
|
||||
|
||||
def test_set_variable(command, db):
|
||||
command.set_variable('foo', 'bar')
|
||||
var = Variable.objects.get(name='foo')
|
||||
assert var.value == 'bar'
|
||||
|
@ -227,8 +220,7 @@ def test_set_variable(db):
|
|||
assert var.auto is True
|
||||
|
||||
|
||||
def test_set_attribute(db):
|
||||
command = Command()
|
||||
def test_set_attribute(command, db):
|
||||
command.set_attribute('foo_name', 'foo_label')
|
||||
values = AttributeDefinition.objects.filter(name='foo_name')
|
||||
assert values.count() == 1
|
||||
|
@ -242,8 +234,7 @@ def test_set_attribute(db):
|
|||
assert values[0].disabled is True
|
||||
|
||||
|
||||
def test_disable_attribute(db):
|
||||
command = Command()
|
||||
def test_disable_attribute(command, db):
|
||||
command.set_attribute('foo_name', 'foo_label')
|
||||
values = AttributeDefinition.objects.filter(name='foo_name')
|
||||
assert values.count() == 1
|
||||
|
@ -259,8 +250,7 @@ def test_disable_attribute(db):
|
|||
assert values.count() == 0
|
||||
|
||||
|
||||
def test_enable_attribute(db):
|
||||
command = Command()
|
||||
def test_enable_attribute(command, db):
|
||||
command.set_attribute('foo_name', 'foo_label', disabled=True)
|
||||
values = AttributeDefinition.objects.filter(name='foo_name')
|
||||
assert values.count() == 1
|
||||
|
@ -276,8 +266,7 @@ def test_enable_attribute(db):
|
|||
assert values.count() == 0
|
||||
|
||||
|
||||
def test_create_superuser(db):
|
||||
command = Command()
|
||||
def test_create_superuser(command, db):
|
||||
command.create_superuser()
|
||||
assert User.objects.count() == 1
|
||||
user = User.objects.all()[0]
|
||||
|
@ -285,8 +274,7 @@ def test_create_superuser(db):
|
|||
assert user.is_superuser is True
|
||||
|
||||
|
||||
def test_create_site(db):
|
||||
command = Command()
|
||||
def test_create_site(command, db):
|
||||
base_url = 'http://entrouvert.org'
|
||||
title = 'site title'
|
||||
slug = None
|
||||
|
@ -319,8 +307,7 @@ def test_create_site(db):
|
|||
@patch('hobo.environment.management.commands.cook.connection')
|
||||
@patch('hobo.environment.management.commands.cook.call_command')
|
||||
@patch('hobo.environment.management.commands.cook.TenantMiddleware')
|
||||
def test_create_hobo_primary(mocked_TenantMiddleware, mocked_call_command, mocked_connection):
|
||||
command = Command()
|
||||
def test_create_hobo_primary(mocked_TenantMiddleware, mocked_call_command, mocked_connection, command):
|
||||
command.create_site = Mock()
|
||||
tenant = Mock()
|
||||
tenant.schema_name = 'public'
|
||||
|
@ -346,8 +333,7 @@ def test_create_hobo_primary(mocked_TenantMiddleware, mocked_call_command, mocke
|
|||
@patch('hobo.environment.management.commands.cook.connection')
|
||||
@patch('hobo.environment.management.commands.cook.call_command')
|
||||
@patch('hobo.environment.management.commands.cook.TenantMiddleware')
|
||||
def test_create_hobo_not_primary(mocked_TenantMiddleware, mocked_call_command, mocked_connection):
|
||||
command = Command()
|
||||
def test_create_hobo_not_primary(mocked_TenantMiddleware, mocked_call_command, mocked_connection, command):
|
||||
command.timeout = 42
|
||||
command.wait_operationals = Mock()
|
||||
command.create_site = Mock()
|
||||
|
@ -369,8 +355,7 @@ def test_create_hobo_not_primary(mocked_TenantMiddleware, mocked_call_command, m
|
|||
assert mocked_open.mock_calls == []
|
||||
|
||||
|
||||
def test_create_services():
|
||||
command = Command()
|
||||
def test_create_services(command):
|
||||
command.create_site = Mock()
|
||||
command.create_authentic('url', 'title')
|
||||
command.create_combo('url', 'title')
|
||||
|
@ -396,9 +381,7 @@ def test_create_services():
|
|||
]
|
||||
|
||||
|
||||
def test_set_idp(db):
|
||||
command = Command()
|
||||
|
||||
def test_set_idp(command, db):
|
||||
# exceptions maybe we should handle into cook.py ?
|
||||
with pytest.raises(Authentic.DoesNotExist, match='Authentic matching query does not exist'):
|
||||
command.set_idp('url')
|
||||
|
@ -427,9 +410,8 @@ def test_set_idp(db):
|
|||
@patch('hobo.environment.management.commands.cook.set_theme')
|
||||
@patch('hobo.environment.management.commands.cook.connection')
|
||||
@patch('hobo.agent.common.management.commands.hobo_deploy.Command.configure_theme')
|
||||
def test_set_theme(mocked_configure_theme, mocked_connection, mocked_set_theme):
|
||||
def test_set_theme(mocked_configure_theme, mocked_connection, mocked_set_theme, command):
|
||||
mocked_connection.get_tenant = Mock(return_value='the tenant')
|
||||
command = Command()
|
||||
command.set_theme('the theme')
|
||||
|
||||
assert mocked_set_theme.mock_calls == [call('the theme')]
|
||||
|
@ -438,10 +420,9 @@ def test_set_theme(mocked_configure_theme, mocked_connection, mocked_set_theme):
|
|||
|
||||
|
||||
@patch('hobo.environment.management.commands.cook.connection')
|
||||
def test_cook(mocked_connection):
|
||||
def test_cook(mocked_connection, command):
|
||||
mocked_connection.get_tenant = Mock(return_value='the tenant')
|
||||
mocked_connection.set_tenant = Mock()
|
||||
command = Command()
|
||||
command.run_cook = Mock()
|
||||
command.cook('a-recipe-file.json')
|
||||
|
||||
|
|
Loading…
Reference in New Issue