summaryrefslogtreecommitdiffstats
path: root/test/unit/common/test_fs_utils.py
blob: 186e07d59b6b1197998c4220a2a05677f8d9fecb (plain)
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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
# Copyright (c) 2012 Red Hat, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
# implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import os
import shutil
import random
import unittest
from tempfile import mkdtemp, mkstemp
from gluster.swift.common import fs_utils as fs
from gluster.swift.common.exceptions import NotDirectoryError, \
    FileOrDirNotFoundError

class TestUtils(unittest.TestCase):
    """ Tests for common.utils """

    def test_do_walk(self):
        try:
            # create directory structure
            tmpparent = mkdtemp()
            tmpdirs = []
            tmpfiles = []
            for i in range(5):
                tmpdirs.append(mkdtemp(dir=tmpparent).rsplit(os.path.sep, 1)[1])
                tmpfiles.append(mkstemp(dir=tmpparent)[1].rsplit(os.path.sep, \
                                                                     1)[1])

                for path, dirnames, filenames in fs.do_walk(tmpparent):
                    assert path == tmpparent
                    assert dirnames.sort() == tmpdirs.sort()
                    assert filenames.sort() == tmpfiles.sort()
                    break
        finally:
            shutil.rmtree(tmpparent)

    def test_do_open(self):
        try:
            fd, tmpfile = mkstemp()
            f = fs.do_open(tmpfile, 'r')
            try:
                f.write('test')
            except IOError as err:
                pass
            else:
                self.fail("IOError expected")
        finally:
            f.close()
            os.close(fd)
            os.remove(tmpfile)

    def test_do_open_err(self):
        try:
            fs.do_open(os.path.join('/tmp', str(random.random())), 'r')
        except IOError:
            pass
        else:
            self.fail("IOError expected")

    def test_do_write(self):
        try:
            fd, tmpfile = mkstemp()
            cnt = fs.do_write(fd, "test")
            assert cnt == len("test")
        finally:
            os.close(fd)
            os.remove(tmpfile)

    def test_do_write_err(self):
        try:
            fd, tmpfile = mkstemp()
            fd1 = os.open(tmpfile, os.O_RDONLY)
            fs.do_write(fd1, "test")
        except OSError:
            pass
        else:
            self.fail("OSError expected")
        finally:
            os.close(fd)
            os.close(fd1)

    def test_do_mkdir(self):
        try:
            path = os.path.join('/tmp', str(random.random()))
            fs.do_mkdir(path)
            assert os.path.exists(path)
            assert fs.do_mkdir(path)
        finally:
            os.rmdir(path)

    def test_do_mkdir_err(self):
        try:
            path = os.path.join('/tmp', str(random.random()), str(random.random()))
            fs.do_mkdir(path)
        except OSError:
            pass
        else:
            self.fail("OSError expected")


    def test_do_makedirs(self):
        try:
            subdir = os.path.join('/tmp', str(random.random()))
            path = os.path.join(subdir, str(random.random()))
            fs.do_makedirs(path)
            assert os.path.exists(path)
            assert fs.do_makedirs(path)
        finally:
            shutil.rmtree(subdir)

    def test_do_listdir(self):
        try:
            tmpdir = mkdtemp()
            subdir = []
            for i in range(5):
                subdir.append(mkdtemp(dir=tmpdir).rsplit(os.path.sep, 1)[1])

            assert subdir.sort() == fs.do_listdir(tmpdir).sort()
        finally:
            shutil.rmtree(tmpdir)

    def test_do_listdir_err(self):
        try:
            path = os.path.join('/tmp', str(random.random()))
            fs.do_listdir(path)
        except OSError:
            pass
        else:
            self.fail("OSError expected")

    def test_do_stat(self):
        try:
            tmpdir = mkdtemp()
            fd, tmpfile = mkstemp(dir=tmpdir)
            buf1 = os.stat(tmpfile)
            buf2 = fs.do_stat(fd)
            buf3 = fs.do_stat(tmpfile)

            assert buf1 == buf2
            assert buf1 == buf3
        finally:
            os.close(fd)
            os.remove(tmpfile)
            os.rmdir(tmpdir)

    def test_do_stat_err(self):
        try:
            fs.do_stat(os.path.join('/tmp', str(random.random())))
        except OSError:
            pass
        else:
            self.fail("OSError expected")

    def test_do_close(self):
        try:
            fd, tmpfile = mkstemp()
            fs.do_close(fd);
            try:
                os.write(fd, "test")
            except OSError:
                pass
            else:
                self.fail("OSError expected")
            fp = open(tmpfile)
            fs.do_close(fp)
        finally:
            os.remove(tmpfile)

    def test_do_unlink(self):
        try:
            fd, tmpfile = mkstemp()
            fs.do_unlink(tmpfile)
            assert not os.path.exists(tmpfile)
            assert fs.do_unlink(os.path.join('/tmp', str(random.random())))
        finally:
            os.close(fd)

    def test_do_unlink_err(self):
        try:
            tmpdir = mkdtemp()
            fs.do_unlink(tmpdir)
        except OSError:
            pass
        else:
            self.fail('OSError expected')
        finally:
            os.rmdir(tmpdir)

    def test_do_rmdir(self):
        tmpdir = mkdtemp()
        fs.do_rmdir(tmpdir)
        assert not os.path.exists(tmpdir)
        assert not fs.do_rmdir(os.path.join('/tmp', str(random.random())))

    def test_do_rmdir_err(self):
        try:
            fd, tmpfile = mkstemp()
            fs.do_rmdir(tmpfile)
        except OSError:
            pass
        else:
            self.fail('OSError expected')
        finally:
            os.close(fd)
            os.remove(tmpfile)

    def test_do_rename(self):
        try:
            srcpath = mkdtemp()
            destpath = os.path.join('/tmp', str(random.random()))
            fs.do_rename(srcpath, destpath)
            assert not os.path.exists(srcpath)
            assert os.path.exists(destpath)
        finally:
            os.rmdir(destpath)

    def test_do_rename_err(self):
        try:
            srcpath = os.path.join('/tmp', str(random.random()))
            destpath = os.path.join('/tmp', str(random.random()))
            fs.do_rename(srcpath, destpath)
        except OSError:
            pass
        else:
            self.fail("OSError expected")

    def test_dir_empty(self):
        try:
            tmpdir = mkdtemp()
            subdir = mkdtemp(dir=tmpdir)
            assert not fs.dir_empty(tmpdir)
            assert fs.dir_empty(subdir)
        finally:
            shutil.rmtree(tmpdir)

    def test_dir_empty_err(self):
        try:
            try:
                assert fs.dir_empty(os.path.join('/tmp', str(random.random())))
            except FileOrDirNotFoundError:
                pass
            else:
                self.fail("FileOrDirNotFoundError exception expected")

            fd, tmpfile = mkstemp()
            try:
                fs.dir_empty(tmpfile)
            except NotDirectoryError:
                pass
            else:
                self.fail("NotDirectoryError exception expected")
        finally:
            os.close(fd)
            os.unlink(tmpfile)

    def test_rmdirs(self):
        try:
            tmpdir = mkdtemp()
            subdir = mkdtemp(dir=tmpdir)
            fd, tmpfile = mkstemp(dir=tmpdir)
            assert not fs.rmdirs(tmpfile)
            assert not fs.rmdirs(tmpdir)
            assert fs.rmdirs(subdir)
            assert not os.path.exists(subdir)
        finally:
            os.close(fd)
            shutil.rmtree(tmpdir)