2
from argparse import Namespace
3
from mock import Mock, patch, call
6
from assess_resources import (
18
from utility import JujuAssertionError
21
class TestParseArgs(TestCase):
23
def test_common_args(self):
29
self.assertEqual("an-env", args.env)
30
self.assertEqual("/bin/juju", args.juju_bin)
31
self.assertEqual("/tmp/logs", args.logs)
32
self.assertEqual("an-env-mod", args.temp_env_name)
33
self.assertEqual(False, args.debug)
36
fake_stdout = StringIO.StringIO()
37
with parse_error(self) as fake_stderr:
38
with patch("sys.stdout", fake_stdout):
39
parse_args(["--help"])
40
self.assertEqual("", fake_stderr.getvalue())
41
self.assertNotIn("TODO", fake_stdout.getvalue())
44
class TestMain(TestCase):
54
client = Mock(spec=["is_jes_enabled"])
55
with patch("assess_resources.configure_logging",
56
autospec=True) as mock_cl:
57
with patch("assess_resources.BootstrapManager.booted_context",
58
autospec=True) as mock_bc:
59
with patch("deploy_stack.client_from_config",
60
return_value=client) as mock_c:
61
with patch("assess_resources.assess_resources",
62
autospec=True) as mock_assess:
64
mock_cl.assert_called_once_with(logging.DEBUG)
65
mock_c.assert_called_once_with('an-env', "/bin/juju", debug=False,
67
self.assertEqual(mock_bc.call_count, 1)
68
mock_assess.assert_called_once_with(client, make_args())
71
class TestAssessResources(TestCase):
73
def test_verify_status(self):
75
make_resource_list(), 'dummy-resource/foo', 'foo', '1234', 27)
77
def test_verify_status_serviceid(self):
78
verify_status(make_resource_list('serviceid'), 'dummy-resource/foo',
81
def test_verify_status_exception(self):
82
status = make_resource_list()
83
status['resources'][0]['expected']['origin'] = 'charmstore'
84
with self.assertRaisesRegexp(
85
JujuAssertionError, 'Unexpected resource list values'):
86
verify_status(status, 'dummy-resource/foo', 'foo', '1234', 27)
88
def test_verify_status_unit_exception(self):
89
status = make_resource_list()
90
status['resources'][0]['unit']['origin'] = 'charmstore'
91
with self.assertRaisesRegexp(
92
JujuAssertionError, 'Unexpected unit resource list values'):
93
verify_status(status, 'dummy-resource/foo', 'foo', '1234', 27)
95
def test_verify_status_no_resoruce_id_exception(self):
96
with self.assertRaisesRegexp(
97
JujuAssertionError, 'Resource id not found.'):
98
verify_status(make_resource_list(), 'NO_ID', 'foo', '1234', 27)
100
def test_push_resource(self):
102
spec=["deploy", "wait_for_started", "list_resources",
103
"wait_for_resource", "show_status"])
104
mock_client.version = '2.0.0'
105
mock_client.list_resources.return_value = make_resource_list()
106
push_resource(mock_client, 'foo', '1234', 27, 1800, 1800)
107
mock_client.deploy.assert_called_once_with(
108
'dummy-resource', resource='foo=dummy-resource/foo.txt')
109
mock_client.wait_for_started.assert_called_once_with(timeout=1800)
110
mock_client.show_status.assert_called_once_with()
112
def test_push_resource_attach(self):
114
spec=["attach", "wait_for_started", "list_resources",
115
"wait_for_resource", "show_status"])
116
mock_client.version = '2.0.0'
117
mock_client.list_resources.return_value = make_resource_list()
118
push_resource(mock_client, 'foo', '1234', 27, 1800, 1800, deploy=False)
119
mock_client.attach.assert_called_once_with(
120
'dummy-resource', resource='foo=dummy-resource/foo.txt')
121
mock_client.wait_for_started.assert_called_once_with(timeout=1800)
123
def test_assess_resources(self):
124
fake_file = FakeFile()
126
with patch("assess_resources.push_resource", autospec=True) as mock_p:
127
with patch('assess_resources.NamedTemporaryFile',
128
autospec=True) as mock_ntf:
129
mock_ntf.return_value.__enter__.return_value = fake_file
130
assess_resources(None, args)
134
'4ddc48627c6404e538bb0957632ef68618c0839649d9ad9e41ad94472c158'
135
'9f4b7f9d830df6c4b209d7eb1b4b5522c4d', 27, 1800, 1800),
138
'ffbf43d68a6960de63908bb05c14a026abeda136119d3797431bdd7b469c1'
139
'f027e57a28aeec0df01a792e9e70aad2d6b', 17, 1800, 1800,
143
'2a3821585efcccff1562efea4514dd860cd536441954e182a7649910e21f6'
144
'a179a015677a68a351a11d3d2f277e551e4', 27, 1800, 1800,
145
deploy=False, resource_file='baz.txt'),
148
'3164673a8ac27576ab5fc06b9adc4ce0aca5bd3025384b1cf2128a8795e74'
149
'7c431e882785a0bf8dc70b42995db388575', 1024 * 1024, 1800, 1800,
150
deploy=False, resource_file='/tmp/fake'),
152
self.assertEqual(mock_p.mock_calls, calls)
154
def test_assess_resources_large_test(self):
155
fake_file = FakeFile()
157
args.large_test_enabled = True
158
with patch("assess_resources.push_resource", autospec=True) as mock_p:
159
with patch('assess_resources.fill_dummy_file',
160
autospec=True) as mock_fdf:
161
with patch('assess_resources.NamedTemporaryFile',
162
autospec=True) as mock_ntf:
163
mock_ntf.return_value.__enter__.return_value = fake_file
164
with patch('assess_resources.large_assess',
165
autospec=True) as mock_lt:
166
assess_resources(None, args)
170
'4ddc48627c6404e538bb0957632ef68618c0839649d9ad9e41ad94472c158'
171
'9f4b7f9d830df6c4b209d7eb1b4b5522c4d', 27, 1800, 1800),
174
'ffbf43d68a6960de63908bb05c14a026abeda136119d3797431bdd7b469c1'
175
'f027e57a28aeec0df01a792e9e70aad2d6b', 17, 1800, 1800,
179
'2a3821585efcccff1562efea4514dd860cd536441954e182a7649910e21f6'
180
'a179a015677a68a351a11d3d2f277e551e4', 27, 1800, 1800,
181
deploy=False, resource_file='baz.txt'),
183
'3164673a8ac27576ab5fc06b9adc4ce0aca5bd3025384b1cf2128a8795e7'
184
'47c431e882785a0bf8dc70b42995db388575', 1024 * 1024, 1800,
185
1800, deploy=False, resource_file='/tmp/fake')]
186
self.assertEqual(mock_p.mock_calls, calls)
187
mock_fdf.assert_called_once_with('/tmp/fake', size=1024 * 1024)
188
mock_lt.assert_called_once_with(None, 1800, 1800)
190
def test_large_tests(self):
191
fake_file = FakeFile()
192
with patch("assess_resources.push_resource", autospec=True) as mock_pr:
193
with patch('assess_resources.NamedTemporaryFile',
194
autospec=True) as mock_ntf:
195
mock_ntf.return_value.__enter__.return_value = fake_file
196
with patch('assess_resources.fill_dummy_file',
198
large_assess(None, 1800, 1800)
202
'd7c014629d74ae132cc9f88e3ec2f31652f40a7a1fcc52c54b04d6c0d0891'
203
'69bcd55958d1277b4cdf6262f21c712d0a7', 1024 * 1024 * 10, 1800,
204
1800, deploy=False, resource_file='/tmp/fake'),
207
'c11e93892b66de781e4d0883efe10482f8d1642f3b6574ba2ee0da6f8db03'
208
'f53c0eadfb5e5e0463574c113024ded369e', 1024 * 1024 * 100, 1800,
209
1800, deploy=False, resource_file='/tmp/fake'),
212
'77db39eca74c6205e31a7701e488a1df4b9b38a527a6084bdbb6843fd430a'
213
'0b51047378ee0255e633b32c0dda3cf43ab', 1024 * 1024 * 200, 1800,
214
1800, deploy=False, resource_file='/tmp/fake')]
215
self.assertEqual(mock_pr.mock_calls, calls)
220
agent_stream=None, agent_timeout=1800, agent_url=None,
221
bootstrap_host=None, debug=False, env='an-env', juju_bin='/bin/juju',
222
keep_env=False, large_test_enabled=False, logs='/tmp/logs', machine=[],
223
region=None, resource_timeout=1800, series=None,
224
temp_env_name='an-env-mod', upload_tools=False, verbose=10,
228
def make_resource_list(service_app_id='applicationId'):
229
return {'resources': [{
231
'origin': 'upload', 'used': True, 'description': 'foo resource.',
232
'username': 'admin@local', 'resourceid': 'dummy-resource/foo',
233
'name': 'foo', service_app_id: 'dummy-resource', 'size': 27,
234
'fingerprint': '1234', 'type': 'file', 'path': 'foo.txt'},
236
'origin': 'upload', 'username': 'admin@local', 'used': True,
237
'name': 'foo', 'resourceid': 'dummy-resource/foo',
238
service_app_id: 'dummy-resource', 'fingerprint': '1234',
239
'path': 'foo.txt', 'size': 27, 'type': 'file',
240
'description': 'foo resource.'}}]}