Commits

Mikhail Korobov committed 8179e77

Extract fabtest to separate package

Comments (0)

Files changed (6)

 Preparations
 ------------
 
-In order to run tests you'll need `VirtualBox`_ 4.x and a configured OS image.
-Images should:
+django-fab-deploy requires `fabtest`_ package for running tests and
+(optionally) ``coverage.py`` for test coverage reports::
 
-* have root user with '123' password
-* have openssh server installed
+    pip install -U fabtest
+    pip install coverage
 
-.. note::
-
-    Example VMs:
-
-    * `Lenny.ova (312M) <http://dl.dropbox.com/u/21197464/Lenny.ova>`_
-    * `Squeeze.ova (436M) <http://dl.dropbox.com/u/21197464/Squeeze.ova>`_
-
-After you get the image, make sure it is not running and execute the
-'preparevm.py' script from fab_deploy_tests folder (pass your VM name)::
-
-    ./preparevm.py Lenny
-
-This command configures port forwarding (127.0.0.1:2222 to guest 22 port,
-127.0.0.1:8888 to guest 80 port) and takes 'initial' snapshot used for test
-rollbacks (it is taken from booted machine in order to speedup tests).
+Please follow `instructions <http://pypi.python.org/pypi/fabtest>`_ for
+fabtest package in order to prepare OS image. django-fab-deploy tests
+have 1 additional requirement: root user should have
+'123' password (fabtest example VM images are configured this way).
 
 .. _VirtualBox: http://www.virtualbox.org/
+.. _fabtest: https://bitbucket.org/kmike/fabtest
 
 Running
 -------
 Tests can take a long time to run because VM is rolled back to clean
 state before each test.
 
-In order to get coverage reports, install coverage.py (``pip install coverage``)
-and the run::
+In order to get coverage reports run::
 
     cd fab_deploy_tests
     ./runcoverage.sh <OS name> <VM name or uid>
 
 html reports will be placed in ``htmlcov`` folder.
-

fab_deploy_tests/fabfile.py

-from fab_deploy import *
-
-def foo_node():
-    env.hosts = ['foo@127.0.0.1:2222']
-    env.conf = dict(
-        DB_PASSWORD = '1234',
-        OS = env._OS,
-    )
-    update_env()

fab_deploy_tests/preparevm.py

 #!/usr/bin/env python
-"""
-Prepares VirtualBox image: takes initial snapshot for rollbacks
-and configures port forwarding.
-"""
+import sys
+from fabtest import fab, VirtualBox
 
-import sys
-from time import sleep
-from fab_deploy_tests.utils import VirtualBox
-
-def prepare_vbox(name):
+def deep_prepare(name):
+    """ Deep VM preparation for test speedups.
+    Should only be run if tests are passed. """
     box = VirtualBox(name)
-    box.modifyvm('--natpf1', 'guestssh,tcp,,2222,,22')
-    box.modifyvm('--natpf1', 'http,tcp,,8888,,80')
-
-    box.start()
-    print 'Vaiting 100 seconds for OS to boot...'
-    for i in range(1,11):
-        sleep(10)
-        print '%ds remains' % (100-i*10)
-
-    box.snapshot('take', 'initial')
-    box.stop()
+    # does nothing now
 
 if __name__ == '__main__':
-    prepare_vbox(sys.argv[1])
+    deep_prepare(sys.argv[1])
 

fab_deploy_tests/runtests.py

 from fab_deploy_tests.tests import *
 
 if __name__ == '__main__':
-    env._OS = sys.argv[1]
-    env._VM_NAME = sys.argv[2]
+    FabDeployTest.os = sys.argv[1]
+    FabDeployTest.vm_name = sys.argv[2]
     sys.argv = [sys.argv[0]] + sys.argv[3:]
     unittest.main()

fab_deploy_tests/tests/__init__.py

-import unittest
 import os.path
 from fabric.api import *
 from fab_deploy import create_linux_account, prepare_server
 from fab_deploy.utils import run_as, update_env
 
-from fab_deploy_tests.utils import fab, VirtualBox
-from fabric.state import _AttributeDict
+from fabtest import FabTest, fab
 
 path = os.path.abspath(os.path.join(os.path.dirname(__file__), '..'))
 
-class VirtualBoxTest(unittest.TestCase):
-    def setUp(self):
-        self.box = VirtualBox(env._VM_NAME)
-        self.activate_snapshot('initial')
-
-    def tearDown(self):
-        self.box.stop()
-
-    def activate_snapshot(self, name):
-        self.box.stop()
-        self.box.snapshot('restore', name)
-        self.box.start()
-
-
-class FabricTest(VirtualBoxTest):
-    host = 'root@127.0.0.1:2222'
-    password = '123'
-    key_filename = None
-
-    def setUp(self):
-        super(FabricTest, self).setUp()
-        self._default_env = _AttributeDict(env)
-        self.setup_env()
-
-    def tearDown(self):
-        env.update(self._default_env)
-        super(FabricTest, self).tearDown()
-
-    def setup_env(self):
-        env.hosts = [self.host]
-        env.password = self.password
-        env.key_filename = self.key_filename
-        env.disable_known_hosts = True
-
-    def assertUserIs(self, user):
-        # use it inside fab commands
-        assert run('whoami') == user
-
-class FabDeployTest(FabricTest):
+class FabDeployTest(FabTest):
     host = 'foo@127.0.0.1:2222'
     key_filename = [os.path.join(path, "keys/id_rsa")]
 
 
     def setup_conf(self):
         env.conf = getattr(env, 'conf', {})
-        env.conf['OS'] = env._OS
+        env.conf['OS'] = self.os
 
     def create_linux_account(self):
         fab(create_linux_account, os.path.join(path, "keys/id_rsa.pub"))
         state = self.get_package_state(name)
         self.assertNotEqual(state, 'i')
 
+    def assertUserIs(self, user):
+        # use it inside fab commands
+        output = run('whoami')
+        self.assertEqual(output, user)
+
 class BasicTest(FabDeployTest):
     def test_run_as(self):
         @run_as('root')

fab_deploy_tests/utils.py

-import subprocess
-from functools import partial
-from fabric import state
-from fabric.main import get_hosts
-from fabric.network import interpret_host_string, HostConnectionCache
-
-def fab(command, *args, **kwargs):
-    """ Runs fab command. Accepts callable. """
-    state.connections = HostConnectionCache()
-    state.env.command = command.__name__
-    state.env.all_hosts = hosts = get_hosts(command, None, None)
-    for host in hosts:
-        interpret_host_string(host)
-        command(*args, **kwargs)
-
-
-class VirtualBox(object):
-    def __init__(self, name):
-        self.name = name
-
-    def __getattr__(self, name):
-        """
-        Attributes are converted to a functions calling
-        VBoxManage shell command.
-        """
-        return partial(self, name)
-
-    def __call__(self, command, *args):
-        params = ['VBoxManage', command, self.name] + list(args)
-        print '$ ' + ' '.join(params)
-        return subprocess.call(params)
-
-    def start(self):
-        # headless variant leads to invalid snapshots for some reason
-        # (bug in virtualbox?)
-        # self.startvm('--type', 'headless')
-        self.startvm()
-
-    def stop(self):
-        self.controlvm('poweroff')