From 0029cdc00cd7d92af000194743fc1923da189ad8 Mon Sep 17 00:00:00 2001 From: Antoine Nguyen Date: Sat, 2 Nov 2013 19:26:50 +0100 Subject: [PATCH] Fixed silent installation and unit tests. fixes #489 --- modoboa/core/commands/deploy.py | 43 +++++++------ .../core/commands/templates/settings.py.tpl | 3 +- tests/install_from_scratch.py | 64 +++++-------------- 3 files changed, 41 insertions(+), 69 deletions(-) diff --git a/modoboa/core/commands/deploy.py b/modoboa/core/commands/deploy.py index 322decb85..5d10ad69b 100644 --- a/modoboa/core/commands/deploy.py +++ b/modoboa/core/commands/deploy.py @@ -9,7 +9,7 @@ import dj_database_url from django.core import management from django.template import Context, Template from modoboa.lib.sysutils import exec_cmd -from . import Command +from modoboa.core.commands import Command dbconn_tpl = """ '{{ conn_name }}': { @@ -18,7 +18,7 @@ dbconn_tpl = """ 'USER': '{% if USER %}{{ USER }}{% endif %}', # Not used with sqlite3. 'PASSWORD': '{% if PASSWORD %}{{ PASSWORD }}{% endif %}', # Not used with sqlite3. 'HOST': '{% if HOST %}{{ HOST }}{% endif %}', # Set to empty string for localhost. Not used with sqlite3. - 'PORT': '{% if PORT %}{{ PORT }}{% endif %}', # Set to empty string for default. Not used with sqlite3.{% if dbtype == 'django.db.backends.mysql' %} + 'PORT': '{% if PORT %}{{ PORT }}{% endif %}', # Set to empty string for default. Not used with sqlite3.{% if ENGINE == 'django.db.backends.mysql' %} 'OPTIONS' : { "init_command" : 'SET foreign_key_checks = 0;', },{% endif %} @@ -90,26 +90,27 @@ class DeployCommand(Command): info = {'conn_name': name, 'ENGINE': raw_input('Database type (mysql, postgres or sqlite3): ')} if info['ENGINE'] not in ['mysql', 'postgres', 'sqlite3']: - info['ENGINE'] = 'django.db.backends.mysql' - default_port = 3306 - if info['ENGINE'] == 'postgres': - info['ENGINE'] = 'django.db.backends.postgresql_psycopg2' - default_port = 5432 + raise RuntimeError('Unsupported database engine') + if info['ENGINE'] == 'sqlite3': info['ENGINE'] = 'django.db.backends.sqlite3' info['NAME'] = '%s.db' % name + return info + if info['ENGINE'] == 'postgres': + info['ENGINE'] = 'django.db.backends.postgresql_psycopg2' + default_port = 5432 else: - # No need to set HOST to localhost, since django will do this - # automatically if HOST is not supplied - info['HOST'] = raw_input("Database host (default: localhost )") - - info['PORT'] = raw_input("Database port: (default: '%s'): " % default_port) - #leave port setting empty, if default value is supplied and leave it to django - if info['PORT'] == default_port: - info['PORT'] = '' - info['NAME'] = raw_input('Database name: ') - info['USER'] = raw_input('Username: ') - info['PASSWORD'] = getpass.getpass('Password: ') + info['ENGINE'] = 'django.db.backends.mysql' + default_port = 3306 + info['HOST'] = raw_input("Database host (default: 'localhost'): ") + info['PORT'] = raw_input("Database port (default: '%s'): " % default_port) + # leave port setting empty, if default value is supplied and + # leave it to django + if info['PORT'] == default_port: + info['PORT'] = '' + info['NAME'] = raw_input('Database name: ') + info['USER'] = raw_input('Username: ') + info['PASSWORD'] = getpass.getpass('Password: ') return info def handle(self, parsed_args): @@ -172,15 +173,15 @@ class DeployCommand(Command): 'allowed_host': allowed_host } ) - fp = open("%s/settings.py" % path, "w") - fp.write(tpl) - fp.close() + with open("%s/settings.py" % path, "w") as fp: + fp.write(tpl) shutil.copyfile( "%s/urls.py.tpl" % self._templates_dir, "%s/urls.py" % path ) os.mkdir("%s/media" % path) if parsed_args.syncdb: + os.unlink("%s/settings.pyc" % path) self._exec_django_command( "syncdb", parsed_args.name, '--noinput' ) diff --git a/modoboa/core/commands/templates/settings.py.tpl b/modoboa/core/commands/templates/settings.py.tpl index c46ddd6f7..f86ad1e39 100644 --- a/modoboa/core/commands/templates/settings.py.tpl +++ b/modoboa/core/commands/templates/settings.py.tpl @@ -159,7 +159,6 @@ MODOBOA_APPS = ( 'modoboa.extensions.webmail', 'modoboa.extensions.stats', 'modoboa.extensions.sievefilters', - 'modoboa.extensions.stats', {% if not amavis_conn %}#{% endif %}'modoboa.extensions.amavis', ) @@ -167,6 +166,8 @@ INSTALLED_APPS += MODOBOA_APPS AUTH_USER_MODEL = 'core.User' +SOUTH_TESTS_MIGRATE = False + #MODOBOA_CUSTOM_LOGO = os.path.join(MEDIA_URL, "custom_logo.png") AUTHENTICATION_BACKENDS = ( diff --git a/tests/install_from_scratch.py b/tests/install_from_scratch.py index 4924cf03f..50e03947c 100644 --- a/tests/install_from_scratch.py +++ b/tests/install_from_scratch.py @@ -1,19 +1,11 @@ # coding: utf-8 import os -import sys -import subprocess import tempfile import pexpect import shutil import unittest - - -def runcmd(cmd, **kwargs): - p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, - shell=True, **kwargs) - output = p.communicate()[0] - return p.returncode, output +from modoboa.lib.sysutils import exec_cmd class DeployTest(unittest.TestCase): @@ -25,19 +17,23 @@ class DeployTest(unittest.TestCase): def setUp(self): cmd = "mysqladmin -u %s -p%s create %s" % (self.dbuser, self.dbpassword, self.projname) - code, out = runcmd(cmd) + code, out = exec_cmd(cmd) self.assertEqual(code, 0) self.workdir = tempfile.mkdtemp() def tearDown(self): + path = os.path.join(self.workdir, self.projname) + code, output = exec_cmd("python manage.py test admin", cwd=path) + self.assertEqual(code, 0) + if hasattr(self, "workdir"): - shutil.rmtree(self.workdir) + shutil.rmtree(self.workdir) child = pexpect.spawn("mysqladmin -u root -p%s drop %s" % (self.dbpassword, self.projname)) child.expect("\[y/N\]") child.sendline("y") child.expect('Database "%s" dropped' % self.projname) - def test(self): + def test_standard(self): timeout = 2 cmd = "modoboa-admin.py deploy --syncdb --collectstatic %s" % self.projname child = pexpect.spawn(cmd, cwd=self.workdir) @@ -47,6 +43,8 @@ class DeployTest(unittest.TestCase): child.sendline(self.dbtype) child.expect("Database host \(default: 'localhost'\):", timeout=timeout) child.sendline(self.dbhost) + child.expect("Database port \(default: '3306'\):", timeout=timeout) + child.sendline('3306') child.expect("Database name:", timeout=timeout) child.sendline(self.projname) child.expect("Username:", timeout=timeout) @@ -59,43 +57,15 @@ class DeployTest(unittest.TestCase): fout.close() self.assertEqual(child.exitstatus, 0) - path = os.path.join(self.workdir, self.projname) - code, output = runcmd("python manage.py test admin", cwd=path) + def test_silent(self): + dburl = "%s://%s:%s@%s/%s" \ + % (self.dbtype, self.dbuser, self.dbpassword, + self.dbhost, self.projname) + cmd = "modoboa-admin.py deploy --syncdb --collectstatic --dburl %s --domain %s %s" \ + % (dburl, 'localhost', self.projname) + code, output = exec_cmd(cmd, cwd=self.workdir) self.assertEqual(code, 0) -class SilentDeployTest(unittest.TestCase): - dbtype = "mysql" - dbhost = "localhost" - projname = "modoboa_silenttest" - dbuser = "root" - dbpassword = "toto" - dburl = "%s://%s:%s@%s/%s" % (self.dbtype, self.dbuser, self.dbpassword, self.dbhost, self.projname) - - def setUp(self): - cmd = "mysqladmin -u %s -p%s create %s" % (self.dbuser, self.dbpassword, self.projname) - code, out = runcmd(cmd) - self.assertEqual(code, 0) - self.workdir = tempfile.mkdtemp() - - def tearDown(self): - if hasattr(self, "workdir"): - shutil.rmtree(self.workdir) - child = pexpect.spawn("mysqladmin -u root -p%s drop %s" % (self.dbpassword, self.projname)) - child.expect("\[y/N\]") - child.sendline("y") - child.expect('Database "%s" dropped' % self.projname) - - def test(self): - timeout = 2 - cmd = "modoboa-admin.py deploy --syncdb --collectstatic %s --dburl --domain %s" % (self.projname, self.dburl, 'localhost') - child = pexpect.spawn(cmd, cwd=self.workdir) - fout = open('install_silently_from_scratch.log','w') - child.logfile = fout - child.wait() - fout.close() - self.assertEqual(child.exitstatus, 0) - - if __name__ == "__main__": unittest.main()