Mikhail Korobov avatar Mikhail Korobov committed b129a75

Better tests

Comments (0)

Files changed (9)

 CHANGES
 =======
 
+dev
+---
+
+- OS is now auto-detected;
+- Ubuntu 10.10 maverick initial support;
+- `fabtest <https://bitbucket.org/kmike/fabtest>`_ package is extracted
+  from the test suite;
+- more tests.
+
 0.4.2 (2011-02-16)
 ------------------
 
-Running tests
-=============
+Test suite
+==========
+
+django-fab-deploy test suite executes fab commands against VirtualBox
+virtual machines. Tests can take a very long time to run and require
+fast internet connection: all operations are really performed.
+VM is rolled back to clean state or an appropriate snapshot before each test.
+
+This approach is quite extreme but I believe it's the only way to make sure
+deployment system works: actually execute the deployment scripts against
+concrete servers.
 
 Preparations
 ------------
 .. _VirtualBox: http://www.virtualbox.org/
 .. _fabtest: https://bitbucket.org/kmike/fabtest
 
-Running
--------
+Running tests
+-------------
 
 Pass VM name (e.g. Lenny) to runtests.py script::
 
     cd fab_deploy_tests
     ./runtests.py <VM name or uid> <what to run>
 
-Tests can take a long time to run because VM is rolled back to clean
-state before each test.
+<what to run> can be `fast`, `all` or any value acceptable by
+``unittest.main()`` (e.g. a list of test cases).
 
 In order to get coverage reports run::
 

fab_deploy_tests/runtests.py

 #!/usr/bin/env python
 import sys
 import os.path
+import unittest
+from fab_deploy_tests.tests import *
 
 # always use fab_deploy from the checkout, not the installed version
+# plus make fab_deploy_tests available for imports
 path = os.path.abspath(os.path.join(os.path.dirname(__file__), '..'))
 sys.path.insert(0, path)
 
-import unittest
-from fab_deploy_tests.tests import *
+def get_suite(cases):
+    suites = map(unittest.TestLoader().loadTestsFromTestCase, cases)
+    return unittest.TestSuite(suites)
+
+def help():
+    print 'usage: runtests.py "VM_NAME" <what_to_run>\n'
 
 if __name__ == '__main__':
-    FabDeployTest.os = sys.argv[1]
-    FabDeployTest.vm_name = sys.argv[2]
-    sys.argv = [sys.argv[0]] + sys.argv[3:]
-    unittest.main()
+    if len(sys.argv) == 1:
+        help()
+        sys.exit(1)
+
+    FabDeployTest.vm_name = sys.argv[1]
+
+    suites = {
+        'fast': get_suite([BasicTest, MysqlTest, FastSystemTest])
+    }
+    suites['all'] = unittest.TestSuite(suites.values())
+
+    suite_name = sys.argv[2]
+    if suite_name in suites:
+        unittest.TextTestRunner().run(suites[suite_name])
+    else:
+        sys.argv = [sys.argv[0]] + sys.argv[2:]
+        unittest.main()

fab_deploy_tests/tests/__init__.py

-from fabric.api import *
-from fab_deploy import prepare_server
-from fab_deploy.utils import run_as, update_env
-from fab_deploy_tests.utils import get_package_state, private_key_path, setup_ssh
+from __future__ import absolute_import
 
-from fabtest import FabTest, fab
-
-class FabDeployTest(FabTest):
-    host = 'foo@127.0.0.1:2222'
-    key_filename = [private_key_path()]
-
-    def setup_env(self):
-        super(FabDeployTest, self).setup_env()
-        self.setup_conf()
-        update_env()
-
-    def setup_conf(self):
-        env.conf = getattr(env, 'conf', {})
-        env.conf['OS'] = self.os
-
-    def assertPackageInstalled(self, name):
-        self.assertEqual(get_package_state(name), 'i')
-
-    def assertPackageNotInstalled(self, name):
-        self.assertNotEqual(get_package_state(name), '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')
-        def command():
-            self.assertUserIs('root')
-        fab(command)
-
-class SystemTest(FabDeployTest):
-
-    def test_create_linux_account(self):
-        setup_ssh()
-        def command():
-            self.assertUserIs('foo')
-        fab(command)
-
-    def test_prepare_server_ok(self):
-        fab(prepare_server)
-        self.assertPackageInstalled('memcached')
-        self.assertPackageInstalled('python')
-
+from .base import *
+from .mysql import *
+from .system_tests import *
+from .utils_tests import *

fab_deploy_tests/tests/base.py

+from __future__ import absolute_import
+from fabric.api import *
+from fabtest import FabTest
+from fab_deploy.utils import update_env
+from fab_deploy_tests.utils import get_package_state, private_key_path
+
+class FabDeployTest(FabTest):
+    host = 'foo@127.0.0.1:2222'
+    key_filename = [private_key_path()]
+
+    def setup_env(self):
+        super(FabDeployTest, self).setup_env()
+        self.setup_conf()
+        update_env()
+
+    def setup_conf(self):
+        env.conf = getattr(env, 'conf', {})
+
+    def assertPackageInstalled(self, name):
+        self.assertEqual(get_package_state(name), 'i')
+
+    def assertPackageNotInstalled(self, name):
+        self.assertNotEqual(get_package_state(name), 'i')
+
+    def assertUserIs(self, user):
+        # use it inside fab commands
+        output = run('whoami')
+        self.assertEqual(output, user)
+

fab_deploy_tests/tests/mysql.py

+from __future__ import absolute_import
+from fabric.api import *
+from fabtest import fab
+from fab_deploy.mysql import mysql_install, _mysql_is_installed, mysql_create_db, mysql_execute
+from .base import FabDeployTest
+
+def mysql_is_installed():
+    return fab(_mysql_is_installed)[0]
+
+def database_exists(db_name):
+    databases = fab(mysql_execute, 'show databases;')[0].splitlines()
+    return db_name in databases
+
+class MysqlTest(FabDeployTest):
+    host = 'root@127.0.0.1:2222'
+
+    def setup_conf(self):
+        super(MysqlTest, self).setup_conf()
+        env.conf['DB_PASSWORD'] = '123'
+        env.conf['DB_NAME'] = 'new_database'
+
+    def test_mysql(self):
+        self.assertFalse(mysql_is_installed())
+
+        fab(mysql_install)
+        self.assertTrue(mysql_is_installed())
+
+        self.assertFalse(database_exists('new_database'))
+        fab(mysql_create_db)
+        self.assertTrue(database_exists('new_database'))
+

fab_deploy_tests/tests/system_tests.py

+from __future__ import absolute_import
+from fabtest import fab
+
+from fab_deploy.system import prepare_server
+
+from .base import FabDeployTest
+from fab_deploy_tests.utils import setup_ssh
+
+class FastSystemTest(FabDeployTest):
+    def test_python(self):
+        self.assertPackageInstalled('python-minimal')
+
+    def test_create_linux_account(self):
+        setup_ssh()
+        def command():
+            self.assertUserIs('foo')
+        fab(command)
+
+class SystemTest(FabDeployTest):
+    def test_prepare_server_ok(self):
+        fab(prepare_server)
+        self.assertPackageInstalled('memcached')
+        self.assertPackageInstalled('python')
+

fab_deploy_tests/tests/utils_tests.py

+from __future__ import absolute_import
+from fabric.api import *
+from fabtest import fab
+from fab_deploy.utils import run_as
+from .base import FabDeployTest
+
+@run_as('root')
+def whoami():
+    return run('whoami')
+
+class BasicTest(FabDeployTest):
+    def test_run_as(self):
+        user = fab(whoami)[0]
+        self.assertEqual(user, 'root')
+

fab_deploy_tests/utils.py

 
 def get_package_state(name):
     """ Returns package state as in aptitude output: i, v, etc. """
-    state = {}
     @run_as('root')
     def command():
         regexp = "^%s$" % name
         output = run('aptitude -q -F "%%c" search %s' % regexp)
-        state[name] = output.splitlines()[-1]
-    fab(command)
-    return state[name]
+        return output.splitlines()[-1]
+    return fab(command)[0]
 
 def setup_ssh():
     fab(create_linux_account, public_key_path())
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.