OLD | NEW |
(Empty) | |
| 1 #!/usr/bin/env python |
| 2 # Copyright (c) 2012 Amazon.com, Inc. or its affiliates. All Rights Reserved |
| 3 # |
| 4 # Permission is hereby granted, free of charge, to any person obtaining a |
| 5 # copy of this software and associated documentation files (the |
| 6 # "Software"), to deal in the Software without restriction, including |
| 7 # without limitation the rights to use, copy, modify, merge, publish, dis- |
| 8 # tribute, sublicense, and/or sell copies of the Software, and to permit |
| 9 # persons to whom the Software is furnished to do so, subject to the fol- |
| 10 # lowing conditions: |
| 11 # |
| 12 # The above copyright notice and this permission notice shall be included |
| 13 # in all copies or substantial portions of the Software. |
| 14 # |
| 15 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS |
| 16 # OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABIL- |
| 17 # ITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT |
| 18 # SHALL THE AUTHOR BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, |
| 19 # WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 20 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS |
| 21 # IN THE SOFTWARE. |
| 22 # |
| 23 from Queue import Queue |
| 24 |
| 25 import mock |
| 26 from tests.unit import unittest |
| 27 from tests.unit import AWSMockServiceTestCase |
| 28 |
| 29 from boto.glacier.concurrent import ConcurrentUploader, ConcurrentDownloader |
| 30 |
| 31 |
| 32 class FakeThreadedConcurrentUploader(ConcurrentUploader): |
| 33 def _start_upload_threads(self, results_queue, upload_id, |
| 34 worker_queue, filename): |
| 35 self.results_queue = results_queue |
| 36 self.worker_queue = worker_queue |
| 37 self.upload_id = upload_id |
| 38 |
| 39 def _wait_for_upload_threads(self, hash_chunks, result_queue, total_parts): |
| 40 for i in xrange(total_parts): |
| 41 hash_chunks[i] = 'foo' |
| 42 |
| 43 class FakeThreadedConcurrentDownloader(ConcurrentDownloader): |
| 44 def _start_download_threads(self, results_queue, worker_queue): |
| 45 self.results_queue = results_queue |
| 46 self.worker_queue = worker_queue |
| 47 |
| 48 def _wait_for_download_threads(self, filename, result_queue, total_parts): |
| 49 pass |
| 50 |
| 51 |
| 52 class TestConcurrentUploader(unittest.TestCase): |
| 53 |
| 54 def setUp(self): |
| 55 super(TestConcurrentUploader, self).setUp() |
| 56 self.stat_patch = mock.patch('os.stat') |
| 57 self.stat_mock = self.stat_patch.start() |
| 58 # Give a default value for tests that don't care |
| 59 # what the file size is. |
| 60 self.stat_mock.return_value.st_size = 1024 * 1024 * 8 |
| 61 |
| 62 def tearDown(self): |
| 63 self.stat_mock = self.stat_patch.start() |
| 64 |
| 65 def test_calculate_required_part_size(self): |
| 66 self.stat_mock.return_value.st_size = 1024 * 1024 * 8 |
| 67 uploader = ConcurrentUploader(mock.Mock(), 'vault_name') |
| 68 total_parts, part_size = uploader._calculate_required_part_size( |
| 69 1024 * 1024 * 8) |
| 70 self.assertEqual(total_parts, 2) |
| 71 self.assertEqual(part_size, 4 * 1024 * 1024) |
| 72 |
| 73 def test_calculate_required_part_size_too_small(self): |
| 74 too_small = 1 * 1024 * 1024 |
| 75 self.stat_mock.return_value.st_size = 1024 * 1024 * 1024 |
| 76 uploader = ConcurrentUploader(mock.Mock(), 'vault_name', |
| 77 part_size=too_small) |
| 78 total_parts, part_size = uploader._calculate_required_part_size( |
| 79 1024 * 1024 * 1024) |
| 80 self.assertEqual(total_parts, 256) |
| 81 # Part size if 4MB not the passed in 1MB. |
| 82 self.assertEqual(part_size, 4 * 1024 * 1024) |
| 83 |
| 84 def test_work_queue_is_correctly_populated(self): |
| 85 uploader = FakeThreadedConcurrentUploader(mock.MagicMock(), |
| 86 'vault_name') |
| 87 uploader.upload('foofile') |
| 88 q = uploader.worker_queue |
| 89 items = [q.get() for i in xrange(q.qsize())] |
| 90 self.assertEqual(items[0], (0, 4 * 1024 * 1024)) |
| 91 self.assertEqual(items[1], (1, 4 * 1024 * 1024)) |
| 92 # 2 for the parts, 10 for the end sentinels (10 threads). |
| 93 self.assertEqual(len(items), 12) |
| 94 |
| 95 def test_correct_low_level_api_calls(self): |
| 96 api_mock = mock.MagicMock() |
| 97 uploader = FakeThreadedConcurrentUploader(api_mock, 'vault_name') |
| 98 uploader.upload('foofile') |
| 99 # The threads call the upload_part, so we're just verifying the |
| 100 # initiate/complete multipart API calls. |
| 101 api_mock.initiate_multipart_upload.assert_called_with( |
| 102 'vault_name', 4 * 1024 * 1024, None) |
| 103 api_mock.complete_multipart_upload.assert_called_with( |
| 104 'vault_name', mock.ANY, mock.ANY, 8 * 1024 * 1024) |
| 105 |
| 106 def test_downloader_work_queue_is_correctly_populated(self): |
| 107 job = mock.MagicMock() |
| 108 job.archive_size = 8 * 1024 * 1024 |
| 109 downloader = FakeThreadedConcurrentDownloader(job) |
| 110 downloader.download('foofile') |
| 111 q = downloader.worker_queue |
| 112 items = [q.get() for i in xrange(q.qsize())] |
| 113 self.assertEqual(items[0], (0, 4 * 1024 * 1024)) |
| 114 self.assertEqual(items[1], (1, 4 * 1024 * 1024)) |
| 115 # 2 for the parts, 10 for the end sentinels (10 threads). |
| 116 self.assertEqual(len(items), 12) |
| 117 |
| 118 |
| 119 if __name__ == '__main__': |
| 120 unittest.main() |
OLD | NEW |