Несколько вызовов botocore.stub.Stubber вызывает исключение - PullRequest
0 голосов
/ 12 июня 2018

Я использую botocore.stub.Stubber, чтобы высмеивать kmsclient.Код, который я использую:

with botocore.stub.Stubber(s3) as stubber:
        with botocore.stub.Stubber(kms) as stubber2:
            stubber.add_response('copy_object', response, expectedParams)
            stubber.activate()
            stubber2.add_response('decrypt', response2, expectedParams2)
            stubber2.activate()
            handleCore(__makeValidEvent(), None, s3, kms)
            stubber.assert_no_pending_responses()
            stubber2.assert_no_pending_responses()

При фактической реализации вызов kmsclient происходит дважды, что приводит к следующему исключению

params = {'CiphertextBlob': b"\x01\x02\x02\x00x#\xc1\xdbp6\xe1Y\x0fS\x15\x80<\x86\xb5\xb2\x86\x9f\xaf\xa2Z\x07\xfef\x8d\xb2\xd7...\t'\xe2\xb9\x10w0\x83\xcb\xe1\xcb`\xd1\xc2\x8c\xe4\x82Q/*\xb3]\xcfZ\xb9\xbd\x1c\x9c\x96(e\x94j\x1a\x91\xba\xaeO[>\x97"}

    def _assert_expected_call_order(self, model, params):
        if not self._queue:
            raise UnStubbedResponseError(
                operation_name=model.name,
                reason=(
>                       'Unexpected API Call: A call was made but no additional calls expected.'
                        'Either the API Call was not stubbed or it was called multiple times.'
                        )
            )
E           botocore.exceptions.UnStubbedResponseError: Error getting response stub for operation Decrypt: Unexpected API Call: A call was made but no additional calls expected.Either the API Call was not stubbed or it was called multiple times.

Может кто-нибудь сообщить мне, как его можно использоватьдля нескольких вызовов одного и того же объекта (в данном случае kmsclient)

1 Ответ

0 голосов
/ 09 апреля 2019

Я решил эту проблему, добавив несколько ответов для одного и того же метода.

Пример тестового кода:

############## Test Code
client = botocore.session.get_session().create_client("cloudformation", region_name="us-east-1")
stubber = Stubber(client)

# Generate multiple responses to be returned by boto
responses = [
    {
        "StackId": "stack-a",
    },
    {
        "StackId": "stack-b",
    },
    {
        "StackId": "stack-c",
    },
]

# Add each response to stubber for the same method - "update_termination_protection"
for response in responses:
    stubber.add_response(
        "update_termination_protection",
        response,
    )
stubber.activate()
actual = method_to_test(client, data)
stubber.deactivate()
assert actual == True

Пример метода для тестирования:

############## Real method
def method_to_test(self, client, data):
    for item in data:
        client.update_termination_protection(
           EnableTerminationProtection=True,
           StackName=item,
        )
    return True

Это работаети не выдает никаких исключений.

...