~didrocks/ubuntuone-client/use_result_var

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
# tests.platform.linux - linux platform tests
#
# Author: Guillermo Gonzalez <guillermo.gonzalez@canonical.com>
#
# Copyright 2010 Canonical Ltd.
#
# This program is free software: you can redistribute it and/or modify it
# under the terms of the GNU General Public License version 3, as published
# by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranties of
# MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
# PURPOSE.  See the GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program.  If not, see <http://www.gnu.org/licenses/>.

"""Linux specific tests for the platform module."""

import errno
import os

from contrib.testing.testcase import BaseTwistedTestCase
from ubuntuone.platform import (
    access,
    allow_writes,
    listdir,
    make_dir,
    open_file,
    path_exists,
    remove_dir,
    remove_file,
    rename,
    set_dir_readonly,
    set_dir_readwrite,
    set_file_readonly,
    set_file_readwrite,
    stat_path,
)


class OSWrapperTests(BaseTwistedTestCase):
    """Tests for os wrapper functions."""

    def setUp(self):
        BaseTwistedTestCase.setUp(self)
        self.basedir = self.mktemp('test_root')
        self.testfile = os.path.join(self.basedir, "test_file")
        open(self.testfile, 'w').close()

    def tearDown(self):
        self.rmtree(self.basedir)
        BaseTwistedTestCase.tearDown(self)

    def test_set_dir_readonly(self):
        """Test for set_dir_readonly."""
        set_dir_readonly(self.basedir)
        self.assertRaises(OSError, os.mkdir, os.path.join(self.basedir, 'foo'))

    def test_set_dir_readwrite(self):
        """Test for set_dir_readwrite."""
        set_dir_readonly(self.basedir)
        self.assertRaises(OSError, os.mkdir, os.path.join(self.basedir, 'foo'))
        set_dir_readwrite(self.basedir)
        os.mkdir(os.path.join(self.basedir, 'foo'))

    def test_allow_writes(self):
        """Test for allow_writes."""
        set_dir_readonly(self.basedir)
        self.assertRaises(OSError, os.mkdir, os.path.join(self.basedir, 'foo'))
        with allow_writes(self.basedir):
            os.mkdir(os.path.join(self.basedir, 'foo'))

    def test_set_file_readonly(self):
        """Test for set_file_readonly."""
        set_file_readonly(self.testfile)
        self.assertRaises(IOError, open, self.testfile, 'w')

    def test_set_file_readwrite(self):
        """Test for set_file_readwrite."""
        set_file_readonly(self.testfile)
        self.assertRaises(IOError, open, self.testfile, 'w')
        set_file_readwrite(self.testfile)
        open(self.testfile, 'w')

    def test_path_file_exist_yes(self):
        """Test that the file exists."""
        self.assertTrue(path_exists(self.testfile))

    def test_path_file_exist_no(self):
        """Test that the file doesn't exist."""
        os.remove(self.testfile)
        self.assertFalse(path_exists(self.testfile))

    def test_path_dir_exist_yes(self):
        """Test that the dir exists."""
        self.assertTrue(path_exists(self.basedir))

    def test_path_dir_exist_no(self):
        """Test that the dir doesn't exist."""
        os.remove(self.testfile)
        self.assertFalse(path_exists(os.path.join(self.basedir, 'nodir')))

    def test_remove_file(self):
        """Test the remove file."""
        remove_file(self.testfile)
        self.assertFalse(os.path.exists(self.testfile))

    def test_remove_dir(self):
        """Test the remove dir."""
        testdir = os.path.join(self.basedir, 'foodir')
        os.mkdir(testdir)
        assert os.path.exists(testdir)
        remove_dir(testdir)
        self.assertFalse(path_exists(testdir))

    def test_make_dir_one(self):
        """Test the make dir with one dir."""
        testdir = os.path.join(self.basedir, 'foodir')
        assert not os.path.exists(testdir)
        make_dir(testdir)
        self.assertTrue(os.path.exists(testdir))

    def test_make_dir_already_there(self):
        """Test the make dir with one dir that exists."""
        self.assertRaises(OSError, make_dir, self.basedir)

    def test_make_dir_recursive_no(self):
        """Test the make dir with some dirs, not recursive explicit."""
        testdir = os.path.join(self.basedir, 'foo', 'bar')
        assert not os.path.exists(testdir)
        self.assertRaises(OSError, make_dir, testdir)

    def test_make_dir_recursive_yes(self):
        """Test the make dir with some dirs, recursive."""
        testdir = os.path.join(self.basedir, 'foo', 'bar')
        assert not os.path.exists(testdir)
        make_dir(testdir, recursive=True)
        self.assertTrue(os.path.exists(testdir))

    def test_open_file_not_there(self):
        """Open a file that does not exist."""
        self.assertRaises(IOError, open_file, os.path.join(self.basedir, 'no'))

    def test_open_file_gets_a_fileobject(self):
        """Open a file, and get a file object."""
        testfile = os.path.join(self.basedir, 'testfile')
        open(testfile, 'w').close()
        f = open_file(testfile)
        self.assertTrue(isinstance(f, file))

    def test_open_file_read(self):
        """Open a file, and read."""
        testfile = os.path.join(self.basedir, 'testfile')
        with open(testfile, 'w') as fh:
            fh.write("foo")
        f = open_file(testfile, 'r')
        self.assertTrue(f.read(), "foo")

    def test_open_file_write(self):
        """Open a file, and write."""
        testfile = os.path.join(self.basedir, 'testfile')
        with open_file(testfile, 'w') as fh:
            fh.write("foo")
        f = open(testfile)
        self.assertTrue(f.read(), "foo")

    def test_rename_not_there(self):
        """Rename something that does not exist."""
        self.assertRaises(OSError, rename,
                          os.path.join(self.basedir, 'no'), 'foo')

    def test_rename_file(self):
        """Rename a file."""
        testfile1 = os.path.join(self.basedir, 'testfile1')
        testfile2 = os.path.join(self.basedir, 'testfile2')
        open(testfile1, 'w').close()
        rename(testfile1, testfile2)
        self.assertFalse(os.path.exists(testfile1))
        self.assertTrue(os.path.exists(testfile2))

    def test_rename_dir(self):
        """Rename a dir."""
        testdir1 = os.path.join(self.basedir, 'testdir1')
        testdir2 = os.path.join(self.basedir, 'testdir2')
        os.mkdir(testdir1)
        rename(testdir1, testdir2)
        self.assertFalse(os.path.exists(testdir1))
        self.assertTrue(os.path.exists(testdir2))

    def test_listdir(self):
        """Return a list of the files in a dir."""
        open(os.path.join(self.basedir, 'foo'), 'w').close()
        open(os.path.join(self.basedir, 'bar'), 'w').close()
        l = listdir(self.basedir)
        self.assertEqual(sorted(l), ['bar', 'foo', 'test_file'])

    def test_access_rw(self):
        """Test access on a file with full permission."""
        self.assertTrue(access(self.testfile))

    def test_access_ro(self):
        """Test access on a file with read only permission."""
        os.chmod(self.testfile, 0o444)
        self.assertTrue(access(self.testfile))
        os.chmod(self.testfile, 0o664)

    def test_access_nothing(self):
        """Test access on a file with no permission at all."""
        os.chmod(self.testfile, 0o000)
        self.assertFalse(access(self.testfile))
        os.chmod(self.testfile, 0o664)

    def test_stat_normal(self):
        """Test on a normal file."""
        self.assertEqual(os.stat(self.testfile), stat_path(self.testfile))

    def test_stat_symlink(self):
        """Test that it doesn't follow symlinks.

        We compare the inode only (enough to see if it's returning info
        from the link or the linked), as we can not compare the full stat
        because the st_mode will be different.
        """
        link = os.path.join(self.basedir, 'foo')
        os.symlink(self.testfile, link)
        self.assertNotEqual(os.stat(link).st_ino, stat_path(link).st_ino)
        self.assertEqual(os.lstat(link).st_ino, stat_path(link).st_ino)

    def test_stat_no_path(self):
        """Test that it raises proper error when no file is there."""
        try:
            return stat_path(os.path.join(self.basedir, 'nofile'))
        except OSError, e:
            self.assertEqual(e.errno, errno.ENOENT)

    def test_path_exists_file_yes(self):
        """The file is there."""
        self.assertTrue(path_exists(self.testfile))

    def test_path_exists_file_no(self):
        """The file is not there."""
        os.remove(self.testfile)
        self.assertFalse(path_exists(self.testfile))

    def test_path_exists_dir_yes(self):
        """The dir is there."""
        self.assertTrue(path_exists(self.basedir))

    def test_path_exists_dir_no(self):
        """The dir is not there."""
        self.rmtree(self.basedir)
        self.assertFalse(path_exists(self.basedir))