2014-10-10 14:39:46 +00:00
|
|
|
import ftplib
|
|
|
|
import os
|
2015-04-02 16:14:55 +00:00
|
|
|
from io import StringIO
|
2014-10-10 14:39:46 +00:00
|
|
|
|
|
|
|
from django.conf import settings as djsettings
|
|
|
|
|
2015-04-05 10:46:24 +00:00
|
|
|
from orchestra.contrib.orchestration.models import Server, Route
|
2021-03-30 11:11:41 +00:00
|
|
|
from orchestra.contrib.systemusers.backends import UNIXUserController
|
2014-10-10 14:39:46 +00:00
|
|
|
from orchestra.utils.tests import BaseLiveServerTestCase, random_ascii, snapshot_on_error, save_response_on_error
|
|
|
|
|
2015-04-04 18:10:39 +00:00
|
|
|
from ... import backends
|
2014-10-10 14:39:46 +00:00
|
|
|
|
|
|
|
|
|
|
|
class WebAppMixin(object):
|
|
|
|
MASTER_SERVER = os.environ.get('ORCHESTRA_MASTER_SERVER', 'localhost')
|
|
|
|
DEPENDENCIES = (
|
2015-04-05 10:46:24 +00:00
|
|
|
'orchestra.contrib.orchestration',
|
|
|
|
'orchestra.contrib.systemusers',
|
|
|
|
'orchestra.contrib.webapps',
|
2014-10-10 14:39:46 +00:00
|
|
|
)
|
2021-03-30 11:11:41 +00:00
|
|
|
|
2014-10-10 14:39:46 +00:00
|
|
|
def setUp(self):
|
|
|
|
super(WebAppMixin, self).setUp()
|
|
|
|
self.add_route()
|
|
|
|
djsettings.DEBUG = True
|
2021-03-30 11:11:41 +00:00
|
|
|
|
2014-10-10 14:39:46 +00:00
|
|
|
def add_route(self):
|
2014-10-14 13:50:19 +00:00
|
|
|
server, __ = Server.objects.get_or_create(name=self.MASTER_SERVER)
|
2021-03-30 11:11:41 +00:00
|
|
|
backend = UNIXUserController.get_name()
|
2014-10-14 13:50:19 +00:00
|
|
|
Route.objects.get_or_create(backend=backend, match=True, host=server)
|
|
|
|
backend = self.backend.get_name()
|
|
|
|
match = 'webapp.type == "%s"' % self.type_value
|
|
|
|
Route.objects.create(backend=backend, match=match, host=server)
|
2021-03-30 11:11:41 +00:00
|
|
|
|
2014-10-14 13:50:19 +00:00
|
|
|
def upload_webapp(self, name):
|
|
|
|
try:
|
|
|
|
ftp = ftplib.FTP(self.MASTER_SERVER)
|
|
|
|
ftp.login(user=self.account.username, passwd=self.account_password)
|
|
|
|
ftp.cwd('webapps/%s' % name)
|
|
|
|
index = StringIO()
|
|
|
|
index.write(self.page[1])
|
|
|
|
index.seek(0)
|
|
|
|
ftp.storbinary('STOR %s' % self.page[0], index)
|
|
|
|
index.close()
|
|
|
|
finally:
|
|
|
|
ftp.close()
|
2021-03-30 11:11:41 +00:00
|
|
|
|
2014-10-10 14:39:46 +00:00
|
|
|
def test_add(self):
|
|
|
|
name = '%s_%s_webapp' % (random_ascii(10), self.type_value)
|
|
|
|
self.add_webapp(name)
|
2014-10-14 13:50:19 +00:00
|
|
|
self.addCleanup(self.delete_webapp, name)
|
|
|
|
self.upload_webapp(name)
|
2014-10-10 14:39:46 +00:00
|
|
|
|
|
|
|
|
|
|
|
class StaticWebAppMixin(object):
|
2016-03-08 10:16:49 +00:00
|
|
|
backend = backends.static.StaticController
|
2014-10-10 14:39:46 +00:00
|
|
|
type_value = 'static'
|
|
|
|
token = random_ascii(100)
|
|
|
|
page = (
|
|
|
|
'index.html',
|
|
|
|
'<html>Hello World! %s </html>\n' % token,
|
|
|
|
'<html>Hello World! %s </html>\n' % token,
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
class PHPFcidWebAppMixin(StaticWebAppMixin):
|
|
|
|
backend = backends.phpfcgid.PHPFcgidBackend
|
2014-10-23 21:25:44 +00:00
|
|
|
type_value = 'php5.2'
|
2014-10-10 14:39:46 +00:00
|
|
|
token = random_ascii(100)
|
|
|
|
page = (
|
|
|
|
'index.php',
|
|
|
|
'<?php print("Hello World! %s");\n?>\n' % token,
|
|
|
|
'Hello World! %s' % token,
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2014-10-10 17:17:20 +00:00
|
|
|
class PHPFPMWebAppMixin(PHPFcidWebAppMixin):
|
2014-10-10 14:39:46 +00:00
|
|
|
backend = backends.phpfpm.PHPFPMBackend
|
|
|
|
type_value = 'php5.5'
|
|
|
|
|
|
|
|
|
|
|
|
class RESTWebAppMixin(object):
|
|
|
|
def setUp(self):
|
|
|
|
super(RESTWebAppMixin, self).setUp()
|
|
|
|
self.rest_login()
|
|
|
|
# create main user
|
|
|
|
self.save_systemuser()
|
2021-03-30 11:11:41 +00:00
|
|
|
|
2014-10-10 14:39:46 +00:00
|
|
|
@save_response_on_error
|
|
|
|
def save_systemuser(self):
|
2014-10-28 09:51:27 +00:00
|
|
|
systemuser = self.rest.systemusers.retrieve().get()
|
|
|
|
systemuser.update(is_active=True)
|
2021-03-30 11:11:41 +00:00
|
|
|
|
2014-10-10 14:39:46 +00:00
|
|
|
@save_response_on_error
|
|
|
|
def add_webapp(self, name, options=[]):
|
2014-10-14 13:50:19 +00:00
|
|
|
self.rest.webapps.create(name=name, type=self.type_value, options=options)
|
2021-03-30 11:11:41 +00:00
|
|
|
|
2014-10-10 14:39:46 +00:00
|
|
|
@save_response_on_error
|
|
|
|
def delete_webapp(self, name):
|
2014-10-14 13:50:19 +00:00
|
|
|
self.rest.webapps.retrieve(name=name).delete()
|
2014-10-10 14:39:46 +00:00
|
|
|
|
|
|
|
|
|
|
|
class AdminWebAppMixin(WebAppMixin):
|
|
|
|
def setUp(self):
|
|
|
|
super(AdminWebAppMixin, self).setUp()
|
|
|
|
self.admin_login()
|
|
|
|
# create main user
|
|
|
|
self.save_systemuser()
|
2021-03-30 11:11:41 +00:00
|
|
|
|
2014-10-14 13:50:19 +00:00
|
|
|
@snapshot_on_error
|
|
|
|
def save_systemuser(self):
|
|
|
|
url = ''
|
2021-03-30 11:11:41 +00:00
|
|
|
|
2014-10-10 14:39:46 +00:00
|
|
|
@snapshot_on_error
|
|
|
|
def add(self, name, password, admin_email):
|
2014-10-14 13:50:19 +00:00
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
class StaticRESTWebAppTest(StaticWebAppMixin, RESTWebAppMixin, WebAppMixin, BaseLiveServerTestCase):
|
2014-10-10 17:17:20 +00:00
|
|
|
pass
|
|
|
|
|
|
|
|
|
2014-10-14 13:50:19 +00:00
|
|
|
class PHPFcidRESTWebAppTest(PHPFcidWebAppMixin, RESTWebAppMixin, WebAppMixin, BaseLiveServerTestCase):
|
2014-10-10 14:39:46 +00:00
|
|
|
pass
|
|
|
|
|
|
|
|
|
2014-10-14 13:50:19 +00:00
|
|
|
class PHPFPMRESTWebAppTest(PHPFPMWebAppMixin, RESTWebAppMixin, WebAppMixin, BaseLiveServerTestCase):
|
2014-10-10 17:17:20 +00:00
|
|
|
pass
|
|
|
|
|
|
|
|
|
2014-10-10 14:39:46 +00:00
|
|
|
#class AdminWebAppTest(AdminWebAppMixin, BaseLiveServerTestCase):
|
|
|
|
# pass
|
|
|
|
|
|
|
|
|
|
|
|
|