Closed aesedepece closed 7 years ago
@elecay In your opinion, what would be a better practice instead? @kronolynx Same question ↑ :wink:
What about a named tuple?, like:
Status = namedtuple('Status', 'success, message')
def doing_some_magic_here():
return Status(False, 'some message')
s = doing_some_magic_here()
if s.success:
print('Wooha!')
else:
print(s.message)
I think that tests pass or not pass. If pass, could have message (warning) or not (success). A "3" response should actually never been raise. If a previous condition did not pass, then, test didn't pass. But maybe I don't fully understand the logic yet.
Great suggestion!
The thing is that values 0
, 1
, 2
, and 3
are indeed used for choosing the messages in the report without the need to use conditional expressions or switch/case constructions. Report messages are loaded from https://github.com/stampery/mongoaudit/blob/master/mongoaudit/data/tests.json and then accessed directly by index.
I know, not the most elegant stuff in the world, but the performance is likely unmatchable :sweat_smile:
I understand. Then, we can add another field, like:
Status = namedtuple('Status', 'success, severity, message')
return Status(False, 2, 'some message')
The important thing is to return always the same contract.
Can you give me some feedback on the branch that I've created?
Hi @elecay, thanks a lot! Everything seems fine but a single crash when reaching test 24 in the results summary (only happens on the advanced test suite):
Traceback (most recent call last):
File "~/anaconda2/lib/python2.7/runpy.py", line 174, in _run_module_as_main
"__main__", fname, loader, pkg_name)
File "~/anaconda2/lib/python2.7/runpy.py", line 72, in _run_code
exec code in run_globals
File "~/Stampery/Desarrollo/mongoaudit/mongoaudit/__main__.py", line 66, in <module>
main()
File "~/Stampery/Desarrollo/mongoaudit/mongoaudit/__main__.py", line 63, in main
App().main()
File "~/Stampery/Desarrollo/mongoaudit/mongoaudit/__main__.py", line 23, in __init__
self.main()
File "~/Stampery/Desarrollo/mongoaudit/mongoaudit/__main__.py", line 58, in main
self.loop.run()
File "~/anaconda2/lib/python2.7/site-packages/urwid/main_loop.py", line 278, in run
self._run()
File "~/anaconda2/lib/python2.7/site-packages/urwid/main_loop.py", line 376, in _run
self.event_loop.run()
File "~/anaconda2/lib/python2.7/site-packages/urwid/main_loop.py", line 682, in run
self._loop()
File "~/anaconda2/lib/python2.7/site-packages/urwid/main_loop.py", line 719, in _loop
self._watch_files[fd]()
File "~/anaconda2/lib/python2.7/site-packages/urwid/raw_display.py", line 393, in <lambda>
event_loop, callback, self.get_available_raw_input())
File "~/anaconda2/lib/python2.7/site-packages/urwid/raw_display.py", line 493, in parse_input
callback(processed, processed_codes)
File "~/anaconda2/lib/python2.7/site-packages/urwid/main_loop.py", line 403, in _update
self.process_input(keys)
File "~/anaconda2/lib/python2.7/site-packages/urwid/main_loop.py", line 503, in process_input
k = self._topmost_widget.keypress(self.screen_size, k)
File "~/anaconda2/lib/python2.7/site-packages/urwid/container.py", line 592, in keypress
*self.calculate_padding_filler(size, True)), key)
File "~/anaconda2/lib/python2.7/site-packages/urwid/container.py", line 1587, in keypress
key = self.focus.keypress(tsize, key)
File "~/anaconda2/lib/python2.7/site-packages/urwid/container.py", line 1587, in keypress
key = self.focus.keypress(tsize, key)
File "~/anaconda2/lib/python2.7/site-packages/urwid/decoration.py", line 621, in keypress
return self._original_widget.keypress(maxvals, key)
File "~/anaconda2/lib/python2.7/site-packages/urwid/decoration.py", line 385, in keypress
return self._original_widget.keypress((maxcol, self.height), key)
File "~/anaconda2/lib/python2.7/site-packages/urwid/listbox.py", line 987, in keypress
key = focus_widget.keypress((maxcol,),key)
File "~/anaconda2/lib/python2.7/site-packages/urwid/decoration.py", line 621, in keypress
return self._original_widget.keypress(maxvals, key)
File "~/anaconda2/lib/python2.7/site-packages/urwid/container.py", line 2269, in keypress
key = w.keypress((mc,) + size[1:], key)
File "~/anaconda2/lib/python2.7/site-packages/urwid/wimp.py", line 535, in keypress
self._emit('click')
File "~/anaconda2/lib/python2.7/site-packages/urwid/widget.py", line 464, in _emit
signals.emit_signal(self, name, self, *args)
File "~/anaconda2/lib/python2.7/site-packages/urwid/signals.py", line 264, in emit
result |= self._call_callback(callback, user_arg, user_args, args)
File "~/anaconda2/lib/python2.7/site-packages/urwid/signals.py", line 294, in _call_callback
return bool(callback(*args_to_pass))
File "mongoaudit/widgets.py", line 380, in <lambda>
lambda _: self.update_view(text))), 'button')
File "mongoaudit/widgets.py", line 411, in update_view
self.result[self.currently_displayed - 1], self.test_result.options)
File "mongoaudit/widgets.py", line 361, in test_display
test['extra_data'] + test['message'][1]
TypeError: coercing to Unicode: need string or buffer, NoneType found
Yes, we're getting WARNING type message instead of OMITTED ones. Let me check. BTW, next step will be adding unit tests.
As pointed out by @elecay in #28:
Currently, possible return values are:
False
or0
, standing for failure (did not pass)True
or1
, standing for success (did pass)[2, "some message"]
, standing for partial success (a warning message is thrown)3
, standing for omission because of unmet precondition (likely: previous check did not pass)