django-orchestra/orchestra/apps/websites/tests/functional_tests/tests.py

156 lines
6.0 KiB
Python
Raw Normal View History

2014-10-10 14:39:46 +00:00
import os
import socket
import time
import textwrap
from django.conf import settings as djsettings
from django.contrib.contenttypes.models import ContentType
from django.core.management.base import CommandError
from django.core.urlresolvers import reverse
import requests
from selenium.webdriver.support.select import Select
from orchestra.apps.accounts.models import Account
from orchestra.apps.domains.models import Domain, Record
from orchestra.apps.domains.backends import Bind9MasterDomainBackend
from orchestra.apps.orchestration.models import Server, Route
from orchestra.apps.resources.models import Resource
from orchestra.apps.webapps.tests.functional_tests.tests import StaticWebAppMixin, RESTWebAppMixin, WebAppMixin, PHPFcidWebAppMixin, PHPFPMWebAppMixin
from orchestra.utils.system import run, sshrun
from orchestra.utils.tests import BaseLiveServerTestCase, random_ascii, snapshot_on_error, save_response_on_error
from ... import backends, settings
from ...models import Website
class WebsiteMixin(WebAppMixin):
MASTER_SERVER = os.environ.get('ORCHESTRA_MASTER_SERVER', 'localhost')
MASTER_SERVER_ADDR = socket.gethostbyname(MASTER_SERVER)
DEPENDENCIES = (
'orchestra.apps.orchestration',
'orchestra.apps.domains',
'orchestra.apps.websites',
'orchestra.apps.webapps',
'orchestra.apps.systemusers',
)
def add_route(self):
super(WebsiteMixin, self).add_route()
server = Server.objects.get()
backend = backends.apache.Apache2Backend.get_name()
Route.objects.get_or_create(backend=backend, match=True, host=server)
2014-10-10 14:39:46 +00:00
backend = Bind9MasterDomainBackend.get_name()
Route.objects.get_or_create(backend=backend, match=True, host=server)
2014-10-10 14:39:46 +00:00
def validate_add_website(self, name, domain):
url = 'http://%s/%s' % (domain.name, self.page[0])
self.assertEqual(self.page[2], requests.get(url).content)
def test_add(self):
# TODO domains with "_" bad name!
domain_name = '%sdomain.lan' % random_ascii(10)
domain = Domain.objects.create(name=domain_name, account=self.account)
domain.records.create(type=Record.A, value=self.MASTER_SERVER_ADDR)
self.save_domain(domain)
webapp = '%s_%s_webapp' % (random_ascii(10), self.type_value)
self.add_webapp(webapp)
self.addCleanup(self.delete_webapp, webapp)
self.upload_webapp(webapp)
2014-10-10 14:39:46 +00:00
website = '%s_website' % random_ascii(10)
self.add_website(website, domain, webapp)
self.addCleanup(self.delete_website, website)
2014-10-10 14:39:46 +00:00
self.validate_add_website(website, domain)
class RESTWebsiteMixin(RESTWebAppMixin):
@save_response_on_error
def save_domain(self, domain):
self.rest.domains.retrieve().get().save()
@save_response_on_error
def add_website(self, name, domain, webapp, path='/'):
domain = self.rest.domains.retrieve(name=domain).get()
webapp = self.rest.webapps.retrieve(name=webapp).get()
contents = [{
'webapp': webapp,
'path': path
}]
self.rest.websites.create(name=name, domains=[domain], contents=contents)
@save_response_on_error
def delete_website(self, name):
self.rest.websites.retrieve(name=name).delete()
@save_response_on_error
def add_content(self, website, webapp, path):
website = self.rest.websites.retrieve(name=website).get()
webapp = self.rest.webapps.retrieve(name=webapp).get()
website.contents.append({
'webapp': webapp,
'path': path,
})
website.save()
2014-10-15 12:47:28 +00:00
# TODO test disable
# TODO test https (refactor ssl)
# TODO test php options
# TODO read php-version /fpm/fcgid
# TODO max_processes, timeouts, memory...
class StaticRESTWebsiteTest(RESTWebsiteMixin, StaticWebAppMixin, WebsiteMixin, BaseLiveServerTestCase):
def test_mix_webapps(self):
domain_name = '%sdomain.lan' % random_ascii(10)
domain = Domain.objects.create(name=domain_name, account=self.account)
domain.records.create(type=Record.A, value=self.MASTER_SERVER_ADDR)
self.save_domain(domain)
webapp = '%s_%s_webapp' % (random_ascii(10), self.type_value)
self.add_webapp(webapp)
self.addCleanup(self.delete_webapp, webapp)
self.upload_webapp(webapp)
website = '%s_website' % random_ascii(10)
self.add_website(website, domain, webapp)
self.addCleanup(self.delete_website, website)
self.validate_add_website(website, domain)
self.type_value = PHPFcidWebAppMixin.type_value
self.backend = PHPFcidWebAppMixin.backend
self.page = PHPFcidWebAppMixin.page
self.add_route()
webapp = '%s_%s_webapp' % (random_ascii(10), self.type_value)
self.add_webapp(webapp)
self.addCleanup(self.delete_webapp, webapp)
self.upload_webapp(webapp)
path = '/%s' % webapp
self.add_content(website, webapp, path)
url = 'http://%s%s/%s' % (domain.name, path, self.page[0])
self.assertEqual(self.page[2], requests.get(url).content)
self.type_value = PHPFPMWebAppMixin.type_value
self.backend = PHPFPMWebAppMixin.backend
self.page = PHPFPMWebAppMixin.page
self.add_route()
webapp = '%s_%s_webapp' % (random_ascii(10), self.type_value)
self.add_webapp(webapp)
self.addCleanup(self.delete_webapp, webapp)
self.upload_webapp(webapp)
path = '/%s' % webapp
self.add_content(website, webapp, path)
url = 'http://%s%s/%s' % (domain.name, path, self.page[0])
self.assertEqual(self.page[2], requests.get(url).content)
2014-10-10 14:39:46 +00:00
class PHPFcidRESTWebsiteTest(RESTWebsiteMixin, PHPFcidWebAppMixin, WebsiteMixin, BaseLiveServerTestCase):
2014-10-10 17:17:20 +00:00
pass
2014-10-10 14:39:46 +00:00
class PHPFPMRESTWebsiteTest(RESTWebsiteMixin, PHPFPMWebAppMixin, WebsiteMixin, BaseLiveServerTestCase):
2014-10-10 14:39:46 +00:00
pass
#class AdminWebsiteTest(AdminWebsiteMixin, BaseLiveServerTestCase):
# pass