Esempio n. 1
0
def test_it_does_not_update_status_if_stats_fails(mock_deserializer,
                                                  mock_stats, mock_status,
                                                  mock_is_record):
    mock_deserializer.return_value = {
        "Id": "job123",
        "Sk": "123456",
        "Type": "JobEvent",
    }
    mock_stats.side_effect = ValueError
    mock_is_record.side_effect = [False, False, True]

    with pytest.raises(ValueError):
        handler(
            {
                "Records": [{
                    "eventName": "INSERT",
                    "dynamodb": {
                        "NewImage": {
                            "Id": {
                                "S": "job123"
                            },
                            "Sk": {
                                "S": "123456"
                            },
                            "Type": {
                                "S": "JobEvent"
                            },
                        }
                    },
                }]
            },
            SimpleNamespace(),
        )

    mock_status.assert_not_called()
Esempio n. 2
0
def test_it_handles_job_deletions(mock_deserializer, mock_cleanup,
                                  mock_is_record):
    mock_deserializer.return_value = {
        "Id": "job123",
        "Sk": "job123",
        "Type": "Job",
    }
    mock_is_record.side_effect = [False, True, False]
    handler(
        {
            "Records": [{
                "eventName": "REMOVE",
                "dynamodb": {
                    "OldImage": {
                        "Id": {
                            "S": "job123"
                        },
                        "Sk": {
                            "S": "job123"
                        },
                        "Type": {
                            "S": "Job"
                        },
                    }
                },
            }]
        },
        SimpleNamespace(),
    )

    assert 1 == mock_cleanup.call_count
    assert 1 == mock_deserializer.call_count
Esempio n. 3
0
def test_it_handles_job_records(mock_deserializer, mock_process,
                                mock_is_record):
    mock_deserializer.return_value = {
        "Id": "job123",
        "Sk": "job123",
        "Type": "Job",
    }
    mock_is_record.side_effect = [True, False, False]
    handler(
        {
            "Records": [{
                "eventName": "INSERT",
                "dynamodb": {
                    "NewImage": {
                        "Id": {
                            "S": "job123"
                        },
                        "Sk": {
                            "S": "job123"
                        },
                        "Type": {
                            "S": "Job"
                        },
                    }
                },
            }]
        },
        SimpleNamespace(),
    )

    assert 1 == mock_process.call_count
    assert 1 == mock_deserializer.call_count
Esempio n. 4
0
def test_it_does_not_emit_skipped_event_for_non_failures(
        mock_deserializer, mock_emit, mock_clear, mock_status, mock_is_record):
    mock_deserializer.return_value = {
        "Id": "job123",
        "Sk": "event123",
        "Type": "JobEvent",
        "EventName": "AnEvent",
    }
    statuses = [
        "RUNNING",
        "QUEUED",
        "COMPLETED",
        "COMPLETED_CLEANUP_FAILED",
    ]
    mock_is_record.side_effect = list(
        sum([(False, False, True) for _ in statuses], ()))
    mock_status.side_effect = [{
        "Id": "job123",
        "Sk": "event123",
        "Type": "JobEvent",
        "JobStatus": status,
    } for status in statuses]
    for _ in statuses:
        handler(
            {
                "Records": [{
                    "eventName": "INSERT",
                    "dynamodb": {
                        "NewImage": {
                            "Id": {
                                "S": "job123"
                            },
                            "Sk": {
                                "S": "event123"
                            },
                            "Type": {
                                "S": "JobEvent"
                            },
                            "EventName": {
                                "S": "ForgetPhaseEnded"
                            },
                        }
                    },
                }]
            },
            SimpleNamespace(),
        )

    for call in mock_emit.call_args_list:
        assert call[0][1] != "CleanupSkipped"
Esempio n. 5
0
def test_it_emits_skipped_event_for_failures(mock_deserializer, mock_emit,
                                             mock_clear, mock_status,
                                             mock_is_record):
    mock_deserializer.return_value = {
        "Id": "job123",
        "Sk": "event123",
        "Type": "JobEvent",
        "EventName": "AnEvent",
    }
    locked_statuses = [
        "FIND_FAILED",
        "FORGET_FAILED",
        "FAILED",
    ]
    mock_status.side_effect = [{
        "Id": "job123",
        "Sk": "event123",
        "Type": "JobEvent",
        "JobStatus": status,
    } for status in locked_statuses]
    mock_is_record.side_effect = list(
        sum([(False, False, True) for _ in locked_statuses], ()))
    for _ in locked_statuses:
        handler(
            {
                "Records": [{
                    "eventName": "INSERT",
                    "dynamodb": {
                        "NewImage": {
                            "Id": {
                                "S": "job123"
                            },
                            "Sk": {
                                "S": "event123"
                            },
                            "Type": {
                                "S": "JobEvent"
                            },
                            "EventName": {
                                "S": "ForgetPhaseEnded"
                            },
                        }
                    },
                }]
            },
            SimpleNamespace(),
        )

        mock_clear.assert_not_called()
        mock_emit.assert_called_with(ANY, "CleanupSkipped", ANY, ANY)
Esempio n. 6
0
def test_it_emits_event_for_cleanup_error(mock_deserializer, mock_emit,
                                          mock_clear, mock_status,
                                          mock_is_record):
    mock_is_record.side_effect = [False, False, True]
    mock_deserializer.return_value = {
        "Id": "job123",
        "Sk": "event123",
        "Type": "JobEvent",
        "EventName": "ForgetPhaseSucceeded",
    }
    mock_clear.side_effect = ClientError({}, "delete_item")
    mock_status.return_value = {
        "Id": "job123",
        "Sk": "event123",
        "Type": "JobEvent",
        "JobStatus": "FORGET_COMPLETED_CLEANUP_IN_PROGRESS",
    }
    handler(
        {
            "Records": [{
                "eventName": "INSERT",
                "dynamodb": {
                    "NewImage": {
                        "Id": {
                            "S": "job123"
                        },
                        "Sk": {
                            "S": "job123"
                        },
                        "Type": {
                            "S": "Job"
                        },
                        "EventName": {
                            "S": "ForgetPhaseComplete"
                        },
                    }
                },
            }]
        },
        SimpleNamespace(),
    )

    mock_clear.assert_called()
    mock_emit.assert_called_with(ANY, "CleanupFailed", ANY, ANY)
Esempio n. 7
0
def test_it_handles_job_event_records(mock_deserializer, mock_stats,
                                      mock_status, mock_is_record):
    mock_deserializer.return_value = {
        "Id": "job123",
        "Sk": "123456",
        "Type": "JobEvent",
    }
    mock_is_record.side_effect = [False, False, True]
    mock_status.return_value = {"JobStatus": "RUNNING"}
    mock_stats.return_value = {}

    handler(
        {
            "Records": [{
                "eventName": "INSERT",
                "dynamodb": {
                    "NewImage": {
                        "Id": {
                            "S": "job123"
                        },
                        "Sk": {
                            "S": "123456"
                        },
                        "Type": {
                            "S": "JobEvent"
                        },
                    }
                },
            }]
        },
        SimpleNamespace(),
    )
    mock_is_record.side_effect = [False, False, True]

    assert 1 == mock_status.call_count
    assert 1 == mock_stats.call_count
    assert 1 == mock_deserializer.call_count