summaryrefslogtreecommitdiffstats
path: root/test/unit/gluster/test_gfapi.py
blob: 8fcf9380556f85599a7a509cd31e139c44485484 (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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
# Copyright (c) 2012-2014 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 unittest
import glusterfs
import os
import stat
import errno

from glusterfs import gfapi
from nose import SkipTest
from mock import Mock, patch
from contextlib import nested


def _mock_glfs_close(fd):
    return 0


def _mock_glfs_closedir(fd):
    return


def _mock_glfs_new(volid):
    return 2


def _mock_glfs_set_volfile_server(fs, proto, host, port):
    return


def _mock_glfs_fini(fs):
    return


class TestFile(unittest.TestCase):

    @classmethod
    def setUpClass(cls):
        cls.fd = gfapi.File(2)

    @classmethod
    def tearDownClass(cls):
        cls.fd = None

    def setUp(self):
        self._saved_glfs_close = glusterfs.gfapi.api.glfs_close
        glusterfs.gfapi.api.glfs_close = _mock_glfs_close

    def tearDown(self):
        glusterfs.gfapi.api.glfs_close = self._saved_glfs_close

    def test_fchown_success(self):
        mock_glfs_fchown = Mock()
        mock_glfs_fchown.return_value = 0

        with patch("glusterfs.gfapi.api.glfs_fchown", mock_glfs_fchown):
            ret = self.fd.fchown(9, 11)
            self.assertEquals(ret, 0)

    def test_fchown_fail_exception(self):
        mock_glfs_fchown = Mock()
        mock_glfs_fchown.return_value = -1

        with patch("glusterfs.gfapi.api.glfs_fchown", mock_glfs_fchown):
            self.assertRaises(OSError, self.fd.fchown, 9, 11)

    def test_fdatasync_success(self):
        mock_glfs_fdatasync = Mock()
        mock_glfs_fdatasync.return_value = 4

        with patch("glusterfs.gfapi.api.glfs_fdatasync", mock_glfs_fdatasync):
            ret = self.fd.fdatasync()
            self.assertEquals(ret, 4)

    def test_fdatasync_fail_exception(self):
        mock_glfs_fdatasync = Mock()
        mock_glfs_fdatasync.return_value = -1

        with patch("glusterfs.gfapi.api.glfs_fdatasync", mock_glfs_fdatasync):
            self.assertRaises(OSError, self.fd.fdatasync)

    def test_fstat_success(self):
        mock_glfs_fstat = Mock()
        mock_glfs_fstat.return_value = 0

        with patch("glusterfs.gfapi.api.glfs_fstat", mock_glfs_fstat):
            s = self.fd.fstat()
            self.assertTrue(isinstance(s, gfapi.Stat))

    def test_fstat_fail_exception(self):
        mock_glfs_fstat = Mock()
        mock_glfs_fstat.return_value = -1

        with patch("glusterfs.gfapi.api.glfs_fstat", mock_glfs_fstat):
            self.assertRaises(OSError, self.fd.fstat)

    def test_fsync_success(self):
        mock_glfs_fsync = Mock()
        mock_glfs_fsync.return_value = 4

        with patch("glusterfs.gfapi.api.glfs_fsync", mock_glfs_fsync):
            ret = self.fd.fsync()
            self.assertEquals(ret, 4)

    def test_fsync_fail_exception(self):
        mock_glfs_fsync = Mock()
        mock_glfs_fsync.return_value = -1

        with patch("glusterfs.gfapi.api.glfs_fsync", mock_glfs_fsync):
            self.assertRaises(OSError, self.fd.fsync)

    def test_read_success(self):
        def _mock_glfs_read(fd, rbuf, buflen, flags):
            rbuf.value = "hello"
            return 5

        with patch("glusterfs.gfapi.api.glfs_read", _mock_glfs_read):
            b = self.fd.read(5)
            self.assertEqual(b.value, "hello")

    def test_read_fail_exception(self):
        mock_glfs_read = Mock()
        mock_glfs_read.return_value = -1

        with patch("glusterfs.gfapi.api.glfs_read", mock_glfs_read):
            self.assertRaises(OSError, self.fd.read, 5)

    def test_read_fail_empty_buffer(self):
        mock_glfs_read = Mock()
        mock_glfs_read.return_value = 0

        with patch("glusterfs.gfapi.api.glfs_read", mock_glfs_read):
            b = self.fd.read(5)
            self.assertEqual(b, 0)

    def test_write_success(self):
        mock_glfs_write = Mock()
        mock_glfs_write.return_value = 5

        with patch("glusterfs.gfapi.api.glfs_write", mock_glfs_write):
            ret = self.fd.write("hello")
            self.assertEqual(ret, 5)

    def test_write_binary_success(self):
        mock_glfs_write = Mock()
        mock_glfs_write.return_value = 3

        with patch("glusterfs.gfapi.api.glfs_write", mock_glfs_write):
            b = bytearray(3)
            ret = self.fd.write(b)
            self.assertEqual(ret, 3)

    def test_write_fail_exception(self):
        mock_glfs_write = Mock()
        mock_glfs_write.return_value = -1

        with patch("glusterfs.gfapi.api.glfs_write", mock_glfs_write):
            self.assertRaises(OSError, self.fd.write, "hello")

    def test_fallocate_success(self):
        raise SkipTest("need to solve issue with dependency on libgfapi.so")
        mock_glfs_fallocate = Mock()
        mock_glfs_fallocate.return_value = 0

        with patch("glusterfs.gfapi.api.glfs_fallocate", mock_glfs_fallocate):
            ret = self.fd.fallocate(0, 0, 1024)
            self.assertEqual(ret, 0)

    def test_fallocate_fail_exception(self):
        raise SkipTest("need to solve issue with dependency on libgfapi.so")
        mock_glfs_fallocate = Mock()
        mock_glfs_fallocate.return_value = -1

        with patch("glusterfs.gfapi.api.glfs_fallocate", mock_glfs_fallocate):
            self.assertRaises(OSError, self.fd.fallocate, 0, 0, 1024)

    def test_discard_success(self):
        raise SkipTest("need to solve issue with dependency on libgfapi.so")
        mock_glfs_discard = Mock()
        mock_glfs_discard.return_value = 0

        with patch("glusterfs.gfapi.api.glfs_discard", mock_glfs_discard):
            ret = self.fd.discard(1024, 1024)
            self.assertEqual(ret, 0)

    def test_discard_fail_exception(self):
        raise SkipTest("need to solve issue with dependency on libgfapi.so")
        mock_glfs_discard = Mock()
        mock_glfs_discard.return_value = -1

        with patch("glusterfs.gfapi.api.glfs_discard", mock_glfs_discard):
            self.assertRaises(OSError, self.fd.discard, 1024, 1024)


class TestDir(unittest.TestCase):

    def setUp(self):
        self._saved_glfs_closedir = glusterfs.gfapi.api.glfs_closedir
        glusterfs.gfapi.api.glfs_closedir = _mock_glfs_closedir

    def tearDown(self):
        glusterfs.gfapi.api.glfs_closedir = self._saved_glfs_closedir

    def test_next_success(self):
        raise SkipTest("need to solve issue with dependency on libgfapi.so")

        def mock_glfs_readdir_r(fd, ent, cursor):
            cursor.contents = "bla"
            return 0

        with patch("glusterfs.gfapi.api.glfs_readdir_r", mock_glfs_readdir_r):
            fd = gfapi.Dir(2)
            ent = fd.next()
            self.assertTrue(isinstance(ent, gfapi.Dirent))


class TestVolume(unittest.TestCase):

    @classmethod
    def setUpClass(cls):
        cls._saved_glfs_new = glusterfs.gfapi.api.glfs_new
        glusterfs.gfapi.api.glfs_new = _mock_glfs_new

        cls._saved_glfs_set_volfile_server = \
            glusterfs.gfapi.api.glfs_set_volfile_server
        glusterfs.gfapi.api.glfs_set_volfile_server = \
            _mock_glfs_set_volfile_server

        cls._saved_glfs_fini = glusterfs.gfapi.api.glfs_fini
        glusterfs.gfapi.api.glfs_fini = _mock_glfs_fini

        cls._saved_glfs_close = glusterfs.gfapi.api.glfs_close
        glusterfs.gfapi.api.glfs_close = _mock_glfs_close

        cls._saved_glfs_closedir = glusterfs.gfapi.api.glfs_closedir
        glusterfs.gfapi.api.glfs_closedir = _mock_glfs_closedir
        cls.vol = gfapi.Volume("mockhost", "test")

    @classmethod
    def tearDownClass(cls):
        cls.vol = None
        glusterfs.gfapi.api.glfs_new = cls._saved_glfs_new
        glusterfs.gfapi.api.glfs_set_volfile_server = \
            cls._saved_glfs_set_volfile_server
        glusterfs.gfapi.api.glfs_fini = cls._saved_glfs_fini
        glusterfs.gfapi.api.glfs_close = cls._saved_glfs_close
        glusterfs.gfapi.api.glfs_closedir = cls._saved_glfs_closedir

    def test_chown_success(self):
        mock_glfs_chown = Mock()
        mock_glfs_chown.return_value = 0

        with patch("glusterfs.gfapi.api.glfs_chown", mock_glfs_chown):
            ret = self.vol.chown("file.txt", 9, 11)
            self.assertEquals(ret, 0)

    def test_chown_fail_exception(self):
        mock_glfs_chown = Mock()
        mock_glfs_chown.return_value = -1

        with patch("glusterfs.gfapi.api.glfs_chown", mock_glfs_chown):
            self.assertRaises(OSError, self.vol.chown, "file.txt", 9, 11)

    def test_creat_success(self):
        mock_glfs_creat = Mock()
        mock_glfs_creat.return_value = 2

        with patch("glusterfs.gfapi.api.glfs_creat", mock_glfs_creat):
            with self.vol.creat("file.txt", os.O_WRONLY, 0644) as fd:
                self.assertTrue(isinstance(fd, gfapi.File))
                self.assertEqual(mock_glfs_creat.call_count, 1)
                mock_glfs_creat.assert_called_once_with(2,
                                                        "file.txt",
                                                        os.O_WRONLY, 0644)

    def test_creat_fail_exception(self):
        mock_glfs_creat = Mock()
        mock_glfs_creat.return_value = None

        def assert_creat():
            with self.vol.creat("file.txt", os.O_WRONLY, 0644) as fd:
                self.assertEqual(fd, None)

        with patch("glusterfs.gfapi.api.glfs_creat", mock_glfs_creat):
            self.assertRaises(OSError, assert_creat)

    def test_exists_true(self):
        mock_glfs_stat = Mock()
        mock_glfs_stat.return_value = 0

        with patch("glusterfs.gfapi.api.glfs_stat", mock_glfs_stat):
            ret = self.vol.exists("file.txt")
            self.assertTrue(ret)

    def test_not_exists_false(self):
        mock_glfs_stat = Mock()
        mock_glfs_stat.return_value = -1

        with patch("glusterfs.gfapi.api.glfs_stat", mock_glfs_stat):
            ret = self.vol.exists("file.txt")
            self.assertFalse(ret)

    def test_isdir_true(self):
        mock_glfs_stat = Mock()
        s = gfapi.Stat()
        s.st_mode = stat.S_IFDIR
        mock_glfs_stat.return_value = s

        with patch("glusterfs.gfapi.Volume.stat", mock_glfs_stat):
            ret = self.vol.isdir("dir")
            self.assertTrue(ret)

    def test_isdir_false(self):
        mock_glfs_stat = Mock()
        s = gfapi.Stat()
        s.st_mode = stat.S_IFREG
        mock_glfs_stat.return_value = s

        with patch("glusterfs.gfapi.Volume.stat", mock_glfs_stat):
            ret = self.vol.isdir("file")
            self.assertFalse(ret)

    def test_isdir_false_nodir(self):
        mock_glfs_stat = Mock()
        mock_glfs_stat.return_value = -1

        with patch("glusterfs.gfapi.api.glfs_stat", mock_glfs_stat):
            ret = self.vol.isdir("dirdoesnotexist")
            self.assertFalse(ret)

    def test_isfile_true(self):
        mock_glfs_stat = Mock()
        s = gfapi.Stat()
        s.st_mode = stat.S_IFREG
        mock_glfs_stat.return_value = s

        with patch("glusterfs.gfapi.Volume.stat", mock_glfs_stat):
            ret = self.vol.isfile("file")
            self.assertTrue(ret)

    def test_isfile_false(self):
        mock_glfs_stat = Mock()
        s = gfapi.Stat()
        s.st_mode = stat.S_IFDIR
        mock_glfs_stat.return_value = s

        with patch("glusterfs.gfapi.Volume.stat", mock_glfs_stat):
            ret = self.vol.isfile("dir")
            self.assertFalse(ret)

    def test_isfile_false_nofile(self):
        mock_glfs_stat = Mock()
        mock_glfs_stat.return_value = -1

        with patch("glusterfs.gfapi.api.glfs_stat", mock_glfs_stat):
            ret = self.vol.isfile("filedoesnotexist")
            self.assertFalse(ret)

    def test_islink_true(self):
        mock_glfs_lstat = Mock()
        s = gfapi.Stat()
        s.st_mode = stat.S_IFLNK
        mock_glfs_lstat.return_value = s

        with patch("glusterfs.gfapi.Volume.lstat", mock_glfs_lstat):
            ret = self.vol.islink("solnk")
            self.assertTrue(ret)

    def test_islink_false(self):
        mock_glfs_lstat = Mock()
        s = gfapi.Stat()
        s.st_mode = stat.S_IFREG
        mock_glfs_lstat.return_value = s

        with patch("glusterfs.gfapi.Volume.lstat", mock_glfs_lstat):
            ret = self.vol.islink("file")
            self.assertFalse(ret)

    def test_islink_false_nolink(self):
        mock_glfs_lstat = Mock()
        mock_glfs_lstat.return_value = -1

        with patch("glusterfs.gfapi.api.glfs_lstat", mock_glfs_lstat):
            ret = self.vol.islink("linkdoesnotexist")
            self.assertFalse(ret)

    def test_getxattr_success(self):
        def mock_glfs_getxattr(fs, path, key, buf, maxlen):
            buf.value = "fake_xattr"
            return 10

        with patch("glusterfs.gfapi.api.glfs_getxattr", mock_glfs_getxattr):
            buf = self.vol.getxattr("file.txt", "key1", 32)
            self.assertEquals("fake_xattr", buf)

    def test_getxattr_fail_exception(self):
        mock_glfs_getxattr = Mock()
        mock_glfs_getxattr.return_value = -1

        with patch("glusterfs.gfapi.api.glfs_getxattr", mock_glfs_getxattr):
            self.assertRaises(IOError, self.vol.getxattr, "file.txt",
                              "key1", 32)

    def test_listdir_success(self):
        mock_glfs_opendir = Mock()
        mock_glfs_opendir.return_value = 2

        dirent1 = gfapi.Dirent()
        dirent1.d_name = "mockfile"
        dirent1.d_reclen = 8
        dirent2 = gfapi.Dirent()
        dirent2.d_name = "mockdir"
        dirent2.d_reclen = 7
        dirent3 = gfapi.Dirent()
        dirent3.d_name = "."
        dirent3.d_reclen = 1
        mock_Dir_next = Mock()
        mock_Dir_next.side_effect = [dirent1, dirent2, dirent3, None]

        with nested(patch("glusterfs.gfapi.api.glfs_opendir", mock_glfs_opendir),
                    patch("glusterfs.gfapi.Dir.next", mock_Dir_next)):
            d = self.vol.listdir("testdir")
            self.assertEqual(len(d), 2)
            self.assertEqual(d[0], 'mockfile')

    def test_listdir_fail_exception(self):
        mock_glfs_opendir = Mock()
        mock_glfs_opendir.return_value = None

        with patch("glusterfs.gfapi.api.glfs_opendir", mock_glfs_opendir):
            self.assertRaises(OSError, self.vol.listdir, "test.txt")

    def test_listxattr_success(self):
        def mock_glfs_listxattr(fs, path, buf, buflen):
            buf.raw = "key1\0key2\0"
            return 10

        with patch("glusterfs.gfapi.api.glfs_listxattr", mock_glfs_listxattr):
            xattrs = self.vol.listxattr("file.txt")
            self.assertTrue("key1" in xattrs)
            self.assertTrue("key2" in xattrs)

    def test_listxattr_fail_exception(self):
        mock_glfs_listxattr = Mock()
        mock_glfs_listxattr.return_value = -1

        with patch("glusterfs.gfapi.api.glfs_listxattr", mock_glfs_listxattr):
            self.assertRaises(IOError, self.vol.listxattr, "file.txt")

    def test_lstat_success(self):
        mock_glfs_lstat = Mock()
        mock_glfs_lstat.return_value = 0

        with patch("glusterfs.gfapi.api.glfs_lstat", mock_glfs_lstat):
            s = self.vol.lstat("file.txt")
            self.assertTrue(isinstance(s, gfapi.Stat))

    def test_lstat_fail_exception(self):
        mock_glfs_lstat = Mock()
        mock_glfs_lstat.return_value = -1

        with patch("glusterfs.gfapi.api.glfs_lstat", mock_glfs_lstat):
            self.assertRaises(OSError, self.vol.lstat, "file.txt")

    def test_stat_success(self):
        mock_glfs_stat = Mock()
        mock_glfs_stat.return_value = 0

        with patch("glusterfs.gfapi.api.glfs_stat", mock_glfs_stat):
            s = self.vol.stat("file.txt")
            self.assertTrue(isinstance(s, gfapi.Stat))

    def test_stat_fail_exception(self):
        mock_glfs_stat = Mock()
        mock_glfs_stat.return_value = -1

        with patch("glusterfs.gfapi.api.glfs_stat", mock_glfs_stat):
            self.assertRaises(OSError, self.vol.stat, "file.txt")

    def test_makedirs_success(self):
        mock_glfs_mkdir = Mock()
        mock_glfs_mkdir.side_effect = [0, 0]

        mock_exists = Mock()
        mock_exists.side_effect = (False, True, False)

        with nested(patch("glusterfs.gfapi.api.glfs_mkdir", mock_glfs_mkdir),
                    patch("glusterfs.gfapi.Volume.exists", mock_exists)):
            self.vol.makedirs("dir1/", 0775)
            self.assertEqual(mock_glfs_mkdir.call_count, 1)
            mock_glfs_mkdir.assert_any_call(self.vol.fs, "dir1/", 0775)

    def test_makedirs_success_EEXIST(self):
        err = errno.EEXIST
        mock_glfs_mkdir = Mock()
        mock_glfs_mkdir.side_effect = [OSError(err, os.strerror(err)), 0]

        mock_exists = Mock()
        mock_exists.side_effect = [False, True, False]

        with nested(patch("glusterfs.gfapi.api.glfs_mkdir", mock_glfs_mkdir),
                    patch("glusterfs.gfapi.Volume.exists", mock_exists)):
            self.vol.makedirs("./dir1/dir2", 0775)
            self.assertEqual(mock_glfs_mkdir.call_count, 2)
            mock_glfs_mkdir.assert_any_call(self.vol.fs, "./dir1", 0775)
            mock_glfs_mkdir.assert_called_with(self.vol.fs, "./dir1/dir2",
                                               0775)

    def test_makedirs_fail_exception(self):
        mock_glfs_mkdir = Mock()
        mock_glfs_mkdir.return_value = -1

        mock_exists = Mock()
        mock_exists.return_value = False

        with nested(patch("glusterfs.gfapi.api.glfs_mkdir", mock_glfs_mkdir),
                    patch("glusterfs.gfapi.Volume.exists", mock_exists)):
            self.assertRaises(OSError, self.vol.makedirs, "dir1/dir2", 0775)

    def test_mkdir_success(self):
        mock_glfs_mkdir = Mock()
        mock_glfs_mkdir.return_value = 0

        with patch("glusterfs.gfapi.api.glfs_mkdir", mock_glfs_mkdir):
            ret = self.vol.mkdir("testdir", 0775)
            self.assertEquals(ret, 0)

    def test_mkdir_fail_exception(self):
        mock_glfs_mkdir = Mock()
        mock_glfs_mkdir.return_value = -1

        with patch("glusterfs.gfapi.api.glfs_mkdir", mock_glfs_mkdir):
            self.assertRaises(OSError, self.vol.mkdir, "testdir", 0775)

    def test_open_success(self):
        mock_glfs_open = Mock()
        mock_glfs_open.return_value = 2

        with patch("glusterfs.gfapi.api.glfs_open", mock_glfs_open):
            with self.vol.open("file.txt", os.O_WRONLY) as fd:
                self.assertTrue(isinstance(fd, gfapi.File))
                self.assertEqual(mock_glfs_open.call_count, 1)
                mock_glfs_open.assert_called_once_with(2,
                                                       "file.txt", os.O_WRONLY)

    def test_open_fail_exception(self):
        mock_glfs_open = Mock()
        mock_glfs_open.return_value = None

        def assert_open():
            with self.vol.open("file.txt", os.O_WRONLY) as fd:
                self.assertEqual(fd, None)

        with patch("glusterfs.gfapi.api.glfs_open", mock_glfs_open):
            self.assertRaises(OSError, assert_open)

    def test_opendir_success(self):
        mock_glfs_opendir = Mock()
        mock_glfs_opendir.return_value = 2

        with patch("glusterfs.gfapi.api.glfs_opendir", mock_glfs_opendir):
            d = self.vol.opendir("testdir")
            self.assertTrue(isinstance(d, gfapi.Dir))

    def test_opendir_fail_exception(self):
        mock_glfs_opendir = Mock()
        mock_glfs_opendir.return_value = None

        with patch("glusterfs.gfapi.api.glfs_opendir", mock_glfs_opendir):
            self.assertRaises(OSError, self.vol.opendir, "testdir")

    def test_rename_success(self):
        mock_glfs_rename = Mock()
        mock_glfs_rename.return_value = 0

        with patch("glusterfs.gfapi.api.glfs_rename", mock_glfs_rename):
            ret = self.vol.rename("file.txt", "newfile.txt")
            self.assertEquals(ret, 0)

    def test_rename_fail_exception(self):
        mock_glfs_rename = Mock()
        mock_glfs_rename.return_value = -1

        with patch("glusterfs.gfapi.api.glfs_rename", mock_glfs_rename):
            self.assertRaises(OSError, self.vol.rename,
                              "file.txt", "newfile.txt")

    def test_rmdir_success(self):
        mock_glfs_rmdir = Mock()
        mock_glfs_rmdir.return_value = 0

        with patch("glusterfs.gfapi.api.glfs_rmdir", mock_glfs_rmdir):
            ret = self.vol.rmdir("testdir")
            self.assertEquals(ret, 0)

    def test_rmdir_fail_exception(self):
        mock_glfs_rmdir = Mock()
        mock_glfs_rmdir.return_value = -1

        with patch("glusterfs.gfapi.api.glfs_rmdir", mock_glfs_rmdir):
            self.assertRaises(OSError, self.vol.rmdir, "testdir")

    def test_unlink_success(self):
        mock_glfs_unlink = Mock()
        mock_glfs_unlink.return_value = 0

        with patch("glusterfs.gfapi.api.glfs_unlink", mock_glfs_unlink):
            ret = self.vol.unlink("file.txt")
            self.assertEquals(ret, 0)

    def test_unlink_fail_exception(self):
        mock_glfs_unlink = Mock()
        mock_glfs_unlink.return_value = -1

        with patch("glusterfs.gfapi.api.glfs_unlink", mock_glfs_unlink):
            self.assertRaises(OSError, self.vol.unlink, "file.txt")

    def test_removexattr_success(self):
        mock_glfs_removexattr = Mock()
        mock_glfs_removexattr.return_value = 0

        with patch("glusterfs.gfapi.api.glfs_removexattr",
                   mock_glfs_removexattr):
            ret = self.vol.removexattr("file.txt", "key1")
            self.assertEquals(ret, 0)

    def test_removexattr_fail_exception(self):
        mock_glfs_removexattr = Mock()
        mock_glfs_removexattr.return_value = -1

        with patch("glusterfs.gfapi.api.glfs_removexattr",
                   mock_glfs_removexattr):
            self.assertRaises(IOError, self.vol.removexattr, "file.txt",
                              "key1")

    def test_rmtree_success(self):
        dir1_list = ["dir2", "file"]
        empty_list = []
        mock_listdir = Mock()
        mock_listdir.side_effect = [dir1_list, empty_list]

        mock_isdir = Mock()
        mock_isdir.side_effect = [True, False]

        mock_unlink = Mock()
        mock_unlink.return_value = 0

        mock_rmdir = Mock()
        mock_rmdir.return_value = 0

        mock_islink = Mock()
        mock_islink.return_value = False

        with nested(patch("glusterfs.gfapi.Volume.listdir", mock_listdir),
                    patch("glusterfs.gfapi.Volume.isdir", mock_isdir),
                    patch("glusterfs.gfapi.Volume.islink", mock_islink),
                    patch("glusterfs.gfapi.Volume.unlink", mock_unlink),
                    patch("glusterfs.gfapi.Volume.rmdir", mock_rmdir)):
            self.vol.rmtree("dir1")
            mock_rmdir.assert_any_call("dir1/dir2")
            mock_unlink.assert_called_once_with("dir1/file")
            mock_rmdir.assert_called_with("dir1")

    def test_rmtree_listdir_exception(self):
        mock_listdir = Mock()
        mock_listdir.side_effect = [OSError]

        mock_islink = Mock()
        mock_islink.return_value = False

        with nested(patch("glusterfs.gfapi.Volume.listdir", mock_listdir),
                    patch("glusterfs.gfapi.Volume.islink", mock_islink)):
            self.assertRaises(OSError, self.vol.rmtree, "dir1")

    def test_rmtree_islink_exception(self):
        mock_islink = Mock()
        mock_islink.return_value = True

        with patch("glusterfs.gfapi.Volume.islink", mock_islink):
            self.assertRaises(OSError, self.vol.rmtree, "dir1")

    def test_rmtree_ignore_unlink_rmdir_exception(self):
        dir1_list = ["dir2", "file"]
        empty_list = []
        mock_listdir = Mock()
        mock_listdir.side_effect = [dir1_list, empty_list]

        mock_isdir = Mock()
        mock_isdir.side_effect = [True, False]

        mock_unlink = Mock()
        mock_unlink.side_effect = [OSError]

        mock_rmdir = Mock()
        mock_rmdir.side_effect = [0, OSError]

        mock_islink = Mock()
        mock_islink.return_value = False

        with nested(patch("glusterfs.gfapi.Volume.listdir", mock_listdir),
                    patch("glusterfs.gfapi.Volume.isdir", mock_isdir),
                    patch("glusterfs.gfapi.Volume.islink", mock_islink),
                    patch("glusterfs.gfapi.Volume.unlink", mock_unlink),
                    patch("glusterfs.gfapi.Volume.rmdir", mock_rmdir)):
            self.vol.rmtree("dir1", True)
            mock_rmdir.assert_any_call("dir1/dir2")
            mock_unlink.assert_called_once_with("dir1/file")
            mock_rmdir.assert_called_with("dir1")

    def test_setxattr_success(self):
        mock_glfs_setxattr = Mock()
        mock_glfs_setxattr.return_value = 0

        with patch("glusterfs.gfapi.api.glfs_setxattr", mock_glfs_setxattr):
            ret = self.vol.setxattr("file.txt", "key1", "hello", 5)
            self.assertEquals(ret, 0)

    def test_setxattr_fail_exception(self):
        mock_glfs_setxattr = Mock()
        mock_glfs_setxattr.return_value = -1

        with patch("glusterfs.gfapi.api.glfs_setxattr", mock_glfs_setxattr):
            self.assertRaises(IOError, self.vol.setxattr, "file.txt",
                              "key1", "hello", 5)

    def test_symlink_success(self):
        mock_glfs_symlink = Mock()
        mock_glfs_symlink.return_value = 0

        with patch("glusterfs.gfapi.api.glfs_symlink", mock_glfs_symlink):
            ret = self.vol.symlink("file.txt", "filelink")
            self.assertEquals(ret, 0)

    def test_symlink_fail_exception(self):
        mock_glfs_symlink = Mock()
        mock_glfs_symlink.return_value = -1

        with patch("glusterfs.gfapi.api.glfs_symlink", mock_glfs_symlink):
            self.assertRaises(OSError, self.vol.symlink, "file.txt",
                              "filelink")

    def test_walk_success(self):
        dir1_list = ["dir2", "file"]
        empty_list = []
        mock_listdir = Mock()
        mock_listdir.side_effect = [dir1_list, empty_list]

        mock_isdir = Mock()
        mock_isdir.side_effect = [True, False]

        with nested(patch("glusterfs.gfapi.Volume.listdir", mock_listdir),
                    patch("glusterfs.gfapi.Volume.isdir", mock_isdir)):
            for (path, dirs, files) in self.vol.walk("dir1"):
                self.assertEqual(dirs, ['dir2'])
                self.assertEqual(files, ['file'])
                break

    def test_walk_listdir_exception(self):
        mock_listdir = Mock()
        mock_listdir.side_effect = [OSError]

        def mock_onerror(err):
            self.assertTrue(isinstance(err, OSError))

        with patch("glusterfs.gfapi.Volume.listdir", mock_listdir):
            for (path, dirs, files) in self.vol.walk("dir1",
                                                     onerror=mock_onerror):
                pass