summaryrefslogtreecommitdiffstats
path: root/test/unit/common/test_utils.py
blob: f88229a938e3bc132c02c30732e55f47843ec687 (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
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
# Copyright (c) 2012-2013 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.

""" Tests for common.utils """

import os
import json
import unittest
import errno
import xattr
import tempfile
import hashlib
import tarfile
import shutil
import cPickle as pickle
from collections import defaultdict
from mock import patch, Mock
from gluster.swift.common import utils, Glusterfs
from gluster.swift.common.utils import deserialize_metadata, \
    serialize_metadata, PICKLE_PROTOCOL
from gluster.swift.common.exceptions import GlusterFileSystemOSError,\
    GlusterFileSystemIOError
from swift.common.exceptions import DiskFileNoSpace

#
# Somewhat hacky way of emulating the operation of xattr calls. They are made
# against a dictionary that stores the xattr key/value pairs.
#
_xattrs = {}
_xattr_op_cnt = defaultdict(int)
_xattr_set_err = {}
_xattr_get_err = {}
_xattr_rem_err = {}
_xattr_set = None
_xattr_get = None
_xattr_remove = None


def _xkey(path, key):
    return "%s:%s" % (path, key)


def _setxattr(path, key, value, *args, **kwargs):
    _xattr_op_cnt['set'] += 1
    xkey = _xkey(path, key)
    if xkey in _xattr_set_err:
        e = IOError()
        e.errno = _xattr_set_err[xkey]
        raise e
    global _xattrs
    _xattrs[xkey] = value


def _getxattr(path, key, *args, **kwargs):
    _xattr_op_cnt['get'] += 1
    xkey = _xkey(path, key)
    if xkey in _xattr_get_err:
        e = IOError()
        e.errno = _xattr_get_err[xkey]
        raise e
    global _xattrs
    if xkey in _xattrs:
        ret_val = _xattrs[xkey]
    else:
        e = IOError("Fake IOError")
        e.errno = errno.ENODATA
        raise e
    return ret_val


def _removexattr(path, key, *args, **kwargs):
    _xattr_op_cnt['remove'] += 1
    xkey = _xkey(path, key)
    if xkey in _xattr_rem_err:
        e = IOError()
        e.errno = _xattr_rem_err[xkey]
        raise e
    global _xattrs
    if xkey in _xattrs:
        del _xattrs[xkey]
    else:
        e = IOError("Fake IOError")
        e.errno = errno.ENODATA
        raise e


def _initxattr():
    global _xattrs
    _xattrs = {}
    global _xattr_op_cnt
    _xattr_op_cnt = defaultdict(int)
    global _xattr_set_err, _xattr_get_err, _xattr_rem_err
    _xattr_set_err = {}
    _xattr_get_err = {}
    _xattr_rem_err = {}

    # Save the current methods
    global _xattr_set;    _xattr_set    = xattr.setxattr
    global _xattr_get;    _xattr_get    = xattr.getxattr
    global _xattr_remove; _xattr_remove = xattr.removexattr

    # Monkey patch the calls we use with our internal unit test versions
    xattr.setxattr    = _setxattr
    xattr.getxattr    = _getxattr
    xattr.removexattr = _removexattr


def _destroyxattr():
    # Restore the current methods just in case
    global _xattr_set;    xattr.setxattr    = _xattr_set
    global _xattr_get;    xattr.getxattr    = _xattr_get
    global _xattr_remove; xattr.removexattr = _xattr_remove
    # Destroy the stored values and
    global _xattrs; _xattrs = None


class SimMemcache(object):
    def __init__(self):
        self._d = {}

    def get(self, key):
        return self._d.get(key, None)

    def set(self, key, value):
        self._d[key] = value


def _mock_os_fsync(fd):
    return


class TestSafeUnpickler(unittest.TestCase):

    class Exploit(object):
        def __reduce__(self):
            return (os.system, ('touch /tmp/pickle-exploit',))

    def test_loads(self):
        valid_md = {'key1': 'val1', 'key2': 'val2'}
        for protocol in (0, 1, 2):
            valid_dump = pickle.dumps(valid_md, protocol)
            mal_dump = pickle.dumps(self.Exploit(), protocol)
            # malicious dump is appended to valid dump
            payload1 = valid_dump[:-1] + mal_dump
            # malicious dump is prefixed to valid dump
            payload2 = mal_dump[:-1] + valid_dump
            # entire dump is malicious
            payload3 = mal_dump
            for payload in (payload1, payload2, payload3):
                try:
                    utils.SafeUnpickler.loads(payload)
                except pickle.UnpicklingError as err:
                    self.assertTrue('Potentially unsafe pickle' in err)
                else:
                    self.fail("Expecting cPickle.UnpicklingError")


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

    def setUp(self):
        _initxattr()

    def tearDown(self):
        _destroyxattr()

    def test_write_metadata(self):
        path = "/tmp/foo/w"
        orig_d = {'bar': 'foo'}
        utils.write_metadata(path, orig_d)
        xkey = _xkey(path, utils.METADATA_KEY)
        assert len(_xattrs.keys()) == 1
        assert xkey in _xattrs
        assert orig_d == deserialize_metadata(_xattrs[xkey])
        assert _xattr_op_cnt['set'] == 1

    def test_write_metadata_err(self):
        path = "/tmp/foo/w"
        orig_d = {'bar': 'foo'}
        xkey = _xkey(path, utils.METADATA_KEY)
        _xattr_set_err[xkey] = errno.EOPNOTSUPP
        try:
            utils.write_metadata(path, orig_d)
        except IOError as e:
            assert e.errno == errno.EOPNOTSUPP
            assert len(_xattrs.keys()) == 0
            assert _xattr_op_cnt['set'] == 1
        else:
            self.fail("Expected an IOError exception on write")

    def test_write_metadata_space_err(self):

        def _mock_xattr_setattr(item, name, value):
            raise IOError(errno.ENOSPC, os.strerror(errno.ENOSPC))

        with patch('xattr.setxattr', _mock_xattr_setattr):
            path = "/tmp/foo/w"
            orig_d = {'bar': 'foo'}
            try:
                utils.write_metadata(path, orig_d)
            except DiskFileNoSpace:
                pass
            else:
                self.fail("Expected DiskFileNoSpace exception")
            fd = 0
            try:
                utils.write_metadata(fd, orig_d)
            except DiskFileNoSpace:
                pass
            else:
                self.fail("Expected DiskFileNoSpace exception")

    def test_write_metadata_multiple(self):
        # At 64 KB an xattr key/value pair, this should generate three keys.
        path = "/tmp/foo/w"
        orig_d = {'bar': 'x' * 150000}
        utils.write_metadata(path, orig_d)
        assert len(_xattrs.keys()) == 3, "Expected 3 keys, found %d" % len(_xattrs.keys())
        payload = ''
        for i in range(0, 3):
            xkey = _xkey(path, "%s%s" % (utils.METADATA_KEY, i or ''))
            assert xkey in _xattrs
            assert len(_xattrs[xkey]) <= utils.MAX_XATTR_SIZE
            payload += _xattrs[xkey]
        assert orig_d == deserialize_metadata(payload)
        assert _xattr_op_cnt['set'] == 3, "%r" % _xattr_op_cnt

    def test_clean_metadata(self):
        path = "/tmp/foo/c"
        expected_d = {'a': 'y' * 150000}
        expected_p = serialize_metadata(expected_d)
        for i in range(0, 3):
            xkey = _xkey(path, "%s%s" % (utils.METADATA_KEY, i or ''))
            _xattrs[xkey] = expected_p[:utils.MAX_XATTR_SIZE]
            expected_p = expected_p[utils.MAX_XATTR_SIZE:]
        assert not expected_p
        utils.clean_metadata(path)
        assert _xattr_op_cnt['remove'] == 4, "%r" % _xattr_op_cnt

    def test_clean_metadata_err(self):
        path = "/tmp/foo/c"
        xkey = _xkey(path, utils.METADATA_KEY)
        _xattrs[xkey] = serialize_metadata({'a': 'y'})
        _xattr_rem_err[xkey] = errno.EOPNOTSUPP
        try:
            utils.clean_metadata(path)
        except IOError as e:
            assert e.errno == errno.EOPNOTSUPP
            assert _xattr_op_cnt['remove'] == 1, "%r" % _xattr_op_cnt
        else:
            self.fail("Expected an IOError exception on remove")

    def test_read_metadata(self):
        path = "/tmp/foo/r"
        expected_d = {'a': 'y'}
        xkey = _xkey(path, utils.METADATA_KEY)
        _xattrs[xkey] = serialize_metadata(expected_d)
        res_d = utils.read_metadata(path)
        assert res_d == expected_d, "Expected %r, result %r" % (expected_d, res_d)
        assert _xattr_op_cnt['get'] == 1, "%r" % _xattr_op_cnt

    def test_read_metadata_notfound(self):
        path = "/tmp/foo/r"
        res_d = utils.read_metadata(path)
        assert res_d == {}
        assert _xattr_op_cnt['get'] == 1, "%r" % _xattr_op_cnt

    def test_read_metadata_err(self):
        path = "/tmp/foo/r"
        expected_d = {'a': 'y'}
        xkey = _xkey(path, utils.METADATA_KEY)
        _xattrs[xkey] = serialize_metadata(expected_d)
        _xattr_get_err[xkey] = errno.EOPNOTSUPP
        try:
            utils.read_metadata(path)
        except IOError as e:
            assert e.errno == errno.EOPNOTSUPP
            assert (_xattr_op_cnt['get'] == 1), "%r" % _xattr_op_cnt
        else:
            self.fail("Expected an IOError exception on get")

    def test_read_metadata_multiple(self):
        path = "/tmp/foo/r"
        expected_d = {'a': 'y' * 150000}
        expected_p = serialize_metadata(expected_d)
        for i in range(0, 3):
            xkey = _xkey(path, "%s%s" % (utils.METADATA_KEY, i or ''))
            _xattrs[xkey] = expected_p[:utils.MAX_XATTR_SIZE]
            expected_p = expected_p[utils.MAX_XATTR_SIZE:]
        assert not expected_p
        res_d = utils.read_metadata(path)
        assert res_d == expected_d, "Expected %r, result %r" % (expected_d, res_d)
        assert _xattr_op_cnt['get'] == 4, "%r" % _xattr_op_cnt

    def test_read_metadata_multiple_one_missing(self):
        path = "/tmp/foo/r"
        expected_d = {'a': 'y' * 150000}
        expected_p = serialize_metadata(expected_d)
        for i in range(0, 2):
            xkey = _xkey(path, "%s%s" % (utils.METADATA_KEY, i or ''))
            _xattrs[xkey] = expected_p[:utils.MAX_XATTR_SIZE]
            expected_p = expected_p[utils.MAX_XATTR_SIZE:]
        assert len(expected_p) <= utils.MAX_XATTR_SIZE
        res_d = utils.read_metadata(path)
        assert res_d == {}
        assert _xattr_op_cnt['get'] == 3, "%r" % _xattr_op_cnt

    def test_restore_metadata_none(self):
        # No initial metadata
        path = "/tmp/foo/i"
        res_d = utils.restore_metadata(path, {'b': 'y'}, {})
        expected_d = {'b': 'y'}
        assert res_d == expected_d, "Expected %r, result %r" % (expected_d, res_d)
        assert _xattr_op_cnt['set'] == 1, "%r" % _xattr_op_cnt

    def test_restore_metadata(self):
        # Initial metadata
        path = "/tmp/foo/i"
        initial_d = {'a': 'z'}
        xkey = _xkey(path, utils.METADATA_KEY)
        _xattrs[xkey] = serialize_metadata(initial_d)
        res_d = utils.restore_metadata(path, {'b': 'y'}, initial_d)
        expected_d = {'a': 'z', 'b': 'y'}
        assert res_d == expected_d, "Expected %r, result %r" % (expected_d, res_d)
        assert _xattr_op_cnt['set'] == 1, "%r" % _xattr_op_cnt

    def test_restore_metadata_nochange(self):
        # Initial metadata but no changes
        path = "/tmp/foo/i"
        initial_d = {'a': 'z'}
        xkey = _xkey(path, utils.METADATA_KEY)
        _xattrs[xkey] = serialize_metadata(initial_d)
        res_d = utils.restore_metadata(path, {}, initial_d)
        expected_d = {'a': 'z'}
        assert res_d == expected_d, "Expected %r, result %r" % (expected_d, res_d)
        assert _xattr_op_cnt['set'] == 0, "%r" % _xattr_op_cnt

    def test_deserialize_metadata_pickle(self):
        orig__read_pickled_metadata = Glusterfs._read_pickled_metadata
        orig_md = {'key1': 'value1', 'key2': 'value2'}
        pickled_md = pickle.dumps(orig_md, PICKLE_PROTOCOL)
        _m_pickle_loads = Mock(return_value={})
        try:
            with patch('gluster.swift.common.utils.pickle.loads',
                       _m_pickle_loads):
                # Conf option turned off
                Glusterfs._read_pickled_metadata = False
                # pickled
                utils.deserialize_metadata(pickled_md)
                self.assertFalse(_m_pickle_loads.called)
                _m_pickle_loads.reset_mock()
                # not pickled
                utils.deserialize_metadata("not_pickle")
                self.assertFalse(_m_pickle_loads.called)
                _m_pickle_loads.reset_mock()

                # Conf option turned on
                Glusterfs._read_pickled_metadata = True
                # pickled
                md = utils.deserialize_metadata(pickled_md)
                self.assertTrue(_m_pickle_loads.called)
                self.assertTrue(isinstance(md, dict))
                _m_pickle_loads.reset_mock()
                # not pickled
                utils.deserialize_metadata("not_pickle")
                self.assertFalse(_m_pickle_loads.called)
                _m_pickle_loads.reset_mock()

                # malformed pickle
                _m_pickle_loads.side_effect = pickle.UnpicklingError
                md = utils.deserialize_metadata("malformed_pickle")
                self.assertTrue(isinstance(md, dict))
        finally:
            Glusterfs._read_pickled_metadata = orig__read_pickled_metadata

    def test_deserialize_metadata_json(self):
        orig_md = {'key1': 'value1', 'key2': 'value2'}
        json_md = json.dumps(orig_md, separators=(',', ':'))
        _m_json_loads = Mock(return_value={})
        with patch('gluster.swift.common.utils.json.loads',
                   _m_json_loads):
            utils.deserialize_metadata("not_json")
            self.assertFalse(_m_json_loads.called)
            _m_json_loads.reset_mock()
            utils.deserialize_metadata("{fake_valid_json}")
            self.assertTrue(_m_json_loads.called)
            _m_json_loads.reset_mock()

    def test_add_timestamp_empty(self):
        orig = {}
        res = utils._add_timestamp(orig)
        assert res == {}

    def test_add_timestamp_none(self):
        orig = {'a': 1, 'b': 2, 'c': 3}
        exp = {'a': (1, 0), 'b': (2, 0), 'c': (3, 0)}
        res = utils._add_timestamp(orig)
        assert res == exp

    def test_add_timestamp_mixed(self):
        orig = {'a': 1, 'b': (2, 1), 'c': 3}
        exp = {'a': (1, 0), 'b': (2, 1), 'c': (3, 0)}
        res = utils._add_timestamp(orig)
        assert res == exp

    def test_add_timestamp_all(self):
        orig = {'a': (1, 0), 'b': (2, 1), 'c': (3, 0)}
        res = utils._add_timestamp(orig)
        assert res == orig

    def test_get_etag_empty(self):
        tf = tempfile.NamedTemporaryFile()
        hd = utils._get_etag(tf.name)
        assert hd == hashlib.md5().hexdigest()

    def test_get_etag(self):
        tf = tempfile.NamedTemporaryFile()
        tf.file.write('123' * utils.CHUNK_SIZE)
        tf.file.flush()
        hd = utils._get_etag(tf.name)
        tf.file.seek(0)
        md5 = hashlib.md5()
        while True:
            chunk = tf.file.read(utils.CHUNK_SIZE)
            if not chunk:
                break
            md5.update(chunk)
        assert hd == md5.hexdigest()

    def test_get_etag_dup_fd_closed(self):
        fd, path = tempfile.mkstemp()
        data = "It's not who we are underneath, but what we do that defines us"
        os.write(fd, data)
        os.lseek(fd, 0, os.SEEK_SET)

        mock_do_close = Mock()
        with patch("gluster.swift.common.utils.do_close", mock_do_close):
            etag = utils._get_etag(fd)
        self.assertEqual(etag, hashlib.md5(data).hexdigest())
        self.assertTrue(mock_do_close.called)

        # We mocked out close, so we have to close the fd for real
        os.close(mock_do_close.call_args[0][0])
        os.close(fd)

    def test_get_object_metadata_dne(self):
        md = utils.get_object_metadata("/tmp/doesNotEx1st")
        assert md == {}

    def test_get_object_metadata_err(self):
        tf = tempfile.NamedTemporaryFile()
        try:
            utils.get_object_metadata(
                os.path.join(tf.name, "doesNotEx1st"))
        except GlusterFileSystemOSError as e:
            assert e.errno != errno.ENOENT
        else:
            self.fail("Expected exception")

    obj_keys = (utils.X_TIMESTAMP, utils.X_CONTENT_TYPE, utils.X_ETAG,
                utils.X_CONTENT_LENGTH, utils.X_TYPE, utils.X_OBJECT_TYPE)

    def test_get_object_metadata_file(self):
        tf = tempfile.NamedTemporaryFile()
        tf.file.write('123')
        tf.file.flush()
        md = utils.get_object_metadata(tf.name)
        for key in self.obj_keys:
            assert key in md, "Expected key %s in %r" % (key, md)
        assert md[utils.X_TYPE] == utils.OBJECT
        assert md[utils.X_OBJECT_TYPE] == utils.FILE
        assert md[utils.X_CONTENT_TYPE] == utils.FILE_TYPE
        assert md[utils.X_CONTENT_LENGTH] == os.path.getsize(tf.name)
        assert md[utils.X_TIMESTAMP] == utils.normalize_timestamp(os.path.getctime(tf.name))
        assert md[utils.X_ETAG] == utils._get_etag(tf.name)

    def test_get_object_metadata_dir(self):
        td = tempfile.mkdtemp()
        try:
            md = utils.get_object_metadata(td)
            for key in self.obj_keys:
                assert key in md, "Expected key %s in %r" % (key, md)
            assert md[utils.X_TYPE] == utils.OBJECT
            assert md[utils.X_OBJECT_TYPE] == utils.DIR_NON_OBJECT
            assert md[utils.X_CONTENT_TYPE] == utils.DIR_TYPE
            assert md[utils.X_CONTENT_LENGTH] == 0
            assert md[utils.X_TIMESTAMP] == utils.normalize_timestamp(os.path.getctime(td))
            assert md[utils.X_ETAG] == hashlib.md5().hexdigest()
        finally:
            os.rmdir(td)

    def test_create_object_metadata_file(self):
        tf = tempfile.NamedTemporaryFile()
        tf.file.write('4567')
        tf.file.flush()
        r_md = utils.create_object_metadata(tf.name)

        xkey = _xkey(tf.name, utils.METADATA_KEY)
        assert len(_xattrs.keys()) == 1
        assert xkey in _xattrs
        assert _xattr_op_cnt['set'] == 1
        md = deserialize_metadata(_xattrs[xkey])
        assert r_md == md

        for key in self.obj_keys:
            assert key in md, "Expected key %s in %r" % (key, md)
        assert md[utils.X_TYPE] == utils.OBJECT
        assert md[utils.X_OBJECT_TYPE] == utils.FILE
        assert md[utils.X_CONTENT_TYPE] == utils.FILE_TYPE
        assert md[utils.X_CONTENT_LENGTH] == os.path.getsize(tf.name)
        assert md[utils.X_TIMESTAMP] == utils.normalize_timestamp(os.path.getctime(tf.name))
        assert md[utils.X_ETAG] == utils._get_etag(tf.name)

    def test_create_object_metadata_dir(self):
        td = tempfile.mkdtemp()
        try:
            r_md = utils.create_object_metadata(td)

            xkey = _xkey(td, utils.METADATA_KEY)
            assert len(_xattrs.keys()) == 1
            assert xkey in _xattrs
            assert _xattr_op_cnt['set'] == 1
            md = deserialize_metadata(_xattrs[xkey])
            assert r_md == md

            for key in self.obj_keys:
                assert key in md, "Expected key %s in %r" % (key, md)
            assert md[utils.X_TYPE] == utils.OBJECT
            assert md[utils.X_OBJECT_TYPE] == utils.DIR_NON_OBJECT
            assert md[utils.X_CONTENT_TYPE] == utils.DIR_TYPE
            assert md[utils.X_CONTENT_LENGTH] == 0
            assert md[utils.X_TIMESTAMP] == utils.normalize_timestamp(os.path.getctime(td))
            assert md[utils.X_ETAG] == hashlib.md5().hexdigest()
        finally:
            os.rmdir(td)

    def test_get_container_metadata(self):
        def _mock_get_container_details(path):
            o_list = ['a', 'b', 'c']
            o_count = 3
            b_used = 47
            return o_list, o_count, b_used
        orig_gcd = utils.get_container_details
        utils.get_container_details = _mock_get_container_details
        td = tempfile.mkdtemp()
        try:
            exp_md = {
                utils.X_TYPE: (utils.CONTAINER, 0),
                utils.X_TIMESTAMP: (utils.normalize_timestamp(os.path.getctime(td)), 0),
                utils.X_PUT_TIMESTAMP: (utils.normalize_timestamp(os.path.getmtime(td)), 0),
                utils.X_OBJECTS_COUNT: (3, 0),
                utils.X_BYTES_USED: (47, 0),
            }
            md = utils.get_container_metadata(td)
            assert md == exp_md
        finally:
            utils.get_container_details = orig_gcd
            os.rmdir(td)

    def test_get_account_metadata(self):
        def _mock_get_account_details(path):
            c_list = ['123', 'abc']
            c_count = 2
            return c_list, c_count
        orig_gad = utils.get_account_details
        utils.get_account_details = _mock_get_account_details
        td = tempfile.mkdtemp()
        try:
            exp_md = {
                utils.X_TYPE: (utils.ACCOUNT, 0),
                utils.X_TIMESTAMP: (utils.normalize_timestamp(os.path.getctime(td)), 0),
                utils.X_PUT_TIMESTAMP: (utils.normalize_timestamp(os.path.getmtime(td)), 0),
                utils.X_OBJECTS_COUNT: (0, 0),
                utils.X_BYTES_USED: (0, 0),
                utils.X_CONTAINER_COUNT: (2, 0),
            }
            md = utils.get_account_metadata(td)
            assert md == exp_md
        finally:
            utils.get_account_details = orig_gad
            os.rmdir(td)

    cont_keys = [utils.X_TYPE, utils.X_TIMESTAMP, utils.X_PUT_TIMESTAMP,
                 utils.X_OBJECTS_COUNT, utils.X_BYTES_USED]

    def test_create_container_metadata(self):
        td = tempfile.mkdtemp()
        try:
            r_md = utils.create_container_metadata(td)

            xkey = _xkey(td, utils.METADATA_KEY)
            assert len(_xattrs.keys()) == 1
            assert xkey in _xattrs
            assert _xattr_op_cnt['get'] == 1
            assert _xattr_op_cnt['set'] == 1
            md = deserialize_metadata(_xattrs[xkey])
            assert r_md == md

            for key in self.cont_keys:
                assert key in md, "Expected key %s in %r" % (key, md)
            assert md[utils.X_TYPE] == (utils.CONTAINER, 0)
            assert md[utils.X_TIMESTAMP] == (utils.normalize_timestamp(os.path.getctime(td)), 0)
            assert md[utils.X_PUT_TIMESTAMP] == (utils.normalize_timestamp(os.path.getmtime(td)), 0)
            assert md[utils.X_OBJECTS_COUNT] == (0, 0)
            assert md[utils.X_BYTES_USED] == (0, 0)
        finally:
            os.rmdir(td)

    acct_keys = [val for val in cont_keys]
    acct_keys.append(utils.X_CONTAINER_COUNT)

    def test_create_account_metadata(self):
        td = tempfile.mkdtemp()
        try:
            r_md = utils.create_account_metadata(td)

            xkey = _xkey(td, utils.METADATA_KEY)
            assert len(_xattrs.keys()) == 1
            assert xkey in _xattrs
            assert _xattr_op_cnt['get'] == 1
            assert _xattr_op_cnt['set'] == 1
            md = deserialize_metadata(_xattrs[xkey])
            assert r_md == md

            for key in self.acct_keys:
                assert key in md, "Expected key %s in %r" % (key, md)
            assert md[utils.X_TYPE] == (utils.ACCOUNT, 0)
            assert md[utils.X_TIMESTAMP] == (utils.normalize_timestamp(os.path.getctime(td)), 0)
            assert md[utils.X_PUT_TIMESTAMP] == (utils.normalize_timestamp(os.path.getmtime(td)), 0)
            assert md[utils.X_OBJECTS_COUNT] == (0, 0)
            assert md[utils.X_BYTES_USED] == (0, 0)
            assert md[utils.X_CONTAINER_COUNT] == (0, 0)
        finally:
            os.rmdir(td)

    def test_get_account_details(self):
        orig_cwd = os.getcwd()
        td = tempfile.mkdtemp()
        try:
            tf = tarfile.open("common/data/account_tree.tar.bz2", "r:bz2")
            os.chdir(td)
            tf.extractall()

            container_list, container_count = utils.get_account_details(td)
            assert container_count == 3
            assert set(container_list) == set(['c1', 'c2', 'c3'])
        finally:
            os.chdir(orig_cwd)
            shutil.rmtree(td)

    def test_get_account_details_notadir(self):
        tf = tempfile.NamedTemporaryFile()
        container_list, container_count = utils.get_account_details(tf.name)
        assert container_count == 0
        assert container_list == []

    def test_get_container_details_notadir(self):
        tf = tempfile.NamedTemporaryFile()
        obj_list, object_count, bytes_used = \
            utils.get_container_details(tf.name)
        assert bytes_used == 0
        assert object_count == 0
        assert obj_list == []

    def test_get_container_details(self):
        orig_cwd = os.getcwd()
        __do_getsize = Glusterfs._do_getsize
        td = tempfile.mkdtemp()
        try:
            tf = tarfile.open("common/data/container_tree.tar.bz2", "r:bz2")
            os.chdir(td)
            tf.extractall()

            Glusterfs._do_getsize = False

            obj_list, object_count, bytes_used = \
                utils.get_container_details(td)
            assert bytes_used == 0, repr(bytes_used)
            # Should not include the directories
            assert object_count == 5, repr(object_count)
            assert set(obj_list) == set(['file1', 'file3', 'file2',
                                         'dir1/file1', 'dir1/file2'
                                         ]), repr(obj_list)
        finally:
            Glusterfs._do_getsize = __do_getsize
            os.chdir(orig_cwd)
            shutil.rmtree(td)

    def test_get_container_details_from_fs_do_getsize_true(self):
        orig_cwd = os.getcwd()
        __do_getsize = Glusterfs._do_getsize
        td = tempfile.mkdtemp()
        try:
            tf = tarfile.open("common/data/container_tree.tar.bz2", "r:bz2")
            os.chdir(td)
            tf.extractall()

            Glusterfs._do_getsize = True

            obj_list, object_count, bytes_used = \
                utils.get_container_details(td)
            assert bytes_used == 30, repr(bytes_used)
            assert object_count == 5, repr(object_count)
            assert set(obj_list) == set(['file1', 'file3', 'file2',
                                         'dir1/file1', 'dir1/file2'
                                         ]), repr(obj_list)
        finally:
            Glusterfs._do_getsize = __do_getsize
            os.chdir(orig_cwd)
            shutil.rmtree(td)

    def test_validate_container_empty(self):
        ret = utils.validate_container({})
        assert not ret

    def test_validate_container_missing_keys(self):
        ret = utils.validate_container({'foo': 'bar'})
        assert not ret

    def test_validate_container_bad_type(self):
        md = {utils.X_TYPE: ('bad', 0),
              utils.X_TIMESTAMP: ('na', 0),
              utils.X_PUT_TIMESTAMP: ('na', 0),
              utils.X_OBJECTS_COUNT: ('na', 0),
              utils.X_BYTES_USED: ('na', 0)}
        ret = utils.validate_container(md)
        assert not ret

    def test_validate_container_good_type(self):
        md = {utils.X_TYPE: (utils.CONTAINER, 0),
              utils.X_TIMESTAMP: ('na', 0),
              utils.X_PUT_TIMESTAMP: ('na', 0),
              utils.X_OBJECTS_COUNT: ('na', 0),
              utils.X_BYTES_USED: ('na', 0)}
        ret = utils.validate_container(md)
        assert ret

    def test_validate_account_empty(self):
        ret = utils.validate_account({})
        assert not ret

    def test_validate_account_missing_keys(self):
        ret = utils.validate_account({'foo': 'bar'})
        assert not ret

    def test_validate_account_bad_type(self):
        md = {utils.X_TYPE: ('bad', 0),
              utils.X_TIMESTAMP: ('na', 0),
              utils.X_PUT_TIMESTAMP: ('na', 0),
              utils.X_OBJECTS_COUNT: ('na', 0),
              utils.X_BYTES_USED: ('na', 0),
              utils.X_CONTAINER_COUNT: ('na', 0)}
        ret = utils.validate_account(md)
        assert not ret

    def test_validate_account_good_type(self):
        md = {utils.X_TYPE: (utils.ACCOUNT, 0),
              utils.X_TIMESTAMP: ('na', 0),
              utils.X_PUT_TIMESTAMP: ('na', 0),
              utils.X_OBJECTS_COUNT: ('na', 0),
              utils.X_BYTES_USED: ('na', 0),
              utils.X_CONTAINER_COUNT: ('na', 0)}
        ret = utils.validate_account(md)
        assert ret

    def test_validate_object_empty(self):
        ret = utils.validate_object({})
        assert not ret

    def test_validate_object_missing_keys(self):
        ret = utils.validate_object({'foo': 'bar'})
        assert not ret

    def test_validate_object_bad_type(self):
        md = {utils.X_TIMESTAMP: 'na',
              utils.X_CONTENT_TYPE: 'na',
              utils.X_ETAG: 'bad',
              utils.X_CONTENT_LENGTH: 'na',
              utils.X_TYPE: 'bad',
              utils.X_OBJECT_TYPE: 'na'}
        ret = utils.validate_object(md)
        assert not ret

    def test_validate_object_good_type(self):
        md = {utils.X_TIMESTAMP: 'na',
              utils.X_CONTENT_TYPE: 'na',
              utils.X_ETAG: 'bad',
              utils.X_CONTENT_LENGTH: 'na',
              utils.X_TYPE: utils.OBJECT,
              utils.X_OBJECT_TYPE: 'na'}
        ret = utils.validate_object(md)
        assert ret

    def test_validate_object_with_stat(self):
        md = {utils.X_TIMESTAMP: 'na',
              utils.X_CONTENT_TYPE: 'na',
              utils.X_ETAG: 'bad',
              utils.X_CONTENT_LENGTH: '12345',
              utils.X_TYPE: utils.OBJECT,
              utils.X_OBJECT_TYPE: 'na'}
        fake_stat = Mock(st_size=12346)
        self.assertFalse(utils.validate_object(md, fake_stat))
        fake_stat = Mock(st_size=12345)
        self.assertTrue(utils.validate_object(md, fake_stat))


class TestUtilsDirObjects(unittest.TestCase):

    def setUp(self):
        _initxattr()
        self.dirs = [
            'dir1',
            'dir1/dir2',
            'dir1/dir2/dir3']
        self.files = [
            'file1',
            'file2',
            'dir1/dir2/file3']
        self.tempdir = tempfile.mkdtemp()
        self.rootdir = os.path.join(self.tempdir, 'a')
        for d in self.dirs:
            os.makedirs(os.path.join(self.rootdir, d))
        for f in self.files:
            open(os.path.join(self.rootdir, f), 'w').close()

    def tearDown(self):
        _destroyxattr()
        shutil.rmtree(self.tempdir)

    def _set_dir_object(self, obj):
        metadata = utils.read_metadata(os.path.join(self.rootdir, obj))
        metadata[utils.X_OBJECT_TYPE] = utils.DIR_OBJECT
        utils.write_metadata(os.path.join(self.rootdir, self.dirs[0]),
                             metadata)

    def _clear_dir_object(self, obj):
        metadata = utils.read_metadata(os.path.join(self.rootdir, obj))
        metadata[utils.X_OBJECT_TYPE] = utils.DIR_NON_OBJECT
        utils.write_metadata(os.path.join(self.rootdir, obj),
                             metadata)

    def test_rmobjdir_removing_files(self):
        self.assertFalse(utils.rmobjdir(self.rootdir))

        # Remove the files
        for f in self.files:
            os.unlink(os.path.join(self.rootdir, f))

        self.assertTrue(utils.rmobjdir(self.rootdir))

    def test_rmobjdir_removing_dirs(self):
        self.assertFalse(utils.rmobjdir(self.rootdir))

        # Remove the files
        for f in self.files:
            os.unlink(os.path.join(self.rootdir, f))

        self._set_dir_object(self.dirs[0])
        self.assertFalse(utils.rmobjdir(self.rootdir))
        self._clear_dir_object(self.dirs[0])
        self.assertTrue(utils.rmobjdir(self.rootdir))

    def test_rmobjdir_metadata_errors(self):

        def _mock_rm(path):
            print "_mock_rm-metadata_errors(%s)" % path
            if path.endswith("dir3"):
                raise OSError(13, "foo")
            return {}

        _orig_rm = utils.read_metadata
        utils.read_metadata = _mock_rm
        try:
            try:
                utils.rmobjdir(self.rootdir)
            except OSError:
                pass
            else:
                self.fail("Expected OSError")
        finally:
            utils.read_metadata = _orig_rm

    def test_rmobjdir_metadata_enoent(self):

        def _mock_rm(path):
            print "_mock_rm-metadata_enoent(%s)" % path
            shutil.rmtree(path)
            raise GlusterFileSystemIOError(errno.ENOENT,
                                           os.strerror(errno.ENOENT))

        # Remove the files
        for f in self.files:
            os.unlink(os.path.join(self.rootdir, f))

        _orig_rm = utils.read_metadata
        utils.read_metadata = _mock_rm
        try:
            try:
                self.assertTrue(utils.rmobjdir(self.rootdir))
            except IOError:
                self.fail("Unexpected IOError")
            else:
                pass
        finally:
            utils.read_metadata = _orig_rm

    def test_rmobjdir_rmdir_enoent(self):

        seen = [0]
        _orig_rm = utils.do_rmdir

        def _mock_rm(path):
            print "_mock_rm-rmdir_enoent(%s)" % path
            if path == self.rootdir and not seen[0]:
                seen[0] = 1
                raise OSError(errno.ENOTEMPTY, os.strerror(errno.ENOTEMPTY))
            else:
                shutil.rmtree(path)
                raise OSError(errno.ENOENT, os.strerror(errno.ENOENT))

        # Remove the files
        for f in self.files:
            os.unlink(os.path.join(self.rootdir, f))

        utils.do_rmdir = _mock_rm
        try:
            try:
                self.assertTrue(utils.rmobjdir(self.rootdir))
            except OSError:
                self.fail("Unexpected OSError")
            else:
                pass
        finally:
            utils.do_rmdir = _orig_rm

    def test_rmobjdir_rmdir_error(self):

        seen = [0]
        _orig_rm = utils.do_rmdir

        def _mock_rm(path):
            print "_mock_rm-rmdir_enoent(%s)" % path
            if path == self.rootdir and not seen[0]:
                seen[0] = 1
                raise OSError(errno.ENOTEMPTY, os.strerror(errno.ENOTEMPTY))
            else:
                raise OSError(errno.EACCES, os.strerror(errno.EACCES))

        # Remove the files
        for f in self.files:
            os.unlink(os.path.join(self.rootdir, f))

        utils.do_rmdir = _mock_rm
        try:
            try:
                utils.rmobjdir(self.rootdir)
            except OSError:
                pass
            else:
                self.fail("Expected OSError")
        finally:
            utils.do_rmdir = _orig_rm

    def test_rmobjdir_files_left_in_top_dir(self):

        seen = [0]
        _orig_rm = utils.do_rmdir

        def _mock_rm(path):
            print "_mock_rm-files_left_in_top_dir(%s)" % path
            if path == self.rootdir:
                if not seen[0]:
                    seen[0] = 1
                    raise OSError(errno.ENOTEMPTY, os.strerror(errno.ENOTEMPTY))
                else:
                    return _orig_rm(path)
            else:
                shutil.rmtree(path)
                raise OSError(errno.ENOENT, os.strerror(errno.ENOENT))

        # Remove the files, leaving the ones at the root
        for f in self.files:
            if f.startswith('dir'):
                os.unlink(os.path.join(self.rootdir, f))

        utils.do_rmdir = _mock_rm
        try:
            try:
                self.assertFalse(utils.rmobjdir(self.rootdir))
            except OSError:
                self.fail("Unexpected OSError")
            else:
                pass
        finally:
            utils.do_rmdir = _orig_rm

    def test_rmobjdir_error_final_rmdir(self):

        seen = [0]
        _orig_rm = utils.do_rmdir

        def _mock_rm(path):
            print "_mock_rm-files_left_in_top_dir(%s)" % path
            if path == self.rootdir:
                if not seen[0]:
                    seen[0] = 1
                    raise OSError(errno.ENOTEMPTY, os.strerror(errno.ENOTEMPTY))
                else:
                    raise OSError(errno.EACCES, os.strerror(errno.EACCES))
            else:
                shutil.rmtree(path)
                raise OSError(errno.ENOENT, os.strerror(errno.ENOENT))

        # Remove the files, leaving the ones at the root
        for f in self.files:
            os.unlink(os.path.join(self.rootdir, f))

        utils.do_rmdir = _mock_rm
        try:
            try:
                utils.rmobjdir(self.rootdir)
            except OSError:
                pass
            else:
                self.fail("Expected OSError")
        finally:
            utils.do_rmdir = _orig_rm