-
Notifications
You must be signed in to change notification settings - Fork 290
Expand file tree
/
Copy path_awsbraket_boto3_client_test.py
More file actions
286 lines (229 loc) · 10.3 KB
/
_awsbraket_boto3_client_test.py
File metadata and controls
286 lines (229 loc) · 10.3 KB
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
# Copyright 2021 ProjectQ-Framework (www.projectq.ch)
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Test for projectq.backends._awsbraket._awsbraket_boto3_client.py"""
import pytest
from ._awsbraket_boto3_client_test_fixtures import * # noqa: F401,F403
# ==============================================================================
_has_boto3 = True
try:
import botocore
from projectq.backends._awsbraket import _awsbraket_boto3_client
except ImportError:
_has_boto3 = False
has_boto3 = pytest.mark.skipif(not _has_boto3, reason="boto3 package is not installed")
# ==============================================================================
@has_boto3
def test_show_devices(mocker, show_devices_setup):
creds, search_value, device_value, devicelist_result = show_devices_setup
mock_boto3_client = mocker.MagicMock(spec=['search_devices', 'get_device'])
mock_boto3_client.search_devices.return_value = search_value
mock_boto3_client.get_device.return_value = device_value
mocker.patch('boto3.client', return_value=mock_boto3_client, autospec=True)
devicelist = _awsbraket_boto3_client.show_devices(credentials=creds)
assert devicelist == devicelist_result
# ==============================================================================
completed_value = {
'deviceArn': 'arndevice',
'deviceParameters': 'parameters',
'failureReason': 'None',
'outputS3Bucket': 'amazon-braket-bucket',
'outputS3Directory': 'complete/directory',
'quantumTaskArn': 'arntask',
'shots': 123,
'status': 'COMPLETED',
'tags': {'tagkey': 'tagvalue'},
}
failed_value = {
'failureReason': 'This is a failure reason',
'outputS3Bucket': 'amazon-braket-bucket',
'outputS3Directory': 'complete/directory',
'status': 'FAILED',
}
cancelling_value = {
'failureReason': 'None',
'outputS3Bucket': 'amazon-braket-bucket',
'outputS3Directory': 'complete/directory',
'status': 'CANCELLING',
}
other_value = {
'failureReason': 'None',
'outputS3Bucket': 'amazon-braket-bucket',
'outputS3Directory': 'complete/directory',
'status': 'OTHER',
}
# ------------------------------------------------------------------------------
@has_boto3
@pytest.mark.parametrize(
"var_status, var_result",
[
('completed', completed_value),
('failed', failed_value),
('cancelling', cancelling_value),
('other', other_value),
],
)
def test_retrieve(mocker, var_status, var_result, retrieve_setup):
arntask, creds, device_value, res_completed, results_dict = retrieve_setup
mock_boto3_client = mocker.MagicMock(spec=['get_quantum_task', 'get_device', 'get_object'])
mock_boto3_client.get_quantum_task.return_value = var_result
mock_boto3_client.get_device.return_value = device_value
mock_boto3_client.get_object.return_value = results_dict
mocker.patch('boto3.client', return_value=mock_boto3_client, autospec=True)
if var_status == 'completed':
res = _awsbraket_boto3_client.retrieve(credentials=creds, task_arn=arntask)
assert res == res_completed
else:
with pytest.raises(Exception) as exinfo:
_awsbraket_boto3_client.retrieve(credentials=creds, task_arn=arntask, num_retries=2)
print(exinfo.value)
if var_status == 'failed':
assert str(exinfo.value) == "Error while running the code: FAILED. \
The failure reason was: This is a failure reason."
if var_status == 'cancelling':
assert str(exinfo.value) == "The job received a CANCEL operation: CANCELLING."
if var_status == 'other':
assert str(exinfo.value) == "Timeout. The Arn of your submitted job \
is arn:aws:braket:us-east-1:id:taskuuid \
and the status of the job is OTHER."
# ==============================================================================
@has_boto3
def test_retrieve_devicetypes(mocker, retrieve_devicetypes_setup):
(
arntask,
creds,
device_value,
results_dict,
res_completed,
) = retrieve_devicetypes_setup
mock_boto3_client = mocker.MagicMock(spec=['get_quantum_task', 'get_device', 'get_object'])
mock_boto3_client.get_quantum_task.return_value = completed_value
mock_boto3_client.get_device.return_value = device_value
mock_boto3_client.get_object.return_value = results_dict
mocker.patch('boto3.client', return_value=mock_boto3_client, autospec=True)
res = _awsbraket_boto3_client.retrieve(credentials=creds, task_arn=arntask)
assert res == res_completed
# ==============================================================================
@has_boto3
def test_send_too_many_qubits(mocker, send_too_many_setup):
creds, s3_folder, search_value, device_value, info_too_much = send_too_many_setup
mock_boto3_client = mocker.MagicMock(spec=['search_devices', 'get_device'])
mock_boto3_client.search_devices.return_value = search_value
mock_boto3_client.get_device.return_value = device_value
mocker.patch('boto3.client', return_value=mock_boto3_client, autospec=True)
with pytest.raises(_awsbraket_boto3_client.DeviceTooSmall):
_awsbraket_boto3_client.send(info_too_much, device='name2', credentials=creds, s3_folder=s3_folder)
# ==============================================================================
@has_boto3
@pytest.mark.parametrize(
"var_status, var_result",
[
('completed', completed_value),
('failed', failed_value),
('cancelling', cancelling_value),
('other', other_value),
],
)
def test_send_real_device_online_verbose(mocker, var_status, var_result, real_device_online_setup):
(
qtarntask,
creds,
s3_folder,
info,
search_value,
device_value,
res_completed,
results_dict,
) = real_device_online_setup
mock_boto3_client = mocker.MagicMock(
spec=['search_devices', 'get_device', 'create_quantum_task', 'get_quantum_task', 'get_object']
)
mock_boto3_client.search_devices.return_value = search_value
mock_boto3_client.get_device.return_value = device_value
mock_boto3_client.create_quantum_task.return_value = qtarntask
mock_boto3_client.get_quantum_task.return_value = var_result
mock_boto3_client.get_object.return_value = results_dict
mocker.patch('boto3.client', return_value=mock_boto3_client, autospec=True)
# This is a ficticios situation because the job will be always queued
# at the beginning. After that the status will change at some point in time
# If the status change while the _get_result loop with num_retries, is
# active the result will change. We mock this using some preconfigured
# statuses in var_status for the tests
if var_status == 'completed':
res = _awsbraket_boto3_client.send(info, device='name2', credentials=creds, s3_folder=s3_folder, verbose=True)
assert res == res_completed
else:
with pytest.raises(Exception) as exinfo:
_awsbraket_boto3_client.send(
info,
device='name2',
credentials=creds,
s3_folder=s3_folder,
verbose=True,
num_retries=2,
)
print(exinfo.value)
if var_status == 'failed':
assert str(exinfo.value) == "Error while running the code: FAILED. The failure \
reason was: This is a failure reason."
if var_status == 'cancelling':
assert str(exinfo.value) == "The job received a CANCEL operation: CANCELLING."
if var_status == 'other':
assert str(exinfo.value) == "Timeout. The Arn of your submitted job \
is arn:aws:braket:us-east-1:id:taskuuid \
and the status of the job is OTHER."
# ==============================================================================
@has_boto3
@pytest.mark.parametrize(
"var_error",
[
('AccessDeniedException'),
('DeviceOfflineException'),
('InternalServiceException'),
('ServiceQuotaExceededException'),
('ValidationException'),
],
)
def test_send_that_errors_are_caught(mocker, var_error, send_that_error_setup):
creds, s3_folder, info, search_value, device_value = send_that_error_setup
mock_boto3_client = mocker.MagicMock(spec=['search_devices', 'get_device', 'create_quantum_task'])
mock_boto3_client.search_devices.return_value = search_value
mock_boto3_client.get_device.return_value = device_value
mock_boto3_client.create_quantum_task.side_effect = botocore.exceptions.ClientError(
{"Error": {"Code": var_error, "Message": f"Msg error for {var_error}"}},
"create_quantum_task",
)
mocker.patch('boto3.client', return_value=mock_boto3_client, autospec=True)
with pytest.raises(botocore.exceptions.ClientError):
_awsbraket_boto3_client.send(info, device='name2', credentials=creds, s3_folder=s3_folder, num_retries=2)
with pytest.raises(_awsbraket_boto3_client.DeviceOfflineError):
_awsbraket_boto3_client.send(
info,
device='unknown',
credentials=creds,
s3_folder=s3_folder,
num_retries=2,
)
# ==============================================================================
@has_boto3
@pytest.mark.parametrize("var_error", ['ResourceNotFoundException'])
def test_retrieve_error_arn_not_exist(mocker, var_error, arntask, creds):
mock_boto3_client = mocker.MagicMock(spec=['get_quantum_task'])
mock_boto3_client.get_quantum_task.side_effect = botocore.exceptions.ClientError(
{"Error": {"Code": var_error, "Message": f"Msg error for {var_error}"}},
"get_quantum_task",
)
mocker.patch('boto3.client', return_value=mock_boto3_client, autospec=True)
with pytest.raises(botocore.exceptions.ClientError):
_awsbraket_boto3_client.retrieve(credentials=creds, task_arn=arntask)
# ==============================================================================