summaryrefslogtreecommitdiff
path: root/test/unit/obj/test_vfile.py
blob: 66c1125a5cf1932fef998c9ba2a41729f3f1c5ca (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
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
# Copyright (c) 2010-2012 OpenStack Foundation
#
# 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 swift.obj.vfile"""
import errno
import fcntl
import shutil
import unittest
from random import randint

import six

from swift.common.storage_policy import StoragePolicy
from swift.obj.header import ObjectHeader, STATE_OBJ_FILE, \
    MAX_OBJECT_HEADER_LEN
from swift.obj.meta_pb2 import Metadata
from swift.obj.vfile import VFileWriter
from swift.obj.vfile_utils import VOSError, next_aligned_offset
from swift.obj.rpc_http import RpcError, StatusCode
from test.unit import patch_policies, FakeRing
import os.path
from swift.common import utils
from shutil import rmtree
from swift.obj import vfile, header, fmgr_pb2
import tempfile
import mock


class TestVFileModuleMethods(unittest.TestCase):

    def setUp(self):
        self.testdir = tempfile.mkdtemp()
        self.object_dir = os.path.join(self.testdir, 'objects')
        utils.mkdirs(self.object_dir)
        self.ring = FakeRing(part_power=18)

    def tearDown(self):
        rmtree(self.testdir, ignore_errors=1)

    @patch_policies([StoragePolicy(0, 'zero', False,
                                   object_ring=FakeRing(part_power=18)),
                     StoragePolicy(1, 'one', True,
                                   object_ring=FakeRing(part_power=20))])
    def test_vfile_listdir_partitions(self):

        tests = [
            {"path": "/srv/node/sda/objects",
             "expected": ["/srv/node/sda/losf/rpc.socket", 18]},
            {"path": "/sdb/objects",
             "expected": ["/sdb/losf/rpc.socket", 18]},
            {"path": "/sdc/objects-1",
             "expected": ["/sdc/losf-1/rpc.socket", 20]},
        ]
        with mock.patch(
                "swift.obj.vfile.rpc.list_partitions") as m_list_partitions:
            for test in tests:
                vfile.listdir(test["path"])
                m_list_partitions.assert_called_once_with(*test["expected"])
                m_list_partitions.reset_mock()

    @patch_policies([StoragePolicy(0, 'zero', False,
                                   object_ring=FakeRing(part_power=18)),
                     StoragePolicy(1, 'one', True,
                                   object_ring=FakeRing(part_power=20))])
    def test_vfile_listdir_partition(self):

        tests = [
            {"path": "/srv/node/sda/objects/123",
             "expected": ["/srv/node/sda/losf/rpc.socket", 123, 18]},
            {"path": "/sdb/objects/124",
             "expected": ["/sdb/losf/rpc.socket", 124, 18]},
            {"path": "/sdc/objects-1/789",
             "expected": ["/sdc/losf-1/rpc.socket", 789, 20]},
        ]
        with mock.patch(
                "swift.obj.vfile.rpc.list_partition") as m_list_partition:
            for test in tests:
                vfile.listdir(test["path"])
                m_list_partition.assert_called_once_with(*test["expected"])
                m_list_partition.reset_mock()

    @patch_policies([StoragePolicy(0, 'zero', False,
                                   object_ring=FakeRing(part_power=18)),
                     StoragePolicy(1, 'one', True,
                                   object_ring=FakeRing(part_power=20))])
    def test_vfile_listdir_suffix(self):

        tests = [
            {"path": "/srv/node/sda/objects/123/abc",
             "expected": ["/srv/node/sda/losf/rpc.socket", 123, "abc", 18]},
            {"path": "/sdb/objects/124/bcd",
             "expected": ["/sdb/losf/rpc.socket", 124, "bcd", 18]},
            {"path": "/sdc/objects-1/789/def",
             "expected": ["/sdc/losf-1/rpc.socket", 789, "def", 20]},
        ]
        with mock.patch(
                "swift.obj.vfile.rpc.list_suffix") as m_list_suffix:
            for test in tests:
                vfile.listdir(test["path"])
                m_list_suffix.assert_called_once_with(*test["expected"])
                m_list_suffix.reset_mock()

    def test_vfile_listdir_with_invalid_path(self):
        self.assertRaises(VOSError, vfile.listdir, "/test/no-obj/")


@patch_policies([StoragePolicy(0, 'zero', False),
                 StoragePolicy(1, 'one', True)])
class TestVFileWriter(unittest.TestCase):
    @mock.patch("swift.obj.vfile.open_or_create_volume")
    @mock.patch("swift.obj.rpc_http.get_next_offset")
    @mock.patch("swift.obj.vfile._may_grow_volume")
    @mock.patch("swift.obj.vfile.os.lseek")
    @mock.patch("swift.obj.vfile.VFileWriter.__new__")
    def test_create(self, m_cls, m_lseek, m_grow_vol, m_get_next_offset,
                    m_open_or_create_vol):
        vfile_conf = {
            'volume_alloc_chunk_size': 16 * 1024,
            'volume_low_free_space': 8 * 1024,
            'metadata_reserve': 500,
            'max_volume_count': 1000,
            'max_volume_size': 10 * 1024 * 1024 * 1024,
        }

        test_sets = [
            # small file with policy 0
            {
                "datadir": "/sda/objects/123/4d8/"
                           "acbd18db4cc2f85cedef654fccc4a4d8",
                "obj_size": 421,

                "volume_file": 101,  # file descriptor
                "lock_file": 102,   # file descriptor
                "volume_path": "/sda/losf/volumes/v0000001",
                "volume_next_offset": 8192,

                "expected_socket": "/sda/losf/rpc.socket",
                "expected_ohash": "acbd18db4cc2f85cedef654fccc4a4d8",
                "expected_partition": "123",
                "expected_extension": None,
                "expected_vol_dir": "/sda/losf/volumes",
                "expected_vol_index": 1,
            },
            # large file with policy 1
            {
                "datadir": "/sdb/objects-1/456/289/"
                           "48e2e79fec9bc01d9a00e0a8fa68b289",
                "obj_size": 2 * 1024 * 1024 * 1024,

                "volume_file": 201,
                "lock_file": 202,
                "volume_path": "/sdb/losf-1/volumes/v0012345",
                "volume_next_offset": 8 * 1024 * 1024 * 1024,

                "expected_socket": "/sdb/losf-1/rpc.socket",
                "expected_ohash": "48e2e79fec9bc01d9a00e0a8fa68b289",
                "expected_partition": "456",
                "expected_extension": None,
                "expected_vol_dir": "/sdb/losf-1/volumes",
                "expected_vol_index": 12345,
            },
            # file of unknown size, policy 1
            {
                "datadir": "/sdb/objects-1/789/45d/"
                           "b2f5ff47436671b6e533d8dc3614845d",
                "obj_size": None,

                "volume_file": 301,
                "lock_file": 302,
                "volume_path": "/sdb/losf/volumes/v9999999",
                "volume_next_offset": 8 * 1024 * 1024 * 1024,

                "expected_socket": "/sdb/losf-1/rpc.socket",
                "expected_ohash": "b2f5ff47436671b6e533d8dc3614845d",
                "expected_partition": "789",
                "expected_extension": None,
                "expected_vol_dir": "/sdb/losf-1/volumes",
                "expected_vol_index": 9999999,
            },
            # empty file is ok (zero length)
            {
                "datadir": "/sdb/objects-1/789/45d/"
                           "b2f5ff47436671b6e533d8dc3614845d",
                "obj_size": 0,

                "volume_file": 301,
                "lock_file": 302,
                "volume_path": "/sdb/losf/volumes/v9999999",
                "volume_next_offset": 8 * 1024 * 1024 * 1024,

                "expected_socket": "/sdb/losf-1/rpc.socket",
                "expected_ohash": "b2f5ff47436671b6e533d8dc3614845d",
                "expected_partition": "789",
                "expected_extension": None,
                "expected_vol_dir": "/sdb/losf-1/volumes",
                "expected_vol_index": 9999999,
            },
        ]

        for t in test_sets:
            # When creating a new "vfile", expect the writer to seek to the
            # offset where we can start writing the file content.
            # The layout of a single vfile within a volume is :
            # |header | metadata(*) | file content | (more metadata, optional)|
            #
            # The first reserved metadata field size is defined in the
            # configuration by the "metadata_reserve" parameter.
            #
            # So the absolute location is:
            # next_offset for the volume + len(header) + len(metadata_reserve)
            expected_absolute_offset = (t["volume_next_offset"] +
                                        len(ObjectHeader()) +
                                        vfile_conf["metadata_reserve"])
            expected_relative_offset = (len(ObjectHeader()) +
                                        vfile_conf["metadata_reserve"])

            m_open_or_create_vol.return_value = (t["volume_file"],
                                                 t["lock_file"],
                                                 t["volume_path"])

            m_get_next_offset.return_value = t["volume_next_offset"]

            VFileWriter.create(t["datadir"], t["obj_size"], vfile_conf, None)
            ordered_args = m_open_or_create_vol.call_args[0]
            named_args = m_open_or_create_vol.call_args[1]
            self.assertEqual(ordered_args[0], t["expected_socket"])
            self.assertEqual(ordered_args[1], t["expected_partition"])
            self.assertEqual(ordered_args[2], t["expected_extension"])
            self.assertEqual(ordered_args[3], t["expected_vol_dir"])
            self.assertEqual(named_args["size"], t["obj_size"])

            m_get_next_offset.assert_called_once_with(t["expected_socket"],
                                                      t["expected_vol_index"])

            ordered_args = m_grow_vol.call_args[0]
            self.assertEqual(ordered_args[0], t["volume_file"])
            self.assertEqual(ordered_args[1], t["volume_next_offset"])
            self.assertEqual(ordered_args[2], t["obj_size"])

            m_lseek.assert_called_with(t["volume_file"],
                                       expected_absolute_offset, os.SEEK_SET)

            args = m_cls.call_args[0]
            self.assertEqual(args[1], t["datadir"])
            self.assertEqual(args[2], t["volume_file"])
            self.assertEqual(args[3], t["lock_file"])
            self.assertEqual(args[4], t["expected_vol_dir"])
            self.assertEqual(args[5], t["expected_vol_index"])
            header = args[6]
            self.assertEqual(header.ohash, t["expected_ohash"])
            self.assertEqual(header.data_offset, expected_relative_offset)
            # we have not written anything yet so data_size should be zero
            self.assertEqual(header.data_size, 0)
            # state should be STATE_OBJ_FILE (not quarantined)
            self.assertEqual(header.state, STATE_OBJ_FILE)

            for test_m in [m_lseek, m_grow_vol, m_get_next_offset,
                           m_open_or_create_vol]:
                test_m.reset_mock()

            with self.assertRaises(VOSError):
                VFileWriter.create("/foo", 123, vfile_conf, None)

            with self.assertRaises(VOSError):
                VFileWriter.create("/mnt/objects/"
                                   "b2f5ff47436671b6e533d8dc3614845d", 123,
                                   vfile_conf, None)

            with self.assertRaises(VOSError):
                VFileWriter.create("/mnt/objects/123/"
                                   "b2f5ff47436671b6e533d8dc3614845d", 123,
                                   vfile_conf, None)

            # negative size
            with self.assertRaises(VOSError):
                VFileWriter.create("/mnt/objects/123/abc/"
                                   "b2f5ff47436671b6e533d8dc3614845d", -1,
                                   vfile_conf, None)

    @mock.patch("swift.obj.vfile.open_or_create_volume")
    @mock.patch("swift.obj.rpc_http.get_next_offset")
    @mock.patch("swift.obj.vfile.os.close")
    def test_create_rpc_error(self, m_os_close, m_get_next_offset,
                              m_open_or_create_vol):
        vfile_conf = {
            'volume_alloc_chunk_size': 16 * 1024,
            'volume_low_free_space': 8 * 1024,
            'metadata_reserve': 500,
            'max_volume_count': 1000,
            'max_volume_size': 10 * 1024 * 1024 * 1024,
        }

        datadir = "/sda/objects/123/4d8/acbd18db4cc2f85cedef654fccc4a4d8"
        obj_size = 0
        volume_file = 101
        lock_file = 102
        volume_path = "/sda/losf/volumes/v0000001"

        m_open_or_create_vol.return_value = (volume_file, lock_file,
                                             volume_path)
        m_get_next_offset.side_effect = RpcError(StatusCode.Unavailable,
                                                 "Unavailable")

        try:
            VFileWriter.create(datadir, obj_size, vfile_conf, None)
        except RpcError:
            close_args = m_os_close.call_args_list
            expected_args = [mock.call(volume_file), mock.call(lock_file)]
            self.assertEqual(close_args, expected_args)

    def _get_vfile_writer(self, offset=0, metadata_reserve=500,
                          ohash="d41d8cd98f00b204e9800998ecf8427e"):
        """
        returns a fake VFileWriter, backed by a TemporaryFile
        :param offset: absolute offset in volume where the file starts
        :param metadata_reserve: space to reserve for metadata between header
        and file content. (if insufficient, the remaining serialized metadata
        will be written after the file content)
        :param ohash: object hash
        :return: a VFileWriter instance
        """
        volume_file = tempfile.TemporaryFile()
        lock_file = 102  # dummy fd
        volume_dir = "/sda/losf/volumes"
        volume_index = randint(1, 9999999)
        datadir = "/sda/objects/123/{}/{}".format(ohash[-3:], ohash)
        header = ObjectHeader(version=vfile.OBJECT_HEADER_VERSION)
        header.ohash = ohash
        header.policy_idx = 0
        header.data_offset = len(header) + metadata_reserve
        header.data_size = 0
        header.state = STATE_OBJ_FILE
        logger = None

        volume_fd = volume_file.fileno()
        # seek to where file data would start as expected by caller
        os.lseek(volume_fd, offset + header.data_offset, os.SEEK_SET)

        return (vfile.VFileWriter(datadir, volume_fd, lock_file, volume_dir,
                                  volume_index, header, offset, logger),
                volume_file)

    @mock.patch("swift.obj.vfile.fdatasync")
    @mock.patch("swift.obj.vfile.rpc.register_object")
    def test_commit(self, m_register, m_fdatasync):
        default_metadata = {"Content-Length": "92",
                            "name": "/AUTH_test/foo/truc",
                            "Content-Type": "application/octet-stream",
                            "ETag": "89408008f2585c957c031716600d5a80",
                            "X-Timestamp": "1560866451.10093",
                            "X-Object-Meta-Mtime": "1523272228.000000"}
        test_sets = [
            # empty file, volume offset 0
            {
                "offset": 0,
                "metadata_reserve": 500,
                "ohash": "d41d8cd98f00b204e9800998ecf8427e",
                "filename": "1560866451.10093.data",
                "metadata": default_metadata,
                "data_bytes": 0,  # file content length
            },
            # non-zero random file size with random offset
            {
                "offset": 4096 * randint(0, 2621440),  # up to 10GB
                "metadata_reserve": 500,
                "ohash": "d41d8cd98f00b204e9800998ecf8427e",
                "filename": "1560866451.10093.data",
                "metadata": default_metadata,
                "data_bytes": randint(1, 1024 * 1024)  # up to 1MB
            },
            {
                "offset": 4096 * randint(0, 2621440),  # up to 10GB
                "metadata_reserve": 10,
                "ohash": "d41d8cd98f00b204e9800998ecf8427e",
                "filename": "1560866451.10093.data",
                "metadata": default_metadata,
                "data_bytes": randint(1, 1024 * 1024)  # up to 1MB
            },
        ]

        for t in test_sets:
            vfile_writer, vol_file = self._get_vfile_writer(
                offset=t["offset"],
                metadata_reserve=t["metadata_reserve"],
            )
            if t["data_bytes"]:
                os.write(vfile_writer.fd, b"x" * t["data_bytes"])
            vfile_writer.commit(t["filename"], t["metadata"])

            # check header
            vol_file.seek(t["offset"])
            serialized_header = vol_file.read(MAX_OBJECT_HEADER_LEN)
            header = ObjectHeader.unpack(serialized_header)
            self.assertEqual(header.version, vfile.OBJECT_HEADER_VERSION)
            self.assertEqual(header.ohash, "d41d8cd98f00b204e9800998ecf8427e")
            self.assertEqual(header.filename, t["filename"])
            self.assertEqual(header.data_size, t["data_bytes"])
            self.assertEqual(header.state, STATE_OBJ_FILE)

            # check swift metadata
            vol_file.seek(t["offset"] + header.metadata_offset)
            # if metadata couldn't fit in the reserved space, we should find
            # the rest after the file content.
            if header.metadata_size <= t["metadata_reserve"]:
                serialized_metadata = vol_file.read(header.metadata_size)
            else:
                reserved_bytes = header.data_offset - header.metadata_offset
                serialized_metadata = vol_file.read(reserved_bytes)
                vol_file.seek(
                    t["offset"] + header.data_offset + header.data_size)
                serialized_metadata += vol_file.read(header.metadata_size -
                                                     t["metadata_reserve"])
            ondisk_meta = Metadata()
            ondisk_meta.ParseFromString(serialized_metadata)
            # Metadata() is a protobuf message instance, with a single field, a
            # repeated Attr. an Attr has a key and a value
            self.assertEqual(len(ondisk_meta.attrs), len(t["metadata"]))
            for attr in ondisk_meta.attrs:
                if six.PY2:
                    self.assertEqual(attr.value, t["metadata"][attr.key])
                else:
                    self.assertEqual(
                        attr.value.decode("utf8", "surrogateescape"),
                        t["metadata"][attr.key.decode("utf8",
                                                      "surrogateescape")])

            # check data has been flushed to disk
            m_fdatasync.assert_called_once_with(vol_file.fileno())

            # check call to register the file to the index server
            m_register.assert_called_once()
            socket_path, full_name, vol_idx, offset, end_offset = \
                m_register.call_args_list[0][0]

            expected_full_name = "{}{}".format(t["ohash"], t["filename"])
            self.assertEqual(socket_path, vfile_writer.socket_path)
            self.assertEqual(full_name, expected_full_name)
            self.assertEqual(vol_idx, vfile_writer.volume_index)
            # end offset should be the next 4k aligned offset
            volume_length = os.lseek(vol_file.fileno(), 0, os.SEEK_END)
            expected_end_offset = next_aligned_offset(volume_length, 4096)
            self.assertEqual(end_offset, expected_end_offset)

            m_fdatasync.reset_mock()
            m_register.reset_mock()

    def test_commit_bad_file(self):
        vfile_writer, _ = self._get_vfile_writer()
        vfile_writer.fd = -1
        self.assertRaises(vfile.VIOError, vfile_writer.commit, "foo", {})

    def test_commit_no_name(self):
        vfile_writer, _ = self._get_vfile_writer()
        self.assertRaises(vfile.VIOError, vfile_writer.commit, "", {})

    @mock.patch("swift.obj.rpc_http.register_object")
    def test_commit_register_fail(self, m_register_object):
        """
        Check that the header object is erased if commit() fails to register
        the object on the index server.
        """
        m_register_object.side_effect = RpcError("failed to register object",
                                                 StatusCode.Unavailable)
        offset = 4096
        metadata_reserve = 500
        vfile_writer, vol_file = self._get_vfile_writer(
            offset=offset, metadata_reserve=metadata_reserve)
        content = b"dummy data"
        os.write(vfile_writer.fd, content)

        filename = "dummy-filename"
        metadata = {"dummy": "metadata"}

        self.assertRaises(RpcError, vfile_writer.commit, filename, metadata)

        # check the header was erased
        vol_file.seek(offset)
        serialized_header = vol_file.read(MAX_OBJECT_HEADER_LEN)
        self.assertEqual(serialized_header, b"\x00" * MAX_OBJECT_HEADER_LEN)

        # check we did not write past the header by checking the file data
        data_offset = (offset +
                       len(ObjectHeader(
                           version=header.OBJECT_HEADER_VERSION)) +
                       metadata_reserve)
        vol_file.seek(data_offset)
        data = vol_file.read(len(content))
        self.assertEqual(data, content)

    @mock.patch("swift.obj.vfile.open", new_callable=mock.mock_open)
    @mock.patch("swift.obj.vfile.fcntl.flock")
    @mock.patch("swift.obj.vfile.get_next_volume_index")
    @mock.patch("swift.obj.vfile.os.open")
    def test__create_new_lock_file(self, m_os_open, m_get_next_v_idx,
                                   m_flock, m_open):
        volume_dir = "/sda/losf/volumes"
        logger = None
        m_get_next_v_idx.return_value = 1

        expected_vol_creation_lock = os.path.join(volume_dir,
                                                  "volume_creation.lock")

        m_creation_lock = mock.mock_open().return_value
        m_open.side_effect = [m_creation_lock]

        m_volume_lock = mock.Mock()
        m_os_open.return_value = m_volume_lock

        index, next_lock_path, lock_file = vfile._create_new_lock_file(
            volume_dir, logger)
        m_open.assert_called_once_with(expected_vol_creation_lock, "w")
        m_os_open.assert_called_once_with(
            "/sda/losf/volumes/v0000001.writelock",
            os.O_CREAT | os.O_EXCL | os.O_WRONLY,
            0o600)

        # Check locking order
        calls = m_flock.call_args_list
        self.assertEqual(calls[0], mock.call(m_creation_lock, fcntl.LOCK_EX))
        self.assertEqual(calls[1], mock.call(m_volume_lock,
                                             fcntl.LOCK_EX | fcntl.LOCK_NB))

        self.assertEqual(index, 1)
        self.assertEqual(next_lock_path,
                         "/sda/losf/volumes/v0000001.writelock")
        self.assertEqual(lock_file, m_volume_lock)

    def test_get_next_volume_index(self):
        tests = [
            {
                "dir_entries": [],
                "expected_next_index": 1,
            },
            {
                "dir_entries": ["invalid-file"],
                "expected_next_index": 1,
            },
            {
                "dir_entries": [
                    "v0000001",
                    "v0000001.writelock",
                    "v0000002",
                    "v0000002.writelock",
                ],
                "expected_next_index": 3,
            },
            # the volume is gone. shouldn't reuse the index anyway
            {
                "dir_entries": [
                    "v0000001",
                    "v0000001.writelock",
                    "v0000002.writelock",
                ],
                "expected_next_index": 3,
            },
            {
                "dir_entries": [
                    "v0000002",
                    "v0000002.writelock",
                    "v0000003",
                    "v0000003.writelock",
                    "v0000005",
                    "v0000005.writelock",
                ],
                "expected_next_index": 1,
            },
            {
                "dir_entries": ["invalid.txt"],
                "expected_next_index": 1,
            },
            {
                "dir_entries": [
                    "v0000001",
                    "v0000001.writelock",
                    "invalid-name",
                    "v0000003",
                    "v0000003.writelock",
                ],
                "expected_next_index": 2,
            },
            # the lock file is gone. shouldn't reuse the index anyway
            {
                "dir_entries": [
                    "v0000001",
                ],
                "expected_next_index": 2,
            },
        ]

        for t in tests:
            tempdir = tempfile.mkdtemp()
            try:
                for filename in t["dir_entries"]:
                    filepath = os.path.join(tempdir, filename)
                    open(filepath, "w").close()
                result = vfile.get_next_volume_index(tempdir)
                self.assertEqual(result, t["expected_next_index"])
            finally:
                shutil.rmtree(tempdir)

    @mock.patch("swift.obj.vfile.open_writable_volume")
    @mock.patch("swift.obj.vfile.create_writable_volume")
    @mock.patch("swift.obj.vfile.os.makedirs")
    def test_open_or_create_volume_available(self, m_makedirs, m_create_vol,
                                             m_open_vol):
        socket_path = "/sda/losf/rpc.socket"
        partition = "123"
        extension = ".data"
        volume_dir = "/sda/losf/volumes"
        conf = {"dummy": "conf"}
        logger = None

        # open_writable_volume finds an available volume
        m_open_vol.return_value = 321, 322, "/path/to/vol"

        vol_file, lock_file, vol_path = vfile.open_or_create_volume(
            socket_path, partition, extension, volume_dir, conf, logger)

        m_open_vol.assert_called_once_with(socket_path, partition, extension,
                                           volume_dir, conf, logger)
        m_create_vol.assert_not_called()

        self.assertEqual((vol_file, lock_file, vol_path),
                         (321, 322, "/path/to/vol"))

    @mock.patch("swift.obj.vfile.open_writable_volume")
    @mock.patch("swift.obj.vfile.create_writable_volume")
    @mock.patch("swift.obj.vfile.os.makedirs")
    def test_open_or_create_volume_create(self, m_makedirs, m_create_vol,
                                          m_open_vol):
        socket_path = "/sda/losf/rpc.socket"
        partition = "123"
        extension = None
        volume_dir = "/sda/losf/volumes"
        conf = {}
        logger = None

        # open_writable_volume does not return a volume, but
        # create_writabl_volume does.
        m_open_vol.return_value = None, None, None
        m_create_vol.return_value = 543, 544, "/path/to/vol"

        vol_file, lock_file, vol_path = vfile.open_or_create_volume(
            socket_path, partition, extension, volume_dir, conf, logger)

        m_open_vol.assert_called_once_with(socket_path, partition, extension,
                                           volume_dir, conf, logger)
        self.assertEqual((vol_file, lock_file, vol_path),
                         (543, 544, "/path/to/vol"))

    @mock.patch("swift.obj.vfile.open_writable_volume")
    @mock.patch("swift.obj.vfile.create_writable_volume")
    @mock.patch("swift.obj.vfile.os.makedirs")
    def test_open_or_create_volume_fail(self, m_makedirs, m_create_vol,
                                        m_open_vol):
        socket_path = "/sda/losf/rpc.socket"
        partition = "123"
        extension = None
        volume_dir = "/sda/losf/volumes"
        conf = {}
        logger = None

        # Cannot find an existing volume, then creating a new one fails
        # because we have exceeded the volume count.
        m_open_vol.return_value = None, None, None
        m_create_vol.side_effect = OSError(errno.EDQUOT, "max vol count")

        try:
            vfile.open_or_create_volume(socket_path, partition, extension,
                                        volume_dir, conf, logger)
        except vfile.VOSError as exc:
            self.assertEqual(str(exc), "[Errno 28] Failed to open or create"
                                       " a volume for writing: max vol count")
        m_open_vol.assert_called_once()
        m_create_vol.assert_called_once()

        # Similar failure but with an exception that has no strerror
        m_open_vol.reset_mock()
        m_create_vol.reset_mock()
        m_create_vol.side_effect = Exception("Dummy exception")
        try:
            vfile.open_or_create_volume(socket_path, partition, extension,
                                        volume_dir, conf, logger)
        except vfile.VOSError as exc:
            self.assertEqual(str(exc), "[Errno 28] Failed to open or create"
                                       " a volume for writing: Unknown error")
        m_open_vol.assert_called_once()
        m_create_vol.assert_called_once()

    @mock.patch("swift.obj.vfile.rpc.list_volumes")
    def test_create_writable_volume_max_count_exceeded(self, m_list_volumes):
        socket_path = "/sda/losf/rpc.socket"
        partition = "123"
        extension = None
        volume_dir = "/sda/losf/volumes"
        conf = {"max_volume_count": 1}
        logger = None

        m_list_volumes.return_value = ["v1"]
        try:
            vfile.create_writable_volume(socket_path, partition, extension,
                                         volume_dir, conf, logger)
        except vfile.VOSError as exc:
            self.assertEqual(str(exc),
                             "[Errno 122] Maximum count of volumes reached for"
                             " partition: 123 type: 0")

    @mock.patch("swift.obj.vfile.rpc.list_volumes")
    @mock.patch("swift.obj.vfile.os.makedirs")
    def test_create_writable_volume_makedirs_exceptions(self, m_os_makedirs,
                                                        m_list_volumes):
        socket_path = "/sda/losf/rpc.socket"
        partition = "123"
        extension = None
        volume_dir = "/sda/losf/volumes"
        conf = {"max_volume_count": 1}
        logger = None

        m_list_volumes.return_value = ["v1"]
        m_os_makedirs.side_effect = OSError(errno.ENOSPC, "No space")

        self.assertRaises(OSError, vfile.create_writable_volume, socket_path,
                          partition, extension, volume_dir, conf, logger)

        m_os_makedirs.side_effect = vfile.VFileException("test error")
        self.assertRaises(VOSError, vfile.create_writable_volume, socket_path,
                          partition, extension, volume_dir, conf, logger)

    @mock.patch("swift.obj.vfile.get_next_volume_index")
    @mock.patch("swift.obj.vfile.rpc.list_volumes")
    @mock.patch("swift.obj.vfile.os.makedirs")
    @mock.patch("swift.obj.vfile.fcntl.flock")
    @mock.patch("swift.obj.vfile._allocate_volume_space")
    @mock.patch("swift.obj.vfile.fsync")
    @mock.patch("swift.obj.vfile.fsync_dir")
    @mock.patch("swift.obj.vfile.rpc.register_volume")
    def test_create_writable_volume(self, m_register_volume,
                                    m_fsync_dir, m_fsync,
                                    m_allocate_volume_space,
                                    m_flock, m_os_makedirs,
                                    m_list_volumes,
                                    m_get_next_volume_index):
        socket_path = "/sda/losf/rpc.socket"
        partition = "123"
        extension = None
        conf = {"max_volume_count": 1, "volume_alloc_chunk_size": 50}
        logger = None

        m_list_volumes.return_value = []
        next_vol_idx = 1
        m_get_next_volume_index.return_value = next_vol_idx

        tempdir = tempfile.mkdtemp()
        volume_dir = tempdir

        try:
            vfile.create_writable_volume(socket_path, partition, extension,
                                         volume_dir, conf, logger)

            # are the expected files here?
            expected = ["v0000001", "v0000001.writelock",
                        "volume_creation.lock"]
            files = os.listdir(tempdir)
            files.sort()
            self.assertEqual(files, expected)

            # have the locks been taken?
            # TODO: how to properly assert both locks? (one for the global
            # volume creation lock, another for the volume itself)
            self.assertEqual(m_flock.call_count, 2)

            # check the volume header is correct
            with open(os.path.join(tempdir, "v0000001"), 'rb') as vol:
                vol_header = header.read_volume_header(vol)
                self.assertEqual(vol_header.volume_idx, next_vol_idx)
                self.assertEqual(vol_header.partition, int(partition))
                self.assertEqual(vol_header.first_obj_offset, 4096)
                self.assertEqual(vol_header.type,
                                 fmgr_pb2.VOLUME_DEFAULT)  # extension was None
                self.assertEqual(vol_header.state, fmgr_pb2.STATE_RW)

            # check volume registration to the index server
            m_register_volume.assert_called_once_with(socket_path,
                                                      str(partition),
                                                      fmgr_pb2.VOLUME_DEFAULT,
                                                      next_vol_idx, 4096,
                                                      fmgr_pb2.STATE_RW)
        finally:
            shutil.rmtree(tempdir)
            m_flock.reset_mock()
            m_register_volume.reset_mock()

        partition = "456"
        extension = ".ts"
        conf = {"max_volume_count": 101, "volume_alloc_chunk_size": 50}
        logger = None
        m_list_volumes.return_value = ["x"] * 100
        next_vol_idx = 101
        m_get_next_volume_index.return_value = next_vol_idx

        tempdir = tempfile.mkdtemp()
        volume_dir = tempdir
        try:
            vfile.create_writable_volume(socket_path, partition, extension,
                                         volume_dir, conf, logger)

            # are the expected files here?
            expected = ["v0000101", "v0000101.writelock",
                        "volume_creation.lock"]
            files = os.listdir(tempdir)
            files.sort()
            self.assertEqual(files, expected)

            # have the locks been taken?
            # TODO: how to properly assert both locks? (one for the global
            # volume creation lock, another for the volume itself)
            self.assertEqual(m_flock.call_count, 2)

            # check the volume header is correct
            with open(os.path.join(tempdir, "v0000101"), 'rb') as vol:
                vol_header = header.read_volume_header(vol)
                self.assertEqual(vol_header.volume_idx, next_vol_idx)
                self.assertEqual(vol_header.partition, int(partition))
                self.assertEqual(vol_header.first_obj_offset, 4096)
                self.assertEqual(vol_header.type, fmgr_pb2.VOLUME_TOMBSTONE)
                self.assertEqual(vol_header.state, fmgr_pb2.STATE_RW)

            # check volume registration to the index server
            m_register_volume.assert_called_once_with(
                socket_path, str(partition), fmgr_pb2.VOLUME_TOMBSTONE,
                next_vol_idx, 4096, fmgr_pb2.STATE_RW)
        finally:
            shutil.rmtree(tempdir)
            m_flock.reset_mock()
            m_register_volume.reset_mock()

    @mock.patch("swift.obj.vfile.get_next_volume_index")
    @mock.patch("swift.obj.vfile.rpc.list_volumes")
    @mock.patch("swift.obj.vfile.os.makedirs")
    @mock.patch("swift.obj.vfile.fcntl.flock")
    @mock.patch("swift.obj.vfile._allocate_volume_space")
    @mock.patch("swift.obj.vfile.fsync")
    @mock.patch("swift.obj.vfile.fsync_dir")
    @mock.patch("swift.obj.vfile.rpc.register_volume")
    @mock.patch("swift.obj.vfile.os.close")
    def test_create_writable_volume_flock_error(self, m_os_close,
                                                m_register_volume,
                                                m_fsync_dir, m_fsync,
                                                m_allocate_volume_space,
                                                m_flock, m_os_makedirs,
                                                m_list_volumes,
                                                m_get_next_volume_index):
        socket_path = "/sda/losf/rpc.socket"
        partition = 123
        extension = None
        conf = {"max_volume_count": 1, "volume_alloc_chunk_size": 50}
        logger = None

        m_list_volumes.return_value = []
        next_vol_idx = 1
        m_get_next_volume_index.return_value = next_vol_idx

        tempdir = tempfile.mkdtemp()
        volume_dir = tempdir

        m_flock.side_effect = [True, IOError(errno.EACCES, "cannot lock")]

        with self.assertRaises(IOError):
            vfile.create_writable_volume(socket_path, partition, extension,
                                         volume_dir, conf, logger)

        try:
            # how to assert close() arguments ?
            self.assertEqual(m_os_close.call_count, 1)
            # check that the volume and its lock file have been removed
            self.assertEqual(os.listdir(tempdir), ['volume_creation.lock'])
            m_register_volume.assert_not_called()
        finally:
            shutil.rmtree(tempdir)

    @mock.patch("swift.obj.vfile.get_next_volume_index")
    @mock.patch("swift.obj.vfile.rpc.list_volumes")
    @mock.patch("swift.obj.vfile.os.makedirs")
    @mock.patch("swift.obj.vfile.fcntl.flock")
    @mock.patch("swift.obj.vfile._allocate_volume_space")
    @mock.patch("swift.obj.vfile.fsync")
    @mock.patch("swift.obj.vfile.fsync_dir")
    @mock.patch("swift.obj.vfile.rpc.register_volume")
    @mock.patch("swift.obj.vfile.os.close")
    def test_create_writable_volume_rpc_error(self, m_os_close,
                                              m_register_volume,
                                              m_fsync_dir, m_fsync,
                                              m_allocate_volume_space,
                                              m_flock, m_os_makedirs,
                                              m_list_volumes,
                                              m_get_next_volume_index):
        socket_path = "/sda/losf/rpc.socket"
        partition = 123
        extension = None
        conf = {"max_volume_count": 1, "volume_alloc_chunk_size": 50}
        logger = None

        m_list_volumes.return_value = []
        next_vol_idx = 1
        m_get_next_volume_index.return_value = next_vol_idx

        tempdir = tempfile.mkdtemp()
        volume_dir = tempdir

        m_register_volume.side_effect = RpcError(StatusCode.InvalidArgument,
                                                 "volume exists")

        try:
            vfile.create_writable_volume(socket_path, partition, extension,
                                         volume_dir, conf, logger)
        except RpcError:
            pass

        try:
            # how to assert close() arguments ?
            self.assertEqual(m_os_close.call_count, 2)
            # check that the volume and its lock file have been removed
            self.assertEqual(os.listdir(tempdir), ['volume_creation.lock'])
        finally:
            shutil.rmtree(tempdir)

    @mock.patch("swift.obj.vfile.rpc.list_volumes")
    def test_open_writable_volume_no_volume(self, m_list_volumes):
        socket_path = "/path/to/rpc.socket"
        volume_dir = "/path/to/volumes"
        partition = 123,
        extension = None
        conf = {}
        logger = None

        rpc_reply = fmgr_pb2.ListVolumesReply()
        volume = fmgr_pb2.Volume(volume_index=1,
                                 volume_type=fmgr_pb2.VOLUME_DEFAULT,
                                 volume_state=fmgr_pb2.STATE_COMPACTION_TARGET,
                                 partition=123)
        rpc_reply.volumes.append(volume)

        m_list_volumes.return_value = rpc_reply.volumes

        m_list_volumes.return_value = []

        vol_file, lock_file, volume_file_path = vfile.open_writable_volume(
            socket_path, partition, extension, volume_dir, conf, logger)
        self.assertEqual((vol_file, lock_file, volume_file_path),
                         (None, None, None))

    @mock.patch("swift.obj.vfile.rpc.list_volumes")
    def test_open_writable_volume_no_rw_volume(self, m_list_volumes):
        socket_path = "/path/to/rpc.socket"
        volume_dir = "/path/to/volumes"
        partition = 123,
        extension = None
        conf = {}
        logger = None

        m_list_volumes.return_value = []

        vol_file, lock_file, volume_file_path = vfile.open_writable_volume(
            socket_path, partition, extension, volume_dir, conf, logger)
        self.assertEqual((vol_file, lock_file, volume_file_path),
                         (None, None, None))

    @mock.patch("swift.obj.vfile.open_volume")
    @mock.patch("swift.obj.vfile.rpc.list_volumes")
    def test_open_writable_volume(self, m_list_volumes, m_open_volume):
        socket_path = "/path/to/rpc.socket"
        volume_dir = "/path/to/volumes"
        conf = {"max_volume_size": 100 * 1024 * 1024}
        logger = None

        test_sets = [
            {
                # partition and extension have no impact on the test as we
                # mock the RPC call that returns a list of volumes based on
                # partition, and extension.
                "partition": 123,
                "extension": None,
                "volumes": [
                    {
                        "index": 1,
                        "partition": 123,
                        "type": fmgr_pb2.VOLUME_DEFAULT,
                        "state": fmgr_pb2.STATE_RW,
                    },
                ],
                "expected_vol_path": "/path/to/volumes/v0000001"

            },
            {
                "partition": 123,
                "extension": None,
                "volumes": [
                    {
                        "index": 1,
                        "partition": 123,
                        "type": fmgr_pb2.VOLUME_DEFAULT,
                        "state": fmgr_pb2.STATE_COMPACTION_SRC,
                    },
                    {
                        "index": 2,
                        "partition": 123,
                        "type": fmgr_pb2.VOLUME_DEFAULT,
                        "state": fmgr_pb2.STATE_RW,
                    }
                ],
                "expected_vol_path": "/path/to/volumes/v0000002"

            },
            {
                "partition": 123,
                "extension": None,
                "volumes": [
                    {
                        "index": 1,
                        "partition": 123,
                        "type": fmgr_pb2.VOLUME_DEFAULT,
                        "state": fmgr_pb2.STATE_COMPACTION_SRC,
                    },
                    {
                        "index": 2,
                        "partition": 123,
                        "type": fmgr_pb2.VOLUME_DEFAULT,
                        "state": fmgr_pb2.STATE_COMPACTION_TARGET,
                    },
                    {
                        "index": 999,
                        "partition": 123,
                        "type": fmgr_pb2.VOLUME_DEFAULT,
                        "state": fmgr_pb2.STATE_RW,
                    },
                    {
                        "index": 1234,
                        "partition": 123,
                        "type": fmgr_pb2.VOLUME_DEFAULT,
                        "state": fmgr_pb2.STATE_COMPACTION_SRC,
                    },
                ],
                "expected_vol_path": "/path/to/volumes/v0000999"

            },
        ]

        for t in test_sets:
            # build the RPC reply
            rpc_reply = fmgr_pb2.ListVolumesReply()
            for vol in t["volumes"]:
                volume = fmgr_pb2.Volume(volume_index=vol["index"],
                                         volume_type=vol["type"],
                                         volume_state=vol["state"],
                                         partition=vol["partition"])
                rpc_reply.volumes.append(volume)

            m_list_volumes.return_value = rpc_reply.volumes

            # files that would be returned by open_volume
            m_vol_file = mock.Mock()
            m_lock_file = mock.Mock()
            m_open_volume.return_value = (m_vol_file, m_lock_file)

            vol_file, lock_file, volume_file_path = vfile.open_writable_volume(
                socket_path, t["partition"], t["extension"], volume_dir, conf,
                logger)
            m_open_volume.assert_called_once_with(t["expected_vol_path"])
            self.assertEqual((vol_file, lock_file, volume_file_path),
                             (m_vol_file, m_lock_file, t["expected_vol_path"]))
            m_open_volume.reset_mock()

    @mock.patch("swift.obj.vfile.os.open")
    @mock.patch("swift.obj.vfile.fcntl.flock")
    def test_open_volume(self, m_flock, m_os_open):
        volume_path = "/path/to/volumes/v0000001"
        expected_lock_path = "/path/to/volumes/v0000001.writelock"

        m_lock_file = mock.Mock()
        m_volume_file = mock.Mock()
        m_os_open.side_effect = [m_lock_file, m_volume_file]
        vol_fd, lock_fd = vfile.open_volume(volume_path)
        # assert the volume lock file has been opened and locked
        args = m_os_open.call_args_list[0]
        self.assertEqual(args, mock.call(expected_lock_path, os.O_WRONLY))
        m_flock.assert_called_once_with(m_lock_file, fcntl.LOCK_EX |
                                        fcntl.LOCK_NB)
        # expect the second call to open to be for the volume itself
        args = m_os_open.call_args_list[1]
        self.assertEqual(args, mock.call(volume_path, os.O_WRONLY))

        self.assertEqual((vol_fd, lock_fd), (m_volume_file, m_lock_file))

    @mock.patch("swift.obj.vfile.os.open")
    @mock.patch("swift.obj.vfile.fcntl.flock")
    @mock.patch("swift.obj.vfile.os.close")
    def test_open_volume_missing_lock_file(self, m_os_close, m_flock,
                                           m_os_open):
        """If the lock file is missing, it should be created"""
        def fake_os_open(path, flags, mode=None):
            if path == "/path/to/volumes/v0000001":
                return 123
            if flags != (os.O_CREAT | os.O_EXCL | os.O_WRONLY):
                raise OSError(errno.ENOENT,
                              "No such file or directory: {}".format(path))
            else:
                return 456

        volume_path = "/path/to/volumes/v0000001"
        expected_lock_path = "/path/to/volumes/v0000001.writelock"

        m_os_open.side_effect = fake_os_open
        vfile.open_volume(volume_path)
        second_open_args = m_os_open.call_args_list[1]
        self.assertEqual(
            second_open_args,
            mock.call(expected_lock_path,
                      os.O_CREAT | os.O_EXCL | os.O_WRONLY, 0o600)
        )

    @mock.patch("swift.obj.vfile.os.open")
    @mock.patch("swift.obj.vfile.fcntl.flock")
    @mock.patch("swift.obj.vfile.os.close")
    def test_open_volume_cannot_open_lock_file(self, m_os_close, m_flock,
                                               m_os_open):
        """The lock file cannot be opened"""
        def fake_os_open(path, flags, mode=None):
            if path == "/path/to/volumes/v0000001":
                return 123
            raise OSError(errno.EPERM, "Permission denied")

        volume_path = "/path/to/volumes/v0000001"

        m_os_open.side_effect = fake_os_open
        self.assertRaises(OSError, vfile.open_volume, volume_path)

    @mock.patch("swift.obj.vfile.os.open")
    @mock.patch("swift.obj.vfile.fcntl.flock")
    def test_open_volume_missing_volume(self, m_flock, m_os_open):
        volume_path = "/path/to/volumes/v0000001"
        expected_lock_path = "/path/to/volumes/v0000001.writelock"

        m_lock_file = mock.Mock()
        m_os_open.side_effect = [m_lock_file,
                                 OSError(2, "No such file or directory")]
        self.assertRaises(OSError, vfile.open_volume, volume_path)
        args_lock = m_os_open.call_args_list[0]
        self.assertEqual(args_lock, mock.call(expected_lock_path, os.O_WRONLY))
        m_flock.assert_called_once_with(m_lock_file, fcntl.LOCK_EX |
                                        fcntl.LOCK_NB)
        args_volume = m_os_open.call_args_list[1]
        self.assertEqual(args_volume, mock.call(volume_path, os.O_WRONLY))

    @mock.patch("swift.obj.vfile.os.open")
    @mock.patch("swift.obj.vfile.os.close")
    @mock.patch("swift.obj.vfile.fcntl.flock")
    def test_open_volume_cannot_lock(self, m_flock, m_os_close, m_os_open):
        volume_path = "/path/to/volumes/v0000001"
        expected_lock_path = "/path/to/volumes/v0000001.writelock"

        # Test we get (None, None) and not an exception if we get an IOError
        # with EACCES when attempting to get a lock
        m_lock_file = mock.Mock()
        m_os_open.return_value = m_lock_file
        m_flock.side_effect = IOError(errno.EACCES, "cannot lock")
        vol_fd, lock_fd = vfile.open_volume(volume_path)
        self.assertEqual((vol_fd, lock_fd), (None, None))
        m_os_open.assert_called_once_with(expected_lock_path, os.O_WRONLY)
        m_flock.assert_called_once_with(m_lock_file, fcntl.LOCK_EX |
                                        fcntl.LOCK_NB)
        m_os_close.assert_called_once_with(m_lock_file)

        # Same test with EAGAIN
        m_os_open.reset_mock()
        m_flock.reset_mock()
        m_os_close.reset_mock()
        m_flock.side_effect = IOError(errno.EAGAIN, "cannot lock")
        vol_fd, lock_fd = vfile.open_volume(volume_path)
        self.assertEqual((vol_fd, lock_fd), (None, None))
        m_os_open.assert_called_once_with(expected_lock_path, os.O_WRONLY)
        m_flock.assert_called_once_with(m_lock_file, fcntl.LOCK_EX |
                                        fcntl.LOCK_NB)
        m_os_close.assert_called_once_with(m_lock_file)

        # Same test with EBADF, this should raise
        m_os_open.reset_mock()
        m_flock.reset_mock()
        m_os_close.reset_mock()
        m_flock.side_effect = IOError(errno.EBADF, "cannot lock")
        self.assertRaises(IOError, vfile.open_volume, volume_path)
        m_os_open.assert_called_once_with(expected_lock_path, os.O_WRONLY)
        m_flock.assert_called_once_with(m_lock_file, fcntl.LOCK_EX |
                                        fcntl.LOCK_NB)
        m_os_close.assert_called_once_with(m_lock_file)

    def test_get_lock_file_name(self):
        name = vfile.get_lock_file_name(1)
        self.assertEqual(name, "v0000001.writelock")

        name = vfile.get_lock_file_name(9999999)
        self.assertEqual(name, "v9999999.writelock")

        self.assertRaises(vfile.VFileException, vfile.get_lock_file_name, 0)
        self.assertRaises(vfile.VFileException, vfile.get_lock_file_name,
                          10000000)

    def test_get_volume_name(self):
        name = vfile.get_volume_name(1)
        self.assertEqual(name, "v0000001")

        name = vfile.get_volume_name(9999999)
        self.assertEqual(name, "v9999999")

        self.assertRaises(vfile.VFileException, vfile.get_volume_name, 0)
        self.assertRaises(vfile.VFileException, vfile.get_volume_name,
                          10000000)


if __name__ == '__main__':
    unittest.main()