hobo/tests_multipublik/test_multipublik.py

77 lines
3.2 KiB
Python

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