Start to implement migration support
Some checks failed
buildbot/tox Build done.

This commit is contained in:
2019-12-02 18:35:29 +01:00
parent ca4e65e926
commit 05e2337957
11 changed files with 214 additions and 11 deletions

View File

@@ -1,2 +1,5 @@
[run]
source = django_deploy
omit = */tests/generic.py
src/django_deploy/tests/fake_app1/migrations/0001_initial.py
src/django_deploy/tests/fake_app1/models.py

View File

@@ -2,6 +2,7 @@ import errno
import importlib
import json
import os
import subprocess
import sys
from .base_types import OrderedDict
@@ -136,6 +137,28 @@ class DjangoProject(object):
text += '\n'
self._append_to_pythonfile(urlconf_file, text)
def add_app_settings(self, module_path, hooks_config_name=None):
hooks_config = self._get_hooks_config_from_app(module_path, hooks_config_name)
app_settings = getattr(hooks_config, 'SETTINGS', None)
if not app_settings:
return
project_dir = self._project_dir
if not os.path.exists(project_dir): # pragma: no cover
raise DjangoDeployError('No such project directory: {}'.format(project_dir),
code=errno.ENOENT)
settings_dir = os.path.join(project_dir, DJANGO_SETTINGS_DIR)
settings_file = os.path.join(settings_dir, 'settings.py')
text = '\n'
text += '# django-deploy\n'
text += app_settings
text += '\n'
self._append_to_pythonfile(settings_file, text)
def install_app(self, module_path, hooks_config_name=None):
hooks_config = self._get_hooks_config_from_app(module_path, hooks_config_name)
installed_apps = getattr(hooks_config, 'INSTALLED_APPS', [])
@@ -146,6 +169,7 @@ class DjangoProject(object):
config[module_path]['INSTALLED_APPS'] = installed_apps
config.write()
self.add_app_settings(module_path, hooks_config_name=hooks_config_name)
def mount_app(self, module_path, route, hooks_config_name=None):
self.install_app(module_path, hooks_config_name=hooks_config_name)
@@ -169,5 +193,23 @@ class DjangoProject(object):
config[module_path]['MOUNT'] = [route, urlconf]
config.write()
def migrate_apps(self, labels=None):
project_dir = self._project_dir
management_script = os.path.join(project_dir, 'manage.py')
if labels:
for label in labels:
cmd = [management_script, 'migrate', label]
try:
subprocess.check_output(cmd, stderr=subprocess.STDOUT)
except subprocess.CalledProcessError as e:
raise DjangoDeployError(e.output.decode('utf8'), exitval=e.returncode)
else:
cmd = [management_script, 'migrate']
try:
subprocess.check_output(cmd, stderr=subprocess.STDOUT)
except subprocess.CalledProcessError as e: # pragma: no cover
raise DjangoDeployError(e.output.decode('utf8'), exitval=e.returncode)
def __init__(self, project_dir):
self._project_dir = project_dir

View File

@@ -1,2 +1,8 @@
# INSTALLED_APPS = ['django_deploy']
# ROOT_URLCONF = '.urls'
# SETTINGS = """WSGI_APPLICATION = 'django_deploy.wsgi.application'
# SOME_APP_SETTING = [
# 'item1',
# 'item2']
# DEBUG = False
# """

View File

@@ -1,2 +1,8 @@
INSTALLED_APPS = ['django_deploy.tests.fake_app1']
ROOT_URLCONF = '.urls'
SETTINGS = """WSGI_APPLICATION = 'django_deploy.tests.fake_app1.wsgi.application'
FAKE_APP1_TEST_SETTING = \"\"\"Test
setting from
fake_app1
\"\"\"
"""

View File

@@ -0,0 +1,19 @@
from django.db import migrations, models
class Migration(migrations.Migration):
initial = True
dependencies = [
]
operations = [
migrations.CreateModel(
name='MyModel',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('chars', models.CharField(max_length=10)),
],
),
]

View File

@@ -0,0 +1,12 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.db import models
from django.utils.encoding import python_2_unicode_compatible
@python_2_unicode_compatible
class MyModel(models.Model):
chars = models.CharField(max_length=10)
def __str__(self):
return self.chars

View File

@@ -1,2 +1,4 @@
INSTALLED_APPS = ['django_deploy.tests.fake_app1', 'django_deploy.tests.fake_app2']
ROOT_URLCONF = 'django_deploy.tests.fake_app2.urls'
SETTINGS = """WSGI_APPLICATION = 'django_deploy.tests.fake_app2.wsgi.application'
"""

View File

@@ -1,5 +1,6 @@
import importlib
import os
import subprocess
import sys
import unittest
import mock
@@ -80,6 +81,38 @@ class DjangoDeployTestCase(unittest.TestCase):
haystack = f.read()
self.assertNotIn(needle, haystack)
def assert_text_in_settings(self, project_dir, settings_text):
settings_dir = os.path.join(project_dir, DJANGO_SETTINGS_DIR)
settings_file = os.path.join(settings_dir, 'settings.py')
with open(settings_file, 'r') as f:
complete_settings = f.read()
self.assertIn(settings_text, complete_settings)
def assert_values_in_settings(self, project_dir, settings_dict):
real_settings = self.get_django_settings(project_dir)
for key in settings_dict:
self.assertTrue(hasattr(real_settings, key))
expected_value = settings_dict[key]
real_value = getattr(real_settings, key)
self.assertEqual(expected_value, real_value)
def assert_settings_from_apps(self, project_dir, apps):
hooks_config_name = 'django_deploy_hooks'
wsgi_application_provider = 'main'
for app in apps:
try:
hooks_config = importlib.import_module('{}.{}'.format(app, hooks_config_name))
except ImportError: # pragma: no cover
continue
if not hasattr(hooks_config, 'SETTINGS'):
continue
settings_text = getattr(hooks_config, 'SETTINGS')
self.assert_text_in_settings(project_dir, settings_text)
wsgi_application_provider = app
wsgi_application = '{}.wsgi.application'.format(wsgi_application_provider)
self.assert_values_in_settings(project_dir, {'WSGI_APPLICATION': wsgi_application})
def assert_installed_apps(self, project_dir, apps, default_apps=None):
settings = self.get_django_settings(project_dir)
@@ -98,6 +131,7 @@ class DjangoDeployTestCase(unittest.TestCase):
expected_apps += apps
self.assertListEqual(expected_apps, settings.INSTALLED_APPS)
self.assert_settings_from_apps(project_dir, apps)
def assert_urlpatterns(self, project_dir, patterns):
root_urlconf = self.get_django_root_urlconf(project_dir)
@@ -135,3 +169,17 @@ class DjangoDeployTestCase(unittest.TestCase):
self.assertEqual(expected[2], real.urlconf_name.__name__)
else: # pragma: no cover
self.fail('Unknown urlpattern class: {}'.format(real_class_name))
def assert_django_database_migration(self, project_dir, app_label=None, migration=None):
management_script = os.path.join(project_dir, 'manage.py')
cmd = [
management_script, 'migrate', '--no-color', '--noinput'
]
if app_label is not None:
cmd.append(app_label)
if migration is not None:
cmd.append(migration)
output = subprocess.check_output(cmd)
text = output.decode('utf8')
self.assertTrue(text.endswith('No migrations to apply.\n'))

View File

@@ -4,7 +4,7 @@ import os
from ..api import DjangoProjectHooksConfig, DjangoProject
from ..exceptions import DjangoDeployError
from .base import DjangoDeployTestCase
from .generic import DjangoDeployTestCase
class DjangoProjectHooksConfigTestCase(DjangoDeployTestCase):
@@ -15,10 +15,12 @@ class DjangoProjectHooksConfigTestCase(DjangoDeployTestCase):
DjangoProjectHooksConfig(project_dir='.', settings_dir='.')
class DjangoProjectTestCase(DjangoDeployTestCase):
class AbstractDjangoProjectTestCase(DjangoDeployTestCase):
def setUp(self):
self._tmp_dir = str(self.tmpdir)
class InitDjangoProjectTestCase(AbstractDjangoProjectTestCase):
def test_init(self):
with self.assertRaises(TypeError):
DjangoProject() # pylint: disable=no-value-for-parameter
@@ -31,6 +33,8 @@ class DjangoProjectTestCase(DjangoDeployTestCase):
os.mknod(project_dir)
DjangoProject(project_dir)
class DjangoProjectCreateTestCase(AbstractDjangoProjectTestCase):
def test_create(self):
# the parent of the to-be-created project dir shall also not exist.
project_dir = os.path.join(self._tmp_dir, 'new', 'sub', 'sub')
@@ -80,6 +84,8 @@ class DjangoProjectTestCase(DjangoDeployTestCase):
else: # pragma: no cover
self.fail('DjangoDeployError not raised')
class DjangoProjectInstallHooksTestCase(AbstractDjangoProjectTestCase):
def test_install_hooks(self):
project_dir = os.path.join(self._tmp_dir, 'new')
project = DjangoProject(project_dir)
@@ -119,24 +125,47 @@ class DjangoProjectTestCase(DjangoDeployTestCase):
else: # pragma: no cover
self.fail('DjangoDeployError not raised')
class DjangoProjectAddAppSettingsTestCase(AbstractDjangoProjectTestCase):
def test_add_app_settings(self):
project_dir = self._tmp_dir
project = DjangoProject(project_dir)
project.create()
apps = [
'django_deploy.tests.fake_app1',
'django_deploy.tests.fake_app2',
'django_deploy',
]
for app in apps:
project.add_app_settings(app)
self.assert_settings_from_apps(project_dir, apps)
class DjangoProjectInstallAppTestCase(AbstractDjangoProjectTestCase):
def test_install_app(self):
project_dir = self._tmp_dir
project = DjangoProject(project_dir)
project.create()
project.install_app('django_deploy.tests.fake_app1')
installed_apps = [
'django_deploy.tests.fake_app1',
]
self.assert_installed_apps(project_dir, installed_apps)
app = 'django_deploy.tests.fake_app1'
project.install_app(app)
self.assert_installed_apps(project_dir, [app])
def test_install_apps(self):
project_dir = self._tmp_dir
project = DjangoProject(project_dir)
project.create()
project.install_app('django_deploy.tests.fake_app1')
project.install_app('django_deploy.tests.fake_app1')
project.install_app('django_deploy.tests.fake_app2')
apps = [
'django_deploy.tests.fake_app1',
'django_deploy.tests.fake_app1',
'django_deploy.tests.fake_app2',
]
for app in apps:
project.install_app(app)
installed_apps = [
'django_deploy.tests.fake_app1',
@@ -167,6 +196,8 @@ class DjangoProjectTestCase(DjangoDeployTestCase):
else: # pragma: no cover
self.fail('DjangoDeployError not raised')
class DjangoProjectMountAppTestCase(AbstractDjangoProjectTestCase):
def test_mount_app(self):
project_dir = self._tmp_dir
project = DjangoProject(project_dir)
@@ -209,3 +240,37 @@ class DjangoProjectTestCase(DjangoDeployTestCase):
self.assertEqual(errno.ENOLINK, e.code)
else: # pragma: no cover
self.fail('DjangoDeployError not raised')
class DjangoProjectMigrateAppTestCase(AbstractDjangoProjectTestCase):
def test_migrate_app(self):
project_dir = self._tmp_dir
project = DjangoProject(project_dir)
project.create()
project.install_app('django_deploy.tests.fake_app1')
project.migrate_apps(['fake_app1'])
self.assert_django_database_migration(project_dir, 'fake_app1')
def test_migrate_all_apps(self):
project_dir = self._tmp_dir
project = DjangoProject(project_dir)
project.create()
project.install_app('django_deploy.tests.fake_app1')
project.install_app('django_deploy.tests.fake_app2')
project.migrate_apps([])
self.assert_django_database_migration(project_dir)
def test_migrate_app_without_migrations(self):
project_dir = self._tmp_dir
project = DjangoProject(project_dir)
project.create()
project.install_app('django_deploy.tests.fake_app2')
try:
project.migrate_apps(['fake_app2'])
except DjangoDeployError as e:
self.assertEqual(e.message, "CommandError: App 'fake_app2' does not have migrations.\n")
else: # pragma: no cover
self.fail('DjangoDeployError not raised')

View File

@@ -3,7 +3,7 @@ import os
from ..config import DJANGO_SETTINGS_DIR
from ..program import Program
from .base import DjangoDeployTestCase
from .generic import DjangoDeployTestCase
class ProgramTestCase(DjangoDeployTestCase):