Пример #1
0
def test_call_docker_executable_entrypoint_null():
    patch_sesh = patch("rpdk.core.contract.resource_client.create_sdk_session",
                       autospec=True)
    patch_creds = patch(
        "rpdk.core.contract.resource_client.get_temporary_credentials",
        autospec=True,
        return_value={},
    )
    patch_account = patch(
        "rpdk.core.contract.resource_client.get_account",
        autospec=True,
        return_value=ACCOUNT,
    )
    patch_docker = patch("rpdk.core.contract.resource_client.docker",
                         autospec=True)
    with patch_sesh as mock_create_sesh, patch_docker, patch_creds:
        with patch_account:
            mock_sesh = mock_create_sesh.return_value
            mock_sesh.region_name = DEFAULT_REGION
            resource_client = ResourceClient(
                DEFAULT_FUNCTION,
                "url",
                DEFAULT_REGION,
                {},
                EMPTY_OVERRIDE,
                docker_image="docker_image",
            )

    try:
        with patch_creds:
            resource_client.call("CREATE", {"resourceModel": SCHEMA})
    except InvalidProjectError:
        pass
Пример #2
0
def test_assert_failed_resource_models_set():
    with pytest.raises(AssertionError):
        ResourceClient.assert_failed(
            OperationStatus.FAILED,
            {
                "errorCode": HandlerErrorCode.AccessDenied.value,
                "resourceModels": []
            },
        )
def test_assert_failed_callback_context_set():
    with pytest.raises(AssertionError):
        ResourceClient.assert_failed(
            OperationStatus.FAILED,
            {
                "errorCode": HandlerErrorCode.AccessDenied.value,
                "callbackContext": []
            },
        )
Пример #4
0
def test_assert_failed_callback_delay_seconds_set():
    with pytest.raises(AssertionError):
        ResourceClient.assert_failed(
            OperationStatus.FAILED,
            {
                "errorCode": HandlerErrorCode.AccessDenied.value,
                "callbackDelaySeconds": 5,
            },
        )
Пример #5
0
def resource_client_inputs():
    endpoint = "https://"
    patch_sesh = patch(
        "rpdk.core.contract.resource_client.create_sdk_session", autospec=True
    )
    patch_creds = patch(
        "rpdk.core.contract.resource_client.get_temporary_credentials",
        autospec=True,
        return_value={},
    )
    with patch_sesh as mock_create_sesh, patch_creds as mock_creds:
        mock_sesh = mock_create_sesh.return_value
        mock_sesh.region_name = DEFAULT_REGION
        client = ResourceClient(
            DEFAULT_FUNCTION,
            endpoint,
            DEFAULT_REGION,
            {},
            EMPTY_OVERRIDE,
            {"CREATE": {"a": 1}, "UPDATE": {"a": 2}, "INVALID": {"b": 2}},
        )

    mock_sesh.client.assert_called_once_with("lambda", endpoint_url=endpoint)
    mock_creds.assert_called_once_with(mock_sesh, LOWER_CAMEL_CRED_KEYS, None)

    assert client._creds == {}
    assert client._function_name == DEFAULT_FUNCTION
    assert client._schema == {}
    assert client._overrides == EMPTY_OVERRIDE

    return client
def test_make_request(resource_type, log_group_name, log_creds):
    desired_resource_state = object()
    previous_resource_state = object()
    token = object()
    request = ResourceClient.make_request(
        desired_resource_state,
        previous_resource_state,
        "us-east-1",
        ACCOUNT,
        "CREATE",
        {},
        resource_type,
        log_group_name,
        log_creds,
        token,
    )
    expected_request = {
        "requestData": {
            "callerCredentials": {},
            "resourceProperties": desired_resource_state,
            "previousResourceProperties": previous_resource_state,
            "logicalResourceId": token,
        },
        "region": DEFAULT_REGION,
        "awsAccountId": ACCOUNT,
        "action": "CREATE",
        "bearerToken": token,
        "callbackContext": None,
        "resourceType": resource_type,
    }
    if log_group_name and log_creds:
        expected_request["requestData"]["providerCredentials"] = log_creds
        expected_request["requestData"][
            "providerLogGroupName"] = log_group_name
    assert request == expected_request
Пример #7
0
def resource_client():
    endpoint = "https://"
    patch_sesh = patch("rpdk.core.contract.resource_client.create_sdk_session",
                       autospec=True)
    patch_creds = patch(
        "rpdk.core.contract.resource_client.get_temporary_credentials",
        autospec=True,
        return_value={},
    )
    patch_account = patch(
        "rpdk.core.contract.resource_client.get_account",
        autospec=True,
        return_value=ACCOUNT,
    )
    with patch_sesh as mock_create_sesh, patch_creds as mock_creds:
        with patch_account as mock_account:
            mock_sesh = mock_create_sesh.return_value
            mock_sesh.region_name = DEFAULT_REGION
            client = ResourceClient(DEFAULT_FUNCTION, endpoint, DEFAULT_REGION,
                                    {}, EMPTY_OVERRIDE)

    mock_sesh.client.assert_called_once_with("lambda", endpoint_url=endpoint)
    mock_creds.assert_called_once_with(mock_sesh, LOWER_CAMEL_CRED_KEYS, None)
    mock_account.assert_called_once_with(mock_sesh, {})
    assert client._function_name == DEFAULT_FUNCTION
    assert client._schema == {}
    assert client._overrides == EMPTY_OVERRIDE
    assert client.account == ACCOUNT

    return client
Пример #8
0
def test_init_sam_cli_client():
    patch_sesh = patch("rpdk.core.contract.resource_client.create_sdk_session",
                       autospec=True)
    patch_creds = patch(
        "rpdk.core.contract.resource_client.get_temporary_credentials",
        autospec=True,
        return_value={},
    )
    patch_account = patch(
        "rpdk.core.contract.resource_client.get_account",
        autospec=True,
        return_value=ACCOUNT,
    )
    with patch_sesh as mock_create_sesh, patch_creds as mock_creds:
        with patch_account as mock_account:
            mock_sesh = mock_create_sesh.return_value
            mock_sesh.region_name = DEFAULT_REGION
            client = ResourceClient(DEFAULT_FUNCTION, DEFAULT_ENDPOINT,
                                    DEFAULT_REGION, {}, EMPTY_OVERRIDE)

    mock_sesh.client.assert_called_once_with("lambda",
                                             endpoint_url=DEFAULT_ENDPOINT,
                                             use_ssl=False,
                                             verify=False,
                                             config=ANY)
    mock_creds.assert_called_once_with(mock_sesh, LOWER_CAMEL_CRED_KEYS, None)
    mock_account.assert_called_once_with(mock_sesh, {})
    assert client.account == ACCOUNT
Пример #9
0
def test_make_request():
    desired_resource_state = object()
    previous_resource_state = object()
    token = object()
    request = ResourceClient.make_request(
        desired_resource_state,
        previous_resource_state,
        "us-east-1",
        ACCOUNT,
        "CREATE",
        {},
        token,
    )
    assert request == {
        "requestData": {
            "callerCredentials": {},
            "resourceProperties": desired_resource_state,
            "previousResourceProperties": previous_resource_state,
            "logicalResourceId": token,
        },
        "region": DEFAULT_REGION,
        "awsAccountId": ACCOUNT,
        "action": "CREATE",
        "bearerToken": token,
        "callbackContext": None,
    }
def test_make_request():
    desired_resource_state = object()
    previous_resource_state = object()
    token = object()
    request = ResourceClient.make_request(desired_resource_state,
                                          previous_resource_state,
                                          clientRequestToken=token)
    assert request == {
        "desiredResourceState": desired_resource_state,
        "previousResourceState": previous_resource_state,
        "logicalResourceIdentifier": None,
        "clientRequestToken": token,
    }
Пример #11
0
def test_call_docker():
    patch_sesh = patch("rpdk.core.contract.resource_client.create_sdk_session",
                       autospec=True)
    patch_creds = patch(
        "rpdk.core.contract.resource_client.get_temporary_credentials",
        autospec=True,
        return_value={},
    )
    patch_account = patch(
        "rpdk.core.contract.resource_client.get_account",
        autospec=True,
        return_value=ACCOUNT,
    )
    patch_docker = patch("rpdk.core.contract.resource_client.docker",
                         autospec=True)
    with patch_sesh as mock_create_sesh, patch_docker as mock_docker, patch_creds:
        with patch_account:
            mock_client = mock_docker.from_env.return_value
            mock_sesh = mock_create_sesh.return_value
            mock_sesh.region_name = DEFAULT_REGION
            resource_client = ResourceClient(
                DEFAULT_FUNCTION,
                "url",
                DEFAULT_REGION,
                {},
                EMPTY_OVERRIDE,
                docker_image="docker_image",
                executable_entrypoint="entrypoint",
            )
    response_str = ("__CFN_RESOURCE_START_RESPONSE__"
                    '{"status": "SUCCESS"}__CFN_RESOURCE_END_RESPONSE__')
    mock_client.containers.run.return_value = str.encode(response_str)
    with patch_creds:
        status, response = resource_client.call("CREATE",
                                                {"resourceModel": SCHEMA})

    mock_client.containers.run.assert_called_once()
    assert status == OperationStatus.SUCCESS
    assert response == {"status": OperationStatus.SUCCESS.value}
Пример #12
0
def resource_client_inputs_composite_key():
    endpoint = "https://"
    patch_sesh = patch("rpdk.core.contract.resource_client.create_sdk_session",
                       autospec=True)
    patch_creds = patch(
        "rpdk.core.contract.resource_client.get_temporary_credentials",
        autospec=True,
        return_value={},
    )
    patch_account = patch(
        "rpdk.core.contract.resource_client.get_account",
        autospec=True,
        return_value=ACCOUNT,
    )
    with patch_sesh as mock_create_sesh, patch_creds as mock_creds:
        with patch_account as mock_account:
            mock_sesh = mock_create_sesh.return_value
            mock_sesh.region_name = DEFAULT_REGION
            client = ResourceClient(
                DEFAULT_FUNCTION,
                endpoint,
                DEFAULT_REGION,
                SCHEMA_WITH_COMPOSITE_KEY,
                EMPTY_OVERRIDE,
                {
                    "CREATE": {
                        "a": 111,
                        "c": 2
                    },
                    "UPDATE": {
                        "a": 1,
                        "c": 2
                    },
                    "INVALID": {
                        "c": 3
                    },
                },
            )

    mock_sesh.client.assert_called_once_with("lambda", endpoint_url=endpoint)
    mock_creds.assert_called_once_with(mock_sesh, LOWER_CAMEL_CRED_KEYS, None)
    mock_account.assert_called_once_with(mock_sesh, {})

    assert client._function_name == DEFAULT_FUNCTION
    assert client._schema == SCHEMA_WITH_COMPOSITE_KEY
    assert client._overrides == EMPTY_OVERRIDE
    assert client.account == ACCOUNT

    return client
Пример #13
0
def test_make_request():
    desired_resource_state = object()
    previous_resource_state = object()
    token = object()
    request = ResourceClient.make_request(
        desired_resource_state,
        previous_resource_state,
        "us-west-2",
        "11111111",
        "aws",
        clientRequestToken=token,
    )
    assert request == {
        "desiredResourceState": desired_resource_state,
        "previousResourceState": previous_resource_state,
        "logicalResourceIdentifier": None,
        "clientRequestToken": token,
        "region": "us-west-2",
        "awsPartition": "aws",
        "awsAccountId": "11111111",
    }
Пример #14
0
def test_generate_token():
    token = ResourceClient.generate_token()
    assert isinstance(token, str)
    assert len(token) == 36
Пример #15
0
def test_assert_failed_error_code_invalid():
    with pytest.raises(KeyError):
        ResourceClient.assert_failed(OperationStatus.FAILED,
                                     {"errorCode": "XXX"})
Пример #16
0
def test_assert_in_progress_callback_delay_seconds_set():
    callback_delay_seconds = ResourceClient.assert_in_progress(
        OperationStatus.IN_PROGRESS, {"callbackDelaySeconds": 5})
    assert callback_delay_seconds == 5
Пример #17
0
def test_assert_in_progress_callback_delay_seconds_unset():
    callback_delay_seconds = ResourceClient.assert_in_progress(
        OperationStatus.IN_PROGRESS, {"resourceModels": None})
    assert callback_delay_seconds == 0
Пример #18
0
def test_assert_failed_returns_error_code():
    error_code = ResourceClient.assert_failed(
        OperationStatus.FAILED,
        {"errorCode": HandlerErrorCode.AccessDenied.value})
    assert error_code == HandlerErrorCode.AccessDenied
Пример #19
0
def test_assert_success_wrong_status(status):
    with pytest.raises(AssertionError):
        ResourceClient.assert_success(status, {})
Пример #20
0
def test_assert_success_error_code_set():
    with pytest.raises(AssertionError):
        ResourceClient.assert_success(
            OperationStatus.SUCCESS,
            {"errorCode": HandlerErrorCode.AccessDenied.value})
Пример #21
0
def test_assert_in_progress_resource_models_set():
    with pytest.raises(AssertionError):
        ResourceClient.assert_in_progress(OperationStatus.IN_PROGRESS,
                                          {"resourceModels": []})
Пример #22
0
def test_assert_success_callback_delay_seconds_set():
    with pytest.raises(AssertionError):
        ResourceClient.assert_success(OperationStatus.SUCCESS,
                                      {"callbackDelaySeconds": 5})
Пример #23
0
def test_assert_failed_error_code_unset():
    with pytest.raises(AssertionError):
        ResourceClient.assert_failed(OperationStatus.FAILED, {})
def test_assert_success_callback_context_set():
    with pytest.raises(AssertionError):
        ResourceClient.assert_success(OperationStatus.SUCCESS,
                                      {"callbackContext": []})
Пример #25
0
def test_assert_failed_wrong_status(status):
    with pytest.raises(AssertionError):
        ResourceClient.assert_failed(status, {})
Пример #26
0
def test_assert_in_progress_error_code_set():
    with pytest.raises(AssertionError):
        ResourceClient.assert_in_progress(
            OperationStatus.IN_PROGRESS,
            {"errorCode": HandlerErrorCode.AccessDenied.value},
        )