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 import unittest |
| 24 from cStringIO import StringIO |
| 25 |
| 26 import mock |
| 27 from mock import ANY |
| 28 |
| 29 from boto.glacier import vault |
| 30 |
| 31 |
| 32 class TestVault(unittest.TestCase): |
| 33 def setUp(self): |
| 34 self.size_patch = mock.patch('os.path.getsize') |
| 35 self.getsize = self.size_patch.start() |
| 36 self.api = mock.Mock() |
| 37 self.vault = vault.Vault(self.api, None) |
| 38 self.vault.name = 'myvault' |
| 39 self.mock_open = mock.mock_open() |
| 40 stringio = StringIO('content') |
| 41 self.mock_open.return_value.read = stringio.read |
| 42 |
| 43 def tearDown(self): |
| 44 self.size_patch.stop() |
| 45 |
| 46 def test_upload_archive_small_file(self): |
| 47 self.getsize.return_value = 1 |
| 48 |
| 49 self.api.upload_archive.return_value = {'ArchiveId': 'archive_id'} |
| 50 with mock.patch('boto.glacier.vault.open', self.mock_open, |
| 51 create=True): |
| 52 archive_id = self.vault.upload_archive( |
| 53 'filename', 'my description') |
| 54 self.assertEqual(archive_id, 'archive_id') |
| 55 self.api.upload_archive.assert_called_with( |
| 56 'myvault', self.mock_open.return_value, |
| 57 mock.ANY, mock.ANY, 'my description') |
| 58 |
| 59 def test_small_part_size_is_obeyed(self): |
| 60 self.vault.DefaultPartSize = 2 * 1024 * 1024 |
| 61 self.vault.create_archive_writer = mock.Mock() |
| 62 |
| 63 self.getsize.return_value = 1 |
| 64 |
| 65 with mock.patch('boto.glacier.vault.open', self.mock_open, |
| 66 create=True): |
| 67 self.vault.create_archive_from_file('myfile') |
| 68 # The write should be created with the default part size of the |
| 69 # instance (2 MB). |
| 70 self.vault.create_archive_writer.assert_called_with( |
| 71 description=mock.ANY, part_size=self.vault.DefaultPartSize) |
| 72 |
| 73 def test_large_part_size_is_obeyed(self): |
| 74 self.vault.DefaultPartSize = 8 * 1024 * 1024 |
| 75 self.vault.create_archive_writer = mock.Mock() |
| 76 self.getsize.return_value = 1 |
| 77 with mock.patch('boto.glacier.vault.open', self.mock_open, |
| 78 create=True): |
| 79 self.vault.create_archive_from_file('myfile') |
| 80 # The write should be created with the default part size of the |
| 81 # instance (8 MB). |
| 82 self.vault.create_archive_writer.assert_called_with( |
| 83 description=mock.ANY, part_size=self.vault.DefaultPartSize) |
| 84 |
| 85 |
| 86 class TestConcurrentUploads(unittest.TestCase): |
| 87 |
| 88 def test_concurrent_upload_file(self): |
| 89 v = vault.Vault(None, None) |
| 90 with mock.patch('boto.glacier.vault.ConcurrentUploader') as c: |
| 91 c.return_value.upload.return_value = 'archive_id' |
| 92 archive_id = v.concurrent_create_archive_from_file( |
| 93 'filename', 'my description') |
| 94 c.return_value.upload.assert_called_with('filename', |
| 95 'my description') |
| 96 self.assertEqual(archive_id, 'archive_id') |
| 97 |
| 98 |
| 99 if __name__ == '__main__': |
| 100 unittest.main() |
OLD | NEW |