| 1 | # This file is part of the "Cleverbox" program. |
|---|
| 2 | # |
|---|
| 3 | # Cleverbox is free software: you can redistribute it and/or modify |
|---|
| 4 | # it under the terms of the GNU General Public License as published by |
|---|
| 5 | # the Free Software Foundation, either version 3 of the License, or |
|---|
| 6 | # (at your option) any later version. |
|---|
| 7 | # |
|---|
| 8 | # Cleverbox is distributed in the hope that it will be useful, |
|---|
| 9 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 10 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|---|
| 11 | # GNU General Public License for more details. |
|---|
| 12 | # |
|---|
| 13 | # You should have received a copy of the GNU General Public License |
|---|
| 14 | # along with Cleverbox. If not, see <http://www.gnu.org/licenses/>. |
|---|
| 15 | # |
|---|
| 16 | # Copyright 2008, Tristan Rivoallan |
|---|
| 17 | |
|---|
| 18 | import unittest |
|---|
| 19 | |
|---|
| 20 | import logging, os, shutil, tempfile |
|---|
| 21 | from cleverbox.model.environment import Environment |
|---|
| 22 | from cleverbox.scripts.admin import CleverboxAdmin |
|---|
| 23 | from cleverbox.model import client |
|---|
| 24 | import cleverbox.tests |
|---|
| 25 | |
|---|
| 26 | logging.basicConfig(stream=open('/dev/null', 'w+')) |
|---|
| 27 | |
|---|
| 28 | class ClientTestCase(unittest.TestCase): |
|---|
| 29 | """ |
|---|
| 30 | Tests related to manipulating clients. |
|---|
| 31 | """ |
|---|
| 32 | |
|---|
| 33 | # Fixtures |
|---|
| 34 | def setUp(self): |
|---|
| 35 | self.env = cleverbox.tests.create_environment('0.5test') |
|---|
| 36 | |
|---|
| 37 | def tearDown(self): |
|---|
| 38 | shutil.rmtree(self.env.path) |
|---|
| 39 | shutil.rmtree(self.env.config.get('general', 'clients_root')) |
|---|
| 40 | |
|---|
| 41 | # client add |
|---|
| 42 | def test_client_add_generates_expected_layout(self): |
|---|
| 43 | client_name = 'testclient' |
|---|
| 44 | client.add(self.env, client_name, {'enable' : 'y'}) |
|---|
| 45 | clients_root = self.env.config.get('general', 'clients_root') |
|---|
| 46 | expected = {os.path.join(self.env.path, 'clients-available', client_name) : ('S_ISREG', '0640', 'root', 'www-data'), |
|---|
| 47 | os.path.join(clients_root, client_name) : ('S_ISDIR', '0750', 'root', 'www-data'), |
|---|
| 48 | os.path.join(clients_root, client_name, 'htdocs') : ('S_ISDIR', '0770', 'root', 'www-data'), |
|---|
| 49 | os.path.join(clients_root, client_name, 'logs') : ('S_ISDIR', '0770', 'root', 'www-data'), |
|---|
| 50 | os.path.join(clients_root, client_name, 'tmp') : ('S_ISDIR', '0770', 'root', 'www-data'), |
|---|
| 51 | os.path.join(clients_root, client_name, 'var') : ('S_ISDIR', '0750', 'root', 'www-data'), |
|---|
| 52 | os.path.join(clients_root, client_name, 'var', 'svn') : ('S_ISDIR', '0750', 'root', 'www-data'), |
|---|
| 53 | os.path.join(clients_root, client_name, 'var', 'trac') : ('S_ISDIR', '0750', 'root', 'www-data')} |
|---|
| 54 | |
|---|
| 55 | cleverbox.tests.validate_layout(expected) |
|---|
| 56 | |
|---|
| 57 | def test_client_add_generates_expected_apache_configuration(self): |
|---|
| 58 | client_name = 'testclient' |
|---|
| 59 | client.add(self.env, client_name, {'enable' : 'y'}) |
|---|
| 60 | expected = """ |
|---|
| 61 | # -- Include enabled projects configuration files |
|---|
| 62 | Include %(env_dir)s/projects-enabled/testclient-* |
|---|
| 63 | """ % {'env_dir' : self.env.path} |
|---|
| 64 | got = file(os.path.join(self.env.path, 'clients-available', client_name)).read() |
|---|
| 65 | |
|---|
| 66 | assert got == expected, "client.add() generates expected apache configuration fragment (expected %s, got %s)" % (expected, got) |
|---|
| 67 | |
|---|
| 68 | def test_client_add_fails_on_existing_client(self): |
|---|
| 69 | """ |
|---|
| 70 | client.add() fails when given already existing client. |
|---|
| 71 | """ |
|---|
| 72 | client_name = 'testclient' |
|---|
| 73 | client.add(self.env, client_name, {'enable' : 'y'}) |
|---|
| 74 | try: |
|---|
| 75 | client.add(self.env, client_name, {'enable' : 'y'}) |
|---|
| 76 | except Exception, e: |
|---|
| 77 | pass |
|---|
| 78 | else: |
|---|
| 79 | self.fail(__doc__) |
|---|
| 80 | |
|---|
| 81 | def test_client_add_fails_on_invalid_client_name(self): |
|---|
| 82 | client_name = 'test-client' |
|---|
| 83 | try: |
|---|
| 84 | client.add(self.env, client_name, {'enable' : 'y'}) |
|---|
| 85 | except Exception, e: |
|---|
| 86 | pass |
|---|
| 87 | else: |
|---|
| 88 | self.fail("client.add() fails when given invalid username") |
|---|
| 89 | |
|---|
| 90 | def test_client_enable_generates_symlink(self): |
|---|
| 91 | |
|---|
| 92 | import stat |
|---|
| 93 | |
|---|
| 94 | # Create and enable client |
|---|
| 95 | client_name = 'testclient' |
|---|
| 96 | client.add(self.env, client_name, {'enable' : 'n'}) |
|---|
| 97 | client.enable(self.env, client_name) |
|---|
| 98 | |
|---|
| 99 | # Verify that an appropriate symlink is generated |
|---|
| 100 | link_path = os.path.join(self.env.path, 'clients-enabled', client_name) |
|---|
| 101 | real_path = os.path.join(self.env.path, 'clients-available', client_name) |
|---|
| 102 | st = os.stat(link_path) |
|---|
| 103 | mode = st[stat.ST_MODE] |
|---|
| 104 | |
|---|
| 105 | # TODO : Find out why this assertion is always false |
|---|
| 106 | #assert stat.S_ISLNK(mode), '%s is a symlink' % link_path |
|---|
| 107 | |
|---|
| 108 | # Verify symlink resolve to the right path |
|---|
| 109 | assert os.path.realpath(link_path) == real_path, '%s resolves to %s' % (link_path, real_path) |
|---|
| 110 | |
|---|
| 111 | |
|---|
| 112 | def test_client_disable_removes_symlink(self): |
|---|
| 113 | |
|---|
| 114 | # Create and enable client |
|---|
| 115 | client_name = 'testclient' |
|---|
| 116 | client.add(self.env, client_name, {'enable' : 'y'}) |
|---|
| 117 | |
|---|
| 118 | # Disable symlink |
|---|
| 119 | client.disable(self.env, client_name) |
|---|
| 120 | |
|---|
| 121 | # Make sure the symlink was removed |
|---|
| 122 | link_path = os.path.join(self.env.path, 'clients-enabled', client_name) |
|---|
| 123 | try: |
|---|
| 124 | os.stat(link_path) |
|---|
| 125 | except OSError: |
|---|
| 126 | pass |
|---|
| 127 | else: |
|---|
| 128 | self.fail('%s symlink was removed' % link_path) |
|---|
| 129 | |
|---|
| 130 | |
|---|
| 131 | # client remove |
|---|
| 132 | |
|---|
| 133 | def suite(): |
|---|
| 134 | return unittest.makeSuite(ClientTestCase,'test') |
|---|
| 135 | |
|---|
| 136 | if __name__ == '__main__': |
|---|
| 137 | unittest.main(defaultTest='suite') |
|---|