ssh-audit/test/test_output.py

175 lines
4.5 KiB
Python
Raw Normal View History

2016-10-06 14:20:02 +02:00
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from __future__ import print_function
import pytest, io, sys
class TestOutput(object):
@pytest.fixture(autouse=True)
def init(self, ssh_audit):
self.Output = ssh_audit.Output
self.OutputBuffer = ssh_audit.OutputBuffer
2016-10-13 17:01:11 +02:00
def test_output_buffer_no_lines(self, output_spy):
output_spy.begin()
2016-10-06 14:20:02 +02:00
with self.OutputBuffer() as obuf:
pass
2016-10-13 17:01:11 +02:00
assert output_spy.flush() == []
output_spy.begin()
2016-10-06 14:20:02 +02:00
with self.OutputBuffer() as obuf:
pass
obuf.flush()
2016-10-13 17:01:11 +02:00
assert output_spy.flush() == []
2016-10-06 14:20:02 +02:00
2016-10-13 17:01:11 +02:00
def test_output_buffer_no_flush(self, output_spy):
output_spy.begin()
2016-10-06 14:20:02 +02:00
with self.OutputBuffer() as obuf:
print(u'abc')
2016-10-13 17:01:11 +02:00
assert output_spy.flush() == []
2016-10-06 14:20:02 +02:00
2016-10-13 17:01:11 +02:00
def test_output_buffer_flush(self, output_spy):
output_spy.begin()
2016-10-06 14:20:02 +02:00
with self.OutputBuffer() as obuf:
print(u'abc')
print()
print(u'def')
obuf.flush()
2016-10-13 17:01:11 +02:00
assert output_spy.flush() == [u'abc', u'', u'def']
2016-10-06 14:20:02 +02:00
def test_output_defaults(self):
out = self.Output()
# default: on
2016-10-07 18:55:49 +02:00
assert out.batch is False
assert out.colors is True
2016-10-06 14:20:02 +02:00
assert out.minlevel == 'info'
2016-10-13 17:01:11 +02:00
def test_output_colors(self, output_spy):
2016-10-06 14:20:02 +02:00
out = self.Output()
# test without colors
out.colors = False
2016-10-13 17:01:11 +02:00
output_spy.begin()
2016-10-06 14:20:02 +02:00
out.info('info color')
2016-10-13 17:01:11 +02:00
assert output_spy.flush() == [u'info color']
output_spy.begin()
2016-10-06 14:20:02 +02:00
out.head('head color')
2016-10-13 17:01:11 +02:00
assert output_spy.flush() == [u'head color']
output_spy.begin()
2016-10-06 14:20:02 +02:00
out.good('good color')
2016-10-13 17:01:11 +02:00
assert output_spy.flush() == [u'good color']
output_spy.begin()
2016-10-06 14:20:02 +02:00
out.warn('warn color')
2016-10-13 17:01:11 +02:00
assert output_spy.flush() == [u'warn color']
output_spy.begin()
2016-10-06 14:20:02 +02:00
out.fail('fail color')
2016-10-13 17:01:11 +02:00
assert output_spy.flush() == [u'fail color']
if not out.colors_supported:
return
2016-10-06 14:20:02 +02:00
# test with colors
out.colors = True
2016-10-13 17:01:11 +02:00
output_spy.begin()
2016-10-06 14:20:02 +02:00
out.info('info color')
2016-10-13 17:01:11 +02:00
assert output_spy.flush() == [u'info color']
output_spy.begin()
2016-10-06 14:20:02 +02:00
out.head('head color')
2016-10-13 17:01:11 +02:00
assert output_spy.flush() == [u'\x1b[0;36mhead color\x1b[0m']
output_spy.begin()
2016-10-06 14:20:02 +02:00
out.good('good color')
2016-10-13 17:01:11 +02:00
assert output_spy.flush() == [u'\x1b[0;32mgood color\x1b[0m']
output_spy.begin()
2016-10-06 14:20:02 +02:00
out.warn('warn color')
2016-10-13 17:01:11 +02:00
assert output_spy.flush() == [u'\x1b[0;33mwarn color\x1b[0m']
output_spy.begin()
2016-10-06 14:20:02 +02:00
out.fail('fail color')
2016-10-13 17:01:11 +02:00
assert output_spy.flush() == [u'\x1b[0;31mfail color\x1b[0m']
2016-10-06 14:20:02 +02:00
2016-10-13 17:01:11 +02:00
def test_output_sep(self, output_spy):
2016-10-06 14:20:02 +02:00
out = self.Output()
2016-10-13 17:01:11 +02:00
output_spy.begin()
2016-10-06 14:20:02 +02:00
out.sep()
out.sep()
out.sep()
2016-10-13 17:01:11 +02:00
assert output_spy.flush() == [u'', u'', u'']
2016-10-06 14:20:02 +02:00
def test_output_levels(self):
out = self.Output()
assert out.getlevel('info') == 0
assert out.getlevel('good') == 0
assert out.getlevel('warn') == 1
assert out.getlevel('fail') == 2
assert out.getlevel('unknown') > 2
def test_output_minlevel_property(self):
out = self.Output()
out.minlevel = 'info'
assert out.minlevel == 'info'
out.minlevel = 'good'
assert out.minlevel == 'info'
out.minlevel = 'warn'
assert out.minlevel == 'warn'
out.minlevel = 'fail'
assert out.minlevel == 'fail'
out.minlevel = 'invalid level'
assert out.minlevel == 'unknown'
2016-10-13 17:01:11 +02:00
def test_output_minlevel(self, output_spy):
2016-10-06 14:20:02 +02:00
out = self.Output()
# visible: all
out.minlevel = 'info'
2016-10-13 17:01:11 +02:00
output_spy.begin()
2016-10-06 14:20:02 +02:00
out.info('info color')
out.head('head color')
out.good('good color')
out.warn('warn color')
out.fail('fail color')
2016-10-13 17:01:11 +02:00
assert len(output_spy.flush()) == 5
2016-10-06 14:20:02 +02:00
# visible: head, warn, fail
out.minlevel = 'warn'
2016-10-13 17:01:11 +02:00
output_spy.begin()
2016-10-06 14:20:02 +02:00
out.info('info color')
out.head('head color')
out.good('good color')
out.warn('warn color')
out.fail('fail color')
2016-10-13 17:01:11 +02:00
assert len(output_spy.flush()) == 3
2016-10-06 14:20:02 +02:00
# visible: head, fail
out.minlevel = 'fail'
2016-10-13 17:01:11 +02:00
output_spy.begin()
2016-10-06 14:20:02 +02:00
out.info('info color')
out.head('head color')
out.good('good color')
out.warn('warn color')
out.fail('fail color')
2016-10-13 17:01:11 +02:00
assert len(output_spy.flush()) == 2
2016-10-06 14:20:02 +02:00
# visible: head
out.minlevel = 'invalid level'
2016-10-13 17:01:11 +02:00
output_spy.begin()
2016-10-06 14:20:02 +02:00
out.info('info color')
out.head('head color')
out.good('good color')
out.warn('warn color')
out.fail('fail color')
2016-10-13 17:01:11 +02:00
assert len(output_spy.flush()) == 1
2016-10-06 14:20:02 +02:00
2016-10-13 17:01:11 +02:00
def test_output_batch(self, output_spy):
2016-10-06 14:20:02 +02:00
out = self.Output()
# visible: all
2016-10-13 17:01:11 +02:00
output_spy.begin()
2016-10-06 14:20:02 +02:00
out.minlevel = 'info'
out.batch = False
out.info('info color')
out.head('head color')
out.good('good color')
out.warn('warn color')
out.fail('fail color')
2016-10-13 17:01:11 +02:00
assert len(output_spy.flush()) == 5
2016-10-06 14:20:02 +02:00
# visible: all except head
2016-10-13 17:01:11 +02:00
output_spy.begin()
2016-10-06 14:20:02 +02:00
out.minlevel = 'info'
out.batch = True
out.info('info color')
out.head('head color')
out.good('good color')
out.warn('warn color')
out.fail('fail color')
2016-10-13 17:01:11 +02:00
assert len(output_spy.flush()) == 4