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 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326
|
# Copyright 2014 Amazon.com, Inc. or its affiliates. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the 'License'). You
# may not use this file except in compliance with the License. A copy of
# the License is located at
#
# https://aws.amazon.com/apache2.0/
#
# or in the 'license' file accompanying this file. This file 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.
import pytest
from boto3.resources.action import BatchAction, ServiceAction, WaiterAction
from boto3.resources.base import ResourceMeta
from boto3.resources.model import Action, Waiter
from boto3.utils import ServiceContext
from tests import BaseTestCase, mock
class TestServiceActionCall(BaseTestCase):
def setUp(self):
super().setUp()
self.action_def = {'request': {'operation': 'GetFrobs', 'params': []}}
@property
def action(self):
return Action('test', self.action_def, {})
@mock.patch(
'boto3.resources.action.create_request_parameters', return_value={}
)
def test_service_action_creates_params(self, params_mock):
resource = mock.Mock()
resource.meta = ResourceMeta('test', client=mock.Mock())
action = ServiceAction(self.action)
action(resource, foo=1)
assert params_mock.called
@mock.patch(
'boto3.resources.action.create_request_parameters',
return_value={'bar': 'baz'},
)
def test_service_action_calls_operation(self, params_mock):
resource = mock.Mock()
resource.meta = ResourceMeta('test', client=mock.Mock())
operation = resource.meta.client.get_frobs
operation.return_value = 'response'
action = ServiceAction(self.action)
response = action(resource, foo=1)
operation.assert_called_with(foo=1, bar='baz')
assert response == 'response'
@mock.patch(
'boto3.resources.action.create_request_parameters', return_value={}
)
@mock.patch('boto3.resources.action.RawHandler')
def test_service_action_calls_raw_handler(self, handler_mock, params_mock):
resource = mock.Mock()
resource.meta = ResourceMeta('test', client=mock.Mock())
operation = resource.meta.client.get_frobs
operation.return_value = 'response'
action = ServiceAction(self.action)
handler_mock.return_value.return_value = 'response'
action(resource)
handler_mock.assert_called_with(None)
handler_mock.return_value.assert_called_with(resource, {}, 'response')
@mock.patch(
'boto3.resources.action.create_request_parameters', return_value={}
)
@mock.patch('boto3.resources.action.ResourceHandler')
def test_service_action_calls_resource_handler(
self, handler_mock, params_mock
):
self.action_def['resource'] = {'type': 'Frob', 'path': 'Container'}
resource = mock.Mock()
resource.meta = ResourceMeta('test', client=mock.Mock())
operation = resource.meta.client.get_frobs
operation.return_value = 'response'
factory = mock.Mock()
resource_defs = {}
service_model = mock.Mock()
action_model = self.action
service_context = ServiceContext(
service_name='test',
service_model=service_model,
resource_json_definitions=resource_defs,
service_waiter_model=None,
)
action = ServiceAction(
action_model=action_model,
factory=factory,
service_context=service_context,
)
handler_mock.return_value.return_value = 'response'
action(resource)
handler_mock.assert_called_with(
search_path='Container',
factory=factory,
resource_model=action_model.resource,
service_context=service_context,
operation_name='GetFrobs',
)
def test_service_action_call_positional_argument(self):
def _api_call(*args, **kwargs):
if args:
raise TypeError(
"%s() only accepts keyword arguments." % 'get_frobs'
)
resource = mock.Mock()
resource.meta = ResourceMeta('test', client=mock.Mock())
resource.meta.client.get_frobs = _api_call
action = ServiceAction(self.action)
with pytest.raises(TypeError):
action(resource, 'item1')
class TestWaiterActionCall(BaseTestCase):
def setUp(self):
super().setUp()
self.waiter_resource_name = 'wait_until_exists'
self.waiter_def = {
"waiterName": "FrobExists",
"params": [
{
"target": "Frob",
"sourceType": "identifier",
"source": "Name",
}
],
}
@property
def waiter(self):
return Waiter('test', self.waiter_def)
@mock.patch(
'boto3.resources.action.create_request_parameters', return_value={}
)
def test_service_waiter_creates_params(self, params_mock):
resource = mock.Mock()
resource.meta = ResourceMeta('test', client=mock.Mock())
action = WaiterAction(self.waiter, self.waiter_resource_name)
action(resource, foo=1)
assert params_mock.called
@mock.patch(
'boto3.resources.action.create_request_parameters',
return_value={'bar': 'baz'},
)
def test_service_action_calls_operation(self, params_mock):
resource = mock.Mock()
resource.meta = ResourceMeta('test', client=mock.Mock())
get_waiter = resource.meta.client.get_waiter
mock_waiter = mock.Mock()
get_waiter.return_value = mock_waiter
action = WaiterAction(self.waiter, self.waiter_resource_name)
action(resource, foo=1)
get_waiter.assert_called_with('frob_exists')
mock_waiter.wait.assert_called_with(foo=1, bar='baz')
class TestBatchActionCall(BaseTestCase):
def setUp(self):
super().setUp()
self.action_def = {'request': {'operation': 'GetFrobs', 'params': []}}
@property
def model(self):
return Action('test', self.action_def, {})
def test_batch_action_gets_pages_from_collection(self):
collection = mock.Mock()
collection.pages.return_value = []
action = BatchAction(self.model)
action(collection)
collection.pages.assert_called_with()
def test_batch_action_creates_parameters_from_items(self):
self.action_def['request']['params'] = [
{'target': 'Bucket', 'source': 'data', 'path': 'BucketName'},
{
'target': 'Delete.Objects[].Key',
'source': 'data',
'path': 'Key',
},
]
client = mock.Mock()
item1 = mock.Mock()
item1.meta = ResourceMeta(
'test',
client=client,
data={'BucketName': 'bucket', 'Key': 'item1'},
)
item2 = mock.Mock()
item2.meta = ResourceMeta(
'test',
client=client,
data={'BucketName': 'bucket', 'Key': 'item2'},
)
collection = mock.Mock()
collection.pages.return_value = [[item1, item2]]
action = BatchAction(self.model)
action(collection)
client.get_frobs.assert_called_with(
Bucket='bucket',
Delete={'Objects': [{'Key': 'item1'}, {'Key': 'item2'}]},
)
@mock.patch(
'boto3.resources.action.create_request_parameters', return_value={}
)
def test_batch_action_skips_operation(self, crp_mock):
# In this test we have an item from the collection, but no
# parameters are set up. Because of this, we do NOT call
# the batch operation.
client = mock.Mock()
item = mock.Mock()
item.meta = ResourceMeta('test', client=client)
collection = mock.Mock()
collection.pages.return_value = [[item]]
model = self.model
action = BatchAction(model)
action(collection)
crp_mock.assert_called_with(item, model.request, params={}, index=0)
client.get_frobs.assert_not_called()
@mock.patch('boto3.resources.action.create_request_parameters')
def test_batch_action_calls_operation(self, crp_mock):
# In this test we have an item and parameters, so the call
# to the batch operation should be made.
def side_effect(resource, model, params=None, index=None):
params['foo'] = 'bar'
crp_mock.side_effect = side_effect
client = mock.Mock()
item = mock.Mock()
item.meta = ResourceMeta('test', client=client)
collection = mock.Mock()
collection.pages.return_value = [[item]]
model = self.model
action = BatchAction(model)
action(collection)
# Here the call is made with params={}, but they are edited
# in-place so we need to compare to the final edited value.
crp_mock.assert_called_with(
item, model.request, params={'foo': 'bar'}, index=0
)
client.get_frobs.assert_called_with(foo='bar')
@mock.patch('boto3.resources.action.create_request_parameters')
def test_batch_action_with_positional_argument(self, crp_mock):
def side_effect(resource, model, params=None, index=None):
params['foo'] = 'bar'
def _api_call(*args, **kwargs):
if args:
raise TypeError(
"%s() only accepts keyword arguments." % 'get_frobs'
)
crp_mock.side_effect = side_effect
client = mock.Mock()
client.get_frobs = _api_call
item = mock.Mock()
item.meta = ResourceMeta('test', client=client)
collection = mock.Mock()
collection.pages.return_value = [[item]]
model = self.model
action = BatchAction(model)
with pytest.raises(TypeError):
action(collection, 'item1')
|