Exemple #1
0
def _build_envoy_location_dict_for_backends(
    envoy_host: str,
    registration: str,
    tasks: Sequence[MarathonTask],
    location: str,
    should_return_individual_backends: bool,
) -> MutableMapping[str, Any]:
    backends = envoy_tools.get_backends(
        registration,
        envoy_host=envoy_host,
        envoy_admin_port=settings.system_paasta_config.get_envoy_admin_port(),
    )
    sorted_envoy_backends = sorted(
        [backend[0] for backend in backends],
        key=lambda backend: backend["eds_health_status"],
    )
    casper_proxied_backends = {
        (backend["address"], backend["port_value"])
        for backend, is_casper_proxied_backend in backends
        if is_casper_proxied_backend
    }

    matched_envoy_backends_and_tasks = envoy_tools.match_backends_and_tasks(
        sorted_envoy_backends,
        tasks,
    )

    return envoy_tools.build_envoy_location_dict(
        location,
        matched_envoy_backends_and_tasks,
        should_return_individual_backends,
        casper_proxied_backends,
    )
Exemple #2
0
def test_match_backends_and_tasks(mock_backends):
    backends = mock_backends

    good_task1 = mock.Mock(host="box4", ports=[31000])
    good_task2 = mock.Mock(host="box5", ports=[31001])
    bad_task = mock.Mock(host="box7", ports=[31000])
    tasks = [good_task1, good_task2, bad_task]

    hostnames = {
        "box4": "10.50.2.4",
        "box5": "10.50.2.5",
        "box6": "10.50.2.6",
        "box7": "10.50.2.7",
        "box8": "10.50.2.8",
    }

    with mock.patch(
            "paasta_tools.envoy_tools.socket.gethostbyname",
            side_effect=lambda x: hostnames[x],
            autospec=True,
    ):
        expected = [
            (backends[0], good_task1),
            (backends[1], good_task2),
            (None, bad_task),
            (backends[2], None),
            (backends[3], None),
            (backends[4], None),
        ]
        actual = match_backends_and_tasks(backends, tasks)

        def keyfunc(t):
            return tuple(sorted((t[0] or {}).items())), t[1]

        assert sorted(actual, key=keyfunc) == sorted(expected, key=keyfunc)
Exemple #3
0
def test_match_backends_and_tasks():
    backends = [
        {
            "address": "10.50.2.4",
            "port_value": 31000,
            "eds_health_status": "HEALTHY",
            "weight": 1,
            "has_associated_task": False,
        },
        {
            "address": "10.50.2.5",
            "port_value": 31001,
            "eds_health_status": "HEALTHY",
            "weight": 1,
            "has_associated_task": False,
        },
        {
            "address": "10.50.2.6",
            "port_value": 31001,
            "eds_health_status": "HEALTHY",
            "weight": 1,
            "has_associated_task": False,
        },
        {
            "address": "10.50.2.6",
            "port_value": 31002,
            "eds_health_status": "HEALTHY",
            "weight": 1,
            "has_associated_task": False,
        },
        {
            "address": "10.50.2.8",
            "port_value": 31000,
            "eds_health_status": "HEALTHY",
            "weight": 1,
            "has_associated_task": False,
        },
    ]
    good_task1 = mock.Mock(host="box4", ports=[31000])
    good_task2 = mock.Mock(host="box5", ports=[31001])
    bad_task = mock.Mock(host="box7", ports=[31000])
    tasks = [good_task1, good_task2, bad_task]

    hostnames = {
        "box4": "10.50.2.4",
        "box5": "10.50.2.5",
        "box6": "10.50.2.6",
        "box7": "10.50.2.7",
        "box8": "10.50.2.8",
    }

    with mock.patch(
            "paasta_tools.envoy_tools.socket.gethostbyname",
            side_effect=lambda x: hostnames[x],
            autospec=True,
    ):
        expected = [
            (backends[0], good_task1),
            (backends[1], good_task2),
            (None, bad_task),
            (backends[2], None),
            (backends[3], None),
            (backends[4], None),
        ]
        actual = match_backends_and_tasks(backends, tasks)

        def keyfunc(t):
            return tuple(sorted((t[0] or {}).items())), t[1]

        assert sorted(actual, key=keyfunc) == sorted(expected, key=keyfunc)