def test_crontab(self): line1 = '@reboot echo 123' line2 = '@reboot echo 345' new_line2 = '@reboot echo 567' self.add(line1, 'line1') self.add(line2, 'line2') current = self.current() self.assertTrue(line1 in current) self.assertTrue(line2 in current) self.assertFalse(new_line2 in current) self.update(new_line2, 'line2') current = self.current() self.assertTrue(line1 in current) self.assertFalse(line2 in current) self.assertTrue(new_line2 in current) self.remove('line1') current = self.current() self.assertFalse(line1 in current) self.assertFalse(line2 in current) self.assertTrue(new_line2 in current) fab(crontab_show)
def test_crontab(self): line1 = '@reboot echo 123' line2 = '@reboot echo 345' new_line2 = '@reboot echo 567' self.add(line1, 'line1') self.add(line2, 'line2') current = self.current() self.assertTrue(line1 in current) self.assertTrue(line2 in current) self.assertFalse(new_line2 in current) self.update(new_line2, 'line2') current = self.current() self.assertTrue(line1 in current) self.assertFalse(line2 in current) self.assertTrue(new_line2 in current) self.remove('line1') current = self.current() self.assertFalse(line1 in current) self.assertFalse(line2 in current) self.assertTrue(new_line2 in current) fab(crontab.puts_content)
def test_deploy(self): # FIXME! This fails because of __getattribute__ at apps module. # And env.conf.APPS is not initialised at all for this moment. #self.assertCommandFails('syncdb') # deploy first site fab(foo_site) with answer_yes(): fab(self.project.deploy) self.assertCommandAvailable('syncdb') self.assertCommandNotAvailable('syncddb') self.assertInstanceWorks('foo') # deploy second site fab(bar_site) with answer_yes(): fab(self.project.deploy) self.assertInstanceWorks('bar') self.assertInstanceWorks('foo') # deploy improperly configured site fab(invalid_site) # config errors should not be silenced with answer_yes(): self.assertRaises(FabricAbortException, fab, project.deploy) # old sites still work self.assertInstanceWorks('bar') self.assertInstanceWorks('foo') self.assertInstanceDoesntWork('invalid')
def test_create_linux_account(self): setup_sudo() setup_ssh() def command(): self.assertUserIs('foo') fab(command)
def test_pip_conf(self): """ FIXME: The test is not valid due to inner contradiction. """ fab(foo_site2) setup_sudo() setup_ssh() self.assertNoFile(env.conf.HOME_DIR+'/.pip/pip.conf') fab(pip.setup_conf) self.assertFileExists(env.conf.HOME_DIR+'/.pip/pip.conf')
def test_mysql(self): setup_sudo() 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'))
def deep_prepare(name): """ Deep VM preparation for test speedups. Should only be run if all related tests are passed. It now prepares an extra snapshot with basic software, apache, nginx and mysql installed. VM is not executed in headless mode because snapshot taking seems to be broken in this mode. """ env.hosts = ['[email protected]:2222'] env.password = '******' env.disable_known_hosts = True env.conf = {'DB_PASSWORD': '******'} env.key_filename = private_key_path() update_env() box = VirtualBox(name) if not box.snapshot_exists('fabtest-prepared-server'): activate_snapshot(box, 'fabtest-initial') setup_sudo() setup_ssh() fab(prepare_server) fab(apache_install) fab(nginx_install) fab(mysql_install) box.snapshot('take', 'fabtest-prepared-server') box.stop()
def test_create_sudo_linux_account(self): setup_sudo() fab(create_sudo_linux_account, public_key_path(), 'testsudo') @run_as('testsudo') def whoami(): return run('whoami') user = fab(whoami)[0] self.assertEqual(user, 'testsudo') @run_as('testsudo') def test_sudo(): return run('sudo aptitude update') fab(test_sudo)
def get_package_state(name): """ Returns package state as in aptitude output: i, v, etc. """ @run_as('root') def command(): regexp = "^%s$" % name output = run('aptitude -q -F "%%c" search %s' % regexp) return output.splitlines()[-1] return fab(command)
def get_package_state(name): """ Returns package state as in aptitude output: i, v, etc. """ @run_as('root') def command(): regexp = "^%s$" % name output = run('aptitude -q -F "%%c" search %s' % regexp) return output.splitlines()[-1] return fab(command)[0]
def test_deploy(self): # deploy first site fab(foo_site) fab(deploy_project) # first site works self.assertResponse('http://foo.example.com/instance/', 'foo') # deploy second site fab(bar_site) fab(deploy_project) # second site works self.assertResponse('http://bar.example.com/instance/', 'bar') # first site is still available self.assertResponse('http://foo.example.com/instance/', 'foo')
def test_push_callback(self): def before_restart(): before_restart.called = True before_restart.called = False def my_push(*args): return push(*args, before_restart=before_restart) fab(foo_site2) setup_sudo() setup_ssh() fab(mysql_create_db) fab(deploy_project) fab(my_push) self.assertTrue(before_restart.called)
def test_prepare_server_ok(self): setup_sudo() setup_ssh() fab(prepare) self.assertPackageInstalled('python') apps = self.project.apps fab(apps.django.backend.install, confirm=False) fab(apps.django.frontend.install) self.assertPackageInstalled('nginx') self.assertPackageInstalled('apache2')
def test_mysql(self): setup_sudo() 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')) # re-creating user shouldn't break anything fab(mysql_create_user) # this will fail if permissions are not set correctly output = fab(mysql_execute, 'use new_database; create table baz (id int); show tables;')[0] tables = output.splitlines()[1:] self.assertEqual(tables[0], 'baz') # mysqldump should work fab(mysqldump, env.conf.HOME_DIR)
def test_prepare_server_ok(self): setup_sudo() setup_ssh() fab(prepare_server) self.assertPackageInstalled('memcached') self.assertPackageInstalled('python') fab(apache_install) fab(nginx_install) self.assertPackageInstalled('nginx') self.assertPackageInstalled('apache2')
def test_postgres(self): self.assertTrue(postgres_is_installed()) self.assertFalse(database_exists('new_database')) fab(postgres.create_user) fab(postgres.create_db) self.assertTrue(database_exists('new_database')) # create table fab(postgres.execute_sql, "create table baz (id int);") # check if it is created test_sql = "SELECT table_name FROM information_schema.tables WHERE table_schema = 'public';" output = fab(postgres.execute_sql, test_sql) tables = output.splitlines()[1:] self.assertEqual(tables[1].strip(), 'baz')
def test_push_callback(self): def before_restart(): before_restart.called = True before_restart.called = False def my_push(*args): return self.project.push(*args, before_restart=before_restart) fab(foo_site2) setup_sudo() setup_ssh() fab(mysql.create_db) with answer_yes(): fab(self.project.deploy) fab(my_push) self.assertTrue(before_restart.called)
def test_snapshots(self): test_file = '~/test_file.tmp' def mkfile(): return run('touch '+test_file) def file_exists(): return exists(test_file) self.assertFalse(fab(file_exists)) fab(mkfile) self.assertTrue(fab(file_exists)) self.take_test_snapshot('test-snapshot') self.assertTrue(fab(file_exists)) self.activate_test_snapshot(self.snapshot) self.assertFalse(fab(file_exists)) self.activate_test_snapshot('test-snapshot') self.assertTrue(fab(file_exists))
def setup_sudo(): fab(install_sudo) fab(create_sudo_linux_account, public_key_path())
def get_file_content(remote_file): @run_as('root') def cat(): with(hide('stdout')): return run('cat '+remote_file) return fab(cat)[0]
def test_add_ssh_key(self): fab(setup_root_ssh) env.password = None # should use ssh key self.assertEqual(fab(whoami)[0], 'root')
def get_ports(): @run_as('root') def ports(): return run('netstat -A inet -lnp') return fab(ports)[0]
def test_no_pip_conf(self): self.assertNoFile(env.conf.HOME_DIR + '/pip.conf') fab(pip_setup_conf) self.assertNoFile(env.conf.HOME_DIR + '/pip.conf')
def this_should_be_catched(): fab(abort, 'aborted')
def current(self): return fab(_get_current)[0]
def test_run_as(self): user = fab(whoami)[0] self.assertEqual(user, 'root')
def test_root_login(self): self.assertEqual(fab(whoami), 'root')
def add(self, content, marker): return fab(crontab_add, content, marker)[0]
def remove(self, marker): return fab(crontab_remove, marker)[0]
def mysql_is_installed(): return fab(_mysql_is_installed)[0]
def database_exists(db_name): user_name = postgres.Postgres.SUPERUSER databases = fab(postgres.execute_sql, 'select datname from pg_database;', user_name) return db_name in databases
def test_install(self): setup_sudo() self.assertFalse(postgres_is_installed()) fab(postgres.install) self.assertTrue(postgres_is_installed())
def assertCommandNotAvailable(self, command): result = fab(self.project.apps.django.command_is_available, command) self.assertFalse(result)
def test_apache_make_wsgi(self): self.assertNoFile(env.conf.ENV_DIR+'/var/wsgi/foo.py') fab(apache_make_wsgi) self.assertFileExists(env.conf.ENV_DIR+'/var/wsgi/foo.py')
def postgres_is_installed(): return fab(postgres.is_installed)
def test_apache_config(self): fab(apache_install) # first site fab(foo_site) fab(apache_make_config) foo_port = env.conf.APACHE_PORT self.assertPortNotBinded(foo_port) self.assertFileExists('/etc/apache2/sites-enabled/foo') fab(apache_restart) self.assertPortBinded(foo_port) # second site fab(bar_site) fab(apache_make_config) bar_port = env.conf.APACHE_PORT self.assertNotEqual(foo_port, bar_port) self.assertPortNotBinded(bar_port) fab(apache_restart) self.assertPortBinded(bar_port) # re-configuring doesn't lead to errors fab(apache_make_config) fab(apache_restart) self.assertPortBinded(bar_port)
def add(self, content, marker): return fab(crontab.add_line, content, marker)
def setup_conf(self): self.cwd = os.getcwd() os.chdir(self.project) fab(foo_site)
def remove(self, marker): return fab(crontab.remove_line, marker)
def setup_ssh(): fab(create_linux_account, public_key_path())
def update(self, content, marker): return fab(crontab.update_line, content, marker)
def database_exists(db_name): databases = fab(mysql_execute, 'show databases;', 'root')[0].splitlines() return db_name in databases
def setup_conf(self): self.cwd = os.getcwd() os.chdir(self.project_dir) fab(foo_site)
def test_deploy(self): url = 'http://foo.example.com/instance/' fab(foo_site2) setup_sudo() setup_ssh() fab(mysql_create_db) fab(deploy_project) self.assertResponse(url, 'foo2') # just check that blank push doesn't break anything # TODO: proper push tests fab(push, 'pip_update', 'syncdb') self.assertResponse(url, 'foo2') # check that updeploy disables the site # TODO: more undeploy tests fab(undeploy, confirm=False) self.assertRaises(Exception, vbox_urlopen, url) # deploying project again should be possible fab(deploy_project) self.assertResponse(url, 'foo2')
def current(self): return fab(crontab.get_content)
def test_no_pip_conf(self): self.assertNoFile(env.conf.HOME_DIR+'/pip.conf') fab(pip_setup_conf) self.assertNoFile(env.conf.HOME_DIR+'/pip.conf')
def update(self, content, marker): return fab(crontab_update, content, marker)[0]