Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding support for supressing UNKNOWN results via --no-unknown #63

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,8 @@ check_docker Usage
--restarts WARN:CRIT Container restart thresholds.
--no-ok Make output terse suppressing OK messages. If all
checks are OK return a single OK.
--no-unknown Make output terse suppressing UNKNOWN messages. If all
checks are UNKNOWN return a single UNKNOWN.
--no-performance Suppress performance data. Reduces output when
performance data is not being used.
-V show program's version number and exit
Expand Down
28 changes: 24 additions & 4 deletions check_docker/check_docker.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@
# Reduce message to a single OK unless a checks fail.
no_ok = False

# Reduce message to a single UNKNOWN unless a checks fail.
no_unknown = False

# Suppress performance data reporting
no_performance = False

Expand Down Expand Up @@ -853,6 +856,12 @@ def process_args(args):
action='store_true',
help='Make output terse suppressing OK messages. If all checks are OK return a single OK.')

# no-unknown
parser.add_argument('--no-unknown',
dest='no_unknown',
action='store_true',
help='Make output terse suppressing UNKNOWN messages. If all checks are UNKNOWN return a single UNKNOWN.')

# no-performance
parser.add_argument('--no-performance',
dest='no_performance',
Expand Down Expand Up @@ -903,17 +912,25 @@ def socketfile_permissions_failure(parsed_args):


def print_results():
if no_ok:
# Remove all the "OK"s
filtered_messages = [message for message in messages if not message.startswith('OK: ')]
if no_ok or no_unknown:
if no_ok:
# Remove all the "OK"s
filtered_messages = [message for message in messages if not message.startswith('OK: ')]
if no_unknown:
# Remove all the "UNKNOWN"s
filtered_messages = [message for message in messages if not message.startswith('UNKNOWN: ')]
if len(filtered_messages) == 0:
messages_concat = 'OK'
if no_ok:
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the user passes --no_ok --no_unknown
UNKNOWN will be added to the output even if one was not removed.

messages_concat = 'OK'
if no_unknown:
messages_concat = 'UNKNOWN'
else:
messages_concat = '; '.join(filtered_messages)

else:
messages_concat = '; '.join(messages)


if no_performance or len(performance_data) == 0:
print(messages_concat)
else:
Expand All @@ -935,6 +952,9 @@ def perform_checks(raw_args):
global no_ok
no_ok = args.no_ok

global no_unknown
no_unknown = args.no_unknown

global no_performance
no_performance = args.no_ok

Expand Down
7 changes: 5 additions & 2 deletions tests/test_check_docker.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ def check_docker_fresh():
def check_docker():
cd.rc = -1
check_docker.no_ok = False
check_docker.no_unknown = False
check_docker.no_performance = False
cd.timeout = 1
cd.messages = []
Expand Down Expand Up @@ -720,6 +721,7 @@ def test_perform(check_docker, fs, args, called):
def test_print_results(check_docker, capsys, messages, perf_data, expected):
# These sometimes get set to true when using random-order plugin, for example --random-order-seed=620808
check_docker.no_ok = False
check_docker.no_unknown = False
check_docker.no_performance = False
check_docker.messages = messages
check_docker.performance_data = perf_data
Expand All @@ -728,7 +730,7 @@ def test_print_results(check_docker, capsys, messages, perf_data, expected):
assert out.strip() == expected


@pytest.mark.parametrize("messages, perf_data, no_ok, no_performance, expected", (
@pytest.mark.parametrize("messages, perf_data, no_ok, no_unknown, no_performance, expected", (
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You added a test parameter but you didn't update the data to match it. Have you used parameterized pytest tests before? It is pretty simple (and also a little ugly).
The string is a ordered list of names for all of the values in each of the tuples that follow. Each tuple in the list describes the setup of a single test case. For example

def test_func(one, two) 
    ...

This executes does something like this

for params in ((1,2),(11,22)):
  one, two = params
  test_func(one=one, two=two)

Can you add the missing entries to the tuples and also add tuples that exercise the code you are changing.

Don't worry if you break something, I can help you out. Also my dev documentation should help you get the test running on your machine.

([], [], False, False, ''),
(['TEST'], [], False, False, 'TEST'),
(['FOO', 'BAR'], [], False, False, 'FOO; BAR'),
Expand All @@ -742,10 +744,11 @@ def test_print_results(check_docker, capsys, messages, perf_data, expected):
(['OK: TEST'], ['1;2;3;4;'], False, True, 'OK: TEST'),
(['OK: FOO', 'OK: BAR'], ['1;2;3;4;'], True, True, 'OK'),
))
def test_print_results_no_ok(check_docker, capsys, messages, perf_data, no_ok, no_performance, expected):
def test_print_results_no_ok(check_docker, capsys, messages, perf_data, no_ok, no_unknown, no_performance, expected):
check_docker.messages = messages
check_docker.performance_data = perf_data
check_docker.no_ok = no_ok
check_docker.no_unknown = no_unknown
check_docker.no_performance = no_performance
check_docker.print_results()
out, err = capsys.readouterr()
Expand Down