~didrocks/ubuntuone-client/use_result_var

« back to all changes in this revision

Viewing changes to tests/platform/linux/test_os_helper.py

  • Committer: Bazaar Package Importer
  • Author(s): Rodney Dawes
  • Date: 2011-02-11 16:18:11 UTC
  • mto: This revision was merged to the branch mainline in revision 67.
  • Revision ID: james.westby@ubuntu.com-20110211161811-n18dj9lde7dxqjzr
Tags: upstream-1.5.4
ImportĀ upstreamĀ versionĀ 1.5.4

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# tests.platform.linux - linux platform tests
 
2
#
 
3
# Author: Guillermo Gonzalez <guillermo.gonzalez@canonical.com>
 
4
#
 
5
# Copyright 2010 Canonical Ltd.
 
6
#
 
7
# This program is free software: you can redistribute it and/or modify it
 
8
# under the terms of the GNU General Public License version 3, as published
 
9
# by the Free Software Foundation.
 
10
#
 
11
# This program is distributed in the hope that it will be useful, but
 
12
# WITHOUT ANY WARRANTY; without even the implied warranties of
 
13
# MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
 
14
# PURPOSE.  See the GNU General Public License for more details.
 
15
#
 
16
# You should have received a copy of the GNU General Public License along
 
17
# with this program.  If not, see <http://www.gnu.org/licenses/>.
 
18
 
 
19
"""Linux specific tests for the platform module."""
 
20
 
 
21
import errno
 
22
import os
 
23
 
 
24
from contrib.testing.testcase import BaseTwistedTestCase
 
25
from ubuntuone.platform import (
 
26
    access,
 
27
    allow_writes,
 
28
    listdir,
 
29
    make_dir,
 
30
    open_file,
 
31
    path_exists,
 
32
    remove_dir,
 
33
    remove_file,
 
34
    rename,
 
35
    set_dir_readonly,
 
36
    set_dir_readwrite,
 
37
    set_file_readonly,
 
38
    set_file_readwrite,
 
39
    stat_path,
 
40
)
 
41
 
 
42
 
 
43
class OSWrapperTests(BaseTwistedTestCase):
 
44
    """Tests for os wrapper functions."""
 
45
 
 
46
    def setUp(self):
 
47
        BaseTwistedTestCase.setUp(self)
 
48
        self.basedir = self.mktemp('test_root')
 
49
        self.testfile = os.path.join(self.basedir, "test_file")
 
50
        open(self.testfile, 'w').close()
 
51
 
 
52
    def tearDown(self):
 
53
        self.rmtree(self.basedir)
 
54
        BaseTwistedTestCase.tearDown(self)
 
55
 
 
56
    def test_set_dir_readonly(self):
 
57
        """Test for set_dir_readonly."""
 
58
        set_dir_readonly(self.basedir)
 
59
        self.assertRaises(OSError, os.mkdir, os.path.join(self.basedir, 'foo'))
 
60
 
 
61
    def test_set_dir_readwrite(self):
 
62
        """Test for set_dir_readwrite."""
 
63
        set_dir_readonly(self.basedir)
 
64
        self.assertRaises(OSError, os.mkdir, os.path.join(self.basedir, 'foo'))
 
65
        set_dir_readwrite(self.basedir)
 
66
        os.mkdir(os.path.join(self.basedir, 'foo'))
 
67
 
 
68
    def test_allow_writes(self):
 
69
        """Test for allow_writes."""
 
70
        set_dir_readonly(self.basedir)
 
71
        self.assertRaises(OSError, os.mkdir, os.path.join(self.basedir, 'foo'))
 
72
        with allow_writes(self.basedir):
 
73
            os.mkdir(os.path.join(self.basedir, 'foo'))
 
74
 
 
75
    def test_set_file_readonly(self):
 
76
        """Test for set_file_readonly."""
 
77
        set_file_readonly(self.testfile)
 
78
        self.assertRaises(IOError, open, self.testfile, 'w')
 
79
 
 
80
    def test_set_file_readwrite(self):
 
81
        """Test for set_file_readwrite."""
 
82
        set_file_readonly(self.testfile)
 
83
        self.assertRaises(IOError, open, self.testfile, 'w')
 
84
        set_file_readwrite(self.testfile)
 
85
        open(self.testfile, 'w')
 
86
 
 
87
    def test_path_file_exist_yes(self):
 
88
        """Test that the file exists."""
 
89
        self.assertTrue(path_exists(self.testfile))
 
90
 
 
91
    def test_path_file_exist_no(self):
 
92
        """Test that the file doesn't exist."""
 
93
        os.remove(self.testfile)
 
94
        self.assertFalse(path_exists(self.testfile))
 
95
 
 
96
    def test_path_dir_exist_yes(self):
 
97
        """Test that the dir exists."""
 
98
        self.assertTrue(path_exists(self.basedir))
 
99
 
 
100
    def test_path_dir_exist_no(self):
 
101
        """Test that the dir doesn't exist."""
 
102
        os.remove(self.testfile)
 
103
        self.assertFalse(path_exists(os.path.join(self.basedir, 'nodir')))
 
104
 
 
105
    def test_remove_file(self):
 
106
        """Test the remove file."""
 
107
        remove_file(self.testfile)
 
108
        self.assertFalse(os.path.exists(self.testfile))
 
109
 
 
110
    def test_remove_dir(self):
 
111
        """Test the remove dir."""
 
112
        testdir = os.path.join(self.basedir, 'foodir')
 
113
        os.mkdir(testdir)
 
114
        assert os.path.exists(testdir)
 
115
        remove_dir(testdir)
 
116
        self.assertFalse(path_exists(testdir))
 
117
 
 
118
    def test_make_dir_one(self):
 
119
        """Test the make dir with one dir."""
 
120
        testdir = os.path.join(self.basedir, 'foodir')
 
121
        assert not os.path.exists(testdir)
 
122
        make_dir(testdir)
 
123
        self.assertTrue(os.path.exists(testdir))
 
124
 
 
125
    def test_make_dir_already_there(self):
 
126
        """Test the make dir with one dir that exists."""
 
127
        self.assertRaises(OSError, make_dir, self.basedir)
 
128
 
 
129
    def test_make_dir_recursive_no(self):
 
130
        """Test the make dir with some dirs, not recursive explicit."""
 
131
        testdir = os.path.join(self.basedir, 'foo', 'bar')
 
132
        assert not os.path.exists(testdir)
 
133
        self.assertRaises(OSError, make_dir, testdir)
 
134
 
 
135
    def test_make_dir_recursive_yes(self):
 
136
        """Test the make dir with some dirs, recursive."""
 
137
        testdir = os.path.join(self.basedir, 'foo', 'bar')
 
138
        assert not os.path.exists(testdir)
 
139
        make_dir(testdir, recursive=True)
 
140
        self.assertTrue(os.path.exists(testdir))
 
141
 
 
142
    def test_open_file_not_there(self):
 
143
        """Open a file that does not exist."""
 
144
        self.assertRaises(IOError, open_file, os.path.join(self.basedir, 'no'))
 
145
 
 
146
    def test_open_file_gets_a_fileobject(self):
 
147
        """Open a file, and get a file object."""
 
148
        testfile = os.path.join(self.basedir, 'testfile')
 
149
        open(testfile, 'w').close()
 
150
        f = open_file(testfile)
 
151
        self.assertTrue(isinstance(f, file))
 
152
 
 
153
    def test_open_file_read(self):
 
154
        """Open a file, and read."""
 
155
        testfile = os.path.join(self.basedir, 'testfile')
 
156
        with open(testfile, 'w') as fh:
 
157
            fh.write("foo")
 
158
        f = open_file(testfile, 'r')
 
159
        self.assertTrue(f.read(), "foo")
 
160
 
 
161
    def test_open_file_write(self):
 
162
        """Open a file, and write."""
 
163
        testfile = os.path.join(self.basedir, 'testfile')
 
164
        with open_file(testfile, 'w') as fh:
 
165
            fh.write("foo")
 
166
        f = open(testfile)
 
167
        self.assertTrue(f.read(), "foo")
 
168
 
 
169
    def test_rename_not_there(self):
 
170
        """Rename something that does not exist."""
 
171
        self.assertRaises(OSError, rename,
 
172
                          os.path.join(self.basedir, 'no'), 'foo')
 
173
 
 
174
    def test_rename_file(self):
 
175
        """Rename a file."""
 
176
        testfile1 = os.path.join(self.basedir, 'testfile1')
 
177
        testfile2 = os.path.join(self.basedir, 'testfile2')
 
178
        open(testfile1, 'w').close()
 
179
        rename(testfile1, testfile2)
 
180
        self.assertFalse(os.path.exists(testfile1))
 
181
        self.assertTrue(os.path.exists(testfile2))
 
182
 
 
183
    def test_rename_dir(self):
 
184
        """Rename a dir."""
 
185
        testdir1 = os.path.join(self.basedir, 'testdir1')
 
186
        testdir2 = os.path.join(self.basedir, 'testdir2')
 
187
        os.mkdir(testdir1)
 
188
        rename(testdir1, testdir2)
 
189
        self.assertFalse(os.path.exists(testdir1))
 
190
        self.assertTrue(os.path.exists(testdir2))
 
191
 
 
192
    def test_listdir(self):
 
193
        """Return a list of the files in a dir."""
 
194
        open(os.path.join(self.basedir, 'foo'), 'w').close()
 
195
        open(os.path.join(self.basedir, 'bar'), 'w').close()
 
196
        l = listdir(self.basedir)
 
197
        self.assertEqual(sorted(l), ['bar', 'foo', 'test_file'])
 
198
 
 
199
    def test_access_rw(self):
 
200
        """Test access on a file with full permission."""
 
201
        self.assertTrue(access(self.testfile))
 
202
 
 
203
    def test_access_ro(self):
 
204
        """Test access on a file with read only permission."""
 
205
        os.chmod(self.testfile, 0o444)
 
206
        self.assertTrue(access(self.testfile))
 
207
        os.chmod(self.testfile, 0o664)
 
208
 
 
209
    def test_access_nothing(self):
 
210
        """Test access on a file with no permission at all."""
 
211
        os.chmod(self.testfile, 0o000)
 
212
        self.assertFalse(access(self.testfile))
 
213
        os.chmod(self.testfile, 0o664)
 
214
 
 
215
    def test_stat_normal(self):
 
216
        """Test on a normal file."""
 
217
        self.assertEqual(os.stat(self.testfile), stat_path(self.testfile))
 
218
 
 
219
    def test_stat_symlink(self):
 
220
        """Test that it doesn't follow symlinks.
 
221
 
 
222
        We compare the inode only (enough to see if it's returning info
 
223
        from the link or the linked), as we can not compare the full stat
 
224
        because the st_mode will be different.
 
225
        """
 
226
        link = os.path.join(self.basedir, 'foo')
 
227
        os.symlink(self.testfile, link)
 
228
        self.assertNotEqual(os.stat(link).st_ino, stat_path(link).st_ino)
 
229
        self.assertEqual(os.lstat(link).st_ino, stat_path(link).st_ino)
 
230
 
 
231
    def test_stat_no_path(self):
 
232
        """Test that it raises proper error when no file is there."""
 
233
        try:
 
234
            return stat_path(os.path.join(self.basedir, 'nofile'))
 
235
        except OSError, e:
 
236
            self.assertEqual(e.errno, errno.ENOENT)
 
237
 
 
238
    def test_path_exists_file_yes(self):
 
239
        """The file is there."""
 
240
        self.assertTrue(path_exists(self.testfile))
 
241
 
 
242
    def test_path_exists_file_no(self):
 
243
        """The file is not there."""
 
244
        os.remove(self.testfile)
 
245
        self.assertFalse(path_exists(self.testfile))
 
246
 
 
247
    def test_path_exists_dir_yes(self):
 
248
        """The dir is there."""
 
249
        self.assertTrue(path_exists(self.basedir))
 
250
 
 
251
    def test_path_exists_dir_no(self):
 
252
        """The dir is not there."""
 
253
        self.rmtree(self.basedir)
 
254
        self.assertFalse(path_exists(self.basedir))