OLD | NEW |
(Empty) | |
| 1 # Copyright (c) 2012 Amazon.com, Inc. or its affiliates. All Rights Reserved |
| 2 # |
| 3 # Permission is hereby granted, free of charge, to any person obtaining a |
| 4 # copy of this software and associated documentation files (the |
| 5 # "Software"), to deal in the Software without restriction, including |
| 6 # without limitation the rights to use, copy, modify, merge, publish, dis- |
| 7 # tribute, sublicense, and/or sell copies of the Software, and to permit |
| 8 # persons to whom the Software is furnished to do so, subject to the fol- |
| 9 # lowing conditions: |
| 10 # |
| 11 # The above copyright notice and this permission notice shall be included |
| 12 # in all copies or substantial portions of the Software. |
| 13 # |
| 14 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS |
| 15 # OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABIL- |
| 16 # ITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT |
| 17 # SHALL THE AUTHOR BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, |
| 18 # WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 19 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS |
| 20 # IN THE SOFTWARE. |
| 21 # |
| 22 from hashlib import sha256 |
| 23 import itertools |
| 24 from StringIO import StringIO |
| 25 |
| 26 from tests.unit import unittest |
| 27 from mock import ( |
| 28 call, |
| 29 Mock, |
| 30 patch, |
| 31 sentinel, |
| 32 ) |
| 33 from nose.tools import assert_equal |
| 34 |
| 35 from boto.glacier.layer1 import Layer1 |
| 36 from boto.glacier.vault import Vault |
| 37 from boto.glacier.writer import Writer, resume_file_upload |
| 38 from boto.glacier.utils import bytes_to_hex, chunk_hashes, tree_hash |
| 39 |
| 40 |
| 41 def create_mock_vault(): |
| 42 vault = Mock(spec=Vault) |
| 43 vault.layer1 = Mock(spec=Layer1) |
| 44 vault.layer1.complete_multipart_upload.return_value = dict( |
| 45 ArchiveId=sentinel.archive_id) |
| 46 vault.name = sentinel.vault_name |
| 47 return vault |
| 48 |
| 49 |
| 50 def partify(data, part_size): |
| 51 for i in itertools.count(0): |
| 52 start = i * part_size |
| 53 part = data[start:start+part_size] |
| 54 if part: |
| 55 yield part |
| 56 else: |
| 57 return |
| 58 |
| 59 |
| 60 def calculate_mock_vault_calls(data, part_size, chunk_size): |
| 61 upload_part_calls = [] |
| 62 data_tree_hashes = [] |
| 63 for i, data_part in enumerate(partify(data, part_size)): |
| 64 start = i * part_size |
| 65 end = start + len(data_part) |
| 66 data_part_tree_hash_blob = tree_hash( |
| 67 chunk_hashes(data_part, chunk_size)) |
| 68 data_part_tree_hash = bytes_to_hex(data_part_tree_hash_blob) |
| 69 data_part_linear_hash = sha256(data_part).hexdigest() |
| 70 upload_part_calls.append( |
| 71 call.layer1.upload_part( |
| 72 sentinel.vault_name, sentinel.upload_id, |
| 73 data_part_linear_hash, data_part_tree_hash, |
| 74 (start, end - 1), data_part)) |
| 75 data_tree_hashes.append(data_part_tree_hash_blob) |
| 76 |
| 77 return upload_part_calls, data_tree_hashes |
| 78 |
| 79 |
| 80 def check_mock_vault_calls(vault, upload_part_calls, data_tree_hashes, |
| 81 data_len): |
| 82 vault.layer1.upload_part.assert_has_calls( |
| 83 upload_part_calls, any_order=True) |
| 84 assert_equal( |
| 85 len(upload_part_calls), vault.layer1.upload_part.call_count) |
| 86 |
| 87 data_tree_hash = bytes_to_hex(tree_hash(data_tree_hashes)) |
| 88 vault.layer1.complete_multipart_upload.assert_called_once_with( |
| 89 sentinel.vault_name, sentinel.upload_id, data_tree_hash, data_len) |
| 90 |
| 91 |
| 92 class TestWriter(unittest.TestCase): |
| 93 def setUp(self): |
| 94 super(TestWriter, self).setUp() |
| 95 self.vault = create_mock_vault() |
| 96 self.chunk_size = 2 # power of 2 |
| 97 self.part_size = 4 # power of 2 |
| 98 upload_id = sentinel.upload_id |
| 99 self.writer = Writer( |
| 100 self.vault, upload_id, self.part_size, self.chunk_size) |
| 101 |
| 102 def check_write(self, write_list): |
| 103 for write_data in write_list: |
| 104 self.writer.write(write_data) |
| 105 self.writer.close() |
| 106 |
| 107 data = ''.join(write_list) |
| 108 upload_part_calls, data_tree_hashes = calculate_mock_vault_calls( |
| 109 data, self.part_size, self.chunk_size) |
| 110 check_mock_vault_calls( |
| 111 self.vault, upload_part_calls, data_tree_hashes, len(data)) |
| 112 |
| 113 def test_single_byte_write(self): |
| 114 self.check_write(['1']) |
| 115 |
| 116 def test_one_part_write(self): |
| 117 self.check_write(['1234']) |
| 118 |
| 119 def test_split_write_1(self): |
| 120 self.check_write(['1', '234']) |
| 121 |
| 122 def test_split_write_2(self): |
| 123 self.check_write(['12', '34']) |
| 124 |
| 125 def test_split_write_3(self): |
| 126 self.check_write(['123', '4']) |
| 127 |
| 128 def test_one_part_plus_one_write(self): |
| 129 self.check_write(['12345']) |
| 130 |
| 131 def test_returns_archive_id(self): |
| 132 self.writer.write('1') |
| 133 self.writer.close() |
| 134 self.assertEquals(sentinel.archive_id, self.writer.get_archive_id()) |
| 135 |
| 136 def test_upload_id(self): |
| 137 self.assertEquals(sentinel.upload_id, self.writer.upload_id) |
| 138 |
| 139 |
| 140 class TestResume(unittest.TestCase): |
| 141 def setUp(self): |
| 142 super(TestResume, self).setUp() |
| 143 self.vault = create_mock_vault() |
| 144 self.chunk_size = 2 # power of 2 |
| 145 self.part_size = 4 # power of 2 |
| 146 |
| 147 def check_no_resume(self, data, resume_set=set()): |
| 148 fobj = StringIO(data) |
| 149 part_hash_map = {} |
| 150 for part_index in resume_set: |
| 151 start = self.part_size * part_index |
| 152 end = start + self.part_size |
| 153 part_data = data[start:end] |
| 154 part_hash_map[part_index] = tree_hash( |
| 155 chunk_hashes(part_data, self.chunk_size)) |
| 156 |
| 157 resume_file_upload( |
| 158 self.vault, sentinel.upload_id, self.part_size, fobj, |
| 159 part_hash_map, self.chunk_size) |
| 160 |
| 161 upload_part_calls, data_tree_hashes = calculate_mock_vault_calls( |
| 162 data, self.part_size, self.chunk_size) |
| 163 resume_upload_part_calls = [ |
| 164 call for part_index, call in enumerate(upload_part_calls) |
| 165 if part_index not in resume_set] |
| 166 check_mock_vault_calls( |
| 167 self.vault, resume_upload_part_calls, data_tree_hashes, len(data)) |
| 168 |
| 169 def test_one_part_no_resume(self): |
| 170 self.check_no_resume('1234') |
| 171 |
| 172 def test_two_parts_no_resume(self): |
| 173 self.check_no_resume('12345678') |
| 174 |
| 175 def test_one_part_resume(self): |
| 176 self.check_no_resume('1234', resume_set=set([0])) |
| 177 |
| 178 def test_two_parts_one_resume(self): |
| 179 self.check_no_resume('12345678', resume_set=set([1])) |
| 180 |
| 181 def test_returns_archive_id(self): |
| 182 archive_id = resume_file_upload( |
| 183 self.vault, sentinel.upload_id, self.part_size, StringIO('1'), {}, |
| 184 self.chunk_size) |
| 185 self.assertEquals(sentinel.archive_id, archive_id) |
OLD | NEW |