add basic multipublik deployment tests

This commit is contained in:
Frédéric Péters 2017-01-04 08:58:31 +01:00
parent f2be7d730e
commit 8371097f3b
4 changed files with 134 additions and 0 deletions

View File

@ -0,0 +1,32 @@
import os
import tempfile
import shutil
import json
import pytest
@pytest.fixture(scope='function')
def tenants(transactional_db, request, settings):
from hobo.multitenant.models import Tenant
base = tempfile.mkdtemp('combo-tenant-base')
settings.TENANT_BASE = base
@pytest.mark.django_db
def make_tenant(name):
tenant_dir = os.path.join(base, name)
os.mkdir(tenant_dir)
with open(os.path.join(tenant_dir, 'unsecure'), 'w') as fd:
fd.write('1')
t = Tenant(domain_url=name,
schema_name=name.replace('-', '_').replace('.', '_'))
t.create_schema()
return t
tenants = [make_tenant('tenant1.example.net')]
def fin():
from django.db import connection
connection.set_schema_to_public()
for t in tenants:
t.delete(True)
shutil.rmtree(base)
request.addfinalizer(fin)
return tenants

View File

@ -0,0 +1,23 @@
import os.path
import __builtin__ as builtin
from mock import mock_open, patch
from hobo.settings import *
LANGUAGE_CODE = 'en-us'
PROJECT_NAME = 'multipublik'
with patch.object(builtin, 'file', mock_open(read_data='xxx')):
execfile(os.path.join(os.path.dirname(__file__), '../debian/debian_config_common.py'))
CACHES = {
'default': {
'BACKEND': 'hobo.multitenant.cache.TenantCache',
'REAL_BACKEND': 'django.core.cache.backends.locmem.LocMemCache',
}
}
STATIC_URL = '/static/'
MELLON_IDENTITY_PROVIDERS = []
LOGGING = None

View File

@ -0,0 +1,76 @@
import json
import os
import random
import shutil
import tempfile
import pytest
from django.conf import settings
from tenant_schemas.utils import tenant_context
from hobo.agent.hobo.management.commands.hobo_deploy import Command as HoboDeployCommand
from hobo.multitenant.management.commands.create_hobo_tenant import Command as CreateHoboTenant
from hobo.multitenant.middleware import TenantMiddleware
from hobo.environment.models import Hobo, Combo
from hobo.deploy.utils import get_hobo_json
def get_hobo_json_filename(tenant):
with tenant_context(tenant):
hobo_json = get_hobo_json()
json_filename = os.path.join(settings.TENANT_BASE, 'tmp%s.json' % random.random())
with open(json_filename, 'w') as fd:
json.dump(hobo_json, fd, indent=2)
return json_filename
def test_multipublik(tenants):
hobo1 = tenants[0]
hobo1.base_url = 'http://tenant1.example.net/'
with tenant_context(hobo1):
hobo2 = Hobo(title='title', slug='slug', base_url='http://hobo2.example.net')
hobo2.save()
hobo3 = Hobo(title='title', slug='slug', base_url='http://hobo3.example.net')
hobo3.save()
combo = Combo(title='xxx', slug='xxx', base_url='http://combo1.example.net')
combo.save()
HoboDeployCommand().handle(hobo2.base_url, get_hobo_json_filename(hobo1))
hobo2 = TenantMiddleware.get_tenant_by_hostname('hobo2.example.net')
hobo2.base_url = 'http://hobo2.example.net/'
with tenant_context(hobo2):
combo = Combo(title='xxx2', slug='xxx2', base_url='http://combo2.example.net')
combo.save()
HoboDeployCommand().handle(hobo1.base_url, get_hobo_json_filename(hobo2))
with tenant_context(hobo1):
assert Combo.objects.filter(secondary=True).count() == 1
assert Combo.objects.filter(secondary=False).count() == 1
HoboDeployCommand().handle(hobo2.base_url, get_hobo_json_filename(hobo1))
with tenant_context(hobo1):
assert Hobo.objects.filter(secondary=True).count() == 0
assert Combo.objects.filter(secondary=False).count() == 1
assert Combo.objects.filter(secondary=False).count() == 1
# another secondary hobo
HoboDeployCommand().handle(hobo3.base_url, get_hobo_json_filename(hobo1))
hobo3 = TenantMiddleware.get_tenant_by_hostname('hobo3.example.net')
hobo3.base_url = 'http://hobo3.example.net/'
with tenant_context(hobo3):
combo = Combo(title='xxx3', slug='xxx3', base_url='http://combo3.example.net')
combo.save()
HoboDeployCommand().handle(hobo1.base_url, get_hobo_json_filename(hobo3))
with tenant_context(hobo1):
assert Combo.objects.filter(secondary=True).count() == 2
assert Combo.objects.filter(secondary=False).count() == 1
HoboDeployCommand().handle(hobo2.base_url, get_hobo_json_filename(hobo1))
with tenant_context(hobo2):
assert Combo.objects.filter(secondary=True).count() == 1
assert Combo.objects.filter(secondary=False).count() == 1
HoboDeployCommand().handle(hobo2.base_url, get_hobo_json_filename(hobo3))
with tenant_context(hobo2):
assert Combo.objects.filter(secondary=True).count() == 1
assert Combo.objects.filter(secondary=False).count() == 1

View File

@ -15,6 +15,8 @@ setenv =
hobo: HOBO_SETTINGS_FILE=tests/settings.py
multitenant: PYTHONPATH=tests_multitenant
multitenant: DJANGO_SETTINGS_MODULE=settings
multipublik: PYTHONPATH=tests_multipublik
multipublik: DJANGO_SETTINGS_MODULE=settings
authentic: DEBIAN_CONFIG_COMMON=debian/debian_config_common.py
authentic: DJANGO_SETTINGS_MODULE=authentic2.settings
authentic: AUTHENTIC2_SETTINGS_FILE=tests_authentic/settings.py
@ -47,6 +49,7 @@ commands =
./getlasso.sh
hobo: py.test {env:COVERAGE:} {env:NOMIGRATIONS:} {posargs:tests/}
multitenant: py.test {env:COVERAGE:} {env:NOMIGRATIONS:} {posargs:tests_multitenant/}
multipublik: py.test {env:COVERAGE:} {env:NOMIGRATIONS:} {posargs:tests_multipublik/}
authentic: py.test {env:FAST:} {env:COVERAGE:} {env:NOMIGRATIONS:} {posargs:tests_authentic/}
passerelle: py.test {env:COVERAGE:} {env:NOMIGRATIONS:} {posargs:tests_passerelle/}
coverage: mv coverage.xml coverage-{envname}.xml