root / test / py / cmdlib / testsupport / processor_mock.py @ 3efa7659
History | View | Annotate | Download (4.1 kB)
1 |
#
|
---|---|
2 |
#
|
3 |
|
4 |
# Copyright (C) 2013 Google Inc.
|
5 |
#
|
6 |
# This program is free software; you can redistribute it and/or modify
|
7 |
# it under the terms of the GNU General Public License as published by
|
8 |
# the Free Software Foundation; either version 2 of the License, or
|
9 |
# (at your option) any later version.
|
10 |
#
|
11 |
# This program is distributed in the hope that it will be useful, but
|
12 |
# WITHOUT ANY WARRANTY; without even the implied warranty of
|
13 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
14 |
# General Public License for more details.
|
15 |
#
|
16 |
# You should have received a copy of the GNU General Public License
|
17 |
# along with this program; if not, write to the Free Software
|
18 |
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
|
19 |
# 02110-1301, USA.
|
20 |
|
21 |
import re |
22 |
|
23 |
from ganeti import constants |
24 |
from ganeti import mcpu |
25 |
|
26 |
|
27 |
class LogRecordingCallback(mcpu.OpExecCbBase): |
28 |
"""Helper class for log output recording.
|
29 |
|
30 |
"""
|
31 |
def __init__(self, processor): |
32 |
self.processor = processor
|
33 |
|
34 |
def Feedback(self, *args): |
35 |
assert len(args) < 3 |
36 |
|
37 |
if len(args) == 1: |
38 |
log_type = constants.ELOG_MESSAGE |
39 |
log_msg = args[0]
|
40 |
else:
|
41 |
(log_type, log_msg) = args |
42 |
|
43 |
self.processor.log_entries.append((log_type, log_msg))
|
44 |
|
45 |
|
46 |
class ProcessorMock(mcpu.Processor): |
47 |
"""Mocked opcode processor for tests.
|
48 |
|
49 |
This class actually performs much more than a mock, as it drives the
|
50 |
execution of LU's. But it also provides access to the log output of the LU
|
51 |
the result of the execution.
|
52 |
|
53 |
See L{ExecOpCodeAndRecordOutput} for the main method of this class.
|
54 |
|
55 |
"""
|
56 |
|
57 |
def __init__(self, context): |
58 |
super(ProcessorMock, self).__init__(context, 0, True) |
59 |
self.log_entries = []
|
60 |
|
61 |
def ExecOpCodeAndRecordOutput(self, op): |
62 |
"""Executes the given opcode and records the output for further inspection.
|
63 |
|
64 |
@param op: the opcode to execute.
|
65 |
@return: see L{mcpu.Processor.ExecOpCode}
|
66 |
|
67 |
"""
|
68 |
return self.ExecOpCode(op, LogRecordingCallback(self)) |
69 |
|
70 |
def GetLogEntries(self): |
71 |
"""Return the list of recorded log entries.
|
72 |
|
73 |
@rtype: list of (string, string) tuples
|
74 |
@return: the list of recorded log entries
|
75 |
|
76 |
"""
|
77 |
return self.log_entries |
78 |
|
79 |
def GetLogMessages(self): |
80 |
"""Return the list of recorded log messages.
|
81 |
|
82 |
@rtype: list of string
|
83 |
@return: the list of recorded log messages
|
84 |
|
85 |
"""
|
86 |
return [msg for _, msg in self.log_entries] |
87 |
|
88 |
def GetLogEntriesString(self): |
89 |
"""Return a string with all log entries separated by a newline.
|
90 |
|
91 |
"""
|
92 |
return "\n".join("%s: %s" % (type, msg) |
93 |
for type, msg in self.GetLogEntries()) |
94 |
|
95 |
def GetLogMessagesString(self): |
96 |
"""Return a string with all log messages separated by a newline.
|
97 |
|
98 |
"""
|
99 |
return "\n".join("%s" % msg for _, msg in self.GetLogEntries()) |
100 |
|
101 |
def assertLogContainsEntry(self, expected_type, expected_msg): |
102 |
"""Asserts that the log contains the exact given entry.
|
103 |
|
104 |
@type expected_type: string
|
105 |
@param expected_type: the expected type
|
106 |
@type expected_msg: string
|
107 |
@param expected_msg: the expected message
|
108 |
|
109 |
"""
|
110 |
for type, msg in self.log_entries: |
111 |
if type == expected_type and msg == expected_msg: |
112 |
return
|
113 |
|
114 |
raise AssertionError( |
115 |
"Could not find '%s' (type '%s') in LU log messages. Log is:\n%s" %
|
116 |
(expected_msg, expected_type, self.GetLogEntriesString()))
|
117 |
|
118 |
def assertLogContainsMessage(self, expected_msg): |
119 |
"""Asserts that the log contains the exact given message.
|
120 |
|
121 |
@type expected_msg: string
|
122 |
@param expected_msg: the expected message
|
123 |
|
124 |
"""
|
125 |
for msg in self.GetLogMessages(): |
126 |
if msg == expected_msg:
|
127 |
return
|
128 |
|
129 |
raise AssertionError( |
130 |
"Could not find '%s' in LU log messages. Log is:\n%s" %
|
131 |
(expected_msg, self.GetLogMessagesString()))
|
132 |
|
133 |
def assertLogContainsRegex(self, expected_regex): |
134 |
"""Asserts that the log contains a message which matches the regex.
|
135 |
|
136 |
@type expected_regex: string
|
137 |
@param expected_regex: regular expression to match messages with.
|
138 |
|
139 |
"""
|
140 |
for msg in self.GetLogMessages(): |
141 |
if re.search(expected_regex, msg) is not None: |
142 |
return
|
143 |
|
144 |
raise AssertionError( |
145 |
"Could not find '%s' in LU log messages. Log is:\n%s" %
|
146 |
(expected_regex, self.GetLogMessagesString())
|
147 |
) |