1
# tests.platform.linux - linux platform tests
3
# Author: Guillermo Gonzalez <guillermo.gonzalez@canonical.com>
5
# Copyright 2010 Canonical Ltd.
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.
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.
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/>.
19
"""Linux specific tests for the platform module."""
24
from contrib.testing.testcase import BaseTwistedTestCase
25
from ubuntuone.platform import (
43
class OSWrapperTests(BaseTwistedTestCase):
44
"""Tests for os wrapper functions."""
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()
53
self.rmtree(self.basedir)
54
BaseTwistedTestCase.tearDown(self)
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'))
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'))
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'))
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')
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')
87
def test_path_file_exist_yes(self):
88
"""Test that the file exists."""
89
self.assertTrue(path_exists(self.testfile))
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))
96
def test_path_dir_exist_yes(self):
97
"""Test that the dir exists."""
98
self.assertTrue(path_exists(self.basedir))
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')))
105
def test_remove_file(self):
106
"""Test the remove file."""
107
remove_file(self.testfile)
108
self.assertFalse(os.path.exists(self.testfile))
110
def test_remove_dir(self):
111
"""Test the remove dir."""
112
testdir = os.path.join(self.basedir, 'foodir')
114
assert os.path.exists(testdir)
116
self.assertFalse(path_exists(testdir))
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)
123
self.assertTrue(os.path.exists(testdir))
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)
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)
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))
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'))
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))
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:
158
f = open_file(testfile, 'r')
159
self.assertTrue(f.read(), "foo")
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:
167
self.assertTrue(f.read(), "foo")
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')
174
def test_rename_file(self):
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))
183
def test_rename_dir(self):
185
testdir1 = os.path.join(self.basedir, 'testdir1')
186
testdir2 = os.path.join(self.basedir, 'testdir2')
188
rename(testdir1, testdir2)
189
self.assertFalse(os.path.exists(testdir1))
190
self.assertTrue(os.path.exists(testdir2))
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'])
199
def test_access_rw(self):
200
"""Test access on a file with full permission."""
201
self.assertTrue(access(self.testfile))
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)
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)
215
def test_stat_normal(self):
216
"""Test on a normal file."""
217
self.assertEqual(os.stat(self.testfile), stat_path(self.testfile))
219
def test_stat_symlink(self):
220
"""Test that it doesn't follow symlinks.
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.
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)
231
def test_stat_no_path(self):
232
"""Test that it raises proper error when no file is there."""
234
return stat_path(os.path.join(self.basedir, 'nofile'))
236
self.assertEqual(e.errno, errno.ENOENT)
238
def test_path_exists_file_yes(self):
239
"""The file is there."""
240
self.assertTrue(path_exists(self.testfile))
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))
247
def test_path_exists_dir_yes(self):
248
"""The dir is there."""
249
self.assertTrue(path_exists(self.basedir))
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))