PK rIm fabfile/__init__.pyfrom fabfile.development import *
from fabfile.environment import *
# Make sure the virtualenv is activated.
setup_virtualenv()
PK rIWW W fabfile/development.py# Copyright 2012, 2013 Canonical Ltd.
#
# This program is free software: you can redistribute it and/or modify it
# under the terms of the GNU Lesser General Public License version 3, as
# published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranties of
# MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
# PURPOSE. See the GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program. If not, see .
import os
import unittest
from fabric import api
def test(*test_regexps):
"""Run tests.
:param test_regexps: A list of test regexps to include.
"""
# late import to ensure the virtual env is active
from sst import (
filters,
loaders,
)
os.environ['DJANGO_SETTINGS_MODULE'] = \
'u1testutils.selftests.django_project.settings'
loader = loaders.TestLoader()
suite = loader.suiteClass()
suite.addTests(loader.discoverTestsFromTree('u1testutils'))
suite.addTests(loader.discoverTestsFromTree('setup_vm'))
suite = filters.include_regexps(test_regexps, suite)
# List the tests as we run them
runner = unittest.TextTestRunner(verbosity=2)
res = runner.run(suite)
print 'Totals: ran({0}), skipped({1}), errors({2}), failures({3})'.format(
res.testsRun, len(res.skipped), len(res.errors), len(res.failures))
if not res.wasSuccessful():
api.abort('Tests failed.')
PK rI]z fabfile/environment.py# -*- coding: utf-8 -*-
# Copyright 2012, 2013 Canonical Ltd.
#
# This program is free software: you can redistribute it and/or modify it
# under the terms of the GNU Lesser General Public License version 3, as
# published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranties of
# MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
# PURPOSE. See the GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program. If not, see .
import os
import sys
from fabric.api import env, local
VIRTUALENV = '.env'
def bootstrap():
local('rm -rf %s' % VIRTUALENV)
local("find -name '*.pyc' -delete")
setup_virtualenv()
_install_dependencies()
def setup_virtualenv():
created = False
virtual_env = os.environ.get('VIRTUAL_ENV', None)
if virtual_env is None:
if not os.path.exists(VIRTUALENV):
_create_virtualenv()
created = True
virtual_env = VIRTUALENV
env.virtualenv = os.path.abspath(virtual_env)
_activate_virtualenv()
return created
def _create_virtualenv():
if not os.path.exists(VIRTUALENV):
virtualenv_bin_path = local('which virtualenv', capture=True)
virtualenv_version = local('{0} {1} --version'.format(
sys.executable, virtualenv_bin_path), capture=True)
# We only care about major.minor revision numbers
version_strings = virtualenv_version.split('.')[0:1]
virtualenv_version = [int(x) for x in version_strings]
args = '--distribute --clear'
if virtualenv_version < [1, 7]:
args += ' --no-site-packages'
local('{0} {1} {2} {3}'.format(sys.executable, virtualenv_bin_path,
args, VIRTUALENV), capture=False)
def _activate_virtualenv():
activate_this = os.path.abspath(
'{0}/bin/activate_this.py'.format(env.virtualenv))
execfile(activate_this, dict(__file__=activate_this))
# Work around fabric unconditionally removing first entry in sys.path by
# adding a dummy one. Agreed with Leo on IRC that the workaround is ok as
# the plan is to stop using fabric in the long run and the bug itself is a
# blocker. -- vila 2013-09-25
sys.path.insert(0, '/I-dont-exist-blow-me-away-I-dont-care')
def _install_dependencies():
# it's possible to get "ImportError: No module named setuptools"
# when using pip<1.4 to upgrade a package that depends on setuptools.
run_in_virtualenv_local('pip install -U setuptools', capture=False)
run_in_virtualenv_local(
'pip install -U -r requirements.txt', capture=False)
def run_in_virtualenv_local(command, capture=True):
prefix = ''
virtual_env = env.get('virtualenv', None)
if virtual_env:
prefix = '. {0}/bin/activate && '.format(virtual_env)
command = prefix + command
return local(command, capture=capture)
PK rI-eW u1testutils/logging.pyfrom __future__ import absolute_import
import logging
from unittest import TestCase
# Originally written by:
# - Guillermo Gonzalez
# - Facundo Batista
# - Natalia Bidart
class LogHandlerTestCase(TestCase):
"""A mixin that adds a memento loghandler for testing logging."""
class MementoHandler(logging.Handler):
"""A handler class which stores logging records in a list.
From http://nessita.pastebin.com/mgc85uQT
"""
def __init__(self, *args, **kwargs):
"""Create the instance, and add a records attribute."""
logging.Handler.__init__(self, *args, **kwargs)
self.records = []
def emit(self, record):
"""Just add the record to self.records."""
self.records.append(record)
def check(self, level, msg, check_traceback=False):
"""Check that something is logged."""
result = False
for rec in self.records:
if rec.levelname == level:
result = str(msg) in rec.getMessage()
if not result and check_traceback:
result = str(msg) in rec.exc_text
if result:
break
return result
def setUp(self):
"""Add the memento handler to the root logger."""
super(LogHandlerTestCase, self).setUp()
self.memento_handler = self.MementoHandler()
self.root_logger = logging.getLogger()
self.root_logger.addHandler(self.memento_handler)
def tearDown(self):
"""Remove the memento handler from the root logger."""
self.root_logger.removeHandler(self.memento_handler)
super(LogHandlerTestCase, self).tearDown()
def assertLogLevelContains(self, level, message, check_traceback=False):
check = self.memento_handler.check(
level, message, check_traceback=check_traceback)
msg = ('Expected logging message/s could not be found:\n%s\n'
'Current logging records are:\n%s')
expected = '\t%s: %s' % (level, message)
records = ['\t%s: %s' % (r.levelname, r.getMessage())
for r in self.memento_handler.records]
self.assertTrue(check, msg % (expected, '\n'.join(records)))
PK rIcC C u1testutils/mail.py# -*- coding: utf-8 -*-
# Copyright 2013 Canonical Ltd.
#
# This program is free software: you can redistribute it and/or modify it
# under the terms of the GNU Lesser General Public License version 3, as
# published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranties of
# MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
# PURPOSE. See the GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program. If not, see