~verterok/ubuntuone-client/tritcask-3

« back to all changes in this revision

Viewing changes to tests/platform/test_os_helper.py

  • Committer: guillermo.gonzalez at canonical
  • Date: 2010-12-13 20:55:10 UTC
  • mfrom: (757.1.16 ubuntuone-client)
  • Revision ID: guillermo.gonzalez@canonical.com-20101213205510-md2aq59wyv9zv0m2
merge with trunk

Show diffs side-by-side

added added

removed removed

Lines of Context:
19
19
import os
20
20
 
21
21
from contrib.testing.testcase import BaseTwistedTestCase
22
 
from ubuntuone.platform.linux import set_readonly, set_readwrite, allow_writes
 
22
from ubuntuone.platform.linux import (
 
23
    set_dir_readonly,
 
24
    set_file_readonly,
 
25
    set_dir_readwrite,
 
26
    set_file_readwrite,
 
27
    allow_writes,
 
28
    remove_file,
 
29
    remove_dir,
 
30
    path_exists,
 
31
    make_dir,
 
32
    open_file,
 
33
    rename,
 
34
)
23
35
 
24
36
 
25
37
class OSWrapperTests(BaseTwistedTestCase):
28
40
    def setUp(self):
29
41
        BaseTwistedTestCase.setUp(self)
30
42
        self.basedir = self.mktemp('test_root')
 
43
        self.testfile = os.path.join(self.basedir, "test_file")
 
44
        open(self.testfile, 'w').close()
31
45
 
32
46
    def tearDown(self):
33
47
        self.rmtree(self.basedir)
34
48
        BaseTwistedTestCase.tearDown(self)
35
49
 
36
 
    def test_set_readonly(self):
37
 
        """Linux specific test for set_readonly."""
38
 
        set_readonly(self.basedir)
 
50
    def test_set_dir_readonly(self):
 
51
        """Test for set_dir_readonly."""
 
52
        set_dir_readonly(self.basedir)
39
53
        self.assertRaises(OSError, os.mkdir, os.path.join(self.basedir, 'foo'))
40
54
 
41
 
    def test_set_readwrite(self):
42
 
        """Linux specific test for set_readwrite."""
43
 
        set_readonly(self.basedir)
 
55
    def test_set_dir_readwrite(self):
 
56
        """Test for set_dir_readwrite."""
 
57
        set_dir_readonly(self.basedir)
44
58
        self.assertRaises(OSError, os.mkdir, os.path.join(self.basedir, 'foo'))
45
 
        set_readwrite(self.basedir)
 
59
        set_dir_readwrite(self.basedir)
46
60
        os.mkdir(os.path.join(self.basedir, 'foo'))
47
61
 
48
62
    def test_allow_writes(self):
49
 
        """Linux specific tests for allow_writes."""
50
 
        set_readonly(self.basedir)
 
63
        """Test for allow_writes."""
 
64
        set_dir_readonly(self.basedir)
51
65
        self.assertRaises(OSError, os.mkdir, os.path.join(self.basedir, 'foo'))
52
66
        with allow_writes(self.basedir):
53
67
            os.mkdir(os.path.join(self.basedir, 'foo'))
54
68
 
 
69
    def test_set_file_readonly(self):
 
70
        """Test for set_file_readonly."""
 
71
        set_file_readonly(self.testfile)
 
72
        self.assertRaises(IOError, open, self.testfile, 'w')
 
73
 
 
74
    def test_set_file_readwrite(self):
 
75
        """Test for set_file_readwrite."""
 
76
        set_file_readonly(self.testfile)
 
77
        self.assertRaises(IOError, open, self.testfile, 'w')
 
78
        set_file_readwrite(self.testfile)
 
79
        open(self.testfile, 'w')
 
80
 
 
81
    def test_path_file_exist_yes(self):
 
82
        """Test that the file exists."""
 
83
        self.assertTrue(path_exists(self.testfile))
 
84
 
 
85
    def test_path_file_exist_no(self):
 
86
        """Test that the file doesn't exist."""
 
87
        os.remove(self.testfile)
 
88
        self.assertFalse(path_exists(self.testfile))
 
89
 
 
90
    def test_path_dir_exist_yes(self):
 
91
        """Test that the dir exists."""
 
92
        self.assertTrue(path_exists(self.basedir))
 
93
 
 
94
    def test_path_dir_exist_no(self):
 
95
        """Test that the dir doesn't exist."""
 
96
        os.remove(self.testfile)
 
97
        self.assertFalse(path_exists(os.path.join(self.basedir, 'nodir')))
 
98
 
 
99
    def test_remove_file(self):
 
100
        """Test the remove file."""
 
101
        remove_file(self.testfile)
 
102
        self.assertFalse(os.path.exists(self.testfile))
 
103
 
 
104
    def test_remove_dir(self):
 
105
        """Test the remove dir."""
 
106
        testdir = os.path.join(self.basedir, 'foodir')
 
107
        os.mkdir(testdir)
 
108
        assert os.path.exists(testdir)
 
109
        remove_dir(testdir)
 
110
        self.assertFalse(path_exists(testdir))
 
111
 
 
112
    def test_make_dir_one(self):
 
113
        """Test the make dir with one dir."""
 
114
        testdir = os.path.join(self.basedir, 'foodir')
 
115
        assert not os.path.exists(testdir)
 
116
        make_dir(testdir)
 
117
        self.assertTrue(os.path.exists(testdir))
 
118
 
 
119
    def test_make_dir_already_there(self):
 
120
        """Test the make dir with one dir that exists."""
 
121
        self.assertRaises(OSError, make_dir, self.basedir)
 
122
 
 
123
    def test_make_dir_recursive_no(self):
 
124
        """Test the make dir with some dirs, not recursive explicit."""
 
125
        testdir = os.path.join(self.basedir, 'foo', 'bar')
 
126
        assert not os.path.exists(testdir)
 
127
        self.assertRaises(OSError, make_dir, testdir)
 
128
 
 
129
    def test_make_dir_recursive_yes(self):
 
130
        """Test the make dir with some dirs, recursive."""
 
131
        testdir = os.path.join(self.basedir, 'foo', 'bar')
 
132
        assert not os.path.exists(testdir)
 
133
        make_dir(testdir, recursive=True)
 
134
        self.assertTrue(os.path.exists(testdir))
 
135
 
 
136
    def test_open_file_not_there(self):
 
137
        """Open a file that does not exist."""
 
138
        self.assertRaises(IOError, open_file, os.path.join(self.basedir, 'no'))
 
139
 
 
140
    def test_open_file_gets_a_fileobject(self):
 
141
        """Open a file, and get a file object."""
 
142
        testfile = os.path.join(self.basedir, 'testfile')
 
143
        open(testfile, 'w').close()
 
144
        f = open_file(testfile)
 
145
        self.assertTrue(isinstance(f, file))
 
146
 
 
147
    def test_open_file_read(self):
 
148
        """Open a file, and read."""
 
149
        testfile = os.path.join(self.basedir, 'testfile')
 
150
        with open(testfile, 'w') as fh:
 
151
            fh.write("foo")
 
152
        f = open_file(testfile, 'r')
 
153
        self.assertTrue(f.read(), "foo")
 
154
 
 
155
    def test_open_file_write(self):
 
156
        """Open a file, and write."""
 
157
        testfile = os.path.join(self.basedir, 'testfile')
 
158
        with open_file(testfile, 'w') as fh:
 
159
            fh.write("foo")
 
160
        f = open(testfile)
 
161
        self.assertTrue(f.read(), "foo")
 
162
 
 
163
    def test_rename_not_there(self):
 
164
        """Rename something that does not exist."""
 
165
        self.assertRaises(OSError, rename,
 
166
                          os.path.join(self.basedir, 'no'), 'foo')
 
167
 
 
168
    def test_rename_file(self):
 
169
        """Rename a file."""
 
170
        testfile1 = os.path.join(self.basedir, 'testfile1')
 
171
        testfile2 = os.path.join(self.basedir, 'testfile2')
 
172
        open(testfile1, 'w').close()
 
173
        rename(testfile1, testfile2)
 
174
        self.assertFalse(os.path.exists(testfile1))
 
175
        self.assertTrue(os.path.exists(testfile2))
 
176
 
 
177
    def test_rename_dir(self):
 
178
        """Rename a dir."""
 
179
        testdir1 = os.path.join(self.basedir, 'testdir1')
 
180
        testdir2 = os.path.join(self.basedir, 'testdir2')
 
181
        os.mkdir(testdir1)
 
182
        rename(testdir1, testdir2)
 
183
        self.assertFalse(os.path.exists(testdir1))
 
184
        self.assertTrue(os.path.exists(testdir2))
 
185