2016-10-25 16:19:08 +02:00
|
|
|
import pytest
|
2016-10-06 14:20:02 +02:00
|
|
|
|
|
|
|
|
2016-10-25 16:19:08 +02:00
|
|
|
# pylint: disable=attribute-defined-outside-init
|
2021-02-01 19:10:06 +01:00
|
|
|
class TestOutputBuffer:
|
2020-06-13 03:01:10 +02:00
|
|
|
@pytest.fixture(autouse=True)
|
|
|
|
def init(self, ssh_audit):
|
|
|
|
self.OutputBuffer = ssh_audit.OutputBuffer
|
2020-06-09 23:54:07 +02:00
|
|
|
|
2021-02-01 19:10:06 +01:00
|
|
|
def test_outputbuffer_no_lines(self, output_spy):
|
2020-06-13 03:01:10 +02:00
|
|
|
output_spy.begin()
|
2021-02-01 19:10:06 +01:00
|
|
|
obuf = self.OutputBuffer()
|
|
|
|
obuf.write()
|
|
|
|
assert output_spy.flush() == ['']
|
2020-06-13 03:01:10 +02:00
|
|
|
output_spy.begin()
|
2020-06-09 23:54:07 +02:00
|
|
|
|
2021-02-01 19:10:06 +01:00
|
|
|
def test_outputbuffer_defaults(self):
|
|
|
|
obuf = self.OutputBuffer()
|
2020-06-13 03:01:10 +02:00
|
|
|
# default: on
|
2021-02-01 19:10:06 +01:00
|
|
|
assert obuf.batch is False
|
|
|
|
assert obuf.use_colors is True
|
|
|
|
assert obuf.level == 'info'
|
2020-06-09 23:54:07 +02:00
|
|
|
|
2021-02-01 19:10:06 +01:00
|
|
|
def test_outputbuffer_colors(self, output_spy):
|
|
|
|
out = self.OutputBuffer()
|
|
|
|
|
|
|
|
# Test without colors.
|
2020-06-13 03:01:10 +02:00
|
|
|
out.use_colors = False
|
2021-02-01 19:10:06 +01:00
|
|
|
|
2020-06-13 03:01:10 +02:00
|
|
|
output_spy.begin()
|
|
|
|
out.info('info color')
|
2021-02-01 19:10:06 +01:00
|
|
|
out.write()
|
2020-06-15 23:05:31 +02:00
|
|
|
assert output_spy.flush() == ['info color']
|
2021-02-01 19:10:06 +01:00
|
|
|
|
2020-06-13 03:01:10 +02:00
|
|
|
output_spy.begin()
|
|
|
|
out.head('head color')
|
2021-02-01 19:10:06 +01:00
|
|
|
out.write()
|
2020-06-15 23:05:31 +02:00
|
|
|
assert output_spy.flush() == ['head color']
|
2021-02-01 19:10:06 +01:00
|
|
|
|
2020-06-13 03:01:10 +02:00
|
|
|
output_spy.begin()
|
|
|
|
out.good('good color')
|
2021-02-01 19:10:06 +01:00
|
|
|
out.write()
|
2020-06-15 23:05:31 +02:00
|
|
|
assert output_spy.flush() == ['good color']
|
2021-02-01 19:10:06 +01:00
|
|
|
|
2020-06-13 03:01:10 +02:00
|
|
|
output_spy.begin()
|
|
|
|
out.warn('warn color')
|
2021-02-01 19:10:06 +01:00
|
|
|
out.write()
|
2020-06-15 23:05:31 +02:00
|
|
|
assert output_spy.flush() == ['warn color']
|
2021-02-01 19:10:06 +01:00
|
|
|
|
2020-06-13 03:01:10 +02:00
|
|
|
output_spy.begin()
|
|
|
|
out.fail('fail color')
|
2021-02-01 19:10:06 +01:00
|
|
|
out.write()
|
2020-06-15 23:05:31 +02:00
|
|
|
assert output_spy.flush() == ['fail color']
|
2021-02-01 19:10:06 +01:00
|
|
|
|
|
|
|
# If colors aren't supported by this system, skip the color tests.
|
2020-06-13 03:01:10 +02:00
|
|
|
if not out.colors_supported:
|
|
|
|
return
|
2021-02-01 19:10:06 +01:00
|
|
|
|
|
|
|
# Test with colors.
|
2020-06-13 03:01:10 +02:00
|
|
|
out.use_colors = True
|
2021-02-01 19:10:06 +01:00
|
|
|
|
2020-06-13 03:01:10 +02:00
|
|
|
output_spy.begin()
|
|
|
|
out.info('info color')
|
2021-02-01 19:10:06 +01:00
|
|
|
out.write()
|
2020-06-15 23:05:31 +02:00
|
|
|
assert output_spy.flush() == ['info color']
|
2021-02-01 19:10:06 +01:00
|
|
|
|
2020-06-13 03:01:10 +02:00
|
|
|
output_spy.begin()
|
|
|
|
out.head('head color')
|
2021-02-01 19:10:06 +01:00
|
|
|
out.write()
|
|
|
|
assert output_spy.flush() in [['\x1b[0;36mhead color\x1b[0m'], ['\x1b[0;96mhead color\x1b[0m']]
|
|
|
|
|
2020-06-13 03:01:10 +02:00
|
|
|
output_spy.begin()
|
|
|
|
out.good('good color')
|
2021-02-01 19:10:06 +01:00
|
|
|
out.write()
|
|
|
|
assert output_spy.flush() in [['\x1b[0;32mgood color\x1b[0m'], ['\x1b[0;92mgood color\x1b[0m']]
|
|
|
|
|
2020-06-13 03:01:10 +02:00
|
|
|
output_spy.begin()
|
|
|
|
out.warn('warn color')
|
2021-02-01 19:10:06 +01:00
|
|
|
out.write()
|
|
|
|
assert output_spy.flush() in [['\x1b[0;33mwarn color\x1b[0m'], ['\x1b[0;93mwarn color\x1b[0m']]
|
|
|
|
|
2020-06-13 03:01:10 +02:00
|
|
|
output_spy.begin()
|
|
|
|
out.fail('fail color')
|
2021-02-01 19:10:06 +01:00
|
|
|
out.write()
|
|
|
|
assert output_spy.flush() in [['\x1b[0;31mfail color\x1b[0m'], ['\x1b[0;91mfail color\x1b[0m']]
|
2020-06-09 23:54:07 +02:00
|
|
|
|
2021-02-01 19:10:06 +01:00
|
|
|
def test_outputbuffer_sep(self, output_spy):
|
|
|
|
out = self.OutputBuffer()
|
2020-06-13 03:01:10 +02:00
|
|
|
output_spy.begin()
|
|
|
|
out.sep()
|
|
|
|
out.sep()
|
|
|
|
out.sep()
|
2021-02-01 19:10:06 +01:00
|
|
|
out.write()
|
2020-06-15 23:05:31 +02:00
|
|
|
assert output_spy.flush() == ['', '', '']
|
2020-06-09 23:54:07 +02:00
|
|
|
|
2021-02-01 19:10:06 +01:00
|
|
|
def test_outputbuffer_levels(self):
|
|
|
|
out = self.OutputBuffer()
|
2020-06-13 03:01:10 +02:00
|
|
|
assert out.get_level('info') == 0
|
|
|
|
assert out.get_level('good') == 0
|
|
|
|
assert out.get_level('warn') == 1
|
|
|
|
assert out.get_level('fail') == 2
|
|
|
|
assert out.get_level('unknown') > 2
|
2020-06-09 23:54:07 +02:00
|
|
|
|
2021-02-01 19:10:06 +01:00
|
|
|
def test_outputbuffer_level_property(self):
|
|
|
|
out = self.OutputBuffer()
|
2020-06-13 03:01:10 +02:00
|
|
|
out.level = 'info'
|
|
|
|
assert out.level == 'info'
|
|
|
|
out.level = 'good'
|
|
|
|
assert out.level == 'info'
|
|
|
|
out.level = 'warn'
|
|
|
|
assert out.level == 'warn'
|
|
|
|
out.level = 'fail'
|
|
|
|
assert out.level == 'fail'
|
|
|
|
out.level = 'invalid level'
|
|
|
|
assert out.level == 'unknown'
|
2020-06-09 23:54:07 +02:00
|
|
|
|
2021-02-01 19:10:06 +01:00
|
|
|
def test_outputbuffer_level(self, output_spy):
|
|
|
|
out = self.OutputBuffer()
|
2020-06-13 03:01:10 +02:00
|
|
|
# visible: all
|
|
|
|
out.level = 'info'
|
|
|
|
output_spy.begin()
|
|
|
|
out.info('info color')
|
|
|
|
out.head('head color')
|
|
|
|
out.good('good color')
|
|
|
|
out.warn('warn color')
|
|
|
|
out.fail('fail color')
|
2021-02-01 19:10:06 +01:00
|
|
|
out.write()
|
2020-06-13 03:01:10 +02:00
|
|
|
assert len(output_spy.flush()) == 5
|
|
|
|
# visible: head, warn, fail
|
|
|
|
out.level = 'warn'
|
|
|
|
output_spy.begin()
|
|
|
|
out.info('info color')
|
|
|
|
out.head('head color')
|
|
|
|
out.good('good color')
|
|
|
|
out.warn('warn color')
|
|
|
|
out.fail('fail color')
|
2021-02-01 19:10:06 +01:00
|
|
|
out.write()
|
2020-06-13 03:01:10 +02:00
|
|
|
assert len(output_spy.flush()) == 3
|
|
|
|
# visible: head, fail
|
|
|
|
out.level = 'fail'
|
|
|
|
output_spy.begin()
|
|
|
|
out.info('info color')
|
|
|
|
out.head('head color')
|
|
|
|
out.good('good color')
|
|
|
|
out.warn('warn color')
|
|
|
|
out.fail('fail color')
|
2021-02-01 19:10:06 +01:00
|
|
|
out.write()
|
2020-06-13 03:01:10 +02:00
|
|
|
assert len(output_spy.flush()) == 2
|
|
|
|
# visible: head
|
|
|
|
out.level = 'invalid level'
|
|
|
|
output_spy.begin()
|
|
|
|
out.info('info color')
|
|
|
|
out.head('head color')
|
|
|
|
out.good('good color')
|
|
|
|
out.warn('warn color')
|
|
|
|
out.fail('fail color')
|
2021-02-01 19:10:06 +01:00
|
|
|
out.write()
|
2020-06-13 03:01:10 +02:00
|
|
|
assert len(output_spy.flush()) == 1
|
2020-06-09 23:54:07 +02:00
|
|
|
|
2021-02-01 19:10:06 +01:00
|
|
|
def test_outputbuffer_batch(self, output_spy):
|
|
|
|
out = self.OutputBuffer()
|
2020-06-13 03:01:10 +02:00
|
|
|
# visible: all
|
|
|
|
output_spy.begin()
|
|
|
|
out.level = 'info'
|
|
|
|
out.batch = False
|
|
|
|
out.info('info color')
|
|
|
|
out.head('head color')
|
|
|
|
out.good('good color')
|
|
|
|
out.warn('warn color')
|
|
|
|
out.fail('fail color')
|
2021-02-01 19:10:06 +01:00
|
|
|
out.write()
|
2020-06-13 03:01:10 +02:00
|
|
|
assert len(output_spy.flush()) == 5
|
|
|
|
# visible: all except head
|
|
|
|
output_spy.begin()
|
|
|
|
out.level = 'info'
|
|
|
|
out.batch = True
|
|
|
|
out.info('info color')
|
|
|
|
out.head('head color')
|
|
|
|
out.good('good color')
|
|
|
|
out.warn('warn color')
|
|
|
|
out.fail('fail color')
|
2021-02-01 19:10:06 +01:00
|
|
|
out.write()
|
2020-06-13 03:01:10 +02:00
|
|
|
assert len(output_spy.flush()) == 4
|