def test_simulate_healthcheck_on_service_enabled_during_grace_period(
    mock_run_healthcheck_on_container,
    mock_time,
    mock_sleep
):
    # prevent grace period from ending
    mock_time.side_effect = [0, 0]
    mock_docker_client = mock.MagicMock(spec_set=docker.Client)
    mock_service_manifest = MarathonServiceConfig(
        service='fake_name',
        cluster='fake_cluster',
        instance='fake_instance',
        config_dict={
            'healthcheck_grace_period_seconds': 1,
        },
        branch_dict={},
    )

    fake_container_id = 'fake_container_id'
    fake_mode = 'http'
    fake_url = 'http://fake_host/fake_status_path'
    assert simulate_healthcheck_on_service(
        mock_service_manifest, mock_docker_client, fake_container_id, fake_mode, fake_url, True)
    assert mock_sleep.call_count == 0
    # First run_healthcheck_on_container call happens silently
    assert mock_run_healthcheck_on_container.call_count == 2
def test_simulate_healthcheck_on_service_enabled_honors_grace_period(
    mock_run_healthcheck_on_container,
    mock_time,
    mock_sleep
):
    # change time to make sure we are sufficiently past grace period
    mock_time.side_effect = [0, 2]
    mock_docker_client = mock.MagicMock(spec_set=docker.Client)
    mock_service_manifest = MarathonServiceConfig(
        service='fake_name',
        cluster='fake_cluster',
        instance='fake_instance',
        config_dict={
            # only one healthcheck will be performed silently
            'healthcheck_grace_period_seconds': 1,
        },
        branch_dict={},
    )

    fake_container_id = 'fake_container_id'
    fake_mode = 'http'
    fake_url = 'http://fake_host/fake_status_path'
    assert simulate_healthcheck_on_service(
        mock_service_manifest, mock_docker_client, fake_container_id, fake_mode, fake_url, True)
    assert mock_sleep.call_count == 0
    assert mock_run_healthcheck_on_container.call_count == 2
Beispiel #3
0
def test_simulate_healthcheck_on_service_enabled_during_grace_period(
    mock_run_healthcheck_on_container,
    mock_time,
    mock_sleep
):
    # prevent grace period from ending
    mock_time.side_effect = [0, 0]
    mock_docker_client = mock.MagicMock(spec_set=docker.Client)
    mock_service_manifest = MarathonServiceConfig(
        service='fake_name',
        cluster='fake_cluster',
        instance='fake_instance',
        config_dict={
            'healthcheck_grace_period_seconds': 1,
        },
        branch_dict={},
    )

    fake_container_id = 'fake_container_id'
    fake_mode = 'http'
    fake_url = 'http://fake_host/fake_status_path'
    assert simulate_healthcheck_on_service(
        mock_service_manifest, mock_docker_client, fake_container_id, fake_mode, fake_url, True)
    assert mock_sleep.call_count == 0
    assert mock_run_healthcheck_on_container.call_count == 1
def test_simulate_healthcheck_on_service_enabled_honors_grace_period(
    mock_run_healthcheck_on_container,
    mock_time,
    mock_sleep,
    capsys,
):
    # change time to make sure we are sufficiently past grace period
    mock_run_healthcheck_on_container.side_effect = iter([
        (False, "noop"), (False, "noop"), (True, "noop")])

    mock_time.side_effect = [0, 1, 5]
    mock_docker_client = mock.MagicMock(spec_set=docker.Client)
    mock_service_manifest = MarathonServiceConfig(
        service='fake_name',
        cluster='fake_cluster',
        instance='fake_instance',
        config_dict={
            # only one healthcheck will be performed silently
            'healthcheck_grace_period_seconds': 2,
        },
        branch_dict={},
    )

    fake_container_id = 'fake_container_id'
    fake_mode = 'http'
    fake_url = 'http://fake_host/fake_status_path'
    assert simulate_healthcheck_on_service(
        mock_service_manifest, mock_docker_client, fake_container_id, fake_mode, fake_url, True)
    assert mock_sleep.call_count == 2
    assert mock_run_healthcheck_on_container.call_count == 3
    out, _ = capsys.readouterr()
    assert out.count('Healthcheck failed! (disregarded due to grace period)') == 1
    assert out.count('Healthcheck failed! (Attempt') == 1
    assert out.count('Healthcheck succeeded!') == 1
Beispiel #5
0
def test_simulate_healthcheck_on_service_enabled_honors_grace_period(
    mock_run_healthcheck_on_container,
    mock_time,
    mock_sleep,
    capsys,
):
    # change time to make sure we are sufficiently past grace period
    mock_time.side_effect = [0, 1, 5]
    mock_docker_client = mock.MagicMock(spec_set=docker.Client)
    mock_service_manifest = MarathonServiceConfig(
        service='fake_name',
        cluster='fake_cluster',
        instance='fake_instance',
        config_dict={
            # only one healthcheck will be performed silently
            'healthcheck_grace_period_seconds': 2,
        },
        branch_dict={},
    )

    fake_container_id = 'fake_container_id'
    fake_mode = 'http'
    fake_url = 'http://fake_host/fake_status_path'
    assert simulate_healthcheck_on_service(
        mock_service_manifest, mock_docker_client, fake_container_id, fake_mode, fake_url, True)
    assert mock_sleep.call_count == 2
    assert mock_run_healthcheck_on_container.call_count == 3
    out, _ = capsys.readouterr()
    assert out.count('Healthcheck failed! (disregarded due to grace period)') == 1
    assert out.count('Healthcheck failed! (Attempt') == 1
    assert out.count('Healthcheck succeeded!') == 1
Beispiel #6
0
def test_simulate_healthcheck_on_service_dead_container_exits_immediately(
        capsys):
    with mock.patch(
            'time.sleep',
            side_effect=AssertionError('sleep should not have been called'),
    ):
        mock_client = mock.MagicMock(spec_set=docker.Client)
        mock_client.inspect_container.return_value = {
            'State': {
                'Running': False,
                'ExitCode': 127
            },
        }
        fake_service_manifest = MarathonServiceConfig(
            service='fake_name',
            cluster='fake_cluster',
            instance='fake_instance',
            config_dict={},
            branch_dict={},
        )
        ret = simulate_healthcheck_on_service(
            fake_service_manifest,
            mock_client,
            mock.sentinel.container_id,
            'http',
            'http://fake_host/status',
            True,
        )
        assert ret == (False, 'Aborted by the user')
        out, _ = capsys.readouterr()
        assert out.count('Container exited with code 127') == 1
Beispiel #7
0
def test_simulate_healthcheck_on_service_enabled_partial_failure(
        mock_run_healthcheck_on_container, mock_sleep):
    mock_run_healthcheck_on_container.side_effect = iter([(False, ""),
                                                          (False, ""),
                                                          (False, ""),
                                                          (False, ""),
                                                          (True, "")])
    mock_docker_client = mock.MagicMock(spec_set=docker.Client)
    mock_service_manifest = MarathonServiceConfig(
        service='fake_name',
        cluster='fake_cluster',
        instance='fake_instance',
        config_dict={
            'healthcheck_grace_period_seconds': 0,
        },
        branch_dict={},
    )

    fake_container_id = 'fake_container_id'
    fake_mode = 'http'
    fake_url = 'http://fake_host/fake_status_path'
    assert simulate_healthcheck_on_service(mock_service_manifest,
                                           mock_docker_client,
                                           fake_container_id, fake_mode,
                                           fake_url, True)
    # First run_healthcheck_on_container call happens silently
    assert mock_run_healthcheck_on_container.call_count == 5
    assert mock_sleep.call_count == 4
Beispiel #8
0
def test_simulate_healthcheck_on_service_enabled_honors_grace_period(
        mock_run_healthcheck_on_container, mock_time, mock_sleep):
    # change time to make sure we are sufficiently past grace period
    mock_time.side_effect = [0, 2]
    mock_docker_client = mock.MagicMock(spec_set=docker.Client)
    mock_service_manifest = MarathonServiceConfig(
        service='fake_name',
        cluster='fake_cluster',
        instance='fake_instance',
        config_dict={
            # only one healthcheck will be performed silently
            'healthcheck_grace_period_seconds': 1,
        },
        branch_dict={},
    )

    fake_container_id = 'fake_container_id'
    fake_mode = 'http'
    fake_url = 'http://fake_host/fake_status_path'
    assert simulate_healthcheck_on_service(mock_service_manifest,
                                           mock_docker_client,
                                           fake_container_id, fake_mode,
                                           fake_url, True)
    assert mock_sleep.call_count == 0
    assert mock_run_healthcheck_on_container.call_count == 2
Beispiel #9
0
def test_simulate_healthcheck_on_service_disabled(mock_sleep):
    mock_docker_client = mock.MagicMock(spec_set=docker.Client)
    mock_service_manifest = mock.MagicMock(spec_set=MarathonServiceConfig)
    fake_container_id = 'fake_container_id'
    fake_mode = 'http'
    fake_url = 'http://fake_host/fake_status_path'
    assert simulate_healthcheck_on_service(
        mock_service_manifest, mock_docker_client, fake_container_id, fake_mode, fake_url, False)
Beispiel #10
0
def test_simulate_healthcheck_on_service_disabled(mock_sleep):
    mock_docker_client = mock.MagicMock(spec_set=docker.Client)
    mock_service_manifest = mock.MagicMock(spec_set=MarathonServiceConfig)
    fake_container_id = 'fake_container_id'
    fake_mode = 'http'
    fake_url = 'http://fake_host/fake_status_path'
    assert simulate_healthcheck_on_service(
        mock_service_manifest, mock_docker_client, fake_container_id, fake_mode, fake_url, False)
Beispiel #11
0
def test_simulate_healthcheck_on_service_enabled_success(mock_run_healthcheck_on_container, mock_sleep):
    mock_docker_client = mock.MagicMock(spec_set=docker.Client)
    mock_service_manifest = MarathonServiceConfig('fake_name', 'fake_instance', {
        'healthcheck_grace_period_seconds': 0,
    }, {})
    fake_container_id = 'fake_container_id'
    fake_mode = 'http'
    fake_url = 'http://fake_host/fake_status_path'
    mock_run_healthcheck_on_container.return_value = True
    assert simulate_healthcheck_on_service(
        mock_service_manifest, mock_docker_client, fake_container_id, fake_mode, fake_url, True)
Beispiel #12
0
def test_simulate_healthcheck_on_service_enabled_partial_failure(mock_run_healthcheck_on_container, mock_sleep):
    mock_docker_client = mock.MagicMock(spec_set=docker.Client)
    mock_service_manifest = MarathonServiceConfig('fake_name', 'fake_instance', {
        'healthcheck_grace_period_seconds': 0,
    }, {})

    fake_container_id = 'fake_container_id'
    fake_mode = 'http'
    fake_url = 'http://fake_host/fake_status_path'
    assert simulate_healthcheck_on_service(
        mock_service_manifest, mock_docker_client, fake_container_id, fake_mode, fake_url, True)
    # First run_healthcheck_on_container call happens silently
    assert mock_run_healthcheck_on_container.call_count == 5
    assert mock_sleep.call_count == 3
Beispiel #13
0
def test_simulate_healthcheck_on_service_enabled_success(mock_run_healthcheck_on_container, mock_sleep):
    mock_docker_client = mock.MagicMock(spec_set=docker.Client)
    mock_service_manifest = MarathonServiceConfig(
        service='fake_name',
        cluster='fake_cluster',
        instance='fake_instance',
        config_dict={
            'healthcheck_grace_period_seconds': 0,
        },
        branch_dict={},
    )
    fake_container_id = 'fake_container_id'
    fake_mode = 'http'
    fake_url = 'http://fake_host/fake_status_path'
    mock_run_healthcheck_on_container.return_value = True
    assert simulate_healthcheck_on_service(
        mock_service_manifest, mock_docker_client, fake_container_id, fake_mode, fake_url, True)
def test_simulate_healthcheck_on_service_enabled_failure(mock_run_healthcheck_on_container, mock_sleep):
    mock_docker_client = mock.MagicMock(spec_set=docker.Client)
    mock_service_manifest = MarathonServiceConfig(
        service='fake_name',
        cluster='fake_cluster',
        instance='fake_instance',
        config_dict={
            'healthcheck_grace_period_seconds': 0,
        },
        branch_dict={},
    )
    mock_service_manifest

    fake_container_id = 'fake_container_id'
    fake_mode = 'http'
    fake_url = 'http://fake_host/fake_status_path'
    mock_run_healthcheck_on_container.return_value = False
    assert not simulate_healthcheck_on_service(
        mock_service_manifest, mock_docker_client, fake_container_id, fake_mode, fake_url, True)
def test_simulate_healthcheck_on_service_enabled_partial_failure(mock_run_healthcheck_on_container, mock_sleep):
    mock_run_healthcheck_on_container.side_effect = iter([
        (False, ""), (False, ""), (False, ""), (False, ""), (True, "")])
    mock_docker_client = mock.MagicMock(spec_set=docker.Client)
    mock_service_manifest = MarathonServiceConfig(
        service='fake_name',
        cluster='fake_cluster',
        instance='fake_instance',
        config_dict={
            'healthcheck_grace_period_seconds': 0,
        },
        branch_dict={},
    )

    fake_container_id = 'fake_container_id'
    fake_mode = 'http'
    fake_url = 'http://fake_host/fake_status_path'
    assert simulate_healthcheck_on_service(
        mock_service_manifest, mock_docker_client, fake_container_id, fake_mode, fake_url, True)
    # First run_healthcheck_on_container call happens silently
    assert mock_run_healthcheck_on_container.call_count == 5
    assert mock_sleep.call_count == 4
def test_simulate_healthcheck_on_service_dead_container_exits_immediately(capsys):
    with mock.patch(
            'time.sleep',
            side_effect=AssertionError('sleep should not have been called'),
    ):
        mock_client = mock.MagicMock(spec_set=docker.Client)
        mock_client.inspect_container.return_value = {
            'State': {'Running': False, 'ExitCode': 127},
        }
        fake_service_manifest = MarathonServiceConfig(
            service='fake_name',
            cluster='fake_cluster',
            instance='fake_instance',
            config_dict={},
            branch_dict={},
        )
        ret = simulate_healthcheck_on_service(
            fake_service_manifest, mock_client, mock.sentinel.container_id,
            'http', 'http://fake_host/status', True,
        )
        assert ret == (False, 'Aborted by the user')
        out, _ = capsys.readouterr()
        assert out.count('Container exited with code 127') == 1