1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
|
# Copyright (c) 2014, 2016 Intel Corporation
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
"""Tests for log.py module."""
from __future__ import (
absolute_import, division, print_function, unicode_literals
)
import collections
import sys
import threading
import pytest
import six
import framework.log as log
# pylint: disable=no-self-use,protected-access
@pytest.fixture
def log_state():
"""Create a unique state instance per test."""
return {'total': 1, 'complete': 0, 'lastlength': 0, 'running': [],
'summary': collections.defaultdict(lambda: 0)}
class TestLogFactory(object):
"""Tests for the LogFactory class."""
class TestGet(object):
"""Tests for the LogFactory.get method."""
def test_returns_log(self):
"""Returns a BaseLog derived instance."""
logger = log.LogManager('quiet', 100)
log_inst = logger.get()
assert isinstance(log_inst, log.BaseLog)
def test_log_state_update(self):
"""log.BaseLog.log updates shared state managed by LogManager"""
logger = log.LogManager('quiet', 100)
log_inst = logger.get()
log_inst.log('pass')
assert logger._state['total'] == 100
assert logger._state['summary'] == {'pass': 1}
assert logger._state['complete'] == 1
class TestQuietLog(object):
"""Test QuietLog class."""
class TestOutput(object):
"""Test the output of the various methods."""
@pytest.fixture(autouse=True, scope='function')
def mock_stdout(self, mocker):
mocker.patch.object(sys, 'stdout', six.StringIO())
def test_log(self, log_state): # pylint: disable=redefined-outer-name
"""Test the output of the log method."""
quiet = log.QuietLog(log_state, threading.Lock())
quiet.log('pass')
sys.stdout.seek(0)
actual = sys.stdout.read()
assert actual == b'[1/1] pass: 1 -\n'
def test_summary(self, log_state): # pylint: disable=redefined-outer-name
"""Test the output of the summary method."""
quiet = log.QuietLog(log_state, threading.Lock())
# Call log to set the total correctly, then truncate and remove the
# values, so the we can test
quiet.log('pass')
sys.stdout.seek(0)
sys.stdout.truncate()
quiet.summary()
sys.stdout.seek(0)
# Because of the 'lastlength' mechanims there will likely be
# trainling whitespace after the the output, it's not useful to
# test that here, so just strip it.
assert sys.stdout.read().rstrip() == b'[1/1] pass: 1'
def test_start(self, log_state): # pylint: disable=redefined-outer-name
"""Test that the start method doesn't have output."""
quiet = log.QuietLog(log_state, threading.Lock())
quiet.start('foo')
sys.stdout.seek(0)
actual = sys.stdout.read()
assert actual == b''
class TestVerboseLog(object):
"""Tests for the VerboseLog class."""
class TestOutput(object):
"""Test the output of the various methods."""
@pytest.fixture(autouse=True, scope='function')
def mock_stdout(self, mocker):
mocker.patch.object(sys, 'stdout', six.StringIO())
def test_log(self, log_state): # pylint: disable=redefined-outer-name
"""Test the output of the log method."""
l = log.VerboseLog(log_state, threading.Lock())
l.start('foo')
sys.stdout.seek(0)
sys.stdout.truncate()
l.log('pass')
sys.stdout.seek(0)
actual = sys.stdout.read()
assert actual == b'pass: foo\n\n[1/1] pass: 1 /\n'
def test_summary(self, log_state): # pylint: disable=redefined-outer-name
"""Test the output of the summary method."""
l = log.VerboseLog(log_state, threading.Lock())
l.log('pass')
sys.stdout.seek(0)
sys.stdout.truncate()
l.summary()
sys.stdout.seek(0)
assert sys.stdout.read().rstrip() == b'[1/1] pass: 1'
def test_start(self, log_state): # pylint: disable=redefined-outer-name
"""Test that the start method doesn't have output."""
l = log.VerboseLog(log_state, threading.Lock())
l.start('foo')
sys.stdout.seek(0)
assert sys.stdout.read().rstrip() == b'running: foo\n\n[0/1] \\'
class TestDummyLog(object):
"""Tests for the DummyLog class."""
class TestOutput(object):
"""Test the output of the various methods."""
@pytest.fixture(autouse=True, scope='function')
def mock_stdout(self, mocker):
mocker.patch.object(sys, 'stdout', six.StringIO())
def test_log(self, log_state): # pylint: disable=redefined-outer-name
"""Test the output of the log method."""
quiet = log.DummyLog(log_state, threading.Lock())
quiet.log('pass')
sys.stdout.seek(0)
actual = sys.stdout.read()
assert actual == b''
def test_summary(self, log_state): # pylint: disable=redefined-outer-name
"""Test the output of the summary method."""
quiet = log.DummyLog(log_state, threading.Lock())
quiet.summary()
sys.stdout.seek(0)
actual = sys.stdout.read()
assert actual == b''
def test_start(self, log_state): # pylint: disable=redefined-outer-name
"""Test that the start method doesn't have output."""
quiet = log.DummyLog(log_state, threading.Lock())
quiet.start('foo')
sys.stdout.seek(0)
actual = sys.stdout.read()
assert actual == b''
|