1. Mikhail Korobov
  2. django-fab-deploy

Commits

Mikhail Korobov  committed e362580

Tests a reorganized

  • Participants
  • Parent commits e97d9cb
  • Branches default

Comments (0)

Files changed (4)

File fab_deploy_tests/preparevm.py

View file
 #!/usr/bin/env python
 import sys
+from fabric.api import env, run
 from fabtest import fab, VirtualBox
+from utils import setup_ssh
 
-def deep_prepare(name):
+from fab_deploy.utils import update_env
+from fab_deploy.system import prepare_server
+from fab_deploy.apache import apache_install
+from fab_deploy.nginx import nginx_install
+from fab_deploy.mysql import mysql_install
+
+def activate_snapshot(box, name):
+    box.stop()
+    box.snapshot('restore', name)
+    box.start()
+
+def deep_prepare(os, name):
     """ Deep VM preparation for test speedups.
-    Should only be run if tests are passed. """
+    Should only be run if all related tests are passed.
+    It prepares 3 snapshots:
+
+    * fabtest-prepared-server - basic software is installed
+    * fabtest-prepared-web-server - basic soft + apache + nginx
+    * fabtest-prepared-full-stack - basic soft + apache + nginx + mysql
+    """
+    env.hosts = ['foo@127.0.0.1:2222']
+    env.password = '123'
+    env.disable_known_hosts = True
+    env.conf = {'DB_PASSWORD': '123', 'OS': os}
+    update_env()
+
     box = VirtualBox(name)
-    # does nothing now
+
+    if not box.snapshot_exists('fabtest-prepared-server'):
+        activate_snapshot(box, 'fabtest-initial')
+        setup_ssh()
+        fab(prepare_server)
+        box.snapshot('take', 'fabtest-prepared-server')
+
+    if not box.snapshot_exists('fabtest-prepared-web-server'):
+        activate_snapshot(box, 'fabtest-prepared-server')
+        fab(apache_install)
+        fab(nginx_install)
+        box.snapshot('take', 'fabtest-prepared-web-server')
+
+    if not box.snapshot_exists('fabtest-prepared-full-stack'):
+        activate_snapshot(box, 'fabtest-prepared-web-server')
+        fab(mysql_install)
+        box.snapshot('take', 'fabtest-prepared-full-stack')
+
+    box.stop()
+
 
 if __name__ == '__main__':
-    deep_prepare(sys.argv[1])
+    deep_prepare(sys.argv[1], sys.argv[2])
 

File fab_deploy_tests/runtests.py

View file
 sys.path.insert(0, path)
 
 import unittest
-from fabric.api import env
 from fab_deploy_tests.tests import *
 
 if __name__ == '__main__':

File fab_deploy_tests/tests/__init__.py

View file
-import os.path
 from fabric.api import *
-from fab_deploy import create_linux_account, prepare_server
+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 fabtest import FabTest, fab
 
-path = os.path.abspath(os.path.join(os.path.dirname(__file__), '..'))
-
 class FabDeployTest(FabTest):
     host = 'foo@127.0.0.1:2222'
-    key_filename = [os.path.join(path, "keys/id_rsa")]
+    key_filename = [private_key_path()]
 
     def setup_env(self):
         super(FabDeployTest, self).setup_env()
         env.conf = getattr(env, 'conf', {})
         env.conf['OS'] = self.os
 
-    def create_linux_account(self):
-        fab(create_linux_account, os.path.join(path, "keys/id_rsa.pub"))
-
-    def get_package_state(self, 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]
-
     def assertPackageInstalled(self, name):
-        state = self.get_package_state(name)
-        self.assertEqual(state, 'i')
+        self.assertEqual(get_package_state(name), 'i')
 
     def assertPackageNotInstalled(self, name):
-        state = self.get_package_state(name)
-        self.assertNotEqual(state, 'i')
+        self.assertNotEqual(get_package_state(name), 'i')
 
     def assertUserIs(self, user):
         # use it inside fab commands
 class SystemTest(FabDeployTest):
 
     def test_create_linux_account(self):
-        self.create_linux_account()
+        setup_ssh()
         def command():
             self.assertUserIs('foo')
         fab(command)

File fab_deploy_tests/utils.py

View file
+import os.path
+from fabric.api import run
+from fabtest import fab
+from fab_deploy.utils import run_as
+from fab_deploy.system import create_linux_account
+
+def public_key_path():
+    return os.path.join(os.path.dirname(__file__), 'keys', 'id_rsa.pub')
+
+def private_key_path():
+    return os.path.join(os.path.dirname(__file__), 'keys', 'id_rsa')
+
+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]
+
+def setup_ssh():
+    fab(create_linux_account, public_key_path())