forked from basak/glacier-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
glacier_test.py
173 lines (153 loc) · 6.94 KB
/
glacier_test.py
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
#!/usr/bin/env python
# Copyright (c) 2013 Robie Basak
#
# Permission is hereby granted, free of charge, to any person obtaining a
# copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish, dis-
# tribute, sublicense, and/or sell copies of the Software, and to permit
# persons to whom the Software is furnished to do so, subject to the fol-
# lowing conditions:
#
# The above copyright notice and this permission notice shall be included
# in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABIL-
# ITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT
# SHALL THE AUTHOR BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
# IN THE SOFTWARE.
from __future__ import print_function
import sys
import unittest
import mock
from mock import Mock, patch, sentinel
import nose.tools
import glacier
EX_TEMPFAIL = 75
class TestCase(unittest.TestCase):
def init_app(self, args, memory_cache=False):
self.connection = Mock()
if memory_cache:
self.cache = glacier.Cache(0, db_path=':memory:')
else:
self.cache = Mock()
self.app = glacier.App(
args=args,
connection=self.connection,
cache=self.cache)
def run_app(self, args):
self.init_app(args)
self.app.main()
def test_vault_list(self):
self.init_app(['vault', 'list'])
mock_vault = Mock()
mock_vault.name = sentinel.vault_name
self.connection.list_vaults.return_value = [mock_vault]
print_mock = Mock()
with patch('__builtin__.print', print_mock):
self.app.main()
print_mock.assert_called_once_with(sentinel.vault_name, sep=u'\n')
def test_vault_create(self):
self.run_app(['vault', 'create', 'vault_name'])
self.connection.create_vault.assert_called_once_with('vault_name')
def test_archive_list(self):
self.init_app(['archive', 'list', 'vault_name'])
archive_list = [sentinel.archive_one, sentinel.archive_two]
self.cache.get_archive_list.return_value = archive_list
print_mock = Mock()
with patch('__builtin__.print', print_mock):
self.app.main()
print_mock.assert_called_once_with(*archive_list, sep="\n")
def test_archive_list_force_ids(self):
self.init_app(
['archive', 'list', '--force-ids', 'vault_name'],
memory_cache=True,
)
self.cache.add_archive('vault_name', 'archive_name_1', 'id_1')
self.cache.add_archive('vault_name', 'archive_name_1', 'id_2')
self.cache.add_archive('vault_name', 'archive_name_3', 'id_3')
print_mock = Mock()
with patch('__builtin__.print', print_mock):
self.app.main()
# print should have been called with a list of the items in some
# arbitrary order. Testing this correctly involves being agnostic with
# the order of args in *args. Does mock provide any other way of doing
# this other than by introspecting mock_calls like this?
nose.tools.assert_equals(print_mock.call_count, 1)
nose.tools.assert_equals(
sorted(print_mock.mock_calls[0][1]),
sorted([
u'id:id_1\tarchive_name_1',
u'id:id_2\tarchive_name_1',
u'id:id_3\tarchive_name_3',
]),
)
nose.tools.assert_equals(
print_mock.mock_calls[0][2],
{'sep': "\n"}
)
def test_archive_upload(self):
file_obj = Mock()
file_obj.name = 'filename'
open_mock = Mock(return_value=file_obj)
with patch('__builtin__.open', open_mock):
self.run_app(['archive', 'upload', 'vault_name', 'filename'])
self.connection.get_vault.assert_called_with('vault_name')
mock_vault = self.connection.get_vault.return_value
mock_vault.create_archive_from_file.assert_called_once_with(
file_obj=file_obj, description='filename')
def test_archive_stdin_upload(self):
self.run_app(['archive', 'upload', 'vault_name', '-'])
self.connection.get_vault.assert_called_once_with('vault_name')
vault = self.connection.get_vault.return_value
vault.create_archive_from_file.assert_called_once_with(
file_obj=sys.stdin, description='<stdin>')
def test_archive_retrieve_no_job(self):
self.init_app(['archive', 'retrieve', 'vault_name', 'archive_name'])
mock_vault = Mock()
mock_vault.list_jobs.return_value = []
self.connection.get_vault.return_value = mock_vault
mock_exit = Mock()
mock_print = Mock()
with patch('sys.exit', mock_exit):
with patch('__builtin__.print', mock_print):
self.app.main()
mock_exit.assert_called_once_with(EX_TEMPFAIL)
mock_print.assert_called_once_with(
u"glacier: queued retrieval job for archive 'archive_name'",
file=sys.stderr)
self.connection.get_vault.assert_called_once_with('vault_name')
mock_vault.retrieve_archive.assert_called_once_with(
self.cache.get_archive_id.return_value)
def test_archive_retrieve_with_job(self):
self.init_app(['archive', 'retrieve', 'vault_name', 'archive_name'])
self.cache.get_archive_id.return_value = sentinel.archive_id
mock_job = Mock(
archive_id=sentinel.archive_id,
completed=True,
completion_date='1970-01-01T00:00:00Z',
archive_size=1)
mock_vault = Mock()
mock_vault.list_jobs.return_value = [mock_job]
self.connection.get_vault.return_value = mock_vault
mock_open = mock.mock_open()
with patch('__builtin__.open', mock_open):
self.app.main()
self.cache.get_archive_id.assert_called_once_with(
'vault_name', 'archive_name')
mock_job.get_output.assert_called_once_with()
mock_job.get_output.return_value.read.assert_called_once_with()
mock_open.assert_called_once_with('archive_name', u'wb')
mock_open.return_value.write.assert_called_once_with(
mock_job.get_output.return_value.read.return_value)
def test_archive_delete(self):
self.run_app(['archive', 'delete', 'vault_name', 'archive_name'])
self.cache.get_archive_id.assert_called_once_with(
'vault_name', 'archive_name')
self.connection.get_vault.assert_called_with('vault_name')
mock_vault = self.connection.get_vault.return_value
mock_vault.delete_archive.assert_called_once_with(
self.cache.get_archive_id.return_value)