Example #1
0
def format_haproxy_backend_row(backend, is_correct_instance):
    """Pretty Prints the status of a given haproxy backend
    Takes the fields described in the CSV format of haproxy:
    http://www.haproxy.org/download/1.5/doc/configuration.txt
    And tries to make a good guess about how to represent them in text
    """
    backend_name = backend['svname']
    backend_hostname = backend_name.split("_")[-1]
    backend_port = backend_name.split("_")[0].split(":")[-1]
    pretty_backend_name = "%s:%s" % (backend_hostname, backend_port)
    if backend['status'] == "UP":
        status = PaastaColors.default(backend['status'])
    elif backend['status'] == 'DOWN' or backend['status'] == 'MAINT':
        status = PaastaColors.red(backend['status'])
    else:
        status = PaastaColors.yellow(backend['status'])
    lastcheck = "%s/%s in %sms" % (backend['check_status'],
                                   backend['check_code'],
                                   backend['check_duration'])
    lastchange = humanize.naturaltime(
        datetime.timedelta(seconds=int(backend['lastchg'])))

    row = (
        '      %s' % pretty_backend_name,
        lastcheck,
        lastchange,
        status,
    )

    if is_correct_instance:
        return row
    else:
        return tuple(
            PaastaColors.grey(remove_ansi_escape_sequences(col))
            for col in row)
def format_haproxy_backend_row(backend, is_correct_instance):
    """Pretty Prints the status of a given haproxy backend
    Takes the fields described in the CSV format of haproxy:
    http://www.haproxy.org/download/1.5/doc/configuration.txt
    And tries to make a good guess about how to represent them in text
    """
    backend_name = backend["svname"]
    backend_hostname = backend_name.split("_")[-1]
    backend_port = backend_name.split("_")[0].split(":")[-1]
    pretty_backend_name = f"{backend_hostname}:{backend_port}"
    if backend["status"] == "UP":
        status = PaastaColors.default(backend["status"])
    elif backend["status"] == "DOWN" or backend["status"] == "MAINT":
        status = PaastaColors.red(backend["status"])
    else:
        status = PaastaColors.yellow(backend["status"])
    lastcheck = "{}/{} in {}ms".format(
        backend["check_status"], backend["check_code"], backend["check_duration"]
    )
    lastchange = humanize.naturaltime(
        datetime.timedelta(seconds=int(backend["lastchg"]))
    )

    row = ("      %s" % pretty_backend_name, lastcheck, lastchange, status)

    if is_correct_instance:
        return row
    else:
        return tuple(
            PaastaColors.grey(remove_ansi_escape_sequences(col)) for col in row
        )
def test_pretty_print_smartstack_backends_for_locations_verbose():
    hosts_grouped_by_location = {"place1": ["host1"], "place2": ["host2"], "place3": ["host3"]}
    host_ip_mapping = {"host1": "169.254.123.1", "host2": "169.254.123.2", "host3": "169.254.123.3"}
    tasks = [
        mock.Mock(host="host1", ports=[1234]),
        mock.Mock(host="host2", ports=[1234]),
        mock.Mock(host="host3", ports=[1234]),
    ]
    backends = {
        "host1": {
            "svname": "169.254.123.1:1234_host1",
            "status": "UP",
            "check_status": "L7OK",
            "check_code": "200",
            "check_duration": 4,
            "lastchg": 0,
        },
        "host2": {
            "svname": "169.254.123.2:1234_host2",
            "status": "UP",
            "check_status": "L7OK",
            "check_code": "200",
            "check_duration": 4,
            "lastchg": 0,
        },
        "host3": {
            "svname": "169.254.123.3:1234_host3",
            "status": "UP",
            "check_status": "L7OK",
            "check_code": "200",
            "check_duration": 4,
            "lastchg": 0,
        },
    }
    with contextlib.nested(
        mock.patch(
            "paasta_tools.marathon_serviceinit.get_backends",
            autospec=True,
            side_effect=lambda _, synapse_host, synapse_port: [backends[synapse_host]],
        ),
        mock.patch("socket.gethostbyname", side_effect=lambda name: host_ip_mapping[name], autospec=True),
    ) as (mock_get_backends, mock_gethostbyname):
        actual = marathon_serviceinit.pretty_print_smartstack_backends_for_locations(
            service_instance="fake_service.fake_instance",
            tasks=tasks,
            locations=hosts_grouped_by_location,
            expected_count=3,
            verbose=True,
        )

        colorstripped_actual = [remove_ansi_escape_sequences(l) for l in actual]
        assert colorstripped_actual == [
            "      Name        LastCheck        LastChange  Status",
            "    place1 - Healthy - in haproxy with (1/1) total backends UP in this namespace.",
            "      host1:1234  L7OK/200 in 4ms  now         UP",
            "    place2 - Healthy - in haproxy with (1/1) total backends UP in this namespace.",
            "      host2:1234  L7OK/200 in 4ms  now         UP",
            "    place3 - Healthy - in haproxy with (1/1) total backends UP in this namespace.",
            "      host3:1234  L7OK/200 in 4ms  now         UP",
        ]
Example #4
0
def format_haproxy_backend_row(backend, is_correct_instance):
    """Pretty Prints the status of a given haproxy backend
    Takes the fields described in the CSV format of haproxy:
    http://www.haproxy.org/download/1.5/doc/configuration.txt
    And tries to make a good guess about how to represent them in text
    """
    backend_name = backend['svname']
    backend_hostname = backend_name.split("_")[-1]
    backend_port = backend_name.split("_")[0].split(":")[-1]
    pretty_backend_name = "%s:%s" % (backend_hostname, backend_port)
    if backend['status'] == "UP":
        status = PaastaColors.default(backend['status'])
    elif backend['status'] == 'DOWN' or backend['status'] == 'MAINT':
        status = PaastaColors.red(backend['status'])
    else:
        status = PaastaColors.yellow(backend['status'])
    lastcheck = "%s/%s in %sms" % (backend['check_status'], backend['check_code'], backend['check_duration'])
    lastchange = humanize.naturaltime(datetime.timedelta(seconds=int(backend['lastchg'])))

    row = (
        '      %s' % pretty_backend_name,
        lastcheck,
        lastchange,
        status,
    )

    if is_correct_instance:
        return row
    else:
        return tuple(PaastaColors.grey(remove_ansi_escape_sequences(col)) for col in row)
Example #5
0
def build_smartstack_backends_table(backends):
    rows = [("Name", "LastCheck", "LastChange", "Status")]
    for backend in backends:
        if backend.status == "UP":
            status = PaastaColors.default(backend.status)
        elif backend.status == "DOWN":
            status = PaastaColors.red(backend.status)
        elif backend.status == "MAINT":
            status = PaastaColors.grey(backend.status)
        else:
            status = PaastaColors.yellow(backend.status)

        if backend.check_duration is None:
            check_duration = ""
        else:
            check_duration = str(backend.check_duration)

        row = (
            f"{backend.hostname}:{backend.port}",
            f"{backend.check_status}/{backend.check_code} in {check_duration}ms",
            humanize.naturaltime(timedelta(seconds=backend.last_change)),
            status,
        )

        if not backend.has_associated_task:
            row = tuple(
                PaastaColors.grey(remove_ansi_escape_sequences(col)) for col in row
            )

        rows.append(row)

    return format_table(rows)
def check_metastatus_return_code_with_flags(context, flags, expected_return_code, expected_output):
    # We don't want to invoke the "paasta metastatus" wrapper because by
    # default it will check every cluster. This is also the way sensu invokes
    # this check.
    cmd = '../paasta_tools/paasta_metastatus.py%s' % flags
    print 'Running cmd %s' % (cmd)
    exit_code, output = _run(cmd)

    # we don't care about the colouring here, so remove any ansi escape sequences
    escaped_output = remove_ansi_escape_sequences(output)
    print 'Got exitcode %s with output:\n%s' % (exit_code, output)
    print '\n'

    assert exit_code == int(expected_return_code)
    assert expected_output in escaped_output
def check_metastatus_return_code_with_flags(context, flags,
                                            expected_return_code,
                                            expected_output):
    # We don't want to invoke the "paasta metastatus" wrapper because by
    # default it will check every cluster. This is also the way sensu invokes
    # this check.
    cmd = "python -m paasta_tools.paasta_metastatus%s" % flags
    print("Running cmd %s" % (cmd))
    exit_code, output = _run(cmd)

    # we don't care about the colouring here, so remove any ansi escape sequences
    escaped_output = remove_ansi_escape_sequences(output)
    print(f"Got exitcode {exit_code} with output:\n{output}")
    print()

    assert exit_code == int(expected_return_code)
    assert expected_output in escaped_output
def check_metastatus_return_code(context, expected_return_code, expected_output):
    # We don't want to invoke the "paasta metastatus" wrapper because by
    # default it will check every cluster. This is also the way sensu invokes
    # this check.
    cmd = '../paasta_tools/paasta_metastatus.py --v'
    env = dict(os.environ)
    env['MESOS_CLI_CONFIG'] = context.mesos_cli_config_filename
    print 'Running cmd %s with MESOS_CLI_CONFIG=%s' % (cmd, env['MESOS_CLI_CONFIG'])
    (exit_code, output) = _run(cmd, env=env)

    # we don't care about the colouring here, so remove any ansi escape sequences
    escaped_output = remove_ansi_escape_sequences(output)
    print 'Got exitcode %s with output:\n%s' % (exit_code, output)
    print '\n'

    assert exit_code == int(expected_return_code)
    assert expected_output in escaped_output
def check_metastatus_return_code(context, expected_return_code,
                                 expected_output):
    # We don't want to invoke the "paasta metastatus" wrapper because by
    # default it will check every cluster. This is also the way sensu invokes
    # this check.
    cmd = '../paasta_tools/paasta_metastatus.py --v'
    env = dict(os.environ)
    env['MESOS_CLI_CONFIG'] = context.mesos_cli_config_filename
    print 'Running cmd %s with MESOS_CLI_CONFIG=%s' % (cmd,
                                                       env['MESOS_CLI_CONFIG'])
    (exit_code, output) = _run(cmd, env=env)

    # we don't care about the colouring here, so remove any ansi escape sequences
    escaped_output = remove_ansi_escape_sequences(output)
    print 'Got exitcode %s with output:\n%s' % (exit_code, output)
    print '\n'

    assert exit_code == int(expected_return_code)
    assert expected_output in escaped_output
Example #10
0
def test_pretty_print_smartstack_backends_for_locations_verbose():
    hosts_grouped_by_location = {'place1': ['host1'], 'place2': ['host2'], 'place3': ['host3']}
    host_ip_mapping = {
        'host1': '169.254.123.1',
        'host2': '169.254.123.2',
        'host3': '169.254.123.3',
    }
    tasks = [
        mock.Mock(host='host1', ports=[1234]),
        mock.Mock(host='host2', ports=[1234]),
        mock.Mock(host='host3', ports=[1234])
    ]
    backends = {
        'host1': {
            'svname': '169.254.123.1:1234_host1',
            'status': 'UP',
            'check_status': 'L7OK',
            'check_code': '200',
            'check_duration': 4,
            'lastchg': 0
        },
        'host2': {
            'svname': '169.254.123.2:1234_host2',
            'status': 'UP',
            'check_status': 'L7OK',
            'check_code': '200',
            'check_duration': 4,
            'lastchg': 0
        },
        'host3': {
            'svname': '169.254.123.3:1234_host3',
            'status': 'UP',
            'check_status': 'L7OK',
            'check_code': '200',
            'check_duration': 4,
            'lastchg': 0
        },
    }
    with contextlib.nested(
        mock.patch(
            'paasta_tools.marathon_serviceinit.get_backends', autospec=True,
            side_effect=lambda _, synapse_host, synapse_port, synapse_haproxy_url_format: [backends[synapse_host]]
        ),
        mock.patch('socket.gethostbyname', side_effect=lambda name: host_ip_mapping[name], autospec=True),
    ) as (
        mock_get_backends,
        mock_gethostbyname,
    ):
        actual = marathon_serviceinit.pretty_print_smartstack_backends_for_locations(
            service_instance='fake_service.fake_instance',
            tasks=tasks,
            locations=hosts_grouped_by_location,
            expected_count=3,
            verbose=True,
            synapse_port=123456,
            synapse_haproxy_url_format=DEFAULT_SYNAPSE_HAPROXY_URL_FORMAT,
        )

        colorstripped_actual = [remove_ansi_escape_sequences(l) for l in actual]
        assert colorstripped_actual == [
            '      Name        LastCheck        LastChange  Status',
            '    place1 - Healthy - in haproxy with (1/1) total backends UP in this namespace.',
            '      host1:1234  L7OK/200 in 4ms  now         UP',
            '    place2 - Healthy - in haproxy with (1/1) total backends UP in this namespace.',
            '      host2:1234  L7OK/200 in 4ms  now         UP',
            '    place3 - Healthy - in haproxy with (1/1) total backends UP in this namespace.',
            '      host3:1234  L7OK/200 in 4ms  now         UP',
        ]
def test_pretty_print_smartstack_backends_for_locations_verbose():
    hosts_grouped_by_location = {
        'place1': ['host1'],
        'place2': ['host2'],
        'place3': ['host3']
    }
    host_ip_mapping = {
        'host1': '169.254.123.1',
        'host2': '169.254.123.2',
        'host3': '169.254.123.3',
    }
    tasks = [
        mock.Mock(host='host1', ports=[1234]),
        mock.Mock(host='host2', ports=[1234]),
        mock.Mock(host='host3', ports=[1234])
    ]
    backends = {
        'host1': {
            'svname': '169.254.123.1:1234_host1',
            'status': 'UP',
            'check_status': 'L7OK',
            'check_code': '200',
            'check_duration': 4,
            'lastchg': 0
        },
        'host2': {
            'svname': '169.254.123.2:1234_host2',
            'status': 'UP',
            'check_status': 'L7OK',
            'check_code': '200',
            'check_duration': 4,
            'lastchg': 0
        },
        'host3': {
            'svname': '169.254.123.3:1234_host3',
            'status': 'UP',
            'check_status': 'L7OK',
            'check_code': '200',
            'check_duration': 4,
            'lastchg': 0
        },
    }
    with contextlib.nested(
            mock.patch('paasta_tools.marathon_serviceinit.get_backends',
                       autospec=True,
                       side_effect=lambda _, synapse_host, synapse_port,
                       synapse_haproxy_url_format: [backends[synapse_host]]),
            mock.patch('socket.gethostbyname',
                       side_effect=lambda name: host_ip_mapping[name],
                       autospec=True),
    ) as (
            mock_get_backends,
            mock_gethostbyname,
    ):
        actual = marathon_serviceinit.pretty_print_smartstack_backends_for_locations(
            service_instance='fake_service.fake_instance',
            tasks=tasks,
            locations=hosts_grouped_by_location,
            expected_count=3,
            verbose=True,
            synapse_port=123456,
            synapse_haproxy_url_format=DEFAULT_SYNAPSE_HAPROXY_URL_FORMAT,
        )

        colorstripped_actual = [
            remove_ansi_escape_sequences(l) for l in actual
        ]
        assert colorstripped_actual == [
            '      Name        LastCheck        LastChange  Status',
            '    place1 - Healthy - in haproxy with (1/1) total backends UP in this namespace.',
            '      host1:1234  L7OK/200 in 4ms  now         UP',
            '    place2 - Healthy - in haproxy with (1/1) total backends UP in this namespace.',
            '      host2:1234  L7OK/200 in 4ms  now         UP',
            '    place3 - Healthy - in haproxy with (1/1) total backends UP in this namespace.',
            '      host3:1234  L7OK/200 in 4ms  now         UP',
        ]
Example #12
0
def test_pretty_print_smartstack_backends_for_locations_verbose():
    hosts_grouped_by_location = {
        "place1": ["host1"],
        "place2": ["host2"],
        "place3": ["host3"],
    }
    host_ip_mapping = {
        "host1": "169.254.123.1",
        "host2": "169.254.123.2",
        "host3": "169.254.123.3",
    }
    tasks = [
        mock.Mock(host="host1", ports=[1234]),
        mock.Mock(host="host2", ports=[1234]),
        mock.Mock(host="host3", ports=[1234]),
    ]
    backends = {
        "host1": {
            "svname": "169.254.123.1:1234_host1",
            "status": "UP",
            "check_status": "L7OK",
            "check_code": "200",
            "check_duration": 4,
            "lastchg": 0,
        },
        "host2": {
            "svname": "169.254.123.2:1234_host2",
            "status": "UP",
            "check_status": "L7OK",
            "check_code": "200",
            "check_duration": 4,
            "lastchg": 0,
        },
        "host3": {
            "svname": "169.254.123.3:1234_host3",
            "status": "UP",
            "check_status": "L7OK",
            "check_code": "200",
            "check_duration": 4,
            "lastchg": 0,
        },
    }
    with mock.patch(
            "paasta_tools.marathon_serviceinit.get_backends",
            autospec=True,
            side_effect=lambda _, synapse_host, synapse_port,
            synapse_haproxy_url_format: [backends[synapse_host]],
    ), mock.patch(
            "socket.gethostbyname",
            side_effect=lambda name: host_ip_mapping[name],
            autospec=True,
    ):
        actual = marathon_serviceinit.pretty_print_smartstack_backends_for_locations(
            registration="fake_service.fake_instance",
            tasks=tasks,
            locations=hosts_grouped_by_location,
            expected_count=3,
            verbose=True,
            synapse_port=123456,
            synapse_haproxy_url_format=DEFAULT_SYNAPSE_HAPROXY_URL_FORMAT,
        )

        colorstripped_actual = [
            remove_ansi_escape_sequences(l) for l in actual
        ]
        assert colorstripped_actual == [
            "      Name        LastCheck        LastChange  Status",
            "    place1 - Healthy - in haproxy with (1/1) total backends UP in this namespace.",
            "      host1:1234  L7OK/200 in 4ms  now         UP",
            "    place2 - Healthy - in haproxy with (1/1) total backends UP in this namespace.",
            "      host2:1234  L7OK/200 in 4ms  now         UP",
            "    place3 - Healthy - in haproxy with (1/1) total backends UP in this namespace.",
            "      host3:1234  L7OK/200 in 4ms  now         UP",
        ]
Example #13
0
def test_remove_ansi_escape_sequences():
    plain_string = 'blackandwhite'
    colored_string = '\033[34m' + plain_string + '\033[0m'
    assert utils.remove_ansi_escape_sequences(colored_string) == plain_string
Example #14
0
def test_remove_ansi_escape_sequences():
    plain_string = "blackandwhite"
    colored_string = "\033[34m" + plain_string + "\033[0m"
    assert utils.remove_ansi_escape_sequences(colored_string) == plain_string