| 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, project |
|---|
| 24 | import cleverbox.tests |
|---|
| 25 | |
|---|
| 26 | logging.basicConfig(stream=open('/dev/null', 'w+')) |
|---|
| 27 | |
|---|
| 28 | default_parameters = project_parameters = {'full_name' : 'Test Project', |
|---|
| 29 | 'profile' : 'default', |
|---|
| 30 | 'tracadmin' : 'admin', |
|---|
| 31 | 'enable' : 'y'} |
|---|
| 32 | class ProjectTestCase(unittest.TestCase): |
|---|
| 33 | |
|---|
| 34 | # Fixtures |
|---|
| 35 | def setUp(self): |
|---|
| 36 | self.env = cleverbox.tests.create_environment('0.5test') |
|---|
| 37 | |
|---|
| 38 | def tearDown(self): |
|---|
| 39 | shutil.rmtree(self.env.path) |
|---|
| 40 | shutil.rmtree(self.env.config.get('general', 'clients_root')) |
|---|
| 41 | |
|---|
| 42 | def test_exists_return_true_if_client_exists(self): |
|---|
| 43 | |
|---|
| 44 | client_name = 'testclient' |
|---|
| 45 | client.add(self.env, client_name, {'enable' : 'y'}) |
|---|
| 46 | |
|---|
| 47 | project_name = 'testproject' |
|---|
| 48 | project.add(self.env, client_name, project_name, default_parameters) |
|---|
| 49 | assert project.exists(self.env, client_name, project_name), 'project.exists() returns true if project already exists' |
|---|
| 50 | |
|---|
| 51 | def test_add_creates_expected_layout(self): |
|---|
| 52 | |
|---|
| 53 | # Fixtures |
|---|
| 54 | client_name = 'testclient' |
|---|
| 55 | client.add(self.env, client_name, {'enable' : 'y'}) |
|---|
| 56 | project_name = 'testproject' |
|---|
| 57 | project.add(self.env, client_name, project_name, default_parameters) |
|---|
| 58 | |
|---|
| 59 | # Specify expected layout |
|---|
| 60 | clients_root = self.env.config.get('general', 'clients_root') |
|---|
| 61 | expected = { |
|---|
| 62 | os.path.join(self.env.path, 'projects-available', client_name + '-' + project_name) : ('S_ISREG', '0640', 'root', 'root'), |
|---|
| 63 | # Control constant should be S_ISLNK, but it does not seem to recognize the generated symlink as such |
|---|
| 64 | os.path.join(self.env.path, 'projects-enabled', client_name + '-' + project_name) : ('S_ISREG', '0640', 'root', 'root'), |
|---|
| 65 | os.path.join(clients_root, client_name, 'htdocs', project_name) : ('S_ISDIR', '0770', 'root', 'www-data'), |
|---|
| 66 | os.path.join(clients_root, client_name, 'var', 'svn', project_name) : ('S_ISDIR', '0750', 'root', 'www-data'), |
|---|
| 67 | os.path.join(clients_root, client_name, 'var', 'svn', project_name, 'db') : ('S_ISDIR', '0770', 'root', 'www-data'), |
|---|
| 68 | os.path.join(clients_root, client_name, 'var', 'trac', project_name) : ('S_ISDIR', '0750', 'root', 'www-data'), |
|---|
| 69 | os.path.join(clients_root, client_name, 'var', 'trac', project_name, 'db') : ('S_ISDIR', '0770', 'root', 'www-data'), |
|---|
| 70 | os.path.join(clients_root, client_name, 'var', 'trac', project_name, 'db', 'trac.db') : ('S_ISREG', '0640', 'root', 'www-data'), |
|---|
| 71 | os.path.join(clients_root, client_name, 'var', 'trac', project_name, 'attachments') : ('S_ISDIR', '0770', 'root', 'www-data'), |
|---|
| 72 | os.path.join(clients_root, client_name, 'var', 'trac', project_name, 'conf', 'trac.ini') : ('S_ISREG', '0640', 'root', 'www-data'), |
|---|
| 73 | } |
|---|
| 74 | |
|---|
| 75 | cleverbox.tests.validate_layout(expected) |
|---|
| 76 | |
|---|
| 77 | def test_enable_creates_symlink(self): |
|---|
| 78 | |
|---|
| 79 | # Fixtures |
|---|
| 80 | client_name = 'testclient' |
|---|
| 81 | client.add(self.env, client_name, {'enable' : 'n'}) |
|---|
| 82 | project_name = 'testproject' |
|---|
| 83 | default_parameters['enable'] = False |
|---|
| 84 | project.add(self.env, client_name, project_name, default_parameters) |
|---|
| 85 | |
|---|
| 86 | # Enable project |
|---|
| 87 | project.enable(self.env, client_name, project_name) |
|---|
| 88 | |
|---|
| 89 | # Validate |
|---|
| 90 | open(self.env.get_path(os.path.join('projects-enabled', client_name + '-' + project_name))) |
|---|
| 91 | |
|---|
| 92 | def test_disable_deletes_symlink(self): |
|---|
| 93 | |
|---|
| 94 | # Fixtures |
|---|
| 95 | client_name = 'testclient' |
|---|
| 96 | client.add(self.env, client_name, {'enable' : 'n'}) |
|---|
| 97 | project_name = 'testproject' |
|---|
| 98 | project.add(self.env, client_name, project_name, default_parameters) |
|---|
| 99 | |
|---|
| 100 | # Enable project |
|---|
| 101 | project.disable(self.env, client_name, project_name) |
|---|
| 102 | |
|---|
| 103 | # Validate |
|---|
| 104 | try: |
|---|
| 105 | open(self.env.get_path(os.path.join('projects-enabled', client_name + '-' + project_name))) |
|---|
| 106 | self.fail('project.disable() deletes projects-enabled symlink') |
|---|
| 107 | except: |
|---|
| 108 | pass |
|---|
| 109 | |
|---|
| 110 | |
|---|
| 111 | def suite(): |
|---|
| 112 | return unittest.makeSuite(ProjectTestCase, 'test') |
|---|
| 113 | |
|---|
| 114 | if __name__ == '__main__': |
|---|
| 115 | unittest.main(defaultTest='suite') |
|---|