Revision b4e29772 kamaki/clients/test/pithos.py

b/kamaki/clients/test/pithos.py
119 119
        x_object_hash="0afdf29f71cd53126225c3f54ca",
120 120
        x_object_version=17737,
121 121
        x_object_modified_by=user_id)]
122
object_hashmap = dict(
123
    block_hash="sha256", block_size=4194304, bytes=33554432,
124
    hashes=[
125
        "4988438cc1c0292c085d289649b28cf547ba3db71c6efaac9f2df7e193d4d0af",
126
        "b214244aa56df7d1df7c6cac066e7cef268d9c2beb4dcf7ce68af667b0626f91",
127
        "17f365f25e0682565ded30576066bb13377a3d306967e4d74e06bb6bbc20f75f",
128
        "2524ae208932669fff89adf8a2fc0df3b67736ca0d3aadce7a2ce640f142af37",
129
        "5d807a2129d2fcd3c221c3da418ed52af3fc48d0817b62e0bb437acffccd3514",
130
        "609de22ce842d997f645fc49d5f14e0e3766dd51a6cbe66383b2bab82c8dfcd0",
131
        "3102851ac168c78be70e35ff5178c7b1ebebd589e5106d565ca1094d1ca8ff59",
132
        "bfe306dd24e92a8d85caf7055643f250fd319e8c4cdd4755ddabbf3ff97e83c7"])
122 133

  
123 134

  
124 135
class Pithos(TestCase):
......
167 178
        self.FR.headers = dict()
168 179
        self.FR.status_code = 200
169 180
        self.FR.json = dict()
181
        self.FR.content = self.FR.json
170 182
        for f in self.files:
171 183
            f.close()
172 184

  
......
614 626
            expected.update(kwargs)
615 627
            expected['permissions'] = expected.pop('sharing')
616 628
            self.assertEqual(put.mock_calls[-1], call(obj, **expected))
629

  
630
    def test_download_object(self):
631
        PC.get_object_hashmap = Mock(return_value=object_hashmap)
632
        tmpFile = self._create_temp_file()
633
        self.FR.content = tmpFile.read(4 * 1024 * 1024)
634
        tmpFile = self._create_temp_file()
635
        PC.object_get = Mock(return_value=self.FR())
636
        GET = PC.object_get
637
        obj = 'obj3c7N4m3'
638
        num_of_blocks = len(object_hashmap['hashes'])
639

  
640
        kwargs = dict(
641
            version='version',
642
            resume=True,
643
            range_str='10-20',
644
            if_match='if and only if',
645
            if_none_match='if and only not',
646
            if_modified_since='what if not?',
647
            if_unmodified_since='this happens if not!',
648
            async_headers=dict(Range='bytes=0-88888888'))
649

  
650
        self.client.download_object(obj, tmpFile)
651
        self.assertEqual(len(GET.mock_calls), num_of_blocks)
652
        self.assertEqual(GET.mock_calls[-1][1], (obj,))
653
        for k, v in kwargs.items():
654
            if k == 'async_headers':
655
                self.assertTrue('Range' in GET.mock_calls[-1][2][k])
656
            elif k in ('resume', 'range_str'):
657
                continue
658
            else:
659
                self.assertEqual(GET.mock_calls[-1][2][k], None)
660

  
661
        #  Check ranges are consecutive
662
        starts = []
663
        ends = []
664
        for c in GET.mock_calls:
665
            rng_str = c[2]['async_headers']['Range']
666
            (start, rng_str) = rng_str.split('=')
667
            (start, end) = rng_str.split('-')
668
            starts.append(start)
669
            ends.append(end)
670
        ends = sorted(ends)
671
        for i, start in enumerate(sorted(starts)):
672
            if i:
673
                int(ends[i - 1]) == int(start) - 1
674

  
675
        #  With progress bars
676
        try:
677
            from progress.bar import ShadyBar
678
            dl_bar = ShadyBar('Mock dl')
679
        except ImportError:
680
            dl_bar = None
681

  
682
        if dl_bar:
683

  
684
            def blck_gen(n):
685
                for i in dl_bar.iter(range(n)):
686
                    yield
687
                yield
688

  
689
            tmpFile.seek(0)
690
            self.client.download_object(obj, tmpFile, download_cb=blck_gen)
691
            self.assertEqual(len(GET.mock_calls), 2 * num_of_blocks)
692

  
693
        tmpFile.seek(0)
694
        kwargs.pop('async_headers')
695
        kwargs.pop('resume')
696
        self.client.download_object(obj, tmpFile, **kwargs)
697
        for k, v in kwargs.items():
698
            if k == 'range_str':
699
                self.assertEqual(
700
                    GET.mock_calls[-1][2]['data_range'],
701
                    'bytes=%s' % v)
702
            else:
703
                self.assertEqual(GET.mock_calls[-1][2][k], v)
704

  
705
        #  ALl options on no tty
706

  
707
        def foo():
708
            return True
709

  
710
        tmpFile.seek(0)
711
        tmpFile.isatty = foo
712
        self.client.download_object(obj, tmpFile, **kwargs)
713
        for k, v in kwargs.items():
714
            if k == 'range_str':
715
                self.assertTrue('data_range' in GET.mock_calls[-1][2])
716
            else:
717
                self.assertEqual(GET.mock_calls[-1][2][k], v)

Also available in: Unified diff